api.c 2.5 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832698336983469835698366983769838698396984069841698426984369844698456984669847698486984969850698516985269853698546985569856698576985869859698606986169862698636986469865698666986769868698696987069871698726987369874698756987669877698786987969880698816988269883698846988569886698876988869889698906989169892698936989469895698966989769898698996990069901699026990369904699056990669907699086990969910699116991269913699146991569916699176991869919699206992169922699236992469925699266992769928699296993069931699326993369934699356993669937699386993969940699416994269943699446994569946699476994869949699506995169952699536995469955699566995769958699596996069961699626996369964699656996669967699686996969970699716997269973699746997569976699776997869979699806998169982699836998469985699866998769988699896999069991699926999369994699956999669997699986999970000700017000270003700047000570006700077000870009700107001170012700137001470015700167001770018700197002070021700227002370024700257002670027700287002970030700317003270033700347003570036700377003870039700407004170042700437004470045700467004770048700497005070051700527005370054700557005670057700587005970060700617006270063700647006570066700677006870069700707007170072700737007470075700767007770078700797008070081700827008370084700857008670087700887008970090700917009270093700947009570096700977009870099701007010170102701037010470105701067010770108701097011070111701127011370114701157011670117701187011970120701217012270123701247012570126701277012870129701307013170132701337013470135701367013770138701397014070141701427014370144701457014670147701487014970150701517015270153701547015570156701577015870159701607016170162701637016470165701667016770168701697017070171701727017370174701757017670177701787017970180701817018270183701847018570186701877018870189701907019170192701937019470195701967019770198701997020070201702027020370204702057020670207702087020970210702117021270213702147021570216702177021870219702207022170222702237022470225702267022770228702297023070231702327023370234702357023670237702387023970240702417024270243702447024570246702477024870249702507025170252702537025470255702567025770258702597026070261702627026370264702657026670267702687026970270702717027270273702747027570276702777027870279702807028170282702837028470285702867028770288702897029070291702927029370294702957029670297702987029970300703017030270303703047030570306703077030870309703107031170312703137031470315703167031770318703197032070321703227032370324703257032670327703287032970330703317033270333703347033570336703377033870339703407034170342703437034470345703467034770348703497035070351703527035370354703557035670357703587035970360703617036270363703647036570366703677036870369703707037170372703737037470375703767037770378703797038070381703827038370384703857038670387703887038970390703917039270393703947039570396703977039870399704007040170402704037040470405704067040770408704097041070411704127041370414704157041670417704187041970420704217042270423704247042570426704277042870429704307043170432704337043470435704367043770438704397044070441704427044370444704457044670447704487044970450704517045270453704547045570456704577045870459704607046170462704637046470465704667046770468704697047070471704727047370474704757047670477704787047970480704817048270483704847048570486704877048870489704907049170492704937049470495704967049770498704997050070501705027050370504705057050670507705087050970510705117051270513705147051570516705177051870519705207052170522705237052470525705267052770528705297053070531705327053370534705357053670537705387053970540705417054270543705447054570546705477054870549705507055170552705537055470555705567055770558705597056070561705627056370564705657056670567705687056970570705717057270573705747057570576705777057870579705807058170582705837058470585705867058770588705897059070591705927059370594705957059670597705987059970600706017060270603706047060570606706077060870609706107061170612706137061470615706167061770618706197062070621706227062370624706257062670627706287062970630706317063270633706347063570636706377063870639706407064170642706437064470645706467064770648706497065070651706527065370654706557065670657706587065970660706617066270663706647066570666706677066870669706707067170672706737067470675706767067770678706797068070681706827068370684706857068670687706887068970690706917069270693706947069570696706977069870699707007070170702707037070470705707067070770708707097071070711707127071370714707157071670717707187071970720707217072270723707247072570726707277072870729707307073170732707337073470735707367073770738707397074070741707427074370744707457074670747707487074970750707517075270753707547075570756707577075870759707607076170762707637076470765707667076770768707697077070771707727077370774707757077670777707787077970780707817078270783707847078570786707877078870789707907079170792707937079470795707967079770798707997080070801708027080370804708057080670807708087080970810708117081270813708147081570816708177081870819708207082170822708237082470825708267082770828708297083070831708327083370834708357083670837708387083970840708417084270843708447084570846708477084870849708507085170852708537085470855708567085770858708597086070861708627086370864708657086670867708687086970870708717087270873708747087570876708777087870879708807088170882708837088470885708867088770888708897089070891708927089370894708957089670897708987089970900709017090270903709047090570906709077090870909709107091170912709137091470915709167091770918709197092070921709227092370924709257092670927709287092970930709317093270933709347093570936709377093870939709407094170942709437094470945709467094770948709497095070951709527095370954709557095670957709587095970960709617096270963709647096570966709677096870969709707097170972709737097470975709767097770978709797098070981709827098370984709857098670987709887098970990709917099270993709947099570996709977099870999710007100171002710037100471005710067100771008710097101071011710127101371014710157101671017710187101971020710217102271023710247102571026710277102871029710307103171032710337103471035710367103771038710397104071041710427104371044710457104671047710487104971050710517105271053710547105571056710577105871059710607106171062710637106471065710667106771068710697107071071710727107371074710757107671077710787107971080710817108271083710847108571086710877108871089710907109171092710937109471095710967109771098710997110071101711027110371104711057110671107711087110971110711117111271113711147111571116711177111871119711207112171122711237112471125711267112771128711297113071131711327113371134711357113671137711387113971140711417114271143711447114571146711477114871149711507115171152711537115471155711567115771158711597116071161711627116371164711657116671167711687116971170711717117271173711747117571176711777117871179711807118171182711837118471185711867118771188711897119071191711927119371194711957119671197711987119971200712017120271203712047120571206712077120871209712107121171212712137121471215712167121771218712197122071221712227122371224712257122671227712287122971230712317123271233712347123571236712377123871239712407124171242712437124471245712467124771248712497125071251712527125371254712557125671257712587125971260712617126271263712647126571266712677126871269712707127171272712737127471275712767127771278712797128071281712827128371284712857128671287712887128971290712917129271293712947129571296712977129871299713007130171302713037130471305713067130771308713097131071311713127131371314713157131671317713187131971320713217132271323713247132571326713277132871329713307133171332713337133471335713367133771338713397134071341713427134371344713457134671347713487134971350713517135271353713547135571356713577135871359713607136171362713637136471365713667136771368713697137071371713727137371374713757137671377713787137971380713817138271383713847138571386713877138871389713907139171392713937139471395713967139771398713997140071401714027140371404714057140671407714087140971410714117141271413714147141571416714177141871419714207142171422714237142471425714267142771428714297143071431714327143371434714357143671437714387143971440714417144271443714447144571446714477144871449714507145171452714537145471455714567145771458714597146071461714627146371464714657146671467714687146971470714717147271473714747147571476714777147871479714807148171482714837148471485714867148771488714897149071491714927149371494714957149671497714987149971500715017150271503715047150571506715077150871509715107151171512715137151471515715167151771518715197152071521715227152371524715257152671527715287152971530715317153271533715347153571536715377153871539715407154171542715437154471545715467154771548715497155071551715527155371554715557155671557715587155971560715617156271563715647156571566715677156871569715707157171572715737157471575715767157771578715797158071581715827158371584715857158671587715887158971590715917159271593715947159571596715977159871599716007160171602716037160471605716067160771608716097161071611716127161371614716157161671617716187161971620716217162271623716247162571626716277162871629716307163171632716337163471635716367163771638716397164071641716427164371644716457164671647716487164971650716517165271653716547165571656716577165871659716607166171662716637166471665716667166771668716697167071671716727167371674716757167671677716787167971680716817168271683716847168571686716877168871689716907169171692716937169471695716967169771698716997170071701717027170371704717057170671707717087170971710717117171271713717147171571716717177171871719717207172171722717237172471725717267172771728717297173071731717327173371734717357173671737717387173971740717417174271743717447174571746717477174871749717507175171752717537175471755717567175771758717597176071761717627176371764717657176671767717687176971770717717177271773717747177571776717777177871779717807178171782717837178471785717867178771788717897179071791717927179371794717957179671797717987179971800718017180271803718047180571806718077180871809718107181171812718137181471815718167181771818718197182071821718227182371824718257182671827718287182971830718317183271833718347183571836718377183871839718407184171842718437184471845718467184771848718497185071851718527185371854718557185671857718587185971860718617186271863718647186571866718677186871869718707187171872718737187471875718767187771878718797188071881718827188371884718857188671887718887188971890718917189271893718947189571896718977189871899719007190171902719037190471905719067190771908719097191071911719127191371914719157191671917719187191971920719217192271923719247192571926719277192871929719307193171932719337193471935719367193771938719397194071941719427194371944719457194671947719487194971950719517195271953719547195571956719577195871959719607196171962719637196471965719667196771968719697197071971719727197371974719757197671977719787197971980719817198271983719847198571986719877198871989719907199171992719937199471995719967199771998719997200072001720027200372004720057200672007720087200972010720117201272013720147201572016720177201872019720207202172022720237202472025720267202772028720297203072031720327203372034720357203672037720387203972040720417204272043720447204572046720477204872049720507205172052720537205472055720567205772058720597206072061720627206372064720657206672067720687206972070720717207272073720747207572076720777207872079720807208172082720837208472085720867208772088720897209072091720927209372094720957209672097720987209972100721017210272103721047210572106721077210872109721107211172112721137211472115721167211772118721197212072121721227212372124721257212672127721287212972130721317213272133721347213572136721377213872139721407214172142721437214472145721467214772148721497215072151721527215372154721557215672157721587215972160721617216272163721647216572166721677216872169721707217172172721737217472175721767217772178721797218072181721827218372184721857218672187721887218972190721917219272193721947219572196721977219872199722007220172202722037220472205722067220772208722097221072211722127221372214722157221672217722187221972220722217222272223722247222572226722277222872229722307223172232722337223472235722367223772238722397224072241722427224372244722457224672247722487224972250722517225272253722547225572256722577225872259722607226172262722637226472265722667226772268722697227072271722727227372274722757227672277722787227972280722817228272283722847228572286722877228872289722907229172292722937229472295722967229772298722997230072301723027230372304723057230672307723087230972310723117231272313723147231572316723177231872319723207232172322723237232472325723267232772328723297233072331723327233372334723357233672337723387233972340723417234272343723447234572346723477234872349723507235172352723537235472355723567235772358723597236072361723627236372364723657236672367723687236972370723717237272373723747237572376723777237872379723807238172382723837238472385723867238772388723897239072391723927239372394723957239672397723987239972400724017240272403724047240572406724077240872409724107241172412724137241472415724167241772418724197242072421724227242372424724257242672427724287242972430724317243272433724347243572436724377243872439724407244172442724437244472445724467244772448724497245072451724527245372454724557245672457724587245972460724617246272463724647246572466724677246872469724707247172472724737247472475724767247772478724797248072481724827248372484724857248672487724887248972490724917249272493724947249572496724977249872499725007250172502725037250472505725067250772508725097251072511725127251372514725157251672517725187251972520725217252272523725247252572526725277252872529725307253172532725337253472535725367253772538725397254072541725427254372544725457254672547725487254972550725517255272553725547255572556725577255872559725607256172562725637256472565725667256772568725697257072571725727257372574725757257672577725787257972580725817258272583725847258572586725877258872589725907259172592725937259472595725967259772598725997260072601726027260372604726057260672607726087260972610726117261272613726147261572616726177261872619726207262172622726237262472625726267262772628726297263072631726327263372634726357263672637726387263972640726417264272643726447264572646726477264872649726507265172652726537265472655726567265772658726597266072661726627266372664726657266672667726687266972670726717267272673726747267572676726777267872679726807268172682726837268472685726867268772688726897269072691726927269372694726957269672697726987269972700727017270272703727047270572706727077270872709727107271172712727137271472715727167271772718727197272072721727227272372724727257272672727727287272972730727317273272733727347273572736727377273872739727407274172742727437274472745727467274772748727497275072751727527275372754727557275672757727587275972760727617276272763727647276572766727677276872769727707277172772727737277472775727767277772778727797278072781727827278372784727857278672787727887278972790727917279272793727947279572796727977279872799728007280172802728037280472805728067280772808728097281072811728127281372814728157281672817728187281972820728217282272823728247282572826728277282872829728307283172832728337283472835728367283772838728397284072841728427284372844728457284672847728487284972850728517285272853728547285572856728577285872859728607286172862728637286472865728667286772868728697287072871728727287372874728757287672877728787287972880728817288272883728847288572886728877288872889728907289172892728937289472895728967289772898728997290072901729027290372904729057290672907729087290972910729117291272913729147291572916729177291872919729207292172922729237292472925729267292772928729297293072931729327293372934729357293672937729387293972940729417294272943729447294572946729477294872949729507295172952729537295472955729567295772958729597296072961729627296372964729657296672967729687296972970729717297272973729747297572976729777297872979729807298172982729837298472985729867298772988729897299072991729927299372994729957299672997729987299973000730017300273003730047300573006730077300873009730107301173012730137301473015730167301773018730197302073021730227302373024730257302673027730287302973030730317303273033730347303573036730377303873039730407304173042730437304473045730467304773048730497305073051730527305373054730557305673057730587305973060730617306273063730647306573066730677306873069730707307173072730737307473075730767307773078730797308073081730827308373084730857308673087730887308973090730917309273093730947309573096730977309873099731007310173102731037310473105731067310773108731097311073111731127311373114731157311673117731187311973120731217312273123731247312573126731277312873129731307313173132731337313473135731367313773138731397314073141731427314373144731457314673147731487314973150731517315273153731547315573156731577315873159731607316173162731637316473165731667316773168731697317073171731727317373174731757317673177731787317973180731817318273183731847318573186731877318873189731907319173192731937319473195731967319773198731997320073201732027320373204732057320673207732087320973210732117321273213732147321573216732177321873219732207322173222732237322473225732267322773228732297323073231732327323373234732357323673237732387323973240732417324273243732447324573246732477324873249732507325173252732537325473255732567325773258732597326073261732627326373264732657326673267732687326973270732717327273273732747327573276732777327873279732807328173282732837328473285732867328773288732897329073291732927329373294732957329673297732987329973300733017330273303733047330573306733077330873309733107331173312733137331473315733167331773318733197332073321733227332373324733257332673327733287332973330733317333273333733347333573336733377333873339733407334173342733437334473345733467334773348733497335073351733527335373354733557335673357733587335973360733617336273363733647336573366733677336873369733707337173372733737337473375733767337773378733797338073381733827338373384733857338673387733887338973390733917339273393733947339573396733977339873399734007340173402734037340473405734067340773408734097341073411734127341373414734157341673417734187341973420734217342273423734247342573426734277342873429734307343173432734337343473435734367343773438734397344073441734427344373444734457344673447734487344973450734517345273453734547345573456734577345873459734607346173462734637346473465734667346773468734697347073471734727347373474734757347673477734787347973480734817348273483734847348573486734877348873489734907349173492734937349473495734967349773498734997350073501735027350373504735057350673507735087350973510735117351273513735147351573516735177351873519735207352173522735237352473525735267352773528735297353073531735327353373534735357353673537735387353973540735417354273543735447354573546735477354873549735507355173552735537355473555735567355773558735597356073561735627356373564735657356673567735687356973570735717357273573735747357573576735777357873579735807358173582735837358473585735867358773588735897359073591735927359373594735957359673597735987359973600736017360273603736047360573606736077360873609736107361173612736137361473615736167361773618736197362073621736227362373624736257362673627736287362973630736317363273633736347363573636736377363873639736407364173642736437364473645736467364773648736497365073651736527365373654736557365673657736587365973660736617366273663736647366573666736677366873669736707367173672736737367473675736767367773678736797368073681736827368373684736857368673687736887368973690736917369273693736947369573696736977369873699737007370173702737037370473705737067370773708737097371073711737127371373714737157371673717737187371973720737217372273723737247372573726737277372873729737307373173732737337373473735737367373773738737397374073741737427374373744737457374673747737487374973750737517375273753737547375573756737577375873759737607376173762737637376473765737667376773768737697377073771737727377373774737757377673777737787377973780737817378273783737847378573786737877378873789737907379173792737937379473795737967379773798737997380073801738027380373804738057380673807738087380973810738117381273813738147381573816738177381873819738207382173822738237382473825738267382773828738297383073831738327383373834738357383673837738387383973840738417384273843738447384573846738477384873849738507385173852738537385473855738567385773858738597386073861738627386373864738657386673867738687386973870738717387273873738747387573876738777387873879738807388173882738837388473885738867388773888738897389073891738927389373894738957389673897738987389973900739017390273903739047390573906739077390873909739107391173912739137391473915739167391773918739197392073921739227392373924739257392673927739287392973930739317393273933739347393573936739377393873939739407394173942739437394473945739467394773948739497395073951739527395373954739557395673957739587395973960739617396273963739647396573966739677396873969739707397173972739737397473975739767397773978739797398073981739827398373984739857398673987739887398973990739917399273993739947399573996739977399873999740007400174002740037400474005740067400774008740097401074011740127401374014740157401674017740187401974020740217402274023740247402574026740277402874029740307403174032740337403474035740367403774038740397404074041740427404374044740457404674047740487404974050740517405274053740547405574056740577405874059740607406174062740637406474065740667406774068740697407074071740727407374074740757407674077740787407974080740817408274083740847408574086740877408874089740907409174092740937409474095740967409774098740997410074101741027410374104741057410674107741087410974110741117411274113741147411574116741177411874119741207412174122741237412474125741267412774128741297413074131741327413374134741357413674137741387413974140741417414274143741447414574146741477414874149741507415174152741537415474155741567415774158741597416074161741627416374164741657416674167741687416974170741717417274173741747417574176741777417874179741807418174182741837418474185741867418774188741897419074191741927419374194741957419674197741987419974200742017420274203742047420574206742077420874209742107421174212742137421474215742167421774218742197422074221742227422374224742257422674227742287422974230742317423274233742347423574236742377423874239742407424174242742437424474245742467424774248742497425074251742527425374254742557425674257742587425974260742617426274263742647426574266742677426874269742707427174272742737427474275742767427774278742797428074281742827428374284742857428674287742887428974290742917429274293742947429574296742977429874299743007430174302743037430474305743067430774308743097431074311743127431374314743157431674317743187431974320743217432274323743247432574326743277432874329743307433174332743337433474335743367433774338743397434074341743427434374344743457434674347743487434974350743517435274353743547435574356743577435874359743607436174362743637436474365743667436774368743697437074371743727437374374743757437674377743787437974380743817438274383743847438574386743877438874389743907439174392743937439474395743967439774398743997440074401744027440374404744057440674407744087440974410744117441274413744147441574416744177441874419744207442174422744237442474425744267442774428744297443074431744327443374434744357443674437744387443974440744417444274443744447444574446744477444874449744507445174452744537445474455744567445774458744597446074461744627446374464
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #endif
  215. #ifndef NO_SIG_WRAPPER
  216. #include <wolfssl/wolfcrypt/signature.h>
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. #include <wolfssl/wolfcrypt/aes.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #ifdef HAVE_LIBZ
  225. #include <wolfssl/wolfcrypt/compress.h>
  226. #endif
  227. #endif
  228. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  229. #include <wolfssl/wolfcrypt/asn.h>
  230. #endif
  231. #ifndef NO_DSA
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #ifndef ONEK_BUF
  234. #define ONEK_BUF 1024
  235. #endif
  236. #ifndef TWOK_BUF
  237. #define TWOK_BUF 2048
  238. #endif
  239. #ifndef FOURK_BUF
  240. #define FOURK_BUF 4096
  241. #endif
  242. #ifndef DSA_SIG_SIZE
  243. #define DSA_SIG_SIZE 40
  244. #endif
  245. #ifndef MAX_DSA_PARAM_SIZE
  246. #define MAX_DSA_PARAM_SIZE 256
  247. #endif
  248. #endif
  249. #ifdef WOLFSSL_CMAC
  250. #include <wolfssl/wolfcrypt/cmac.h>
  251. #endif
  252. #ifdef HAVE_ED25519
  253. #include <wolfssl/wolfcrypt/ed25519.h>
  254. #endif
  255. #ifdef HAVE_CURVE25519
  256. #include <wolfssl/wolfcrypt/curve25519.h>
  257. #endif
  258. #ifdef HAVE_ED448
  259. #include <wolfssl/wolfcrypt/ed448.h>
  260. #endif
  261. #ifdef HAVE_CURVE448
  262. #include <wolfssl/wolfcrypt/curve448.h>
  263. #endif
  264. #ifdef HAVE_PKCS12
  265. #include <wolfssl/wolfcrypt/pkcs12.h>
  266. #endif
  267. #include <wolfssl/wolfcrypt/logging.h>
  268. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  269. #include <wolfssl/openssl/ssl.h>
  270. #ifndef NO_ASN
  271. /* for ASN_COMMON_NAME DN_tags enum */
  272. #include <wolfssl/wolfcrypt/asn.h>
  273. #endif
  274. #ifdef HAVE_OCSP
  275. #include <wolfssl/openssl/ocsp.h>
  276. #endif
  277. #endif
  278. #ifdef OPENSSL_EXTRA
  279. #include <wolfssl/openssl/cmac.h>
  280. #include <wolfssl/openssl/x509v3.h>
  281. #include <wolfssl/openssl/asn1.h>
  282. #include <wolfssl/openssl/crypto.h>
  283. #include <wolfssl/openssl/pkcs12.h>
  284. #include <wolfssl/openssl/evp.h>
  285. #include <wolfssl/openssl/dh.h>
  286. #include <wolfssl/openssl/bn.h>
  287. #include <wolfssl/openssl/buffer.h>
  288. #include <wolfssl/openssl/pem.h>
  289. #include <wolfssl/openssl/ec.h>
  290. #include <wolfssl/openssl/ecdh.h>
  291. #include <wolfssl/openssl/engine.h>
  292. #include <wolfssl/openssl/hmac.h>
  293. #include <wolfssl/openssl/objects.h>
  294. #include <wolfssl/openssl/rand.h>
  295. #include <wolfssl/openssl/modes.h>
  296. #include <wolfssl/openssl/fips_rand.h>
  297. #include <wolfssl/openssl/kdf.h>
  298. #ifdef OPENSSL_ALL
  299. #include <wolfssl/openssl/txt_db.h>
  300. #include <wolfssl/openssl/lhash.h>
  301. #endif
  302. #ifndef NO_AES
  303. #include <wolfssl/openssl/aes.h>
  304. #endif
  305. #ifndef NO_DES3
  306. #include <wolfssl/openssl/des.h>
  307. #endif
  308. #ifndef NO_RC4
  309. #include <wolfssl/openssl/rc4.h>
  310. #endif
  311. #ifdef HAVE_ECC
  312. #include <wolfssl/openssl/ecdsa.h>
  313. #endif
  314. #ifdef HAVE_PKCS7
  315. #include <wolfssl/openssl/pkcs7.h>
  316. #endif
  317. #ifdef HAVE_CURVE25519
  318. #include <wolfssl/openssl/ec25519.h>
  319. #endif
  320. #ifdef HAVE_ED25519
  321. #include <wolfssl/openssl/ed25519.h>
  322. #endif
  323. #ifdef HAVE_CURVE448
  324. #include <wolfssl/openssl/ec448.h>
  325. #endif
  326. #ifdef HAVE_ED448
  327. #include <wolfssl/openssl/ed448.h>
  328. #endif
  329. #endif /* OPENSSL_EXTRA */
  330. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  331. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  332. #include <wolfssl/wolfcrypt/srp.h>
  333. #endif
  334. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  335. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  336. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  337. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  338. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  339. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  340. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  341. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  342. * number tracking */
  343. #include "wolfssl/internal.h"
  344. #endif
  345. /* force enable test buffers */
  346. #ifndef USE_CERT_BUFFERS_2048
  347. #define USE_CERT_BUFFERS_2048
  348. #endif
  349. #ifndef USE_CERT_BUFFERS_256
  350. #define USE_CERT_BUFFERS_256
  351. #endif
  352. #include <wolfssl/certs_test.h>
  353. #include "tests/utils.h"
  354. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  355. * have default (hidden) visibility, and in the absence of visibility, it's
  356. * benign to mask out the library implementation.
  357. */
  358. #define WOLFSSL_MISC_INCLUDED
  359. #include <wolfcrypt/src/misc.c>
  360. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  361. /* FIPS build has replaced ecc.h. */
  362. #define wc_ecc_key_get_priv(key) (&((key)->k))
  363. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  364. #endif
  365. typedef struct testVector {
  366. const char* input;
  367. const char* output;
  368. size_t inLen;
  369. size_t outLen;
  370. } testVector;
  371. #if defined(HAVE_PKCS7)
  372. typedef struct {
  373. const byte* content;
  374. word32 contentSz;
  375. int contentOID;
  376. int encryptOID;
  377. int keyWrapOID;
  378. int keyAgreeOID;
  379. byte* cert;
  380. size_t certSz;
  381. byte* privateKey;
  382. word32 privateKeySz;
  383. } pkcs7EnvelopedVector;
  384. #ifndef NO_PKCS7_ENCRYPTED_DATA
  385. typedef struct {
  386. const byte* content;
  387. word32 contentSz;
  388. int contentOID;
  389. int encryptOID;
  390. byte* encryptionKey;
  391. word32 encryptionKeySz;
  392. } pkcs7EncryptedVector;
  393. #endif
  394. #endif /* HAVE_PKCS7 */
  395. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  396. typedef int (*ssl_cb)(WOLFSSL* ssl);
  397. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  398. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  399. typedef struct test_ssl_cbf {
  400. method_provider method;
  401. ctx_cb ctx_ready;
  402. ssl_cb ssl_ready;
  403. ssl_cb on_result;
  404. ctx_cb on_ctx_cleanup;
  405. ssl_cb on_cleanup;
  406. hs_cb on_handshake;
  407. WOLFSSL_CTX* ctx;
  408. const char* caPemFile;
  409. const char* certPemFile;
  410. const char* keyPemFile;
  411. const char* crlPemFile;
  412. #ifdef WOLFSSL_STATIC_MEMORY
  413. byte* mem;
  414. word32 memSz;
  415. wolfSSL_method_func method_ex;
  416. #endif
  417. int devId;
  418. int return_code;
  419. int last_err;
  420. unsigned char isSharedCtx:1;
  421. unsigned char loadToSSL:1;
  422. unsigned char ticNoInit:1;
  423. unsigned char doUdp:1;
  424. } test_ssl_cbf;
  425. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  426. typedef struct test_ssl_memio_ctx {
  427. WOLFSSL_CTX* s_ctx;
  428. WOLFSSL_CTX* c_ctx;
  429. WOLFSSL* s_ssl;
  430. WOLFSSL* c_ssl;
  431. const char* c_ciphers;
  432. const char* s_ciphers;
  433. char* c_msg;
  434. int c_msglen;
  435. char* s_msg;
  436. int s_msglen;
  437. test_ssl_cbf s_cb;
  438. test_ssl_cbf c_cb;
  439. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  440. int c_len;
  441. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  442. int s_len;
  443. } test_ssl_memio_ctx;
  444. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  445. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  446. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  447. const char* currentTestName;
  448. char tmpDirName[16];
  449. int tmpDirNameSet = 0;
  450. #endif
  451. /*----------------------------------------------------------------------------*
  452. | Constants
  453. *----------------------------------------------------------------------------*/
  454. /* Test result constants and macros. */
  455. /* Test succeeded. */
  456. #define TEST_SUCCESS (1)
  457. /* Test failed. */
  458. #define TEST_FAIL (0)
  459. /* Test skipped - not run. */
  460. #define TEST_SKIPPED (-7777)
  461. /* Returns the result based on whether check is true.
  462. *
  463. * @param [in] check Condition for success.
  464. * @return When condition is true: TEST_SUCCESS.
  465. * @return When condition is false: TEST_FAIL.
  466. */
  467. #ifdef DEBUG_WOLFSSL_VERBOSE
  468. #define XSTRINGIFY(s) STRINGIFY(s)
  469. #define STRINGIFY(s) #s
  470. #define TEST_RES_CHECK(check) ({ \
  471. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  472. if (_ret == TEST_FAIL) { \
  473. fprintf(stderr, " check \"%s\" at %d ", \
  474. XSTRINGIFY(check), __LINE__); \
  475. } \
  476. _ret; })
  477. #else
  478. #define TEST_RES_CHECK(check) \
  479. ((check) ? TEST_SUCCESS : TEST_FAIL)
  480. #endif /* DEBUG_WOLFSSL_VERBOSE */
  481. #define TEST_STRING "Everyone gets Friday off."
  482. #define TEST_STRING_SZ 25
  483. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  484. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  485. #define TEST_RSA_BITS 1024
  486. #else
  487. #define TEST_RSA_BITS 2048
  488. #endif
  489. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  490. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  491. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  492. static const char* bogusFile =
  493. #ifdef _WIN32
  494. "NUL"
  495. #else
  496. "/dev/null"
  497. #endif
  498. ;
  499. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  500. enum {
  501. TESTING_RSA = 1,
  502. TESTING_ECC = 2
  503. };
  504. #ifdef WOLFSSL_QNX_CAAM
  505. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  506. static int testDevId = WOLFSSL_CAAM_DEVID;
  507. #else
  508. static int testDevId = INVALID_DEVID;
  509. #endif
  510. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  511. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  512. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  513. #define HAVE_IO_TESTS_DEPENDENCIES
  514. #endif
  515. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  516. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  517. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  518. #endif
  519. /*----------------------------------------------------------------------------*
  520. | BIO with fixed read/write size
  521. *----------------------------------------------------------------------------*/
  522. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  523. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  524. int len)
  525. {
  526. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  527. len = 0;
  528. }
  529. else {
  530. if (bio->wrSz - bio->wrIdx < len) {
  531. len = bio->wrSz - bio->wrIdx;
  532. }
  533. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  534. bio->wrIdx += len;
  535. }
  536. return len;
  537. }
  538. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  539. {
  540. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  541. len = 0;
  542. }
  543. else {
  544. if (bio->wrSz - bio->rdIdx < len) {
  545. len = bio->wrSz - bio->rdIdx;
  546. }
  547. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  548. bio->rdIdx += len;
  549. }
  550. return len;
  551. }
  552. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  553. {
  554. static WOLFSSL_BIO_METHOD meth;
  555. meth.type = WOLFSSL_BIO_BIO;
  556. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  557. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  558. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  559. return &meth;
  560. }
  561. #endif
  562. /*----------------------------------------------------------------------------*
  563. | Setup
  564. *----------------------------------------------------------------------------*/
  565. static int test_wolfSSL_Init(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  569. return EXPECT_RESULT();
  570. }
  571. static int test_wolfSSL_Cleanup(void)
  572. {
  573. EXPECT_DECLS;
  574. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  575. return EXPECT_RESULT();
  576. }
  577. /* Initialize the wolfCrypt state.
  578. * POST: 0 success.
  579. */
  580. static int test_wolfCrypt_Init(void)
  581. {
  582. EXPECT_DECLS;
  583. ExpectIntEQ(wolfCrypt_Init(), 0);
  584. return EXPECT_RESULT();
  585. } /* END test_wolfCrypt_Init */
  586. static int test_wolfCrypt_Cleanup(void)
  587. {
  588. EXPECT_DECLS;
  589. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  590. return EXPECT_RESULT();
  591. }
  592. #ifdef WOLFSSL_STATIC_MEMORY
  593. #define TEST_LSM_STATIC_SIZE 440000
  594. /* Create new bucket list, using the default list, adding
  595. * one dang large buffer size. */
  596. #define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
  597. #define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
  598. #define TEST_LSM_DIST WOLFMEM_DIST,1
  599. #endif
  600. static int test_wc_LoadStaticMemory_ex(void)
  601. {
  602. EXPECT_DECLS;
  603. #ifdef WOLFSSL_STATIC_MEMORY
  604. byte staticMemory[TEST_LSM_STATIC_SIZE];
  605. word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
  606. word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
  607. WOLFSSL_HEAP_HINT* heap;
  608. /* For this test, the size and dist lists will be the ones configured
  609. * for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
  610. * so these lists are 10 long. For most tests, the value of
  611. * WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
  612. * to that, to make sure the list size is larger than
  613. * WOLFMEM_MAX_BUCKETS. */
  614. /* Pass in zero everything. */
  615. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  616. BAD_FUNC_ARG);
  617. /* Set the heap pointer to NULL. */
  618. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  619. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  620. staticMemory, (word32)sizeof(staticMemory),
  621. 0, 1),
  622. BAD_FUNC_ARG);
  623. /* Set other pointer values to NULL one at a time. */
  624. heap = NULL;
  625. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  626. WOLFMEM_DEF_BUCKETS, NULL, distList,
  627. staticMemory, (word32)sizeof(staticMemory),
  628. 0, 1),
  629. BAD_FUNC_ARG);
  630. heap = NULL;
  631. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  632. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  633. staticMemory, (word32)sizeof(staticMemory),
  634. 0, 1),
  635. BAD_FUNC_ARG);
  636. heap = NULL;
  637. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  638. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  639. NULL, (word32)sizeof(staticMemory),
  640. 0, 1),
  641. BAD_FUNC_ARG);
  642. /* Set the size of the static buffer to 0. */
  643. heap = NULL;
  644. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  645. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  646. staticMemory, 0,
  647. 0, 1),
  648. BUFFER_E);
  649. /* Set the size of the static buffer to one less than minimum allowed. */
  650. heap = NULL;
  651. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  652. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  653. staticMemory,
  654. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  655. 0, 1),
  656. BUFFER_E);
  657. /* Set the size of the static buffer to exactly the minimum size. */
  658. heap = NULL;
  659. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  660. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  661. staticMemory,
  662. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  663. 0, 1),
  664. 0);
  665. wc_UnloadStaticMemory(heap);
  666. /* Use more buckets than able. Success case. */
  667. heap = NULL;
  668. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  669. WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
  670. staticMemory, (word32)sizeof(staticMemory),
  671. 0, 1),
  672. 0);
  673. wc_UnloadStaticMemory(heap);
  674. /* Success case. */
  675. heap = NULL;
  676. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  677. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  678. staticMemory, (word32)sizeof(staticMemory),
  679. 0, 1),
  680. 0);
  681. wc_UnloadStaticMemory(heap);
  682. #endif /* WOLFSSL_STATIC_MEMORY */
  683. return EXPECT_RESULT();
  684. }
  685. /*----------------------------------------------------------------------------*
  686. | Platform dependent function test
  687. *----------------------------------------------------------------------------*/
  688. static int test_fileAccess(void)
  689. {
  690. EXPECT_DECLS;
  691. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  692. const char *fname[] = {
  693. svrCertFile, svrKeyFile, caCertFile,
  694. eccCertFile, eccKeyFile, eccRsaCertFile,
  695. cliCertFile, cliCertDerFile, cliKeyFile,
  696. dhParamFile,
  697. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  698. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  699. NULL
  700. };
  701. const char derfile[] = "./certs/server-cert.der";
  702. XFILE f = XBADFILE;
  703. size_t sz;
  704. byte *buff = NULL;
  705. int i;
  706. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  707. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  708. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  709. XFCLOSE(f);
  710. }
  711. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  712. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  713. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  714. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  715. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  716. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  717. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  718. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  719. XFCLOSE(f);
  720. #endif
  721. return EXPECT_RESULT();
  722. }
  723. /*----------------------------------------------------------------------------*
  724. | Method Allocators
  725. *----------------------------------------------------------------------------*/
  726. static int test_wolfSSL_Method_Allocators(void)
  727. {
  728. EXPECT_DECLS;
  729. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  730. do { \
  731. WOLFSSL_METHOD *method = NULL; \
  732. condition(method = allocator()); \
  733. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  734. } while (0)
  735. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  736. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  737. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  738. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  739. #ifndef NO_OLD_TLS
  740. #ifdef WOLFSSL_ALLOW_SSLV3
  741. #ifndef NO_WOLFSSL_SERVER
  742. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  743. #endif
  744. #ifndef NO_WOLFSSL_CLIENT
  745. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  746. #endif
  747. #endif
  748. #ifdef WOLFSSL_ALLOW_TLSV10
  749. #ifndef NO_WOLFSSL_SERVER
  750. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  751. #endif
  752. #ifndef NO_WOLFSSL_CLIENT
  753. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  754. #endif
  755. #endif
  756. #ifndef NO_WOLFSSL_SERVER
  757. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  758. #endif
  759. #ifndef NO_WOLFSSL_CLIENT
  760. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  761. #endif
  762. #endif /* !NO_OLD_TLS */
  763. #ifndef WOLFSSL_NO_TLS12
  764. #ifndef NO_WOLFSSL_SERVER
  765. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  766. #endif
  767. #ifndef NO_WOLFSSL_CLIENT
  768. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  769. #endif
  770. #endif /* !WOLFSSL_NO_TLS12 */
  771. #ifdef WOLFSSL_TLS13
  772. #ifndef NO_WOLFSSL_SERVER
  773. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  774. #endif
  775. #ifndef NO_WOLFSSL_CLIENT
  776. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  777. #endif
  778. #endif /* WOLFSSL_TLS13 */
  779. #ifndef NO_WOLFSSL_SERVER
  780. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  781. #endif
  782. #ifndef NO_WOLFSSL_CLIENT
  783. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  784. #endif
  785. #ifdef WOLFSSL_DTLS
  786. #ifndef NO_OLD_TLS
  787. #ifndef NO_WOLFSSL_SERVER
  788. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  789. #endif
  790. #ifndef NO_WOLFSSL_CLIENT
  791. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  792. #endif
  793. #endif
  794. #ifndef WOLFSSL_NO_TLS12
  795. #ifndef NO_WOLFSSL_SERVER
  796. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  797. #endif
  798. #ifndef NO_WOLFSSL_CLIENT
  799. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  800. #endif
  801. #endif
  802. #endif /* WOLFSSL_DTLS */
  803. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  804. /* Stubs */
  805. #ifndef NO_WOLFSSL_SERVER
  806. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  807. #endif
  808. #ifndef NO_WOLFSSL_CLIENT
  809. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  810. #endif
  811. #endif
  812. /* Test Either Method (client or server) */
  813. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  814. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  815. #ifndef NO_OLD_TLS
  816. #ifdef WOLFSSL_ALLOW_TLSV10
  817. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  818. #endif
  819. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  820. #endif /* !NO_OLD_TLS */
  821. #ifndef WOLFSSL_NO_TLS12
  822. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  823. #endif /* !WOLFSSL_NO_TLS12 */
  824. #ifdef WOLFSSL_TLS13
  825. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  826. #endif /* WOLFSSL_TLS13 */
  827. #ifdef WOLFSSL_DTLS
  828. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  829. #ifndef NO_OLD_TLS
  830. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  831. #endif /* !NO_OLD_TLS */
  832. #ifndef WOLFSSL_NO_TLS12
  833. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  834. #endif /* !WOLFSSL_NO_TLS12 */
  835. #endif /* WOLFSSL_DTLS */
  836. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  837. return EXPECT_RESULT();
  838. }
  839. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  840. /*----------------------------------------------------------------------------*
  841. | Dual algorithm Certificate Tests
  842. *----------------------------------------------------------------------------*/
  843. #define LARGE_TEMP_SZ 4096
  844. /* To better understand this, please see the X9.146 example in wolfssl-examples
  845. * repo. */
  846. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  847. char *sapkiFile, char *altPrivFile)
  848. {
  849. EXPECT_DECLS;
  850. FILE* file = NULL;
  851. Cert newCert;
  852. DecodedCert preTBS;
  853. byte caKeyBuf[LARGE_TEMP_SZ];
  854. word32 caKeySz = LARGE_TEMP_SZ;
  855. byte sapkiBuf[LARGE_TEMP_SZ];
  856. word32 sapkiSz = LARGE_TEMP_SZ;
  857. byte altPrivBuf[LARGE_TEMP_SZ];
  858. word32 altPrivSz = LARGE_TEMP_SZ;
  859. byte altSigAlgBuf[LARGE_TEMP_SZ];
  860. word32 altSigAlgSz = LARGE_TEMP_SZ;
  861. byte scratchBuf[LARGE_TEMP_SZ];
  862. word32 scratchSz = LARGE_TEMP_SZ;
  863. byte preTbsBuf[LARGE_TEMP_SZ];
  864. word32 preTbsSz = LARGE_TEMP_SZ;
  865. byte altSigValBuf[LARGE_TEMP_SZ];
  866. word32 altSigValSz = LARGE_TEMP_SZ;
  867. byte *outBuf = NULL;
  868. word32 outSz = LARGE_TEMP_SZ;
  869. WC_RNG rng;
  870. RsaKey caKey;
  871. ecc_key altCaKey;
  872. word32 idx = 0;
  873. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  874. DYNAMIC_TYPE_TMP_BUFFER));
  875. ExpectIntEQ(wc_InitRng(&rng), 0);
  876. XMEMSET(caKeyBuf, 0, caKeySz);
  877. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  878. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  879. if (file) {
  880. fclose(file);
  881. file = NULL;
  882. }
  883. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  884. idx = 0;
  885. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  886. 0);
  887. XMEMSET(sapkiBuf, 0, sapkiSz);
  888. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  889. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  890. if (file) {
  891. fclose(file);
  892. file = NULL;
  893. }
  894. XMEMSET(altPrivBuf, 0, altPrivSz);
  895. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  896. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  897. if (file) {
  898. fclose(file);
  899. file = NULL;
  900. }
  901. wc_ecc_init(&altCaKey);
  902. idx = 0;
  903. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  904. (word32)altPrivSz), 0);
  905. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  906. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  907. oidSigType, 0), 0);
  908. wc_InitCert(&newCert);
  909. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  910. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  911. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  912. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  913. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  914. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  915. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  916. newCert.sigType = CTC_SHA256wRSA;
  917. newCert.isCA = 1;
  918. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  919. (const byte *)"This is NOT a critical extension", 32), 0);
  920. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  921. sapkiSz), 0);
  922. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  923. altSigAlgSz), 0);
  924. XMEMSET(scratchBuf, 0, scratchSz);
  925. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  926. &caKey, &rng), 0);
  927. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  928. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  929. XMEMSET(preTbsBuf, 0, preTbsSz);
  930. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  931. XMEMSET(altSigValBuf, 0, altSigValSz);
  932. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  933. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  934. &rng), 0);
  935. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  936. altSigValSz), 0);
  937. /* Finally, generate the new certificate. */
  938. XMEMSET(outBuf, 0, outSz);
  939. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  940. 0);
  941. *out = outBuf;
  942. wc_FreeRsaKey(&caKey);
  943. wc_FreeRng(&rng);
  944. wc_FreeDecodedCert(&preTBS);
  945. return outSz;
  946. }
  947. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  948. char *sapkiFile, char *altPrivFile,
  949. char *serverKeyFile,
  950. byte *caCertBuf, int caCertSz)
  951. {
  952. EXPECT_DECLS;
  953. FILE* file = NULL;
  954. Cert newCert;
  955. DecodedCert preTBS;
  956. byte serverKeyBuf[LARGE_TEMP_SZ];
  957. word32 serverKeySz = LARGE_TEMP_SZ;
  958. byte caKeyBuf[LARGE_TEMP_SZ];
  959. word32 caKeySz = LARGE_TEMP_SZ;
  960. byte sapkiBuf[LARGE_TEMP_SZ];
  961. word32 sapkiSz = LARGE_TEMP_SZ;
  962. byte altPrivBuf[LARGE_TEMP_SZ];
  963. word32 altPrivSz = LARGE_TEMP_SZ;
  964. byte altSigAlgBuf[LARGE_TEMP_SZ];
  965. word32 altSigAlgSz = LARGE_TEMP_SZ;
  966. byte scratchBuf[LARGE_TEMP_SZ];
  967. word32 scratchSz = LARGE_TEMP_SZ;
  968. byte preTbsBuf[LARGE_TEMP_SZ];
  969. word32 preTbsSz = LARGE_TEMP_SZ;
  970. byte altSigValBuf[LARGE_TEMP_SZ];
  971. word32 altSigValSz = LARGE_TEMP_SZ;
  972. byte *outBuf = NULL;
  973. word32 outSz = LARGE_TEMP_SZ;
  974. WC_RNG rng;
  975. RsaKey caKey;
  976. RsaKey serverKey;
  977. ecc_key altCaKey;
  978. word32 idx = 0;
  979. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  980. DYNAMIC_TYPE_TMP_BUFFER));
  981. ExpectIntEQ(wc_InitRng(&rng), 0);
  982. XMEMSET(serverKeyBuf, 0, serverKeySz);
  983. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  984. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  985. 0);
  986. if (file) {
  987. fclose(file);
  988. file = NULL;
  989. }
  990. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  991. idx = 0;
  992. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  993. (word32)serverKeySz), 0);
  994. XMEMSET(caKeyBuf, 0, caKeySz);
  995. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  996. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  997. if (file) {
  998. fclose(file);
  999. file = NULL;
  1000. }
  1001. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  1002. idx = 0;
  1003. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  1004. (word32)caKeySz), 0);
  1005. XMEMSET(sapkiBuf, 0, sapkiSz);
  1006. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  1007. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  1008. if (file) {
  1009. fclose(file);
  1010. file = NULL;
  1011. }
  1012. XMEMSET(altPrivBuf, 0, altPrivSz);
  1013. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  1014. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1015. if (file) {
  1016. fclose(file);
  1017. file = NULL;
  1018. }
  1019. wc_ecc_init(&altCaKey);
  1020. idx = 0;
  1021. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1022. (word32)altPrivSz), 0);
  1023. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1024. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1025. oidSigType, 0), 0);
  1026. wc_InitCert(&newCert);
  1027. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1028. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1029. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1030. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1031. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1032. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1033. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1034. newCert.sigType = CTC_SHA256wRSA;
  1035. newCert.isCA = 0;
  1036. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1037. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  1038. (const byte *)"This is NOT a critical extension", 32), 0);
  1039. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1040. sapkiSz), 0);
  1041. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1042. altSigAlgSz), 0);
  1043. XMEMSET(scratchBuf, 0, scratchSz);
  1044. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1045. &rng), 0);
  1046. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1047. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1048. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1049. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1050. XMEMSET(preTbsBuf, 0, preTbsSz);
  1051. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1052. XMEMSET(altSigValBuf, 0, altSigValSz);
  1053. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1054. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1055. &rng), 0);
  1056. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1057. altSigValBuf, altSigValSz), 0);
  1058. /* Finally, generate the new certificate. */
  1059. XMEMSET(outBuf, 0, outSz);
  1060. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1061. 0);
  1062. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1063. outSz, &caKey, NULL, &rng), 0);
  1064. *out = outBuf;
  1065. wc_FreeRsaKey(&caKey);
  1066. wc_FreeRsaKey(&serverKey);
  1067. wc_FreeRng(&rng);
  1068. wc_FreeDecodedCert(&preTBS);
  1069. return outSz;
  1070. }
  1071. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1072. byte *serverCert, word32 serverCertSz,
  1073. byte *serverKey, word32 serverKeySz,
  1074. int negative_test)
  1075. {
  1076. EXPECT_DECLS;
  1077. WOLFSSL_CTX *ctx_c = NULL;
  1078. WOLFSSL_CTX *ctx_s = NULL;
  1079. WOLFSSL *ssl_c = NULL;
  1080. WOLFSSL *ssl_s = NULL;
  1081. struct test_memio_ctx test_ctx;
  1082. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1083. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1084. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1085. caCert, caCertSz, serverCert, serverCertSz,
  1086. serverKey, serverKeySz), 0);
  1087. if (negative_test) {
  1088. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1089. }
  1090. else {
  1091. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1092. }
  1093. wolfSSL_free(ssl_c);
  1094. wolfSSL_free(ssl_s);
  1095. wolfSSL_CTX_free(ctx_c);
  1096. wolfSSL_CTX_free(ctx_s);
  1097. return EXPECT_RESULT();
  1098. }
  1099. static int extCount = 0;
  1100. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1101. const unsigned char* der, word32 derSz)
  1102. {
  1103. (void) oid;
  1104. (void) oidSz;
  1105. (void) crit;
  1106. (void) der;
  1107. (void) derSz;
  1108. extCount ++;
  1109. /* Accept all extensions. This is only a test. Normally we would be much more
  1110. * careful about critical extensions. */
  1111. return 1;
  1112. }
  1113. static int test_dual_alg_support(void)
  1114. {
  1115. EXPECT_DECLS;
  1116. /* Root CA and server keys will be the same. This is only appropriate for
  1117. * testing. */
  1118. char keyFile[] = "./certs/ca-key.der";
  1119. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1120. char altPrivFile[] = "./certs/ecc-key.der";
  1121. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1122. byte *serverKey = NULL;
  1123. size_t serverKeySz = 0;
  1124. byte *root = NULL;
  1125. int rootSz = 0;
  1126. byte *server = NULL;
  1127. int serverSz = 0;
  1128. WOLFSSL_CERT_MANAGER* cm = NULL;
  1129. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1130. /* Base normal case. */
  1131. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1132. ExpectNotNull(root);
  1133. ExpectIntGT(rootSz, 0);
  1134. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1135. altPrivFile, keyFile, root, rootSz);
  1136. ExpectNotNull(server);
  1137. ExpectIntGT(serverSz, 0);
  1138. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1139. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1140. TEST_SUCCESS);
  1141. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1142. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1143. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1144. * the alternative signature and then set negative_test to true for the
  1145. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1146. * because the signature won't verify. The exception is if
  1147. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1148. * and this is no longer a negative test. */
  1149. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1150. ExpectNotNull(root);
  1151. ExpectIntGT(rootSz, 0);
  1152. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1153. wrongPrivFile, keyFile, root, rootSz);
  1154. ExpectNotNull(server);
  1155. ExpectIntGT(serverSz, 0);
  1156. #ifdef WOLFSSL_TRUST_PEER_CERT
  1157. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1158. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1159. TEST_SUCCESS);
  1160. #else
  1161. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1162. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1163. TEST_SUCCESS);
  1164. #endif
  1165. /* Lets see if CertManager can find the new extensions */
  1166. extCount = 0;
  1167. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1168. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1169. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1170. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1171. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1172. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1173. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1174. * because they are for the dual alg extensions. */
  1175. ExpectIntEQ(extCount, 1);
  1176. wolfSSL_CertManagerFree(cm);
  1177. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1178. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1179. free(serverKey);
  1180. return EXPECT_RESULT();
  1181. }
  1182. #else
  1183. static int test_dual_alg_support(void)
  1184. {
  1185. return TEST_SKIPPED;
  1186. }
  1187. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1188. /*----------------------------------------------------------------------------*
  1189. | Context
  1190. *----------------------------------------------------------------------------*/
  1191. #ifndef NO_WOLFSSL_SERVER
  1192. static int test_wolfSSL_CTX_new(void)
  1193. {
  1194. EXPECT_DECLS;
  1195. WOLFSSL_CTX *ctx;
  1196. WOLFSSL_METHOD* method;
  1197. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1198. ExpectNotNull(method = wolfSSLv23_server_method());
  1199. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1200. wolfSSL_CTX_free(ctx);
  1201. return EXPECT_RESULT();
  1202. }
  1203. #endif
  1204. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1205. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1206. static int test_for_double_Free(void)
  1207. {
  1208. EXPECT_DECLS;
  1209. WOLFSSL_CTX* ctx = NULL;
  1210. WOLFSSL* ssl = NULL;
  1211. int skipTest = 0;
  1212. const char* testCertFile;
  1213. const char* testKeyFile;
  1214. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1215. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1216. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1217. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1218. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1219. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1220. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1221. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1222. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1223. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1224. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1225. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1226. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1227. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1228. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1229. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1230. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1231. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1232. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1233. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1234. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1235. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1236. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1237. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1238. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1239. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1240. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1241. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1242. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1243. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1244. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1245. #ifdef OPENSSL_EXTRA
  1246. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1247. "!SRP:!kRSA:!aNULL:!eNULL";
  1248. #endif
  1249. #ifndef NO_RSA
  1250. testCertFile = svrCertFile;
  1251. testKeyFile = svrKeyFile;
  1252. #elif defined(HAVE_ECC)
  1253. testCertFile = eccCertFile;
  1254. testKeyFile = eccKeyFile;
  1255. #else
  1256. skipTest = 1;
  1257. #endif
  1258. if (skipTest != 1) {
  1259. #ifndef NO_WOLFSSL_SERVER
  1260. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1261. #else
  1262. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1263. #endif
  1264. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1265. WOLFSSL_FILETYPE_PEM));
  1266. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1267. WOLFSSL_FILETYPE_PEM));
  1268. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1269. /* First test freeing SSL, then CTX */
  1270. wolfSSL_free(ssl);
  1271. ssl = NULL;
  1272. wolfSSL_CTX_free(ctx);
  1273. ctx = NULL;
  1274. #ifndef NO_WOLFSSL_CLIENT
  1275. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1276. #else
  1277. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1278. #endif
  1279. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1280. WOLFSSL_FILETYPE_PEM));
  1281. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1282. WOLFSSL_FILETYPE_PEM));
  1283. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1284. /* Next test freeing CTX then SSL */
  1285. wolfSSL_CTX_free(ctx);
  1286. ctx = NULL;
  1287. wolfSSL_free(ssl);
  1288. ssl = NULL;
  1289. #ifndef NO_WOLFSSL_SERVER
  1290. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1291. #else
  1292. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1293. #endif
  1294. /* Test setting ciphers at ctx level */
  1295. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1296. WOLFSSL_FILETYPE_PEM));
  1297. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1298. WOLFSSL_FILETYPE_PEM));
  1299. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1300. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1301. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1302. /* only update TLSv13 suites */
  1303. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1304. #endif
  1305. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1306. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1307. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1308. /* only update pre-TLSv13 suites */
  1309. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1310. "ECDHE-RSA-AES128-GCM-SHA256"));
  1311. #endif
  1312. #ifdef OPENSSL_EXTRA
  1313. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1314. #endif
  1315. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1316. wolfSSL_CTX_free(ctx);
  1317. ctx = NULL;
  1318. wolfSSL_free(ssl);
  1319. ssl = NULL;
  1320. #ifndef NO_WOLFSSL_CLIENT
  1321. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1322. #else
  1323. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1324. #endif
  1325. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1326. WOLFSSL_FILETYPE_PEM));
  1327. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1328. WOLFSSL_FILETYPE_PEM));
  1329. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1330. /* test setting ciphers at SSL level */
  1331. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1332. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1333. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1334. /* only update TLSv13 suites */
  1335. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1336. #endif
  1337. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1338. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1339. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1340. /* only update pre-TLSv13 suites */
  1341. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1342. #endif
  1343. wolfSSL_CTX_free(ctx);
  1344. ctx = NULL;
  1345. wolfSSL_free(ssl);
  1346. ssl = NULL;
  1347. }
  1348. return EXPECT_RESULT();
  1349. }
  1350. #endif
  1351. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1352. {
  1353. EXPECT_DECLS;
  1354. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1355. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1356. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1357. const char* testCertFile;
  1358. const char* testKeyFile;
  1359. WOLFSSL_CTX* ctx = NULL;
  1360. WOLFSSL* ssl = NULL;
  1361. const byte cipherList[] =
  1362. {
  1363. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1364. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1365. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1366. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1367. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1368. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1369. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1370. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1371. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1372. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1373. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1374. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1375. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1376. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1377. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1378. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1379. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1380. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1381. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1382. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1383. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1384. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1385. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1386. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1387. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1388. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1389. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1390. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1391. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1392. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1393. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1394. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1395. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1396. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1397. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1398. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1399. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1400. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1401. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1402. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1403. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1404. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1405. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1406. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1407. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1408. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1409. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1410. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1411. /* SHA256 */
  1412. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1413. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1414. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1415. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1416. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1417. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1418. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1419. /* SHA384 */
  1420. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1421. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1422. /* AES-GCM */
  1423. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1424. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1425. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1426. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1427. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1428. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1429. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1430. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1431. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1432. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1433. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1434. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1435. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1436. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1437. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1438. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1439. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1440. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1441. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1442. * also, in some of the other AES-CCM suites
  1443. * there will be second byte number conflicts
  1444. * with non-ECC AES-GCM */
  1445. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1446. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1447. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1448. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1449. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1450. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1451. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1452. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1453. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1454. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1455. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1456. /* Camellia */
  1457. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1458. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1459. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1460. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1461. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1462. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1463. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1464. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1465. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1466. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1467. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1468. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1469. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1470. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1471. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1472. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1473. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1474. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1475. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1476. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1477. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1478. /* TLS v1.3 cipher suites */
  1479. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1480. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1481. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1482. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1483. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1484. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1485. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1486. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1487. };
  1488. #ifndef NO_RSA
  1489. testCertFile = svrCertFile;
  1490. testKeyFile = svrKeyFile;
  1491. #elif defined(HAVE_ECC)
  1492. testCertFile = eccCertFile;
  1493. testKeyFile = eccKeyFile;
  1494. #endif
  1495. #ifndef NO_WOLFSSL_SERVER
  1496. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1497. #else
  1498. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1499. #endif
  1500. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1501. sizeof(cipherList)));
  1502. wolfSSL_CTX_free(ctx);
  1503. ctx = NULL;
  1504. #ifndef NO_WOLFSSL_SERVER
  1505. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1506. #else
  1507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1508. #endif
  1509. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1510. WOLFSSL_FILETYPE_PEM));
  1511. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1512. WOLFSSL_FILETYPE_PEM));
  1513. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1514. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1515. sizeof(cipherList)));
  1516. wolfSSL_free(ssl);
  1517. wolfSSL_CTX_free(ctx);
  1518. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1519. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1520. return EXPECT_RESULT();
  1521. }
  1522. static int test_wolfSSL_CTX_use_certificate_file(void)
  1523. {
  1524. EXPECT_DECLS;
  1525. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1526. WOLFSSL_CTX *ctx = NULL;
  1527. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1528. /* invalid context */
  1529. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1530. WOLFSSL_FILETYPE_PEM));
  1531. /* invalid cert file */
  1532. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1533. WOLFSSL_FILETYPE_PEM));
  1534. /* invalid cert type */
  1535. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1536. #ifdef NO_RSA
  1537. /* rsa needed */
  1538. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1539. WOLFSSL_FILETYPE_PEM));
  1540. #else
  1541. /* success */
  1542. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1543. WOLFSSL_FILETYPE_PEM));
  1544. #endif
  1545. wolfSSL_CTX_free(ctx);
  1546. #endif
  1547. return EXPECT_RESULT();
  1548. }
  1549. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1550. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1551. {
  1552. EXPECT_DECLS;
  1553. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1554. WOLFSSL_CTX* ctx = NULL;
  1555. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1556. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1557. server_cert_der_2048), WOLFSSL_SUCCESS);
  1558. wolfSSL_CTX_free(ctx);
  1559. #endif
  1560. return EXPECT_RESULT();
  1561. }
  1562. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1563. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1564. * context using buffer.
  1565. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1566. * --enable-testcert flag.
  1567. */
  1568. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1569. {
  1570. EXPECT_DECLS;
  1571. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1572. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1573. WOLFSSL_CTX* ctx = NULL;
  1574. int ret;
  1575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1576. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1577. server_cert_der_2048, sizeof_server_cert_der_2048,
  1578. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1579. wolfSSL_CTX_free(ctx);
  1580. #endif
  1581. return EXPECT_RESULT();
  1582. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1583. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1584. {
  1585. EXPECT_DECLS;
  1586. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1587. WOLFSSL_CTX *ctx = NULL;
  1588. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1589. /* invalid context */
  1590. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1591. WOLFSSL_FILETYPE_PEM));
  1592. /* invalid key file */
  1593. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1594. WOLFSSL_FILETYPE_PEM));
  1595. /* invalid key type */
  1596. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1597. /* success */
  1598. #ifdef NO_RSA
  1599. /* rsa needed */
  1600. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1601. WOLFSSL_FILETYPE_PEM));
  1602. #else
  1603. /* success */
  1604. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1605. WOLFSSL_FILETYPE_PEM));
  1606. #endif
  1607. wolfSSL_CTX_free(ctx);
  1608. #endif
  1609. return EXPECT_RESULT();
  1610. }
  1611. /* test both file and buffer versions along with unloading trusted peer certs */
  1612. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1613. {
  1614. EXPECT_DECLS;
  1615. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1616. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1617. WOLFSSL_CTX *ctx = NULL;
  1618. WOLFSSL* ssl = NULL;
  1619. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1620. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1621. #if !defined(NO_FILESYSTEM)
  1622. /* invalid file */
  1623. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1624. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1625. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1626. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1627. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1628. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1629. /* success */
  1630. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1631. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1632. /* unload cert */
  1633. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1634. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1635. /* invalid file */
  1636. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1637. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1638. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1639. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1640. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1641. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1642. /* success */
  1643. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1644. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1645. #ifdef WOLFSSL_LOCAL_X509_STORE
  1646. /* unload cert */
  1647. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1648. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1649. #endif
  1650. #endif
  1651. /* Test of loading certs from buffers */
  1652. /* invalid buffer */
  1653. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1654. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1655. /* success */
  1656. #ifdef USE_CERT_BUFFERS_1024
  1657. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1658. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1659. #endif
  1660. #ifdef USE_CERT_BUFFERS_2048
  1661. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1662. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1663. #endif
  1664. /* unload cert */
  1665. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1666. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1667. wolfSSL_free(ssl);
  1668. wolfSSL_CTX_free(ctx);
  1669. #endif
  1670. return EXPECT_RESULT();
  1671. }
  1672. static int test_wolfSSL_CTX_load_verify_locations(void)
  1673. {
  1674. EXPECT_DECLS;
  1675. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1676. WOLFSSL_CTX *ctx = NULL;
  1677. #ifndef NO_RSA
  1678. WOLFSSL_CERT_MANAGER* cm = NULL;
  1679. #ifdef PERSIST_CERT_CACHE
  1680. int cacheSz = 0;
  1681. unsigned char* cache = NULL;
  1682. int used = 0;
  1683. #ifndef NO_FILESYSTEM
  1684. const char* cacheFile = "./tests/cert_cache.tmp";
  1685. #endif
  1686. int i;
  1687. int t;
  1688. int* p;
  1689. #endif
  1690. #endif
  1691. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1692. const char* load_certs_path = "./certs/external";
  1693. const char* load_no_certs_path = "./examples";
  1694. const char* load_expired_path = "./certs/test/expired";
  1695. #endif
  1696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1697. /* invalid arguments */
  1698. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1699. WOLFSSL_FAILURE);
  1700. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1701. WOLFSSL_FAILURE);
  1702. /* invalid ca file */
  1703. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1704. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1705. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1706. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1707. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1708. /* invalid path */
  1709. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1710. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1711. #endif
  1712. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1713. /* test ignoring the invalid path */
  1714. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1715. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1716. #endif
  1717. /* load ca cert */
  1718. #ifdef NO_RSA
  1719. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1720. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1721. #else /* Skip the following test without RSA certs. */
  1722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1723. WOLFSSL_SUCCESS);
  1724. #ifdef PERSIST_CERT_CACHE
  1725. /* Get cert cache size */
  1726. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1727. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1728. DYNAMIC_TYPE_TMP_BUFFER));
  1729. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1730. BAD_FUNC_ARG);
  1731. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1732. BAD_FUNC_ARG);
  1733. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1734. BAD_FUNC_ARG);
  1735. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1736. BAD_FUNC_ARG);
  1737. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1738. BAD_FUNC_ARG);
  1739. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1740. BAD_FUNC_ARG);
  1741. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1742. BAD_FUNC_ARG);
  1743. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1744. BAD_FUNC_ARG);
  1745. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1746. BAD_FUNC_ARG);
  1747. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1748. BUFFER_E);
  1749. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1750. ExpectIntEQ(cacheSz, used);
  1751. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1752. BAD_FUNC_ARG);
  1753. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1754. BAD_FUNC_ARG);
  1755. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1756. BAD_FUNC_ARG);
  1757. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1758. BAD_FUNC_ARG);
  1759. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1760. BAD_FUNC_ARG);
  1761. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1762. BAD_FUNC_ARG);
  1763. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1764. BAD_FUNC_ARG);
  1765. /* Smaller than header. */
  1766. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1767. for (i = 1; i < cacheSz; i++) {
  1768. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1769. BUFFER_E);
  1770. }
  1771. if (EXPECT_SUCCESS()) {
  1772. /* Modify header for bad results! */
  1773. p = (int*)cache;
  1774. /* version */
  1775. t = p[0]; p[0] = 0xff;
  1776. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1777. CACHE_MATCH_ERROR);
  1778. p[0] = t; p++;
  1779. /* rows */
  1780. t = p[0]; p[0] = 0xff;
  1781. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1782. CACHE_MATCH_ERROR);
  1783. p[0] = t; p++;
  1784. /* columns[0] */
  1785. t = p[0]; p[0] = -1;
  1786. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1787. PARSE_ERROR);
  1788. p[0] = t; p += CA_TABLE_SIZE;
  1789. /* signerSz*/
  1790. t = p[0]; p[0] = 0xff;
  1791. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1792. CACHE_MATCH_ERROR);
  1793. p[0] = t;
  1794. }
  1795. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1796. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1797. #ifndef NO_FILESYSTEM
  1798. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1799. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1800. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1801. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1802. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1803. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1804. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1805. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1806. WOLFSSL_BAD_FILE);
  1807. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1808. /* File contents is not a cache. */
  1809. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1810. CACHE_MATCH_ERROR);
  1811. #endif
  1812. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1813. #endif
  1814. /* Test unloading CA's */
  1815. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1816. #ifdef PERSIST_CERT_CACHE
  1817. /* Verify no certs (result is less than cacheSz) */
  1818. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1819. #endif
  1820. /* load ca cert again */
  1821. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1822. WOLFSSL_SUCCESS);
  1823. /* Test getting CERT_MANAGER */
  1824. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1825. /* Test unloading CA's using CM */
  1826. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1827. #ifdef PERSIST_CERT_CACHE
  1828. /* Verify no certs (result is less than cacheSz) */
  1829. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1830. #endif
  1831. #endif
  1832. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1833. /* Test loading CA certificates using a path */
  1834. #ifdef NO_RSA
  1835. /* failure here okay since certs in external directory are RSA */
  1836. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1837. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1838. #else
  1839. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1840. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1841. #endif
  1842. /* Test loading path with no files */
  1843. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1844. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1845. /* Test loading expired CA certificates */
  1846. #ifdef NO_RSA
  1847. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1848. load_expired_path,
  1849. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1850. WOLFSSL_SUCCESS);
  1851. #else
  1852. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1853. load_expired_path,
  1854. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1855. WOLFSSL_SUCCESS);
  1856. #endif
  1857. /* Test loading CA certificates and ignoring all errors */
  1858. #ifdef NO_RSA
  1859. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1860. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1861. #else
  1862. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1863. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1864. #endif
  1865. #endif
  1866. wolfSSL_CTX_free(ctx);
  1867. #endif
  1868. return EXPECT_RESULT();
  1869. }
  1870. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1871. {
  1872. int res = TEST_SKIPPED;
  1873. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1874. (!defined(NO_RSA) || defined(HAVE_ECC))
  1875. WOLFSSL_CTX* ctx;
  1876. byte dirValid = 0;
  1877. int ret = 0;
  1878. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1879. if (ctx == NULL) {
  1880. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1881. ret = -1;
  1882. }
  1883. if (ret == 0) {
  1884. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1885. dirValid = 1;
  1886. #else
  1887. word32 numDirs;
  1888. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1889. if (caDirs == NULL || numDirs == 0) {
  1890. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1891. ret = -1;
  1892. }
  1893. else {
  1894. ReadDirCtx dirCtx;
  1895. word32 i;
  1896. for (i = 0; i < numDirs; ++i) {
  1897. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1898. /* Directory isn't empty. */
  1899. dirValid = 1;
  1900. wc_ReadDirClose(&dirCtx);
  1901. break;
  1902. }
  1903. }
  1904. }
  1905. #endif
  1906. }
  1907. /*
  1908. * If the directory isn't empty, we should be able to load CA
  1909. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1910. * usable.
  1911. */
  1912. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1913. WOLFSSL_SUCCESS) {
  1914. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1915. ret = -1;
  1916. }
  1917. #ifdef OPENSSL_EXTRA
  1918. if (ret == 0 &&
  1919. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1920. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1921. ret = -1;
  1922. }
  1923. #endif /* OPENSSL_EXTRA */
  1924. wolfSSL_CTX_free(ctx);
  1925. res = TEST_RES_CHECK(ret == 0);
  1926. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1927. return res;
  1928. }
  1929. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1930. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1931. int file_type)
  1932. {
  1933. int ret;
  1934. WOLFSSL_CERT_MANAGER* cm;
  1935. cm = wolfSSL_CertManagerNew();
  1936. if (cm == NULL) {
  1937. fprintf(stderr, "test_cm_load_ca failed\n");
  1938. return -1;
  1939. }
  1940. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1941. wolfSSL_CertManagerFree(cm);
  1942. return ret;
  1943. }
  1944. static int test_cm_load_ca_file(const char* ca_cert_file)
  1945. {
  1946. int ret = 0;
  1947. byte* cert_buf = NULL;
  1948. size_t cert_sz = 0;
  1949. #if defined(WOLFSSL_PEM_TO_DER)
  1950. DerBuffer* pDer = NULL;
  1951. #endif
  1952. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1953. if (ret == 0) {
  1954. /* normal test */
  1955. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1956. if (ret == WOLFSSL_SUCCESS) {
  1957. /* test including null terminator in length */
  1958. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1959. if (tmp == NULL) {
  1960. ret = MEMORY_E;
  1961. }
  1962. else {
  1963. cert_buf = tmp;
  1964. cert_buf[cert_sz] = '\0';
  1965. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1966. WOLFSSL_FILETYPE_PEM);
  1967. }
  1968. }
  1969. #if defined(WOLFSSL_PEM_TO_DER)
  1970. if (ret == WOLFSSL_SUCCESS) {
  1971. /* test loading DER */
  1972. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1973. if (ret == 0 && pDer != NULL) {
  1974. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1975. WOLFSSL_FILETYPE_ASN1);
  1976. wc_FreeDer(&pDer);
  1977. }
  1978. }
  1979. #endif
  1980. }
  1981. free(cert_buf);
  1982. return ret;
  1983. }
  1984. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1985. int file_type, word32 flags)
  1986. {
  1987. int ret;
  1988. WOLFSSL_CERT_MANAGER* cm;
  1989. cm = wolfSSL_CertManagerNew();
  1990. if (cm == NULL) {
  1991. fprintf(stderr, "test_cm_load_ca failed\n");
  1992. return -1;
  1993. }
  1994. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1995. 0, flags);
  1996. wolfSSL_CertManagerFree(cm);
  1997. return ret;
  1998. }
  1999. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  2000. {
  2001. int ret = 0;
  2002. byte* cert_buf = NULL;
  2003. size_t cert_sz = 0;
  2004. #if defined(WOLFSSL_PEM_TO_DER)
  2005. DerBuffer* pDer = NULL;
  2006. #endif
  2007. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2008. if (ret == 0) {
  2009. /* normal test */
  2010. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  2011. WOLFSSL_FILETYPE_PEM, flags);
  2012. if (ret == WOLFSSL_SUCCESS) {
  2013. /* test including null terminator in length */
  2014. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2015. if (tmp == NULL) {
  2016. ret = MEMORY_E;
  2017. }
  2018. else {
  2019. cert_buf = tmp;
  2020. cert_buf[cert_sz] = '\0';
  2021. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2022. WOLFSSL_FILETYPE_PEM, flags);
  2023. }
  2024. }
  2025. #if defined(WOLFSSL_PEM_TO_DER)
  2026. if (ret == WOLFSSL_SUCCESS) {
  2027. /* test loading DER */
  2028. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2029. if (ret == 0 && pDer != NULL) {
  2030. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2031. WOLFSSL_FILETYPE_ASN1, flags);
  2032. wc_FreeDer(&pDer);
  2033. }
  2034. }
  2035. #endif
  2036. }
  2037. free(cert_buf);
  2038. return ret;
  2039. }
  2040. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2041. static int test_wolfSSL_CertManagerAPI(void)
  2042. {
  2043. EXPECT_DECLS;
  2044. #ifndef NO_CERTS
  2045. WOLFSSL_CERT_MANAGER* cm = NULL;
  2046. unsigned char c;
  2047. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2048. wolfSSL_CertManagerFree(NULL);
  2049. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2050. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2051. #ifdef WOLFSSL_TRUST_PEER_CERT
  2052. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2053. #endif
  2054. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2055. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2056. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2057. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2058. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2059. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2060. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2061. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2062. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2063. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2064. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2065. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2066. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2067. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2068. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2069. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2070. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2071. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2072. WOLFSSL_BAD_FILETYPE);
  2073. #endif
  2074. #if !defined(NO_FILESYSTEM)
  2075. {
  2076. const char* ca_cert = "./certs/ca-cert.pem";
  2077. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2078. const char* ca_cert_der = "./certs/ca-cert.der";
  2079. #endif
  2080. const char* ca_path = "./certs";
  2081. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2082. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2083. BAD_FUNC_ARG);
  2084. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2085. BAD_FUNC_ARG);
  2086. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2087. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2088. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2089. WOLFSSL_BAD_FILETYPE);
  2090. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2092. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2093. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2094. #endif
  2095. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2096. WOLFSSL_FATAL_ERROR);
  2097. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2098. WOLFSSL_FATAL_ERROR);
  2099. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2100. WOLFSSL_FATAL_ERROR);
  2101. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2102. WOLFSSL_FATAL_ERROR);
  2103. }
  2104. #endif
  2105. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2106. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2107. #elif !defined(HAVE_CRL)
  2108. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2109. #endif
  2110. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2111. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2112. #ifdef HAVE_CRL
  2113. /* Test APIs when CRL is disabled. */
  2114. #ifdef HAVE_CRL_IO
  2115. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2116. #endif
  2117. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2118. sizeof_server_cert_der_2048), 1);
  2119. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2120. #endif
  2121. /* OCSP */
  2122. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2123. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2124. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2125. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2126. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2127. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2128. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2129. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2130. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2131. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2132. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2133. #endif
  2134. #ifdef HAVE_OCSP
  2135. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2136. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2137. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2138. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2139. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2140. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2141. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2142. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2143. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2144. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2145. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2146. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2147. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2148. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2149. BAD_FUNC_ARG);
  2150. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2151. BAD_FUNC_ARG);
  2152. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2153. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2154. BAD_FUNC_ARG);
  2155. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2156. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2157. /* Test APIs when OCSP is disabled. */
  2158. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2159. NULL, NULL, NULL, NULL), 1);
  2160. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2161. #endif
  2162. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2163. if (EXPECT_SUCCESS()) {
  2164. wolfSSL_CertManagerFree(cm);
  2165. }
  2166. wolfSSL_CertManagerFree(cm);
  2167. cm = NULL;
  2168. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2169. #ifdef HAVE_OCSP
  2170. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2171. WOLFSSL_OCSP_CHECKALL), 1);
  2172. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2173. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2174. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2175. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2176. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2177. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2178. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2179. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2180. #endif
  2181. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2182. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2183. #endif
  2184. #ifdef WOLFSSL_TRUST_PEER_CERT
  2185. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2186. #endif
  2187. wolfSSL_CertManagerFree(cm);
  2188. #endif
  2189. return EXPECT_RESULT();
  2190. }
  2191. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2192. {
  2193. EXPECT_DECLS;
  2194. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2195. const char* ca_cert = "./certs/ca-cert.pem";
  2196. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2197. int ret;
  2198. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2199. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2200. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2201. #elif defined(NO_RSA)
  2202. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2203. #else
  2204. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2205. #endif
  2206. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2207. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2208. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2209. #elif defined(NO_RSA)
  2210. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2211. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2212. !defined(NO_ASN_TIME)
  2213. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2214. #else
  2215. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2216. #endif
  2217. #endif
  2218. return EXPECT_RESULT();
  2219. }
  2220. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2221. {
  2222. EXPECT_DECLS;
  2223. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2224. const char* ca_cert = "./certs/ca-cert.pem";
  2225. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2226. int ret;
  2227. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2228. 1);
  2229. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2230. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2231. #elif defined(NO_RSA)
  2232. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2233. #else
  2234. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2235. #endif
  2236. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2237. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2238. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2239. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2240. #elif defined(NO_RSA)
  2241. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2242. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2243. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2244. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2245. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2246. #else
  2247. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2248. #endif
  2249. #endif
  2250. return EXPECT_RESULT();
  2251. }
  2252. static int test_wolfSSL_CertManagerGetCerts(void)
  2253. {
  2254. EXPECT_DECLS;
  2255. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2256. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2257. defined(WOLFSSL_SIGNER_DER_CERT)
  2258. WOLFSSL_CERT_MANAGER* cm = NULL;
  2259. WOLFSSL_STACK* sk = NULL;
  2260. X509* x509 = NULL;
  2261. X509* cert1 = NULL;
  2262. FILE* file1 = NULL;
  2263. #ifdef DEBUG_WOLFSSL_VERBOSE
  2264. WOLFSSL_BIO* bio = NULL;
  2265. #endif
  2266. int i = 0;
  2267. int ret = 0;
  2268. const byte* der = NULL;
  2269. int derSz = 0;
  2270. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2271. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2272. if (file1 != NULL) {
  2273. fclose(file1);
  2274. }
  2275. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2276. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2277. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2278. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2279. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2280. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2281. * and full OpenSSL compatibility */
  2282. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2283. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2284. #else
  2285. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2286. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2287. #endif
  2288. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2289. "./certs/ca-cert.pem", NULL));
  2290. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2291. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2292. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2293. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2294. #ifdef DEBUG_WOLFSSL_VERBOSE
  2295. bio = BIO_new(wolfSSL_BIO_s_file());
  2296. if (bio != NULL) {
  2297. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2298. X509_print(bio, x509);
  2299. BIO_free(bio);
  2300. }
  2301. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2302. }
  2303. wolfSSL_X509_free(cert1);
  2304. sk_X509_pop_free(sk, NULL);
  2305. wolfSSL_CertManagerFree(cm);
  2306. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2307. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2308. defined(WOLFSSL_SIGNER_DER_CERT) */
  2309. return EXPECT_RESULT();
  2310. }
  2311. static int test_wolfSSL_CertManagerSetVerify(void)
  2312. {
  2313. EXPECT_DECLS;
  2314. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2315. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2316. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2317. WOLFSSL_CERT_MANAGER* cm = NULL;
  2318. int tmp = myVerifyAction;
  2319. const char* ca_cert = "./certs/ca-cert.pem";
  2320. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2321. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2322. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2323. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2324. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2325. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2326. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2327. #else
  2328. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2329. WOLFSSL_SUCCESS);
  2330. #endif
  2331. /* Use the test CB that always accepts certs */
  2332. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2333. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2334. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2335. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2336. {
  2337. const char* verifyCert = "./certs/server-cert.der";
  2338. /* Use the test CB that always fails certs */
  2339. myVerifyAction = VERIFY_FORCE_FAIL;
  2340. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2341. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2342. }
  2343. #endif
  2344. wolfSSL_CertManagerFree(cm);
  2345. myVerifyAction = tmp;
  2346. #endif
  2347. return EXPECT_RESULT();
  2348. }
  2349. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2350. defined(DEBUG_UNIT_TEST_CERTS)
  2351. /* Used when debugging name constraint tests. Not static to allow use in
  2352. * multiple locations with complex define guards. */
  2353. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2354. {
  2355. BIO* out = BIO_new_file(fileName, "wb");
  2356. if (out != NULL) {
  2357. PEM_write_bio_X509(out, x509);
  2358. BIO_free(out);
  2359. }
  2360. }
  2361. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2362. {
  2363. BIO* out = BIO_new_file(fileName, "wb");
  2364. if (out != NULL) {
  2365. BIO_write(out, der, derSz);
  2366. BIO_free(out);
  2367. }
  2368. }
  2369. #else
  2370. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2371. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2372. #endif
  2373. static int test_wolfSSL_CertManagerNameConstraint(void)
  2374. {
  2375. EXPECT_DECLS;
  2376. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2377. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2378. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2379. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2380. !defined(NO_SHA256)
  2381. WOLFSSL_CERT_MANAGER* cm = NULL;
  2382. WOLFSSL_EVP_PKEY *priv = NULL;
  2383. WOLFSSL_X509_NAME* name = NULL;
  2384. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2385. const char* server_cert = "./certs/test/server-goodcn.pem";
  2386. int i = 0;
  2387. static const byte extNameConsOid[] = {85, 29, 30};
  2388. RsaKey key;
  2389. WC_RNG rng;
  2390. byte *der = NULL;
  2391. int derSz = 0;
  2392. word32 idx = 0;
  2393. byte *pt;
  2394. WOLFSSL_X509 *x509 = NULL;
  2395. WOLFSSL_X509 *ca = NULL;
  2396. wc_InitRng(&rng);
  2397. /* load in CA private key for signing */
  2398. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2399. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2400. sizeof_server_key_der_2048), 0);
  2401. /* get ca certificate then alter it */
  2402. ExpectNotNull(der =
  2403. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2404. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2405. WOLFSSL_FILETYPE_ASN1));
  2406. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2407. if (EXPECT_SUCCESS() && (der != NULL)) {
  2408. XMEMCPY(der, pt, derSz);
  2409. /* find the name constraint extension and alter it */
  2410. pt = der;
  2411. for (i = 0; i < derSz - 3; i++) {
  2412. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2413. pt += 3;
  2414. break;
  2415. }
  2416. pt++;
  2417. }
  2418. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2419. /* go to the length value and set it to 0 */
  2420. while (i < derSz && *pt != 0x81) {
  2421. pt++;
  2422. i++;
  2423. }
  2424. ExpectIntNE(i, derSz); /* did not place to alter */
  2425. pt++;
  2426. *pt = 0x00;
  2427. }
  2428. /* resign the altered certificate */
  2429. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2430. FOURK_BUF, &key, NULL, &rng)), 0);
  2431. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2432. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2433. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2434. wolfSSL_CertManagerFree(cm);
  2435. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2436. wolfSSL_X509_free(x509);
  2437. wc_FreeRsaKey(&key);
  2438. wc_FreeRng(&rng);
  2439. /* add email alt name to satisfy constraint */
  2440. pt = (byte*)server_key_der_2048;
  2441. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2442. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2443. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2444. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2445. WOLFSSL_FILETYPE_ASN1));
  2446. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2447. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2448. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2449. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2450. /* Good cert test with proper alt email name */
  2451. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2452. WOLFSSL_FILETYPE_PEM));
  2453. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2454. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2455. name = NULL;
  2456. ExpectNotNull(name = X509_NAME_new());
  2457. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2458. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2459. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2460. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2461. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2462. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2463. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2464. X509_NAME_free(name);
  2465. name = NULL;
  2466. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2467. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2468. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2469. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2470. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2471. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2472. wolfSSL_X509_free(x509);
  2473. x509 = NULL;
  2474. /* Cert with bad alt name list */
  2475. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2476. WOLFSSL_FILETYPE_PEM));
  2477. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2478. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2479. name = NULL;
  2480. ExpectNotNull(name = X509_NAME_new());
  2481. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2482. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2483. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2484. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2485. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2486. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2487. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2488. X509_NAME_free(name);
  2489. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2490. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2491. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2492. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2493. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2494. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2495. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2496. wolfSSL_CertManagerFree(cm);
  2497. wolfSSL_X509_free(x509);
  2498. wolfSSL_X509_free(ca);
  2499. wolfSSL_EVP_PKEY_free(priv);
  2500. #endif
  2501. return EXPECT_RESULT();
  2502. }
  2503. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2504. {
  2505. EXPECT_DECLS;
  2506. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2507. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2508. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2509. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2510. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2511. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2512. const char* server_cert = "./certs/server-cert.pem";
  2513. WOLFSSL_CERT_MANAGER* cm = NULL;
  2514. WOLFSSL_X509 *x509 = NULL;
  2515. WOLFSSL_X509 *ca = NULL;
  2516. const unsigned char *der = NULL;
  2517. const unsigned char *pt;
  2518. WOLFSSL_EVP_PKEY *priv = NULL;
  2519. WOLFSSL_X509_NAME* name = NULL;
  2520. int derSz = 0;
  2521. /* C=US*/
  2522. char altName[] = {
  2523. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2524. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2525. };
  2526. /* C=ID */
  2527. char altNameFail[] = {
  2528. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2529. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2530. };
  2531. /* C=US ST=California*/
  2532. char altNameExc[] = {
  2533. 0x30, 0x22,
  2534. 0x31, 0x0B,
  2535. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2536. 0x31, 0x13,
  2537. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2538. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2539. };
  2540. /* load in CA private key for signing */
  2541. pt = ca_key_der_2048;
  2542. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2543. sizeof_ca_key_der_2048));
  2544. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2545. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2546. WOLFSSL_FILETYPE_ASN1));
  2547. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2548. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2549. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2550. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2551. WOLFSSL_FILETYPE_PEM));
  2552. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2553. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2554. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2555. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2556. #else
  2557. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2558. #endif
  2559. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2560. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2561. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2562. /* add in matching DIR alt name and resign */
  2563. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2564. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2565. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2566. #else
  2567. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2568. #endif
  2569. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2570. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2571. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2572. wolfSSL_X509_free(x509);
  2573. x509 = NULL;
  2574. /* check verify fail */
  2575. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2576. WOLFSSL_FILETYPE_PEM));
  2577. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2578. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2579. /* add in miss matching DIR alt name and resign */
  2580. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2581. ASN_DIR_TYPE);
  2582. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2583. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2584. #else
  2585. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2586. #endif
  2587. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2588. #ifndef WOLFSSL_NO_ASN_STRICT
  2589. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2590. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2591. #else
  2592. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2593. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2594. #endif
  2595. /* check that it still fails if one bad altname and one good altname is in
  2596. * the certificate */
  2597. wolfSSL_X509_free(x509);
  2598. x509 = NULL;
  2599. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2600. WOLFSSL_FILETYPE_PEM));
  2601. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2602. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2603. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2604. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2605. ASN_DIR_TYPE);
  2606. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2607. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2608. #else
  2609. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2610. #endif
  2611. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2612. #ifndef WOLFSSL_NO_ASN_STRICT
  2613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2615. #else
  2616. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2617. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2618. #endif
  2619. /* check it fails with switching position of bad altname */
  2620. wolfSSL_X509_free(x509);
  2621. x509 = NULL;
  2622. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2623. WOLFSSL_FILETYPE_PEM));
  2624. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2625. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2626. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2627. ASN_DIR_TYPE);
  2628. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2629. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2630. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2631. #else
  2632. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2633. #endif
  2634. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2635. #ifndef WOLFSSL_NO_ASN_STRICT
  2636. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2637. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2638. #else
  2639. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2640. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2641. #endif
  2642. wolfSSL_CertManagerFree(cm);
  2643. wolfSSL_X509_free(x509);
  2644. x509 = NULL;
  2645. wolfSSL_X509_free(ca);
  2646. ca = NULL;
  2647. /* now test with excluded name constraint */
  2648. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2649. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2650. WOLFSSL_FILETYPE_ASN1));
  2651. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2652. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2653. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2654. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2655. WOLFSSL_FILETYPE_PEM));
  2656. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2657. ASN_DIR_TYPE);
  2658. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2659. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2660. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2661. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2662. #else
  2663. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2664. #endif
  2665. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2666. #ifndef WOLFSSL_NO_ASN_STRICT
  2667. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2668. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2669. #else
  2670. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2671. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2672. #endif
  2673. wolfSSL_CertManagerFree(cm);
  2674. wolfSSL_X509_free(x509);
  2675. wolfSSL_X509_free(ca);
  2676. wolfSSL_EVP_PKEY_free(priv);
  2677. #endif
  2678. return EXPECT_RESULT();
  2679. }
  2680. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2681. {
  2682. EXPECT_DECLS;
  2683. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2684. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2685. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2686. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2687. !defined(NO_SHA256)
  2688. WOLFSSL_CERT_MANAGER* cm = NULL;
  2689. WOLFSSL_EVP_PKEY *priv = NULL;
  2690. WOLFSSL_X509_NAME* name = NULL;
  2691. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2692. const char* server_cert = "./certs/test/server-goodcn.pem";
  2693. byte *der = NULL;
  2694. int derSz = 0;
  2695. byte *pt;
  2696. WOLFSSL_X509 *x509 = NULL;
  2697. WOLFSSL_X509 *ca = NULL;
  2698. pt = (byte*)server_key_der_2048;
  2699. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2700. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2701. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2702. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2703. WOLFSSL_FILETYPE_ASN1));
  2704. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2705. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2706. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2707. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2708. /* check satisfying .wolfssl.com constraint passes */
  2709. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2710. WOLFSSL_FILETYPE_PEM));
  2711. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2712. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2713. name = NULL;
  2714. ExpectNotNull(name = X509_NAME_new());
  2715. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2716. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2717. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2718. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2719. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2720. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2721. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2722. X509_NAME_free(name);
  2723. name = NULL;
  2724. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2725. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2726. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2727. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2728. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2729. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2730. wolfSSL_X509_free(x509);
  2731. x509 = NULL;
  2732. /* check satisfying .random.com constraint passes */
  2733. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2734. WOLFSSL_FILETYPE_PEM));
  2735. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2736. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2737. name = NULL;
  2738. ExpectNotNull(name = X509_NAME_new());
  2739. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2740. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2741. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2742. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2743. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2744. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2745. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2746. X509_NAME_free(name);
  2747. name = NULL;
  2748. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2749. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2750. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2751. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2752. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2753. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2754. wolfSSL_X509_free(x509);
  2755. x509 = NULL;
  2756. /* check fail case when neither constraint is matched */
  2757. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2758. WOLFSSL_FILETYPE_PEM));
  2759. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2760. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2761. name = NULL;
  2762. ExpectNotNull(name = X509_NAME_new());
  2763. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2764. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2765. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2766. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2767. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2768. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2769. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2770. X509_NAME_free(name);
  2771. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2772. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2773. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2774. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2775. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2776. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2777. wolfSSL_CertManagerFree(cm);
  2778. wolfSSL_X509_free(x509);
  2779. wolfSSL_X509_free(ca);
  2780. wolfSSL_EVP_PKEY_free(priv);
  2781. #endif
  2782. return EXPECT_RESULT();
  2783. }
  2784. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2785. {
  2786. EXPECT_DECLS;
  2787. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2788. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2789. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2790. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2791. !defined(NO_SHA256)
  2792. WOLFSSL_CERT_MANAGER* cm = NULL;
  2793. WOLFSSL_EVP_PKEY *priv = NULL;
  2794. WOLFSSL_X509_NAME* name = NULL;
  2795. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2796. const char* server_cert = "./certs/test/server-goodcn.pem";
  2797. byte *der = NULL;
  2798. int derSz;
  2799. byte *pt;
  2800. WOLFSSL_X509 *x509 = NULL;
  2801. WOLFSSL_X509 *ca = NULL;
  2802. pt = (byte*)server_key_der_2048;
  2803. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2804. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2805. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2806. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2807. WOLFSSL_FILETYPE_ASN1));
  2808. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2809. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2810. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2811. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2812. /* check satisfying wolfssl.com constraint passes */
  2813. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2814. WOLFSSL_FILETYPE_PEM));
  2815. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2816. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2817. name = NULL;
  2818. ExpectNotNull(name = X509_NAME_new());
  2819. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2820. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2821. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2822. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2823. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2824. X509_NAME_free(name);
  2825. name = NULL;
  2826. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2827. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2828. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2829. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2830. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2831. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2832. wolfSSL_X509_free(x509);
  2833. x509 = NULL;
  2834. /* check satisfying example.com constraint passes */
  2835. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2836. WOLFSSL_FILETYPE_PEM));
  2837. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2838. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2839. name = NULL;
  2840. ExpectNotNull(name = X509_NAME_new());
  2841. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2842. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2843. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2844. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2845. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2846. X509_NAME_free(name);
  2847. name = NULL;
  2848. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2849. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2850. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2851. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2852. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2853. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2854. wolfSSL_X509_free(x509);
  2855. x509 = NULL;
  2856. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2857. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2858. WOLFSSL_FILETYPE_PEM));
  2859. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2860. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2861. name = NULL;
  2862. ExpectNotNull(name = X509_NAME_new());
  2863. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2864. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2865. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2866. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2867. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2868. X509_NAME_free(name);
  2869. name = NULL;
  2870. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2871. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2872. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2873. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2874. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2875. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2876. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2877. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2878. wolfSSL_X509_free(x509);
  2879. x509 = NULL;
  2880. /* check fail when one DNS in the list is bad */
  2881. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2882. WOLFSSL_FILETYPE_PEM));
  2883. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2884. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2885. name = NULL;
  2886. ExpectNotNull(name = X509_NAME_new());
  2887. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2888. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2889. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2890. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2891. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2892. X509_NAME_free(name);
  2893. name = NULL;
  2894. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2895. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2896. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2897. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2898. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2899. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2900. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2901. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2902. wolfSSL_X509_free(x509);
  2903. x509 = NULL;
  2904. /* check fail case when neither constraint is matched */
  2905. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2906. WOLFSSL_FILETYPE_PEM));
  2907. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2908. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2909. name = NULL;
  2910. ExpectNotNull(name = X509_NAME_new());
  2911. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2912. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2913. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2914. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2915. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2916. X509_NAME_free(name);
  2917. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2918. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2919. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2920. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2921. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2922. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2923. wolfSSL_CertManagerFree(cm);
  2924. wolfSSL_X509_free(x509);
  2925. wolfSSL_X509_free(ca);
  2926. wolfSSL_EVP_PKEY_free(priv);
  2927. #endif
  2928. return EXPECT_RESULT();
  2929. }
  2930. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2931. {
  2932. EXPECT_DECLS;
  2933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2934. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2935. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2936. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2937. !defined(NO_SHA256)
  2938. WOLFSSL_CERT_MANAGER* cm = NULL;
  2939. WOLFSSL_EVP_PKEY *priv = NULL;
  2940. WOLFSSL_X509_NAME* name = NULL;
  2941. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2942. const char* server_cert = "./certs/test/server-goodcn.pem";
  2943. byte *der = NULL;
  2944. int derSz;
  2945. byte *pt;
  2946. WOLFSSL_X509 *x509 = NULL;
  2947. WOLFSSL_X509 *ca = NULL;
  2948. pt = (byte*)server_key_der_2048;
  2949. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2950. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2951. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2952. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2953. WOLFSSL_FILETYPE_ASN1));
  2954. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2955. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2956. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2957. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2958. /* check satisfying wolfssl.com constraint passes */
  2959. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2960. WOLFSSL_FILETYPE_PEM));
  2961. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2962. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2963. name = NULL;
  2964. ExpectNotNull(name = X509_NAME_new());
  2965. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2966. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2967. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2968. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2969. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2970. X509_NAME_free(name);
  2971. name = NULL;
  2972. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2973. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2974. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2975. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2976. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2977. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2978. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2979. wolfSSL_X509_free(x509);
  2980. x509 = NULL;
  2981. /* fail with DNS check because of common name */
  2982. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2983. WOLFSSL_FILETYPE_PEM));
  2984. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2985. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2986. name = NULL;
  2987. ExpectNotNull(name = X509_NAME_new());
  2988. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2989. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2990. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2991. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2992. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2993. X509_NAME_free(name);
  2994. name = NULL;
  2995. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2996. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2997. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2998. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2999. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3000. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3001. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3002. wolfSSL_X509_free(x509);
  3003. x509 = NULL;
  3004. /* fail on permitted DNS name constraint */
  3005. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3006. WOLFSSL_FILETYPE_PEM));
  3007. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3008. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3009. name = NULL;
  3010. ExpectNotNull(name = X509_NAME_new());
  3011. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3012. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3013. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3014. X509_NAME_free(name);
  3015. name = NULL;
  3016. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3017. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3018. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3019. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3020. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3021. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3022. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3023. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3024. wolfSSL_X509_free(x509);
  3025. x509 = NULL;
  3026. /* fail on permitted email name constraint */
  3027. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3028. WOLFSSL_FILETYPE_PEM));
  3029. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3030. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3031. name = NULL;
  3032. ExpectNotNull(name = X509_NAME_new());
  3033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3034. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3035. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3036. X509_NAME_free(name);
  3037. name = NULL;
  3038. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3039. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3040. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3041. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3042. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3043. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3044. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3045. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3046. wolfSSL_X509_free(x509);
  3047. x509 = NULL;
  3048. /* success with empty email name */
  3049. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3050. WOLFSSL_FILETYPE_PEM));
  3051. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3052. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3053. name = NULL;
  3054. ExpectNotNull(name = X509_NAME_new());
  3055. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3056. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3057. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3058. X509_NAME_free(name);
  3059. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3060. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3061. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3062. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3063. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3064. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3065. wolfSSL_X509_free(x509);
  3066. wolfSSL_CertManagerFree(cm);
  3067. wolfSSL_X509_free(ca);
  3068. wolfSSL_EVP_PKEY_free(priv);
  3069. #endif
  3070. return EXPECT_RESULT();
  3071. }
  3072. static int test_wolfSSL_CertManagerCRL(void)
  3073. {
  3074. EXPECT_DECLS;
  3075. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3076. !defined(NO_RSA)
  3077. const char* ca_cert = "./certs/ca-cert.pem";
  3078. const char* crl1 = "./certs/crl/crl.pem";
  3079. const char* crl2 = "./certs/crl/crl2.pem";
  3080. #ifdef WC_RSA_PSS
  3081. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3082. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3083. #endif
  3084. const unsigned char crl_buff[] = {
  3085. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3086. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3087. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3088. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3089. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3090. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3091. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3092. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3093. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3094. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3095. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3096. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3097. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3098. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3099. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3100. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3101. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3102. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3103. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3104. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3105. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3106. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3107. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3108. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3109. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3110. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3111. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3112. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3113. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3114. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3115. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3116. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3117. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3118. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3119. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3120. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3121. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3122. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3123. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3124. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3125. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3126. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3127. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3128. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3129. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3130. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3131. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3132. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3133. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3134. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3135. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3136. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3137. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3138. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3139. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3140. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3141. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3142. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3143. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3144. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3145. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3146. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3147. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3148. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3149. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3150. };
  3151. WOLFSSL_CERT_MANAGER* cm = NULL;
  3152. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3153. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3154. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3155. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3156. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3157. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3158. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3159. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3160. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3161. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3162. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3163. BAD_FUNC_ARG);
  3164. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3165. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3166. BAD_FUNC_ARG);
  3167. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3168. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3169. BAD_FUNC_ARG);
  3170. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3171. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3172. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3173. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3174. #ifdef HAVE_CRL_IO
  3175. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3176. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3177. #endif
  3178. #ifndef NO_FILESYSTEM
  3179. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3180. 0), BAD_FUNC_ARG);
  3181. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3182. 0), BAD_FUNC_ARG);
  3183. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3184. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3185. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3186. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3187. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3188. BAD_FUNC_ARG);
  3189. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3190. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3191. ASN_PARSE_E);
  3192. #endif
  3193. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3194. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3195. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3196. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3197. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3198. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3199. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3200. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3201. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3202. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3203. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3204. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3205. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3206. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3207. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3208. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3209. ExpectIntEQ(WOLFSSL_SUCCESS,
  3210. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3211. ExpectIntEQ(WOLFSSL_SUCCESS,
  3212. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3213. ExpectIntEQ(WOLFSSL_SUCCESS,
  3214. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3215. wolfSSL_CertManagerFreeCRL(cm);
  3216. ExpectIntEQ(WOLFSSL_SUCCESS,
  3217. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3218. ExpectIntEQ(WOLFSSL_SUCCESS,
  3219. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3220. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3221. sizeof_server_cert_der_2048), CRL_MISSING);
  3222. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3223. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3224. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3225. WOLFSSL_FILETYPE_ASN1), 1);
  3226. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3227. /* loading should fail without the CA set */
  3228. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3229. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3230. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3231. ExpectIntEQ(WOLFSSL_SUCCESS,
  3232. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3233. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3234. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3235. #endif
  3236. wolfSSL_CertManagerFree(cm);
  3237. #endif
  3238. return EXPECT_RESULT();
  3239. }
  3240. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3241. {
  3242. EXPECT_DECLS;
  3243. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3244. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3245. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3246. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3247. defined(HAVE_LIGHTY)
  3248. WOLFSSL_CERT_MANAGER* cm = NULL;
  3249. /* Raw OCSP response bytes captured using the following setup:
  3250. * - Run responder with
  3251. * openssl ocsp -port 9999 -ndays 9999
  3252. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3253. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3254. * -rkey certs/ocsp/ocsp-responder-key.pem
  3255. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3256. * - Run client with
  3257. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3258. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3259. * -cert certs/ocsp/server1-cert.pem
  3260. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3261. * - Select the response packet in Wireshark, and export it using
  3262. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3263. * packets only". After importing into the editor, remove the initial
  3264. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3265. */
  3266. static const byte response[] = {
  3267. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3268. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3269. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3270. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3271. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3272. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3273. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3274. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3275. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3276. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3277. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3278. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3279. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3280. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3281. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3282. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3283. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3284. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3285. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3286. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3287. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3288. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3289. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3290. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3291. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3292. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3293. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3294. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3295. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3296. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3297. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3298. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3299. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3300. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3301. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3302. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3303. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3304. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3305. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3306. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3307. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3308. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3309. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3310. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3311. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3312. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3313. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3314. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3315. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3316. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3317. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3318. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3319. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3320. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3321. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3322. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3323. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3324. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3325. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3326. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3327. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3328. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3329. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3330. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3331. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3332. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3333. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3334. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3335. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3336. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3337. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3338. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3339. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3340. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3341. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3342. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3343. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3344. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3345. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3346. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3347. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3348. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3349. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3350. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3351. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3352. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3353. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3354. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3355. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3356. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3357. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3358. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3359. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3360. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3361. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3362. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3363. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3364. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3365. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3366. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3367. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3368. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3369. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3370. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3371. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3372. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3373. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3374. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3375. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3376. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3377. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3378. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3379. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3380. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3381. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3382. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3383. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3384. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3385. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3386. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3387. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3388. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3389. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3390. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3391. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3392. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3393. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3394. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3395. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3396. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3397. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3398. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3399. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3400. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3401. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3402. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3403. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3404. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3405. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3406. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3407. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3408. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3409. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3410. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3411. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3412. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3413. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3414. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3415. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3416. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3417. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3418. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3419. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3420. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3421. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3422. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3423. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3424. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3425. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3426. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3427. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3428. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3429. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3430. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3431. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3432. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3433. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3434. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3435. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3436. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3437. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3438. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3439. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3440. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3441. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3442. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3443. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3444. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3445. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3446. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3447. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3448. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3449. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3450. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3451. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3452. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3453. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3454. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3455. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3456. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3457. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3458. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3459. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3460. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3461. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3462. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3463. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3464. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3465. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3466. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3467. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3468. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3469. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3470. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3471. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3472. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3473. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3474. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3475. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3476. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3477. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3478. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3479. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3480. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3481. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3482. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3483. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3484. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3485. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3486. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3487. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3488. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3489. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3490. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3491. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3492. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3493. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3494. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3495. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3496. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3497. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3498. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3499. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3500. };
  3501. OcspEntry entry[1];
  3502. CertStatus status[1];
  3503. OcspRequest* request = NULL;
  3504. #ifndef NO_FILESYSTEM
  3505. const char* ca_cert = "./certs/ca-cert.pem";
  3506. #endif
  3507. byte serial[] = {0x05};
  3508. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3509. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3510. XMEMSET(entry, 0, sizeof(OcspEntry));
  3511. XMEMSET(status, 0, sizeof(CertStatus));
  3512. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3513. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3514. DYNAMIC_TYPE_OCSP_REQUEST));
  3515. if ((request != NULL) && (request->serial != NULL)) {
  3516. request->serialSz = sizeof(serial);
  3517. XMEMCPY(request->serial, serial, sizeof(serial));
  3518. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3519. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3520. }
  3521. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3522. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3523. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3524. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3525. /* Response should be valid. */
  3526. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3527. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3528. /* Flip a byte in the request serial number, response should be invalid
  3529. * now. */
  3530. if ((request != NULL) && (request->serial != NULL))
  3531. request->serial[0] ^= request->serial[0];
  3532. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3533. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3534. #ifndef NO_FILESYSTEM
  3535. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3536. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3537. ExpectIntEQ(WOLFSSL_SUCCESS,
  3538. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3539. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3540. sizeof(server_cert_der_2048)), 1);
  3541. #endif
  3542. wolfSSL_OCSP_REQUEST_free(request);
  3543. wolfSSL_CertManagerFree(cm);
  3544. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3545. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3546. #endif /* HAVE_OCSP */
  3547. return EXPECT_RESULT();
  3548. }
  3549. static int test_wolfSSL_CheckOCSPResponse(void)
  3550. {
  3551. EXPECT_DECLS;
  3552. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3553. defined(OPENSSL_ALL)
  3554. const char* responseFile = "./certs/ocsp/test-response.der";
  3555. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3556. const char* responseNoInternFile =
  3557. "./certs/ocsp/test-response-nointern.der";
  3558. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3559. OcspResponse* res = NULL;
  3560. byte data[4096];
  3561. const unsigned char* pt;
  3562. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3563. * gcc sanitizer with --enable-heapmath.
  3564. */
  3565. XFILE f = XBADFILE;
  3566. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3567. WOLFSSL_X509_STORE* st = NULL;
  3568. WOLFSSL_X509* issuer = NULL;
  3569. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3570. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3571. if (f != XBADFILE) {
  3572. XFCLOSE(f);
  3573. f = XBADFILE;
  3574. }
  3575. pt = data;
  3576. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3577. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3578. SSL_FILETYPE_PEM));
  3579. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3580. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3581. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3582. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3583. wolfSSL_OCSP_BASICRESP_free(bs);
  3584. bs = NULL;
  3585. wolfSSL_OCSP_RESPONSE_free(res);
  3586. res = NULL;
  3587. wolfSSL_X509_STORE_free(st);
  3588. st = NULL;
  3589. wolfSSL_X509_free(issuer);
  3590. issuer = NULL;
  3591. /* check loading a response with optional certs */
  3592. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3593. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3594. if (f != XBADFILE)
  3595. XFCLOSE(f);
  3596. f = XBADFILE;
  3597. pt = data;
  3598. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3599. wolfSSL_OCSP_RESPONSE_free(res);
  3600. res = NULL;
  3601. /* check loading a response with multiple certs */
  3602. {
  3603. WOLFSSL_CERT_MANAGER* cm = NULL;
  3604. OcspEntry *entry = NULL;
  3605. CertStatus* status = NULL;
  3606. OcspRequest* request = NULL;
  3607. byte serial1[] = {0x01};
  3608. byte serial[] = {0x02};
  3609. byte issuerHash[] = {
  3610. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3611. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3612. 0x37, 0x15, 0x8A, 0x88
  3613. };
  3614. byte issuerKeyHash[] = {
  3615. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3616. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3617. 0x7E, 0x72, 0x15, 0x21
  3618. };
  3619. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3620. DYNAMIC_TYPE_OPENSSL));
  3621. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3622. DYNAMIC_TYPE_OPENSSL));
  3623. if (entry != NULL)
  3624. XMEMSET(entry, 0, sizeof(OcspEntry));
  3625. if (status != NULL)
  3626. XMEMSET(status, 0, sizeof(CertStatus));
  3627. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3628. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3629. DYNAMIC_TYPE_OCSP_REQUEST));
  3630. if (request != NULL && request->serial != NULL) {
  3631. request->serialSz = sizeof(serial);
  3632. XMEMCPY(request->serial, serial, sizeof(serial));
  3633. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3634. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3635. sizeof(issuerKeyHash));
  3636. }
  3637. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3638. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3639. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3640. WOLFSSL_SUCCESS);
  3641. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3642. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3643. if (f != XBADFILE)
  3644. XFCLOSE(f);
  3645. f = XBADFILE;
  3646. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3647. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3648. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3649. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3650. ExpectNotNull(entry->status);
  3651. if (request != NULL && request->serial != NULL)
  3652. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3653. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3654. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3655. /* store both status's in the entry to check that "next" is not
  3656. * overwritten */
  3657. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3658. status->next = entry->status;
  3659. entry->status = status;
  3660. }
  3661. if (request != NULL && request->serial != NULL)
  3662. XMEMCPY(request->serial, serial, sizeof(serial));
  3663. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3664. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3665. ExpectNotNull(entry->status->next);
  3666. /* compare the status found */
  3667. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3668. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3669. status->serialSz), 0);
  3670. if (status != NULL && entry != NULL && entry->status != status) {
  3671. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3672. }
  3673. wolfSSL_OCSP_CERTID_free(entry);
  3674. wolfSSL_OCSP_REQUEST_free(request);
  3675. wolfSSL_CertManagerFree(cm);
  3676. }
  3677. #if defined(WC_RSA_PSS)
  3678. {
  3679. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3680. /* check loading a response with RSA-PSS signature */
  3681. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3682. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3683. if (f != XBADFILE)
  3684. XFCLOSE(f);
  3685. pt = data;
  3686. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3687. /* try to verify the response */
  3688. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3689. SSL_FILETYPE_PEM));
  3690. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3691. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3692. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3693. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3694. WOLFSSL_SUCCESS);
  3695. wolfSSL_OCSP_BASICRESP_free(bs);
  3696. wolfSSL_OCSP_RESPONSE_free(res);
  3697. wolfSSL_X509_STORE_free(st);
  3698. wolfSSL_X509_free(issuer);
  3699. }
  3700. #endif
  3701. #endif /* HAVE_OCSP */
  3702. return EXPECT_RESULT();
  3703. }
  3704. static int test_wolfSSL_FPKI(void)
  3705. {
  3706. EXPECT_DECLS;
  3707. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3708. XFILE f = XBADFILE;
  3709. const char* fpkiCert = "./certs/fpki-cert.der";
  3710. DecodedCert cert;
  3711. byte buf[4096];
  3712. byte* uuid = NULL;
  3713. byte* fascn = NULL;
  3714. word32 fascnSz;
  3715. word32 uuidSz;
  3716. int bytes = 0;
  3717. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3718. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3719. if (f != XBADFILE)
  3720. XFCLOSE(f);
  3721. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3722. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3723. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3724. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3725. DYNAMIC_TYPE_TMP_BUFFER));
  3726. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3727. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3728. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3729. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3730. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3731. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3732. wc_FreeDecodedCert(&cert);
  3733. #endif
  3734. return EXPECT_RESULT();
  3735. }
  3736. /* use RID in confuncture with other names to test parsing of unknown other
  3737. * names */
  3738. static int test_wolfSSL_OtherName(void)
  3739. {
  3740. EXPECT_DECLS;
  3741. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3742. XFILE f = XBADFILE;
  3743. const char* ridCert = "./certs/rid-cert.der";
  3744. DecodedCert cert;
  3745. byte buf[4096];
  3746. int bytes = 0;
  3747. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3748. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3749. if (f != XBADFILE)
  3750. XFCLOSE(f);
  3751. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3752. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3753. wc_FreeDecodedCert(&cert);
  3754. #endif
  3755. return EXPECT_RESULT();
  3756. }
  3757. static int test_wolfSSL_CertRsaPss(void)
  3758. {
  3759. EXPECT_DECLS;
  3760. /* FIPS v2 and below don't support long salts. */
  3761. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3762. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3763. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3764. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3765. XFILE f = XBADFILE;
  3766. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3767. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3768. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3769. RSA_MAX_SIZE >= 3072
  3770. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3771. #endif
  3772. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3773. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3774. #endif
  3775. DecodedCert cert;
  3776. byte buf[4096];
  3777. int bytes = 0;
  3778. WOLFSSL_CERT_MANAGER* cm = NULL;
  3779. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3780. ExpectIntEQ(WOLFSSL_SUCCESS,
  3781. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3782. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3783. ExpectIntEQ(WOLFSSL_SUCCESS,
  3784. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3785. #endif
  3786. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3787. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3788. if (f != XBADFILE) {
  3789. XFCLOSE(f);
  3790. f = XBADFILE;
  3791. }
  3792. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3793. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3794. wc_FreeDecodedCert(&cert);
  3795. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3796. RSA_MAX_SIZE >= 3072
  3797. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3798. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3799. if (f != XBADFILE)
  3800. XFCLOSE(f);
  3801. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3802. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3803. wc_FreeDecodedCert(&cert);
  3804. #endif
  3805. wolfSSL_CertManagerFree(cm);
  3806. #endif
  3807. return EXPECT_RESULT();
  3808. }
  3809. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3810. {
  3811. EXPECT_DECLS;
  3812. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3813. !defined(NO_WOLFSSL_CLIENT)
  3814. WOLFSSL_CTX* ctx = NULL;
  3815. const char* ca_cert = "./certs/ca-cert.pem";
  3816. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3817. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3818. /* test good CA */
  3819. ExpectTrue(WOLFSSL_SUCCESS ==
  3820. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3821. WOLFSSL_LOAD_FLAG_NONE));
  3822. /* test expired CA */
  3823. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3824. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3825. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3826. #else
  3827. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3828. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3829. #endif
  3830. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3831. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3832. wolfSSL_CTX_free(ctx);
  3833. #endif
  3834. return EXPECT_RESULT();
  3835. }
  3836. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3837. {
  3838. EXPECT_DECLS;
  3839. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3840. defined(USE_CERT_BUFFERS_2048)
  3841. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3842. WOLFSSL_CTX* ctx;
  3843. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3844. byte ca_expired_cert[TWOK_BUF];
  3845. word32 sizeof_ca_expired_cert = 0;
  3846. XFILE fp = XBADFILE;
  3847. #ifndef NO_WOLFSSL_CLIENT
  3848. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3849. #else
  3850. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3851. #endif
  3852. ExpectNotNull(ctx);
  3853. /* test good CA */
  3854. ExpectTrue(WOLFSSL_SUCCESS ==
  3855. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3856. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3857. WOLFSSL_LOAD_FLAG_NONE));
  3858. /* load expired CA */
  3859. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3860. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3861. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3862. sizeof(ca_expired_cert), fp), 0);
  3863. if (fp != XBADFILE)
  3864. XFCLOSE(fp);
  3865. /* test expired CA failure */
  3866. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3867. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3868. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3869. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3870. #else
  3871. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3872. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3873. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3874. #endif
  3875. /* test expired CA success */
  3876. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3877. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3878. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3879. wolfSSL_CTX_free(ctx);
  3880. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3881. #endif
  3882. return EXPECT_RESULT();
  3883. }
  3884. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3885. {
  3886. EXPECT_DECLS;
  3887. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3888. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3889. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3890. WOLFSSL_CTX* ctx = NULL;
  3891. #ifndef NO_WOLFSSL_CLIENT
  3892. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3893. #else
  3894. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3895. #endif
  3896. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3897. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3898. WOLFSSL_FILETYPE_ASN1));
  3899. wolfSSL_CTX_free(ctx);
  3900. #endif
  3901. return EXPECT_RESULT();
  3902. }
  3903. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3904. {
  3905. EXPECT_DECLS;
  3906. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3907. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3908. WOLFSSL_CTX* ctx;
  3909. WOLFSSL* ssl = NULL;
  3910. const char *certChain[] = {
  3911. "./certs/intermediate/client-int-cert.pem",
  3912. "./certs/intermediate/ca-int2-cert.pem",
  3913. "./certs/intermediate/ca-int-cert.pem",
  3914. "./certs/ca-cert.pem",
  3915. NULL
  3916. };
  3917. const char** cert;
  3918. WOLFSSL_X509* x509 = NULL;
  3919. WOLF_STACK_OF(X509)* chain = NULL;
  3920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3921. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3922. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3923. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3924. WOLFSSL_FILETYPE_PEM));
  3925. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3926. X509_free(x509);
  3927. x509 = NULL;
  3928. }
  3929. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3930. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3931. WOLFSSL_FILETYPE_PEM));
  3932. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3933. X509_free(x509);
  3934. x509 = NULL;
  3935. }
  3936. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3937. ExpectIntEQ(sk_X509_num(chain), 3);
  3938. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3939. ExpectIntEQ(sk_X509_num(chain), 3);
  3940. SSL_free(ssl);
  3941. SSL_CTX_free(ctx);
  3942. #endif
  3943. return EXPECT_RESULT();
  3944. }
  3945. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3946. {
  3947. EXPECT_DECLS;
  3948. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3949. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3950. const char* server_chain_der = "./certs/server-cert-chain.der";
  3951. const char* client_single_pem = "./certs/client-cert.pem";
  3952. WOLFSSL_CTX* ctx;
  3953. (void)server_chain_der;
  3954. (void)client_single_pem;
  3955. (void)ctx;
  3956. #ifndef NO_WOLFSSL_CLIENT
  3957. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3958. #else
  3959. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3960. #endif
  3961. ExpectNotNull(ctx);
  3962. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3963. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3964. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3965. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3966. wolfSSL_CTX_free(ctx);
  3967. #endif
  3968. return EXPECT_RESULT();
  3969. }
  3970. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3971. {
  3972. EXPECT_DECLS;
  3973. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3974. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3975. WOLFSSL_CTX *ctx = NULL;
  3976. (void)ctx;
  3977. #ifndef NO_WOLFSSL_CLIENT
  3978. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3979. #else
  3980. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3981. #endif
  3982. /* invalid context */
  3983. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3984. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3985. /* invalid dhParamFile file */
  3986. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3987. NULL, WOLFSSL_FILETYPE_PEM));
  3988. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3989. bogusFile, WOLFSSL_FILETYPE_PEM));
  3990. /* success */
  3991. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3992. WOLFSSL_FILETYPE_PEM));
  3993. wolfSSL_CTX_free(ctx);
  3994. #endif
  3995. return EXPECT_RESULT();
  3996. }
  3997. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3998. {
  3999. EXPECT_DECLS;
  4000. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4001. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4002. WOLFSSL_CTX *ctx = NULL;
  4003. #ifndef NO_WOLFSSL_CLIENT
  4004. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4005. #else
  4006. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4007. #endif
  4008. /* invalid context */
  4009. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  4010. dh_key_der_2048, sizeof_dh_key_der_2048,
  4011. WOLFSSL_FILETYPE_ASN1));
  4012. /* invalid dhParamFile file */
  4013. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  4014. 0, WOLFSSL_FILETYPE_ASN1));
  4015. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4016. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4017. WOLFSSL_FILETYPE_ASN1));
  4018. /* success */
  4019. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4020. dh_key_der_2048, sizeof_dh_key_der_2048,
  4021. WOLFSSL_FILETYPE_ASN1));
  4022. wolfSSL_CTX_free(ctx);
  4023. #endif
  4024. return EXPECT_RESULT();
  4025. }
  4026. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4027. {
  4028. EXPECT_DECLS;
  4029. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4030. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4031. WOLFSSL_CTX *ctx;
  4032. (void)ctx;
  4033. #ifndef NO_WOLFSSL_CLIENT
  4034. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4035. #else
  4036. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4037. #endif
  4038. ExpectNotNull(ctx);
  4039. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4040. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4041. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4042. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4043. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4044. dh_key_der_2048, sizeof_dh_key_der_2048,
  4045. WOLFSSL_FILETYPE_ASN1));
  4046. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4047. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4048. dh_key_der_2048, sizeof_dh_key_der_2048,
  4049. WOLFSSL_FILETYPE_ASN1));
  4050. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4051. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4052. dh_key_der_2048, sizeof_dh_key_der_2048,
  4053. WOLFSSL_FILETYPE_ASN1));
  4054. wolfSSL_CTX_free(ctx);
  4055. #endif
  4056. return EXPECT_RESULT();
  4057. }
  4058. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4059. {
  4060. EXPECT_DECLS;
  4061. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4062. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4063. WOLFSSL_CTX* ctx = NULL;
  4064. const char* derCert = "./certs/server-cert.der";
  4065. const char* nullPath = NULL;
  4066. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4067. const char* emptyPath = "";
  4068. /* der load Case 1 ctx NULL */
  4069. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4070. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4071. #ifndef NO_WOLFSSL_CLIENT
  4072. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4073. #else
  4074. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4075. #endif
  4076. /* Case 2 filePath NULL */
  4077. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4078. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4079. /* Case 3 invalid format */
  4080. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4081. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4082. /* Case 4 filePath not valid */
  4083. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4084. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4085. /* Case 5 filePath empty */
  4086. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4087. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4088. #ifndef NO_RSA
  4089. /* Case 6 success case */
  4090. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4092. #endif
  4093. wolfSSL_CTX_free(ctx);
  4094. #endif
  4095. return EXPECT_RESULT();
  4096. }
  4097. static int test_wolfSSL_CTX_enable_disable(void)
  4098. {
  4099. EXPECT_DECLS;
  4100. #ifndef NO_CERTS
  4101. WOLFSSL_CTX* ctx = NULL;
  4102. #ifdef HAVE_CRL
  4103. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4104. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4105. #endif
  4106. #ifdef HAVE_OCSP
  4107. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4108. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4109. #endif
  4110. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4111. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4112. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4113. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4114. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4115. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4116. #endif
  4117. #ifndef NO_WOLFSSL_CLIENT
  4118. #ifdef HAVE_EXTENDED_MASTER
  4119. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4120. #endif
  4121. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4122. #ifdef HAVE_EXTENDED_MASTER
  4123. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4124. #endif
  4125. #elif !defined(NO_WOLFSSL_SERVER)
  4126. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4127. #endif
  4128. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4129. #ifdef HAVE_CRL
  4130. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4131. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4132. #endif
  4133. #ifdef HAVE_OCSP
  4134. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4135. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4136. WOLFSSL_SUCCESS);
  4137. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4138. WOLFSSL_SUCCESS);
  4139. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4140. WOLFSSL_SUCCESS);
  4141. #endif
  4142. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4143. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4144. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4145. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4146. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4147. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4148. #endif
  4149. wolfSSL_CTX_free(ctx);
  4150. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4151. #endif /* NO_CERTS */
  4152. return EXPECT_RESULT();
  4153. }
  4154. static int test_wolfSSL_CTX_ticket_API(void)
  4155. {
  4156. EXPECT_DECLS;
  4157. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4158. WOLFSSL_CTX* ctx = NULL;
  4159. void *userCtx = (void*)"this is my ctx";
  4160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4161. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4162. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4163. wolfSSL_CTX_free(ctx);
  4164. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4165. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4166. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4167. return EXPECT_RESULT();
  4168. }
  4169. static int test_wolfSSL_set_minmax_proto_version(void)
  4170. {
  4171. EXPECT_DECLS;
  4172. #ifdef OPENSSL_EXTRA
  4173. WOLFSSL_CTX *ctx = NULL;
  4174. WOLFSSL *ssl = NULL;
  4175. (void)ssl;
  4176. #ifndef NO_WOLFSSL_CLIENT
  4177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4178. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4179. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4180. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4181. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4182. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4183. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4184. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4185. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4186. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4187. wolfSSL_free(ssl);
  4188. wolfSSL_CTX_free(ctx);
  4189. ctx = NULL;
  4190. #endif
  4191. #ifndef NO_WOLFSSL_SERVER
  4192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4193. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4194. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4195. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4196. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4197. wolfSSL_CTX_free(ctx);
  4198. #endif
  4199. #endif
  4200. return EXPECT_RESULT();
  4201. }
  4202. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4203. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4204. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4205. {
  4206. EXPECT_DECLS;
  4207. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4208. return EXPECT_RESULT();
  4209. }
  4210. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4211. {
  4212. EXPECT_DECLS;
  4213. /* Set TLS 1.2 */
  4214. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4215. WOLFSSL_SUCCESS);
  4216. return EXPECT_RESULT();
  4217. }
  4218. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4219. * what was set at ctx creation. */
  4220. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4221. {
  4222. EXPECT_DECLS;
  4223. test_ssl_cbf client_cbs;
  4224. test_ssl_cbf server_cbs;
  4225. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4226. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4227. client_cbs.method = wolfTLS_client_method;
  4228. server_cbs.method = wolfTLS_server_method;
  4229. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4230. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4231. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4232. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4233. &server_cbs, NULL), TEST_SUCCESS);
  4234. return EXPECT_RESULT();
  4235. }
  4236. #else
  4237. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4238. {
  4239. return TEST_SKIPPED;
  4240. }
  4241. #endif
  4242. /*----------------------------------------------------------------------------*
  4243. | SSL
  4244. *----------------------------------------------------------------------------*/
  4245. static int test_server_wolfSSL_new(void)
  4246. {
  4247. EXPECT_DECLS;
  4248. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4249. !defined(NO_WOLFSSL_SERVER)
  4250. WOLFSSL_CTX *ctx = NULL;
  4251. WOLFSSL_CTX *ctx_nocert = NULL;
  4252. WOLFSSL *ssl = NULL;
  4253. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4254. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4255. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4256. WOLFSSL_FILETYPE_PEM));
  4257. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4258. WOLFSSL_FILETYPE_PEM));
  4259. /* invalid context */
  4260. ExpectNull(ssl = wolfSSL_new(NULL));
  4261. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4262. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4263. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4264. #endif
  4265. /* success */
  4266. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4267. wolfSSL_free(ssl);
  4268. wolfSSL_CTX_free(ctx);
  4269. wolfSSL_CTX_free(ctx_nocert);
  4270. #endif
  4271. return EXPECT_RESULT();
  4272. }
  4273. static int test_client_wolfSSL_new(void)
  4274. {
  4275. EXPECT_DECLS;
  4276. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4277. !defined(NO_WOLFSSL_CLIENT)
  4278. WOLFSSL_CTX *ctx = NULL;
  4279. WOLFSSL_CTX *ctx_nocert = NULL;
  4280. WOLFSSL *ssl = NULL;
  4281. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4282. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4283. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4284. /* invalid context */
  4285. ExpectNull(ssl = wolfSSL_new(NULL));
  4286. /* success */
  4287. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4288. wolfSSL_free(ssl);
  4289. ssl = NULL;
  4290. /* success */
  4291. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4292. wolfSSL_free(ssl);
  4293. wolfSSL_CTX_free(ctx);
  4294. wolfSSL_CTX_free(ctx_nocert);
  4295. #endif
  4296. return EXPECT_RESULT();
  4297. }
  4298. static int test_wolfSSL_SetTmpDH_file(void)
  4299. {
  4300. EXPECT_DECLS;
  4301. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4302. !defined(NO_WOLFSSL_SERVER)
  4303. WOLFSSL_CTX *ctx = NULL;
  4304. WOLFSSL *ssl = NULL;
  4305. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4306. #ifndef NO_RSA
  4307. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4308. WOLFSSL_FILETYPE_PEM));
  4309. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4310. WOLFSSL_FILETYPE_PEM));
  4311. #elif defined(HAVE_ECC)
  4312. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4313. WOLFSSL_FILETYPE_PEM));
  4314. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4315. WOLFSSL_FILETYPE_PEM));
  4316. #elif defined(HAVE_ED25519)
  4317. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4318. WOLFSSL_FILETYPE_PEM));
  4319. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4320. WOLFSSL_FILETYPE_PEM));
  4321. #elif defined(HAVE_ED448)
  4322. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4323. WOLFSSL_FILETYPE_PEM));
  4324. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4325. WOLFSSL_FILETYPE_PEM));
  4326. #endif
  4327. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4328. /* invalid ssl */
  4329. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4330. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4331. /* invalid dhParamFile file */
  4332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4333. NULL, WOLFSSL_FILETYPE_PEM));
  4334. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4335. bogusFile, WOLFSSL_FILETYPE_PEM));
  4336. /* success */
  4337. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4338. WOLFSSL_FILETYPE_PEM));
  4339. wolfSSL_free(ssl);
  4340. wolfSSL_CTX_free(ctx);
  4341. #endif
  4342. return EXPECT_RESULT();
  4343. }
  4344. static int test_wolfSSL_SetTmpDH_buffer(void)
  4345. {
  4346. EXPECT_DECLS;
  4347. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4348. WOLFSSL_CTX *ctx = NULL;
  4349. WOLFSSL *ssl = NULL;
  4350. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4351. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4352. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4353. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4354. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4355. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4356. /* invalid ssl */
  4357. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4358. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4359. /* invalid dhParamFile file */
  4360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4361. 0, WOLFSSL_FILETYPE_ASN1));
  4362. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4363. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4364. /* success */
  4365. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4366. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4367. wolfSSL_free(ssl);
  4368. wolfSSL_CTX_free(ctx);
  4369. #endif
  4370. return EXPECT_RESULT();
  4371. }
  4372. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4373. {
  4374. EXPECT_DECLS;
  4375. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4376. WOLFSSL_CTX *ctx = NULL;
  4377. WOLFSSL_CTX *ctx2 = NULL;
  4378. WOLFSSL *ssl = NULL;
  4379. WOLFSSL *ssl2 = NULL;
  4380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4381. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4382. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4383. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4384. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4385. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4386. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4387. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4388. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4389. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4390. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4391. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4392. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4393. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4394. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4395. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4396. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4397. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4398. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4399. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4400. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4401. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4402. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4403. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4404. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4405. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4406. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4407. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4408. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4409. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4410. wolfSSL_free(ssl2);
  4411. wolfSSL_CTX_free(ctx2);
  4412. wolfSSL_free(ssl);
  4413. wolfSSL_CTX_free(ctx);
  4414. #endif
  4415. return EXPECT_RESULT();
  4416. }
  4417. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4418. * allowed.
  4419. * POST: return 1 on success.
  4420. */
  4421. static int test_wolfSSL_SetMinVersion(void)
  4422. {
  4423. int res = TEST_SKIPPED;
  4424. #ifndef NO_WOLFSSL_CLIENT
  4425. int failFlag = WOLFSSL_SUCCESS;
  4426. WOLFSSL_CTX* ctx = NULL;
  4427. WOLFSSL* ssl = NULL;
  4428. int itr;
  4429. #ifndef NO_OLD_TLS
  4430. const int versions[] = {
  4431. #ifdef WOLFSSL_ALLOW_TLSV10
  4432. WOLFSSL_TLSV1,
  4433. #endif
  4434. WOLFSSL_TLSV1_1,
  4435. WOLFSSL_TLSV1_2};
  4436. #elif !defined(WOLFSSL_NO_TLS12)
  4437. const int versions[] = { WOLFSSL_TLSV1_2 };
  4438. #else
  4439. const int versions[] = { WOLFSSL_TLSV1_3 };
  4440. #endif
  4441. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4442. ssl = wolfSSL_new(ctx);
  4443. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4444. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4445. failFlag = WOLFSSL_FAILURE;
  4446. }
  4447. }
  4448. wolfSSL_free(ssl);
  4449. wolfSSL_CTX_free(ctx);
  4450. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4451. #endif
  4452. return res;
  4453. } /* END test_wolfSSL_SetMinVersion */
  4454. #ifdef OPENSSL_EXTRA
  4455. static int test_EC25519(void)
  4456. {
  4457. EXPECT_DECLS;
  4458. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4459. byte priv[CURVE25519_KEYSIZE];
  4460. unsigned int privSz = CURVE25519_KEYSIZE;
  4461. byte pub[CURVE25519_KEYSIZE];
  4462. unsigned int pubSz = CURVE25519_KEYSIZE;
  4463. byte priv2[CURVE25519_KEYSIZE];
  4464. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4465. byte pub2[CURVE25519_KEYSIZE];
  4466. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4467. byte shared[CURVE25519_KEYSIZE];
  4468. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4469. byte shared2[CURVE25519_KEYSIZE];
  4470. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4471. /* Bad parameter testing of key generation. */
  4472. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4473. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4474. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4475. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4476. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4477. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4478. /* Bad length */
  4479. privSz = 1;
  4480. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4481. privSz = CURVE25519_KEYSIZE;
  4482. pubSz = 1;
  4483. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4484. pubSz = CURVE25519_KEYSIZE;
  4485. /* Good case of generating key. */
  4486. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4487. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4488. 1);
  4489. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4490. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4491. /* Bad parameter testing of shared key. */
  4492. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4493. NULL, pubSz), 0);
  4494. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4495. NULL, pubSz), 0);
  4496. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4497. pub, pubSz), 0);
  4498. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4499. pub, pubSz), 0);
  4500. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4501. NULL, pubSz), 0);
  4502. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4503. pub, pubSz), 0);
  4504. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4505. pub, pubSz), 0);
  4506. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4507. pub, pubSz), 0);
  4508. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4509. NULL, pubSz), 0);
  4510. /* Bad length. */
  4511. sharedSz = 1;
  4512. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4513. pub, pubSz), 0);
  4514. sharedSz = CURVE25519_KEYSIZE;
  4515. privSz = 1;
  4516. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4517. pub, pubSz), 0);
  4518. privSz = CURVE25519_KEYSIZE;
  4519. pubSz = 1;
  4520. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4521. pub, pubSz), 0);
  4522. pubSz = CURVE25519_KEYSIZE;
  4523. /* Good case of shared key. */
  4524. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4525. pub2, pub2Sz), 1);
  4526. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4527. pub, pubSz), 1);
  4528. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4529. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4530. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4531. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4532. return EXPECT_RESULT();
  4533. }
  4534. static int test_ED25519(void)
  4535. {
  4536. EXPECT_DECLS;
  4537. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4538. defined(WOLFSSL_KEY_GEN)
  4539. byte priv[ED25519_PRV_KEY_SIZE];
  4540. unsigned int privSz = (unsigned int)sizeof(priv);
  4541. byte pub[ED25519_PUB_KEY_SIZE];
  4542. unsigned int pubSz = (unsigned int)sizeof(pub);
  4543. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4544. const char* msg = TEST_STRING;
  4545. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4546. byte sig[ED25519_SIG_SIZE];
  4547. unsigned int sigSz = (unsigned int)sizeof(sig);
  4548. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4549. /* Bad parameter testing of key generation. */
  4550. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4551. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4552. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4553. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4554. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4555. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4556. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4557. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4558. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4559. /* Bad length. */
  4560. privSz = 1;
  4561. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4562. privSz = ED25519_PRV_KEY_SIZE;
  4563. pubSz = 1;
  4564. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4565. pubSz = ED25519_PUB_KEY_SIZE;
  4566. /* Good case of generating key. */
  4567. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4568. 1);
  4569. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4570. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4571. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4572. /* Bad parameter testing of signing. */
  4573. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4574. NULL), 0);
  4575. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4576. NULL), 0);
  4577. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4578. NULL), 0);
  4579. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4580. NULL), 0);
  4581. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4582. &sigSz), 0);
  4583. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4584. &sigSz), 0);
  4585. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4586. &sigSz), 0);
  4587. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4588. &sigSz), 0);
  4589. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4590. NULL), 0);
  4591. /* Bad length. */
  4592. privSz = 1;
  4593. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4594. &sigSz), 0);
  4595. privSz = ED25519_PRV_KEY_SIZE;
  4596. sigSz = 1;
  4597. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4598. &sigSz), 0);
  4599. sigSz = ED25519_SIG_SIZE;
  4600. /* Good case of signing. */
  4601. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4602. &sigSz), 1);
  4603. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4604. #ifdef HAVE_ED25519_VERIFY
  4605. /* Bad parameter testing of verification. */
  4606. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4607. sigSz), 0);
  4608. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4609. sigSz), 0);
  4610. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4611. sigSz), 0);
  4612. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4613. sigSz), 0);
  4614. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4615. sigSz), 0);
  4616. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4617. sigSz), 0);
  4618. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4619. sigSz), 0);
  4620. /* Bad length. */
  4621. pubSz = 1;
  4622. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4623. sigSz), 0);
  4624. pubSz = ED25519_PUB_KEY_SIZE;
  4625. sigSz = 1;
  4626. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4627. sigSz), 0);
  4628. sigSz = ED25519_SIG_SIZE;
  4629. /* Good case of verification. */
  4630. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4631. sigSz), 1);
  4632. /* Bad signature. */
  4633. if (EXPECT_SUCCESS()) {
  4634. sig[1] ^= 0x80;
  4635. }
  4636. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4637. sigSz), 0);
  4638. #endif /* HAVE_ED25519_VERIFY */
  4639. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4640. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4641. return EXPECT_RESULT();
  4642. }
  4643. static int test_EC448(void)
  4644. {
  4645. EXPECT_DECLS;
  4646. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4647. byte priv[CURVE448_KEY_SIZE];
  4648. unsigned int privSz = CURVE448_KEY_SIZE;
  4649. byte pub[CURVE448_KEY_SIZE];
  4650. unsigned int pubSz = CURVE448_KEY_SIZE;
  4651. byte priv2[CURVE448_KEY_SIZE];
  4652. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4653. byte pub2[CURVE448_KEY_SIZE];
  4654. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4655. byte shared[CURVE448_KEY_SIZE];
  4656. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4657. byte shared2[CURVE448_KEY_SIZE];
  4658. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4659. /* Bad parameter testing of key generation. */
  4660. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4661. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4662. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4663. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4664. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4665. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4666. /* Bad length. */
  4667. privSz = 1;
  4668. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4669. privSz = CURVE448_KEY_SIZE;
  4670. pubSz = 1;
  4671. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4672. pubSz = CURVE448_KEY_SIZE;
  4673. /* Good case of generating key. */
  4674. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4675. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4676. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4677. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4678. /* Bad parameter testing of shared key. */
  4679. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4680. NULL, pubSz), 0);
  4681. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4682. NULL, pubSz), 0);
  4683. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4684. pub, pubSz), 0);
  4685. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4686. pub, pubSz), 0);
  4687. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4688. NULL, pubSz), 0);
  4689. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4690. pub, pubSz), 0);
  4691. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4692. pub, pubSz), 0);
  4693. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4694. pub, pubSz), 0);
  4695. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4696. NULL, pubSz), 0);
  4697. /* Bad length. */
  4698. sharedSz = 1;
  4699. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4700. pub, pubSz), 0);
  4701. sharedSz = CURVE448_KEY_SIZE;
  4702. privSz = 1;
  4703. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4704. pub, pubSz), 0);
  4705. privSz = CURVE448_KEY_SIZE;
  4706. pubSz = 1;
  4707. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4708. pub, pubSz), 0);
  4709. pubSz = CURVE448_KEY_SIZE;
  4710. /* Good case of shared key. */
  4711. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4712. pub2, pub2Sz), 1);
  4713. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4714. pub, pubSz), 1);
  4715. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4716. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4717. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4718. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4719. return EXPECT_RESULT();
  4720. }
  4721. static int test_ED448(void)
  4722. {
  4723. EXPECT_DECLS;
  4724. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4725. defined(WOLFSSL_KEY_GEN)
  4726. byte priv[ED448_PRV_KEY_SIZE];
  4727. unsigned int privSz = (unsigned int)sizeof(priv);
  4728. byte pub[ED448_PUB_KEY_SIZE];
  4729. unsigned int pubSz = (unsigned int)sizeof(pub);
  4730. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4731. const char* msg = TEST_STRING;
  4732. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4733. byte sig[ED448_SIG_SIZE];
  4734. unsigned int sigSz = (unsigned int)sizeof(sig);
  4735. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4736. /* Bad parameter testing of key generation. */
  4737. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4738. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4739. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4740. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4741. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4742. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4743. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4744. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4745. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4746. /* Bad length. */
  4747. privSz = 1;
  4748. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4749. privSz = ED448_PRV_KEY_SIZE;
  4750. pubSz = 1;
  4751. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4752. pubSz = ED448_PUB_KEY_SIZE;
  4753. /* Good case of generating key. */
  4754. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4755. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4756. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4757. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4758. /* Bad parameter testing of signing. */
  4759. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4760. NULL), 0);
  4761. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4762. NULL), 0);
  4763. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4764. NULL), 0);
  4765. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4766. NULL), 0);
  4767. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4768. &sigSz), 0);
  4769. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4770. &sigSz), 0);
  4771. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4772. &sigSz), 0);
  4773. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4774. &sigSz), 0);
  4775. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4776. NULL), 0);
  4777. /* Bad length. */
  4778. privSz = 1;
  4779. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4780. &sigSz), 0);
  4781. privSz = ED448_PRV_KEY_SIZE;
  4782. sigSz = 1;
  4783. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4784. &sigSz), 0);
  4785. sigSz = ED448_SIG_SIZE;
  4786. /* Good case of signing. */
  4787. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4788. &sigSz), 1);
  4789. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4790. #ifdef HAVE_ED448_VERIFY
  4791. /* Bad parameter testing of verification. */
  4792. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4793. sigSz), 0);
  4794. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4795. sigSz), 0);
  4796. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4797. sigSz), 0);
  4798. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4799. sigSz), 0);
  4800. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4801. sigSz), 0);
  4802. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4803. sigSz), 0);
  4804. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4805. sigSz), 0);
  4806. /* Bad length. */
  4807. pubSz = 1;
  4808. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4809. sigSz), 0);
  4810. pubSz = ED448_PUB_KEY_SIZE;
  4811. sigSz = 1;
  4812. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4813. sigSz), 0);
  4814. sigSz = ED448_SIG_SIZE;
  4815. /* Good case of verification. */
  4816. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4817. sigSz), 1);
  4818. /* Bad signature. */
  4819. if (EXPECT_SUCCESS()) {
  4820. sig[1] ^= 0x80;
  4821. }
  4822. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4823. sigSz), 0);
  4824. #endif /* HAVE_ED448_VERIFY */
  4825. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4826. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4827. return EXPECT_RESULT();
  4828. }
  4829. #endif /* OPENSSL_EXTRA */
  4830. #include <wolfssl/openssl/pem.h>
  4831. /*----------------------------------------------------------------------------*
  4832. | EVP
  4833. *----------------------------------------------------------------------------*/
  4834. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4835. {
  4836. EXPECT_DECLS;
  4837. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4838. WOLFSSL_BIO* rbio = NULL;
  4839. WOLFSSL_BIO* wbio = NULL;
  4840. WOLFSSL_EVP_PKEY* pkey = NULL;
  4841. char line[256] = { 0 };
  4842. char line1[256] = { 0 };
  4843. int i = 0;
  4844. /* test error cases */
  4845. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4846. /*
  4847. * test RSA public key print
  4848. * in this test, pass '3' for indent
  4849. */
  4850. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4851. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4852. sizeof_client_keypub_der_1024));
  4853. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4854. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4855. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4856. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4857. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4858. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4859. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4860. strcpy(line1, " Modulus:\n");
  4861. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4862. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4863. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4864. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4865. /* skip to the end of modulus element*/
  4866. for (i = 0; i < 8 ;i++) {
  4867. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4868. }
  4869. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4870. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4871. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4872. /* should reach EOF */
  4873. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4874. EVP_PKEY_free(pkey);
  4875. pkey = NULL;
  4876. BIO_free(rbio);
  4877. BIO_free(wbio);
  4878. rbio = NULL;
  4879. wbio = NULL;
  4880. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4881. /*
  4882. * test DSA public key print
  4883. */
  4884. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4885. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4886. sizeof_dsa_pub_key_der_2048));
  4887. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4888. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4889. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4890. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4891. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4892. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4893. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4894. strcpy(line1, "pub:\n");
  4895. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4896. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4897. strcpy(line1,
  4898. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4899. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4900. /* skip to the end of pub element*/
  4901. for (i = 0; i < 17 ;i++) {
  4902. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4903. }
  4904. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4905. strcpy(line1, "P:\n");
  4906. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4907. /* skip to the end of P element*/
  4908. for (i = 0; i < 18 ;i++) {
  4909. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4910. }
  4911. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4912. strcpy(line1, "Q:\n");
  4913. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4914. /* skip to the end of Q element*/
  4915. for (i = 0; i < 3 ;i++) {
  4916. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4917. }
  4918. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4919. strcpy(line1, "G:\n");
  4920. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4921. /* skip to the end of G element*/
  4922. for (i = 0; i < 18 ;i++) {
  4923. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4924. }
  4925. /* should reach EOF */
  4926. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4927. EVP_PKEY_free(pkey);
  4928. pkey = NULL;
  4929. BIO_free(rbio);
  4930. BIO_free(wbio);
  4931. rbio = NULL;
  4932. wbio = NULL;
  4933. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4934. /*
  4935. * test ECC public key print
  4936. */
  4937. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4938. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4939. sizeof_ecc_clikeypub_der_256));
  4940. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4941. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4942. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4943. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4944. strcpy(line1, "Public-Key: (256 bit)\n");
  4945. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4946. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4947. strcpy(line1, "pub:\n");
  4948. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4949. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4950. strcpy(line1,
  4951. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4952. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4953. /* skip to the end of pub element*/
  4954. for (i = 0; i < 4 ;i++) {
  4955. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4956. }
  4957. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4958. strcpy(line1, "ASN1 OID: prime256v1\n");
  4959. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4960. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4961. strcpy(line1, "NIST CURVE: P-256\n");
  4962. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4963. /* should reach EOF */
  4964. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4965. EVP_PKEY_free(pkey);
  4966. pkey = NULL;
  4967. BIO_free(rbio);
  4968. BIO_free(wbio);
  4969. rbio = NULL;
  4970. wbio = NULL;
  4971. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4972. /*
  4973. * test DH public key print
  4974. */
  4975. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4976. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4977. sizeof_dh_pub_key_der_2048));
  4978. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4979. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4980. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4981. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4982. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4983. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4984. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4985. strcpy(line1, "public-key:\n");
  4986. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4987. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4988. strcpy(line1,
  4989. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4990. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4991. /* skip to the end of public-key element*/
  4992. for (i = 0; i < 17 ;i++) {
  4993. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4994. }
  4995. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4996. strcpy(line1, "prime:\n");
  4997. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4998. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4999. strcpy(line1,
  5000. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  5001. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5002. /* skip to the end of prime element*/
  5003. for (i = 0; i < 17 ;i++) {
  5004. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5005. }
  5006. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5007. strcpy(line1, "generator: 2 (0x02)\n");
  5008. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5009. /* should reach EOF */
  5010. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5011. EVP_PKEY_free(pkey);
  5012. pkey = NULL;
  5013. BIO_free(rbio);
  5014. BIO_free(wbio);
  5015. rbio = NULL;
  5016. wbio = NULL;
  5017. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5018. /* to prevent "unused variable" warning */
  5019. (void)pkey;
  5020. (void)wbio;
  5021. (void)rbio;
  5022. (void)line;
  5023. (void)line1;
  5024. (void)i;
  5025. #endif /* OPENSSL_EXTRA */
  5026. return EXPECT_RESULT();
  5027. }
  5028. /* Test functions for base64 encode/decode */
  5029. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5030. {
  5031. EXPECT_DECLS;
  5032. #if defined(OPENSSL_EXTRA) && \
  5033. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5034. EVP_ENCODE_CTX* ctx = NULL;
  5035. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5036. ExpectIntEQ(ctx->remaining,0);
  5037. ExpectIntEQ(ctx->data[0],0);
  5038. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5039. EVP_ENCODE_CTX_free(ctx);
  5040. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5041. return EXPECT_RESULT();
  5042. }
  5043. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5044. {
  5045. EXPECT_DECLS;
  5046. #if defined(OPENSSL_EXTRA) && \
  5047. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5048. EVP_ENCODE_CTX* ctx = NULL;
  5049. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5050. EVP_ENCODE_CTX_free(ctx);
  5051. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5052. return EXPECT_RESULT();
  5053. }
  5054. static int test_wolfSSL_EVP_EncodeInit(void)
  5055. {
  5056. EXPECT_DECLS;
  5057. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5058. EVP_ENCODE_CTX* ctx = NULL;
  5059. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5060. ExpectIntEQ(ctx->remaining, 0);
  5061. ExpectIntEQ(ctx->data[0], 0);
  5062. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5063. if (ctx != NULL) {
  5064. /* make ctx dirty */
  5065. ctx->remaining = 10;
  5066. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5067. }
  5068. EVP_EncodeInit(ctx);
  5069. ExpectIntEQ(ctx->remaining, 0);
  5070. ExpectIntEQ(ctx->data[0], 0);
  5071. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5072. EVP_ENCODE_CTX_free(ctx);
  5073. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5074. return EXPECT_RESULT();
  5075. }
  5076. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5077. {
  5078. EXPECT_DECLS;
  5079. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5080. int outl;
  5081. int total;
  5082. const unsigned char plain0[] = {"Th"};
  5083. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5084. const unsigned char plain2[] = {"This is additional data."};
  5085. const unsigned char encBlock0[] = {"VGg="};
  5086. const unsigned char enc0[] = {"VGg=\n"};
  5087. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5088. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5089. const unsigned char enc2[] =
  5090. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5091. unsigned char encOutBuff[300];
  5092. EVP_ENCODE_CTX* ctx = NULL;
  5093. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5094. EVP_EncodeInit(ctx);
  5095. /* illegal parameter test */
  5096. ExpectIntEQ(
  5097. EVP_EncodeUpdate(
  5098. NULL, /* pass NULL as ctx */
  5099. encOutBuff,
  5100. &outl,
  5101. plain1,
  5102. sizeof(plain1)-1),
  5103. 0 /* expected result code 0: fail */
  5104. );
  5105. ExpectIntEQ(
  5106. EVP_EncodeUpdate(
  5107. ctx,
  5108. NULL, /* pass NULL as out buff */
  5109. &outl,
  5110. plain1,
  5111. sizeof(plain1)-1),
  5112. 0 /* expected result code 0: fail */
  5113. );
  5114. ExpectIntEQ(
  5115. EVP_EncodeUpdate(
  5116. ctx,
  5117. encOutBuff,
  5118. NULL, /* pass NULL as outl */
  5119. plain1,
  5120. sizeof(plain1)-1),
  5121. 0 /* expected result code 0: fail */
  5122. );
  5123. ExpectIntEQ(
  5124. EVP_EncodeUpdate(
  5125. ctx,
  5126. encOutBuff,
  5127. &outl,
  5128. NULL, /* pass NULL as in */
  5129. sizeof(plain1)-1),
  5130. 0 /* expected result code 0: fail */
  5131. );
  5132. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5133. /* meaningless parameter test */
  5134. ExpectIntEQ(
  5135. EVP_EncodeUpdate(
  5136. ctx,
  5137. encOutBuff,
  5138. &outl,
  5139. plain1,
  5140. 0), /* pass zero input */
  5141. 1 /* expected result code 1: success */
  5142. );
  5143. /* very small data encoding test */
  5144. EVP_EncodeInit(ctx);
  5145. ExpectIntEQ(
  5146. EVP_EncodeUpdate(
  5147. ctx,
  5148. encOutBuff,
  5149. &outl,
  5150. plain0,
  5151. sizeof(plain0)-1),
  5152. 1 /* expected result code 1: success */
  5153. );
  5154. ExpectIntEQ(outl,0);
  5155. if (EXPECT_SUCCESS()) {
  5156. EVP_EncodeFinal(
  5157. ctx,
  5158. encOutBuff + outl,
  5159. &outl);
  5160. }
  5161. ExpectIntEQ( outl, sizeof(enc0)-1);
  5162. ExpectIntEQ(
  5163. XSTRNCMP(
  5164. (const char*)encOutBuff,
  5165. (const char*)enc0,sizeof(enc0) ),
  5166. 0);
  5167. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5168. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5169. sizeof(encBlock0)-1);
  5170. ExpectStrEQ(encOutBuff, encBlock0);
  5171. /* pass small size( < 48bytes ) input, then make sure they are not
  5172. * encoded and just stored in ctx
  5173. */
  5174. EVP_EncodeInit(ctx);
  5175. total = 0;
  5176. outl = 0;
  5177. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5178. ExpectIntEQ(
  5179. EVP_EncodeUpdate(
  5180. ctx,
  5181. encOutBuff, /* buffer for output */
  5182. &outl, /* size of output */
  5183. plain1, /* input */
  5184. sizeof(plain1)-1), /* size of input */
  5185. 1); /* expected result code 1:success */
  5186. total += outl;
  5187. ExpectIntEQ(outl, 0); /* no output expected */
  5188. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5189. ExpectTrue(
  5190. XSTRNCMP((const char*)(ctx->data),
  5191. (const char*)plain1,
  5192. ctx->remaining) ==0 );
  5193. ExpectTrue(encOutBuff[0] == 0);
  5194. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5195. * the stored data and the new input together
  5196. */
  5197. ExpectIntEQ(
  5198. EVP_EncodeUpdate(
  5199. ctx,
  5200. encOutBuff + outl, /* buffer for output */
  5201. &outl, /* size of output */
  5202. plain2, /* additional input */
  5203. sizeof(plain2) -1), /* size of additional input */
  5204. 1); /* expected result code 1:success */
  5205. total += outl;
  5206. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5207. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5208. ExpectIntEQ(
  5209. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5210. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5211. EVP_EncodeFinal(
  5212. ctx,
  5213. encOutBuff + outl,
  5214. &outl);
  5215. total += outl;
  5216. ExpectIntNE(total,0);
  5217. ExpectIntNE(outl,0);
  5218. ExpectIntEQ(XSTRNCMP(
  5219. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5220. /* test with illeagal parameters */
  5221. outl = 1;
  5222. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5223. ExpectIntEQ(outl, 0);
  5224. outl = 1;
  5225. EVP_EncodeFinal(ctx, NULL, &outl);
  5226. ExpectIntEQ(outl, 0);
  5227. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5228. EVP_EncodeFinal(NULL, NULL, NULL);
  5229. EVP_ENCODE_CTX_free(ctx);
  5230. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5231. return EXPECT_RESULT();
  5232. }
  5233. static int test_wolfSSL_EVP_EncodeFinal(void)
  5234. {
  5235. int res = TEST_SKIPPED;
  5236. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5237. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5238. * test_wolfSSL_EVP_EncodeUpdate
  5239. */
  5240. res = TEST_SUCCESS;
  5241. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5242. return res;
  5243. }
  5244. static int test_wolfSSL_EVP_DecodeInit(void)
  5245. {
  5246. EXPECT_DECLS;
  5247. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5248. EVP_ENCODE_CTX* ctx = NULL;
  5249. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5250. ExpectIntEQ( ctx->remaining,0);
  5251. ExpectIntEQ( ctx->data[0],0);
  5252. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5253. if (ctx != NULL) {
  5254. /* make ctx dirty */
  5255. ctx->remaining = 10;
  5256. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5257. }
  5258. EVP_DecodeInit(ctx);
  5259. ExpectIntEQ( ctx->remaining,0);
  5260. ExpectIntEQ( ctx->data[0],0);
  5261. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5262. EVP_ENCODE_CTX_free(ctx);
  5263. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5264. return EXPECT_RESULT();
  5265. }
  5266. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5267. {
  5268. EXPECT_DECLS;
  5269. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5270. int outl;
  5271. unsigned char decOutBuff[300];
  5272. EVP_ENCODE_CTX* ctx = NULL;
  5273. static const unsigned char enc1[] =
  5274. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5275. /* const unsigned char plain1[] =
  5276. {"This is a base64 decoding test."} */
  5277. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5278. EVP_DecodeInit(ctx);
  5279. /* illegal parameter tests */
  5280. /* pass NULL as ctx */
  5281. ExpectIntEQ(
  5282. EVP_DecodeUpdate(
  5283. NULL, /* pass NULL as ctx */
  5284. decOutBuff,
  5285. &outl,
  5286. enc1,
  5287. sizeof(enc1)-1),
  5288. -1 /* expected result code -1: fail */
  5289. );
  5290. ExpectIntEQ( outl, 0);
  5291. /* pass NULL as output */
  5292. ExpectIntEQ(
  5293. EVP_DecodeUpdate(
  5294. ctx,
  5295. NULL, /* pass NULL as out buff */
  5296. &outl,
  5297. enc1,
  5298. sizeof(enc1)-1),
  5299. -1 /* expected result code -1: fail */
  5300. );
  5301. ExpectIntEQ( outl, 0);
  5302. /* pass NULL as outl */
  5303. ExpectIntEQ(
  5304. EVP_DecodeUpdate(
  5305. ctx,
  5306. decOutBuff,
  5307. NULL, /* pass NULL as outl */
  5308. enc1,
  5309. sizeof(enc1)-1),
  5310. -1 /* expected result code -1: fail */
  5311. );
  5312. /* pass NULL as input */
  5313. ExpectIntEQ(
  5314. EVP_DecodeUpdate(
  5315. ctx,
  5316. decOutBuff,
  5317. &outl,
  5318. NULL, /* pass NULL as in */
  5319. sizeof(enc1)-1),
  5320. -1 /* expected result code -1: fail */
  5321. );
  5322. ExpectIntEQ( outl, 0);
  5323. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5324. /* pass zero length input */
  5325. ExpectIntEQ(
  5326. EVP_DecodeUpdate(
  5327. ctx,
  5328. decOutBuff,
  5329. &outl,
  5330. enc1,
  5331. 0), /* pass zero as input len */
  5332. 1 /* expected result code 1: success */
  5333. );
  5334. /* decode correct base64 string */
  5335. {
  5336. static const unsigned char enc2[] =
  5337. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5338. static const unsigned char plain2[] =
  5339. {"This is a base64 decoding test."};
  5340. EVP_EncodeInit(ctx);
  5341. ExpectIntEQ(
  5342. EVP_DecodeUpdate(
  5343. ctx,
  5344. decOutBuff,
  5345. &outl,
  5346. enc2,
  5347. sizeof(enc2)-1),
  5348. 0 /* expected result code 0: success */
  5349. );
  5350. ExpectIntEQ(outl,sizeof(plain2) -1);
  5351. ExpectIntEQ(
  5352. EVP_DecodeFinal(
  5353. ctx,
  5354. decOutBuff + outl,
  5355. &outl),
  5356. 1 /* expected result code 1: success */
  5357. );
  5358. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5359. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5360. sizeof(plain2) -1 ),0);
  5361. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5362. sizeof(plain2)-1);
  5363. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5364. sizeof(plain2) -1 ),0);
  5365. }
  5366. /* decode correct base64 string which does not have '\n' in its last*/
  5367. {
  5368. static const unsigned char enc3[] =
  5369. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5370. static const unsigned char plain3[] =
  5371. {"This is a base64 decoding test."}; /* 31 chars */
  5372. EVP_EncodeInit(ctx);
  5373. ExpectIntEQ(
  5374. EVP_DecodeUpdate(
  5375. ctx,
  5376. decOutBuff,
  5377. &outl,
  5378. enc3,
  5379. sizeof(enc3)-1),
  5380. 0 /* expected result code 0: success */
  5381. );
  5382. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5383. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5384. sizeof(plain3) -1 ),0);
  5385. ExpectIntEQ(
  5386. EVP_DecodeFinal(
  5387. ctx,
  5388. decOutBuff + outl,
  5389. &outl),
  5390. 1 /* expected result code 1: success */
  5391. );
  5392. ExpectIntEQ(outl,0 );
  5393. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5394. sizeof(plain3)-1);
  5395. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5396. sizeof(plain3) -1 ),0);
  5397. }
  5398. /* decode string which has a padding char ('=') in the illegal position*/
  5399. {
  5400. static const unsigned char enc4[] =
  5401. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5402. EVP_EncodeInit(ctx);
  5403. ExpectIntEQ(
  5404. EVP_DecodeUpdate(
  5405. ctx,
  5406. decOutBuff,
  5407. &outl,
  5408. enc4,
  5409. sizeof(enc4)-1),
  5410. -1 /* expected result code -1: error */
  5411. );
  5412. ExpectIntEQ(outl,0);
  5413. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5414. }
  5415. /* small data decode test */
  5416. {
  5417. static const unsigned char enc00[] = {"VG"};
  5418. static const unsigned char enc01[] = {"g=\n"};
  5419. static const unsigned char plain4[] = {"Th"};
  5420. EVP_EncodeInit(ctx);
  5421. ExpectIntEQ(
  5422. EVP_DecodeUpdate(
  5423. ctx,
  5424. decOutBuff,
  5425. &outl,
  5426. enc00,
  5427. sizeof(enc00)-1),
  5428. 1 /* expected result code 1: success */
  5429. );
  5430. ExpectIntEQ(outl,0);
  5431. ExpectIntEQ(
  5432. EVP_DecodeUpdate(
  5433. ctx,
  5434. decOutBuff + outl,
  5435. &outl,
  5436. enc01,
  5437. sizeof(enc01)-1),
  5438. 0 /* expected result code 0: success */
  5439. );
  5440. ExpectIntEQ(outl,sizeof(plain4)-1);
  5441. /* test with illegal parameters */
  5442. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5443. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5444. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5445. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5446. if (EXPECT_SUCCESS()) {
  5447. EVP_DecodeFinal(
  5448. ctx,
  5449. decOutBuff + outl,
  5450. &outl);
  5451. }
  5452. ExpectIntEQ( outl, 0);
  5453. ExpectIntEQ(
  5454. XSTRNCMP(
  5455. (const char*)decOutBuff,
  5456. (const char*)plain4,sizeof(plain4)-1 ),
  5457. 0);
  5458. }
  5459. EVP_ENCODE_CTX_free(ctx);
  5460. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5461. return EXPECT_RESULT();
  5462. }
  5463. static int test_wolfSSL_EVP_DecodeFinal(void)
  5464. {
  5465. int res = TEST_SKIPPED;
  5466. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5467. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5468. * test_wolfSSL_EVP_DecodeUpdate
  5469. */
  5470. res = TEST_SUCCESS;
  5471. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5472. return res;
  5473. }
  5474. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5475. */
  5476. #ifdef OPENSSL_EXTRA
  5477. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5478. {
  5479. EXPECT_DECLS;
  5480. #ifndef NO_AES
  5481. const WOLFSSL_EVP_CIPHER* c;
  5482. c = wolfSSL_EVP_get_cipherbynid(419);
  5483. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5484. defined(WOLFSSL_AES_128)
  5485. ExpectNotNull(c);
  5486. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5487. #else
  5488. ExpectNull(c);
  5489. #endif
  5490. c = wolfSSL_EVP_get_cipherbynid(423);
  5491. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5492. defined(WOLFSSL_AES_192)
  5493. ExpectNotNull(c);
  5494. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5495. #else
  5496. ExpectNull(c);
  5497. #endif
  5498. c = wolfSSL_EVP_get_cipherbynid(427);
  5499. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5500. defined(WOLFSSL_AES_256)
  5501. ExpectNotNull(c);
  5502. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5503. #else
  5504. ExpectNull(c);
  5505. #endif
  5506. c = wolfSSL_EVP_get_cipherbynid(904);
  5507. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5508. ExpectNotNull(c);
  5509. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5510. #else
  5511. ExpectNull(c);
  5512. #endif
  5513. c = wolfSSL_EVP_get_cipherbynid(905);
  5514. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5515. ExpectNotNull(c);
  5516. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5517. #else
  5518. ExpectNull(c);
  5519. #endif
  5520. c = wolfSSL_EVP_get_cipherbynid(906);
  5521. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5522. ExpectNotNull(c);
  5523. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5524. #else
  5525. ExpectNull(c);
  5526. #endif
  5527. c = wolfSSL_EVP_get_cipherbynid(418);
  5528. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5529. ExpectNotNull(c);
  5530. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5531. #else
  5532. ExpectNull(c);
  5533. #endif
  5534. c = wolfSSL_EVP_get_cipherbynid(422);
  5535. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5536. ExpectNotNull(c);
  5537. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5538. #else
  5539. ExpectNull(c);
  5540. #endif
  5541. c = wolfSSL_EVP_get_cipherbynid(426);
  5542. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5543. ExpectNotNull(c);
  5544. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5545. #else
  5546. ExpectNull(c);
  5547. #endif
  5548. #endif /* !NO_AES */
  5549. #ifndef NO_DES3
  5550. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5551. #ifdef WOLFSSL_DES_ECB
  5552. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5553. #endif
  5554. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5555. #ifdef WOLFSSL_DES_ECB
  5556. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5557. #endif
  5558. #endif /* !NO_DES3 */
  5559. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5560. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5561. #endif
  5562. /* test for nid is out of range */
  5563. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5564. return EXPECT_RESULT();
  5565. }
  5566. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5567. {
  5568. EXPECT_DECLS;
  5569. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5570. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5571. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5572. const EVP_CIPHER *test;
  5573. byte key[AES_BLOCK_SIZE] = {0};
  5574. byte iv[AES_BLOCK_SIZE] = {0};
  5575. ExpectNotNull(ctx);
  5576. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5577. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5578. test = EVP_CIPHER_CTX_cipher(ctx);
  5579. ExpectTrue(init == test);
  5580. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5581. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5582. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5583. EVP_CIPHER_CTX_free(ctx);
  5584. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5585. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5586. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5587. return EXPECT_RESULT();
  5588. }
  5589. #endif /* OPENSSL_EXTRA */
  5590. /*----------------------------------------------------------------------------*
  5591. | IO
  5592. *----------------------------------------------------------------------------*/
  5593. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5594. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5595. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5596. #ifdef WC_SHA512_DIGEST_SIZE
  5597. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5598. #else
  5599. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5600. #endif
  5601. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5602. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5603. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5604. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5605. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5606. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5607. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5608. defined(HAVE_AES_CBC)
  5609. typedef struct openssl_key_ctx {
  5610. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5611. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5612. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5613. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5614. } openssl_key_ctx;
  5615. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5616. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5617. static WC_INLINE int OpenSSLTicketInit(void)
  5618. {
  5619. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5620. if (ret != 0) return ret;
  5621. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5622. sizeof(myOpenSSLKey_ctx.name));
  5623. if (ret != 0) return ret;
  5624. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5625. sizeof(myOpenSSLKey_ctx.key));
  5626. if (ret != 0) return ret;
  5627. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5628. sizeof(myOpenSSLKey_ctx.hmacKey));
  5629. if (ret != 0) return ret;
  5630. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5631. sizeof(myOpenSSLKey_ctx.iv));
  5632. if (ret != 0) return ret;
  5633. return 0;
  5634. }
  5635. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5636. byte name[WOLFSSL_TICKET_NAME_SZ],
  5637. byte iv[WOLFSSL_TICKET_IV_SZ],
  5638. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5639. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5640. (void)ssl;
  5641. if (enc) {
  5642. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5643. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5644. }
  5645. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5646. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5647. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5648. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5649. return 0;
  5650. }
  5651. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5652. if (enc)
  5653. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5654. else
  5655. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5656. return 1;
  5657. }
  5658. static WC_INLINE void OpenSSLTicketCleanup(void)
  5659. {
  5660. wc_FreeRng(&myOpenSSLKey_rng);
  5661. }
  5662. #endif
  5663. #endif
  5664. /* helper functions */
  5665. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5666. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5667. void *ctx)
  5668. {
  5669. struct test_ssl_memio_ctx *test_ctx;
  5670. byte *buf;
  5671. int *len;
  5672. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5673. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5674. buf = test_ctx->c_buff;
  5675. len = &test_ctx->c_len;
  5676. }
  5677. else {
  5678. buf = test_ctx->s_buff;
  5679. len = &test_ctx->s_len;
  5680. }
  5681. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5682. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5683. XMEMCPY(buf + *len, data, sz);
  5684. *len += sz;
  5685. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5686. {
  5687. /* This can be imported into Wireshark by transforming the file with
  5688. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5689. * And then loading test_output.dump.hex into Wireshark using the
  5690. * "Import from Hex Dump..." option ion and selecting the TCP
  5691. * encapsulation option. */
  5692. char dump_file_name[64];
  5693. WOLFSSL_BIO *dump_file;
  5694. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5695. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5696. if (dump_file != NULL) {
  5697. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5698. wolfSSL_BIO_free(dump_file);
  5699. }
  5700. }
  5701. #endif
  5702. return sz;
  5703. }
  5704. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5705. void *ctx)
  5706. {
  5707. struct test_ssl_memio_ctx *test_ctx;
  5708. int read_sz;
  5709. byte *buf;
  5710. int *len;
  5711. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5712. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5713. buf = test_ctx->s_buff;
  5714. len = &test_ctx->s_len;
  5715. }
  5716. else {
  5717. buf = test_ctx->c_buff;
  5718. len = &test_ctx->c_len;
  5719. }
  5720. if (*len == 0)
  5721. return WOLFSSL_CBIO_ERR_WANT_READ;
  5722. read_sz = sz < *len ? sz : *len;
  5723. XMEMCPY(data, buf, read_sz);
  5724. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5725. *len -= read_sz;
  5726. return read_sz;
  5727. }
  5728. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5729. {
  5730. EXPECT_DECLS;
  5731. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5732. int c_sharedCtx = 0;
  5733. int s_sharedCtx = 0;
  5734. #endif
  5735. const char* clientCertFile = cliCertFile;
  5736. const char* clientKeyFile = cliKeyFile;
  5737. const char* serverCertFile = svrCertFile;
  5738. const char* serverKeyFile = svrKeyFile;
  5739. /********************************
  5740. * Create WOLFSSL_CTX for client.
  5741. ********************************/
  5742. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5743. if (ctx->c_ctx != NULL) {
  5744. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5745. }
  5746. else
  5747. #endif
  5748. {
  5749. WOLFSSL_METHOD* method = NULL;
  5750. if (ctx->c_cb.method != NULL) {
  5751. method = ctx->c_cb.method();
  5752. }
  5753. else {
  5754. method = wolfSSLv23_client_method();
  5755. }
  5756. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5757. }
  5758. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5759. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5760. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5761. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5762. #endif
  5763. if (ctx->c_cb.caPemFile != NULL)
  5764. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5765. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5766. else
  5767. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5768. caCertFile, 0), WOLFSSL_SUCCESS);
  5769. if (ctx->c_cb.certPemFile != NULL) {
  5770. clientCertFile = ctx->c_cb.certPemFile;
  5771. }
  5772. if (ctx->c_cb.keyPemFile != NULL) {
  5773. clientKeyFile = ctx->c_cb.keyPemFile;
  5774. }
  5775. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5776. if (!c_sharedCtx)
  5777. #endif
  5778. {
  5779. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5780. clientCertFile), WOLFSSL_SUCCESS);
  5781. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5782. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5783. }
  5784. #ifdef HAVE_CRL
  5785. if (ctx->c_cb.crlPemFile != NULL) {
  5786. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5787. WOLFSSL_SUCCESS);
  5788. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5789. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5790. }
  5791. #endif
  5792. if (ctx->c_ciphers != NULL) {
  5793. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5794. WOLFSSL_SUCCESS);
  5795. }
  5796. if (ctx->c_cb.ctx_ready != NULL) {
  5797. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5798. }
  5799. /********************************
  5800. * Create WOLFSSL_CTX for server.
  5801. ********************************/
  5802. if (ctx->s_ctx != NULL) {
  5803. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5804. s_sharedCtx = 1;
  5805. #endif
  5806. ctx->s_cb.isSharedCtx = 1;
  5807. }
  5808. else
  5809. {
  5810. WOLFSSL_METHOD* method = NULL;
  5811. if (ctx->s_cb.method != NULL) {
  5812. method = ctx->s_cb.method();
  5813. }
  5814. else {
  5815. method = wolfSSLv23_server_method();
  5816. }
  5817. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5818. ctx->s_cb.isSharedCtx = 0;
  5819. }
  5820. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5821. #if defined(HAVE_SESSION_TICKET) && \
  5822. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5823. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5824. OpenSSLTicketInit();
  5825. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5826. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5827. TicketInit();
  5828. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5829. #endif
  5830. #endif
  5831. }
  5832. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5833. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5834. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5835. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5836. if (ctx->s_cb.caPemFile != NULL)
  5837. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5838. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5839. else
  5840. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5841. cliCertFile, 0), WOLFSSL_SUCCESS);
  5842. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5843. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5844. #endif
  5845. if (ctx->s_cb.certPemFile != NULL) {
  5846. serverCertFile = ctx->s_cb.certPemFile;
  5847. }
  5848. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5849. if (!s_sharedCtx)
  5850. #endif
  5851. {
  5852. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5853. serverCertFile), WOLFSSL_SUCCESS);
  5854. }
  5855. if (ctx->s_cb.keyPemFile != NULL) {
  5856. serverKeyFile = ctx->s_cb.keyPemFile;
  5857. }
  5858. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5859. if (!s_sharedCtx)
  5860. #endif
  5861. {
  5862. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5863. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5864. }
  5865. if (ctx->s_ciphers != NULL) {
  5866. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5867. WOLFSSL_SUCCESS);
  5868. }
  5869. if (ctx->s_cb.ctx_ready != NULL) {
  5870. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5871. }
  5872. /****************************
  5873. * Create WOLFSSL for client.
  5874. ****************************/
  5875. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5876. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5877. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5878. if (0
  5879. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5880. || c_sharedCtx
  5881. #endif
  5882. )
  5883. {
  5884. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5885. clientCertFile), WOLFSSL_SUCCESS);
  5886. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5887. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5888. }
  5889. if (ctx->c_cb.ssl_ready != NULL) {
  5890. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5891. }
  5892. /****************************
  5893. * Create WOLFSSL for server.
  5894. ****************************/
  5895. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5896. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5897. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5898. if (0
  5899. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5900. || s_sharedCtx
  5901. #endif
  5902. )
  5903. {
  5904. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5905. serverCertFile), WOLFSSL_SUCCESS);
  5906. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5907. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5908. }
  5909. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5910. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5911. #elif !defined(NO_DH)
  5912. /* will repick suites with DHE, higher priority than PSK */
  5913. SetDH(ctx->s_ssl);
  5914. #endif
  5915. if (ctx->s_cb.ssl_ready != NULL) {
  5916. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5917. }
  5918. return EXPECT_RESULT();
  5919. }
  5920. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5921. int* rounds)
  5922. {
  5923. int handshake_complete = 0;
  5924. int hs_c = 0;
  5925. int hs_s = 0;
  5926. int failing_s = 0;
  5927. int failing_c = 0;
  5928. int ret;
  5929. int err;
  5930. if (rounds != NULL) {
  5931. *rounds = 0;
  5932. }
  5933. while ((!handshake_complete) && (max_rounds > 0)) {
  5934. if (!hs_c) {
  5935. wolfSSL_SetLoggingPrefix("client");
  5936. ret = wolfSSL_connect(ctx->c_ssl);
  5937. wolfSSL_SetLoggingPrefix(NULL);
  5938. if (ret == WOLFSSL_SUCCESS) {
  5939. hs_c = 1;
  5940. }
  5941. else {
  5942. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5943. if (err != WOLFSSL_ERROR_WANT_READ &&
  5944. err != WOLFSSL_ERROR_WANT_WRITE) {
  5945. char buff[WOLFSSL_MAX_ERROR_SZ];
  5946. fprintf(stderr, "error = %d, %s\n", err,
  5947. wolfSSL_ERR_error_string(err, buff));
  5948. failing_c = 1;
  5949. hs_c = 1;
  5950. if (failing_c && failing_s) {
  5951. break;
  5952. }
  5953. }
  5954. }
  5955. }
  5956. if (!hs_s) {
  5957. wolfSSL_SetLoggingPrefix("server");
  5958. ret = wolfSSL_accept(ctx->s_ssl);
  5959. wolfSSL_SetLoggingPrefix(NULL);
  5960. if (ret == WOLFSSL_SUCCESS) {
  5961. hs_s = 1;
  5962. }
  5963. else {
  5964. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5965. if (err != WOLFSSL_ERROR_WANT_READ &&
  5966. err != WOLFSSL_ERROR_WANT_WRITE) {
  5967. char buff[WOLFSSL_MAX_ERROR_SZ];
  5968. fprintf(stderr, "error = %d, %s\n", err,
  5969. wolfSSL_ERR_error_string(err, buff));
  5970. failing_s = 1;
  5971. hs_s = 1;
  5972. if (failing_c && failing_s) {
  5973. break;
  5974. }
  5975. }
  5976. }
  5977. }
  5978. handshake_complete = hs_c && hs_s;
  5979. max_rounds--;
  5980. if (rounds != NULL) {
  5981. *rounds += 1;
  5982. }
  5983. }
  5984. if (!handshake_complete || failing_c || failing_s) {
  5985. return TEST_FAIL;
  5986. }
  5987. return TEST_SUCCESS;
  5988. }
  5989. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5990. {
  5991. EXPECT_DECLS;
  5992. char input[1024];
  5993. int idx = 0;
  5994. const char* msg_c = "hello wolfssl!";
  5995. int msglen_c = (int)XSTRLEN(msg_c);
  5996. const char* msg_s = "I hear you fa shizzle!";
  5997. int msglen_s = (int)XSTRLEN(msg_s);
  5998. if (ctx->c_msg != NULL) {
  5999. msg_c = ctx->c_msg;
  6000. msglen_c = ctx->c_msglen;
  6001. }
  6002. if (ctx->s_msg != NULL) {
  6003. msg_s = ctx->s_msg;
  6004. msglen_s = ctx->s_msglen;
  6005. }
  6006. wolfSSL_SetLoggingPrefix("client");
  6007. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  6008. wolfSSL_SetLoggingPrefix("server");
  6009. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  6010. if (idx >= 0) {
  6011. input[idx] = '\0';
  6012. }
  6013. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  6014. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6015. ctx->s_cb.return_code = EXPECT_RESULT();
  6016. wolfSSL_SetLoggingPrefix("client");
  6017. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6018. wolfSSL_SetLoggingPrefix(NULL);
  6019. if (idx >= 0) {
  6020. input[idx] = '\0';
  6021. }
  6022. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6023. ctx->c_cb.return_code = EXPECT_RESULT();
  6024. if (ctx->c_cb.on_result != NULL) {
  6025. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6026. }
  6027. if (ctx->s_cb.on_result != NULL) {
  6028. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6029. }
  6030. return EXPECT_RESULT();
  6031. }
  6032. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6033. {
  6034. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6035. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6036. if (ctx->c_cb.on_cleanup != NULL) {
  6037. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6038. }
  6039. if (ctx->s_cb.on_cleanup != NULL) {
  6040. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6041. }
  6042. wolfSSL_shutdown(ctx->s_ssl);
  6043. wolfSSL_shutdown(ctx->c_ssl);
  6044. wolfSSL_free(ctx->s_ssl);
  6045. wolfSSL_free(ctx->c_ssl);
  6046. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6047. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6048. }
  6049. if (!ctx->c_cb.isSharedCtx) {
  6050. wolfSSL_CTX_free(ctx->c_ctx);
  6051. ctx->c_ctx = NULL;
  6052. }
  6053. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6054. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6055. }
  6056. if (!ctx->s_cb.isSharedCtx) {
  6057. wolfSSL_CTX_free(ctx->s_ctx);
  6058. ctx->s_ctx = NULL;
  6059. }
  6060. if (!ctx->s_cb.ticNoInit) {
  6061. #if defined(HAVE_SESSION_TICKET) && \
  6062. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6063. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6064. OpenSSLTicketCleanup();
  6065. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6066. TicketCleanup();
  6067. #endif
  6068. #endif
  6069. }
  6070. }
  6071. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6072. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6073. {
  6074. EXPECT_DECLS;
  6075. struct test_ssl_memio_ctx test_ctx;
  6076. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6077. size_t msg_len;
  6078. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6079. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6080. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6081. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6082. test_ctx.c_ctx = client_cb->ctx;
  6083. test_ctx.s_ctx = server_cb->ctx;
  6084. test_ctx.c_cb.return_code = TEST_FAIL;
  6085. test_ctx.s_cb.return_code = TEST_FAIL;
  6086. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6087. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6088. if (client_on_handshake != NULL) {
  6089. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6090. TEST_SUCCESS);
  6091. }
  6092. if (client_cb->on_handshake != NULL) {
  6093. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6094. TEST_SUCCESS);
  6095. }
  6096. if (server_cb->on_handshake != NULL) {
  6097. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6098. TEST_SUCCESS);
  6099. }
  6100. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6101. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6102. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6103. MD_MAX_SIZE);
  6104. ExpectIntGE(msg_len, 0);
  6105. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6106. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6107. MD_MAX_SIZE);
  6108. ExpectIntGE(msg_len, 0);
  6109. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6110. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6111. test_ssl_memio_cleanup(&test_ctx);
  6112. client_cb->return_code = test_ctx.c_cb.return_code;
  6113. client_cb->last_err = test_ctx.c_cb.last_err;
  6114. server_cb->return_code = test_ctx.s_cb.return_code;
  6115. server_cb->last_err = test_ctx.s_cb.last_err;
  6116. return EXPECT_RESULT();
  6117. }
  6118. #endif
  6119. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6120. #ifdef WOLFSSL_SESSION_EXPORT
  6121. #ifdef WOLFSSL_DTLS
  6122. /* set up function for sending session information */
  6123. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6124. {
  6125. WOLFSSL_CTX* ctx = NULL;
  6126. WOLFSSL* ssl = NULL;
  6127. AssertNotNull(inSsl);
  6128. AssertNotNull(buf);
  6129. AssertIntNE(0, sz);
  6130. /* Set ctx to DTLS 1.2 */
  6131. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6132. AssertNotNull(ctx);
  6133. ssl = wolfSSL_new(ctx);
  6134. AssertNotNull(ssl);
  6135. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6136. wolfSSL_free(ssl);
  6137. wolfSSL_CTX_free(ctx);
  6138. (void)userCtx;
  6139. return 0;
  6140. }
  6141. #endif
  6142. /* returns negative value on fail and positive (including 0) on success */
  6143. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6144. {
  6145. int ret, err, loop_count, count, timeout = 10;
  6146. char msg[] = "I hear you fa shizzle!";
  6147. char input[1024];
  6148. loop_count = ((func_args*)args)->argc;
  6149. #ifdef WOLFSSL_ASYNC_CRYPT
  6150. err = 0; /* Reset error */
  6151. #endif
  6152. do {
  6153. #ifdef WOLFSSL_ASYNC_CRYPT
  6154. if (err == WC_PENDING_E) {
  6155. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6156. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6157. }
  6158. #endif
  6159. ret = wolfSSL_accept(ssl);
  6160. err = wolfSSL_get_error(ssl, 0);
  6161. if (err == WOLFSSL_ERROR_WANT_READ ||
  6162. err == WOLFSSL_ERROR_WANT_WRITE) {
  6163. int select_ret;
  6164. err = WC_PENDING_E;
  6165. select_ret = tcp_select(*sockfd, timeout);
  6166. if (select_ret == TEST_TIMEOUT) {
  6167. return WOLFSSL_FATAL_ERROR;
  6168. }
  6169. }
  6170. } while (err == WC_PENDING_E);
  6171. if (ret != WOLFSSL_SUCCESS) {
  6172. char buff[WOLFSSL_MAX_ERROR_SZ];
  6173. fprintf(stderr, "error = %d, %s\n", err,
  6174. wolfSSL_ERR_error_string(err, buff));
  6175. return ret;
  6176. }
  6177. for (count = 0; count < loop_count; count++) {
  6178. int select_ret;
  6179. select_ret = tcp_select(*sockfd, timeout);
  6180. if (select_ret == TEST_TIMEOUT) {
  6181. ret = WOLFSSL_FATAL_ERROR;
  6182. break;
  6183. }
  6184. do {
  6185. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6186. if (ret > 0) {
  6187. input[ret] = '\0';
  6188. fprintf(stderr, "Client message: %s\n", input);
  6189. }
  6190. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6191. do {
  6192. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6193. return WOLFSSL_FATAL_ERROR;
  6194. }
  6195. err = wolfSSL_get_error(ssl, ret);
  6196. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6197. }
  6198. return ret;
  6199. }
  6200. #endif /* WOLFSSL_SESSION_EXPORT */
  6201. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6202. {
  6203. SOCKET_T sockfd = 0;
  6204. SOCKET_T clientfd = 0;
  6205. word16 port;
  6206. callback_functions* cbf;
  6207. WOLFSSL_CTX* ctx = NULL;
  6208. WOLFSSL* ssl = NULL;
  6209. func_args* opts = (func_args*)args;
  6210. char msg[] = "I hear you fa shizzle!";
  6211. char input[1024];
  6212. int idx;
  6213. int ret, err = 0;
  6214. int sharedCtx = 0;
  6215. int doUdp = 0;
  6216. SOCKADDR_IN_T cliAddr;
  6217. socklen_t cliLen;
  6218. const char* certFile = svrCertFile;
  6219. const char* keyFile = svrKeyFile;
  6220. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6221. size_t msg_len = 0;
  6222. #endif
  6223. wolfSSL_SetLoggingPrefix("server");
  6224. #ifdef WOLFSSL_TIRTOS
  6225. fdOpenSession(Task_self());
  6226. #endif
  6227. opts->return_code = TEST_FAIL;
  6228. cbf = opts->callbacks;
  6229. if (cbf != NULL && cbf->ctx) {
  6230. ctx = cbf->ctx;
  6231. sharedCtx = 1;
  6232. }
  6233. else
  6234. {
  6235. WOLFSSL_METHOD* method = NULL;
  6236. if (cbf != NULL && cbf->method != NULL) {
  6237. method = cbf->method();
  6238. }
  6239. else {
  6240. method = wolfSSLv23_server_method();
  6241. }
  6242. ctx = wolfSSL_CTX_new(method);
  6243. }
  6244. if (ctx == NULL) {
  6245. /* Release the wait for TCP ready. */
  6246. signal_ready(opts->signal);
  6247. goto done;
  6248. }
  6249. if (cbf == NULL || !cbf->ticNoInit) {
  6250. #if defined(HAVE_SESSION_TICKET) && \
  6251. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6252. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6253. OpenSSLTicketInit();
  6254. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6255. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6256. TicketInit();
  6257. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6258. #endif
  6259. #endif
  6260. }
  6261. #if defined(USE_WINDOWS_API)
  6262. port = opts->signal->port;
  6263. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6264. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6265. /* Let tcp_listen assign port */
  6266. port = 0;
  6267. #else
  6268. /* Use default port */
  6269. port = wolfSSLPort;
  6270. #endif
  6271. if (cbf != NULL)
  6272. doUdp = cbf->doUdp;
  6273. /* do it here to detect failure */
  6274. tcp_accept(
  6275. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6276. if (doUdp) {
  6277. cliLen = sizeof(cliAddr);
  6278. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6279. (struct sockaddr*)&cliAddr, &cliLen);
  6280. AssertIntGT(idx, 0);
  6281. }
  6282. else {
  6283. CloseSocket(sockfd);
  6284. }
  6285. wolfSSL_CTX_set_verify(ctx,
  6286. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6287. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6288. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6289. #endif
  6290. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6291. != WOLFSSL_SUCCESS) {
  6292. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6293. goto done;
  6294. }
  6295. if (cbf != NULL && cbf->certPemFile != NULL)
  6296. certFile = cbf->certPemFile;
  6297. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6298. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6299. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6300. #else
  6301. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6302. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6303. #endif
  6304. /*err_sys("can't load server cert chain file, "
  6305. "Please run from wolfSSL home dir");*/
  6306. goto done;
  6307. }
  6308. if (cbf != NULL && cbf->keyPemFile != NULL)
  6309. keyFile = cbf->keyPemFile;
  6310. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6311. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6312. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6313. #else
  6314. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6315. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6316. #endif
  6317. /*err_sys("can't load server key file, "
  6318. "Please run from wolfSSL home dir");*/
  6319. goto done;
  6320. }
  6321. #ifdef HAVE_CRL
  6322. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6323. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6324. goto done;
  6325. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6326. != WOLFSSL_SUCCESS)
  6327. goto done;
  6328. }
  6329. #endif
  6330. /* call ctx setup callback */
  6331. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6332. cbf->ctx_ready(ctx);
  6333. }
  6334. ssl = wolfSSL_new(ctx);
  6335. if (ssl == NULL) {
  6336. goto done;
  6337. }
  6338. if (doUdp) {
  6339. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6340. if (err != WOLFSSL_SUCCESS)
  6341. goto done;
  6342. }
  6343. #ifdef WOLFSSL_SESSION_EXPORT
  6344. /* only add in more complex nonblocking case with session export tests */
  6345. if (args && opts->argc > 0) {
  6346. /* set as nonblock and time out for waiting on read/write */
  6347. tcp_set_nonblocking(&clientfd);
  6348. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6349. }
  6350. #endif
  6351. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6352. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6353. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6354. #else
  6355. if (wolfSSL_use_certificate_file(ssl, certFile,
  6356. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6357. #endif
  6358. /*err_sys("can't load server cert chain file, "
  6359. "Please run from wolfSSL home dir");*/
  6360. goto done;
  6361. }
  6362. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6363. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6364. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6365. #else
  6366. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6367. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6368. #endif
  6369. /*err_sys("can't load server key file, "
  6370. "Please run from wolfSSL home dir");*/
  6371. goto done;
  6372. }
  6373. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6374. /*err_sys("SSL_set_fd failed");*/
  6375. goto done;
  6376. }
  6377. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6378. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6379. #elif !defined(NO_DH)
  6380. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6381. #endif
  6382. /* call ssl setup callback */
  6383. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6384. cbf->ssl_ready(ssl);
  6385. }
  6386. #ifdef WOLFSSL_SESSION_EXPORT
  6387. /* only add in more complex nonblocking case with session export tests */
  6388. if (opts->argc > 0) {
  6389. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6390. if (ret >= 0) {
  6391. opts->return_code = TEST_SUCCESS;
  6392. }
  6393. #ifdef WOLFSSL_TIRTOS
  6394. Task_yield();
  6395. #endif
  6396. goto done;
  6397. }
  6398. #endif
  6399. #ifdef WOLFSSL_ASYNC_CRYPT
  6400. err = 0; /* Reset error */
  6401. #endif
  6402. do {
  6403. #ifdef WOLFSSL_ASYNC_CRYPT
  6404. if (err == WC_PENDING_E) {
  6405. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6406. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6407. }
  6408. #endif
  6409. ret = wolfSSL_negotiate(ssl);
  6410. err = wolfSSL_get_error(ssl, 0);
  6411. } while (err == WC_PENDING_E);
  6412. if (ret != WOLFSSL_SUCCESS) {
  6413. char buff[WOLFSSL_MAX_ERROR_SZ];
  6414. fprintf(stderr, "error = %d, %s\n", err,
  6415. wolfSSL_ERR_error_string(err, buff));
  6416. /*err_sys("SSL_accept failed");*/
  6417. goto done;
  6418. }
  6419. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6420. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6421. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6422. AssertIntGE(msg_len, 0);
  6423. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6424. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6425. AssertIntGE(msg_len, 0);
  6426. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6427. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6428. if (idx > 0) {
  6429. input[idx] = '\0';
  6430. fprintf(stderr, "Client message: %s\n", input);
  6431. }
  6432. else if (idx < 0) {
  6433. goto done;
  6434. }
  6435. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6436. /*err_sys("SSL_write failed");*/
  6437. goto done;
  6438. }
  6439. if (cbf != NULL && cbf->on_result != NULL)
  6440. cbf->on_result(ssl);
  6441. #ifdef WOLFSSL_TIRTOS
  6442. Task_yield();
  6443. #endif
  6444. opts->return_code = TEST_SUCCESS;
  6445. done:
  6446. if (cbf != NULL)
  6447. cbf->last_err = err;
  6448. if (cbf != NULL && cbf->on_cleanup != NULL)
  6449. cbf->on_cleanup(ssl);
  6450. wolfSSL_shutdown(ssl);
  6451. wolfSSL_free(ssl);
  6452. if (!sharedCtx)
  6453. wolfSSL_CTX_free(ctx);
  6454. CloseSocket(clientfd);
  6455. #ifdef WOLFSSL_TIRTOS
  6456. fdCloseSession(Task_self());
  6457. #endif
  6458. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6459. && defined(HAVE_THREAD_LS)
  6460. wc_ecc_fp_free(); /* free per thread cache */
  6461. #endif
  6462. if (cbf == NULL || !cbf->ticNoInit) {
  6463. #if defined(HAVE_SESSION_TICKET) && \
  6464. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6465. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6466. OpenSSLTicketCleanup();
  6467. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6468. TicketCleanup();
  6469. #endif
  6470. #endif
  6471. }
  6472. wolfSSL_SetLoggingPrefix(NULL);
  6473. WOLFSSL_RETURN_FROM_THREAD(0);
  6474. }
  6475. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6476. !defined(WOLFSSL_NO_TLS12)
  6477. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6478. {
  6479. SOCKET_T sockfd;
  6480. SOCKET_T clientfd = -1;
  6481. word16 port;
  6482. callback_functions* cbf;
  6483. WOLFSSL_CTX* ctx = 0;
  6484. WOLFSSL* ssl = 0;
  6485. char msg[] = "I hear you fa shizzle!";
  6486. char input[1024];
  6487. int idx;
  6488. int ret, err = 0;
  6489. int sharedCtx = 0;
  6490. func_args* opts = (func_args*)args;
  6491. int loop_count = opts->argc;
  6492. int count = 0;
  6493. #ifdef WOLFSSL_TIRTOS
  6494. fdOpenSession(Task_self());
  6495. #endif
  6496. opts->return_code = TEST_FAIL;
  6497. cbf = opts->callbacks;
  6498. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6499. if (cbf != NULL && cbf->ctx) {
  6500. ctx = cbf->ctx;
  6501. sharedCtx = 1;
  6502. }
  6503. else
  6504. #endif
  6505. {
  6506. WOLFSSL_METHOD* method = NULL;
  6507. if (cbf != NULL && cbf->method != NULL) {
  6508. method = cbf->method();
  6509. }
  6510. else {
  6511. method = wolfSSLv23_server_method();
  6512. }
  6513. ctx = wolfSSL_CTX_new(method);
  6514. }
  6515. #if defined(USE_WINDOWS_API)
  6516. port = opts->signal->port;
  6517. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6518. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6519. /* Let tcp_listen assign port */
  6520. port = 0;
  6521. #else
  6522. /* Use default port */
  6523. port = wolfSSLPort;
  6524. #endif
  6525. wolfSSL_CTX_set_verify(ctx,
  6526. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6527. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6528. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6529. #endif
  6530. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6531. != WOLFSSL_SUCCESS) {
  6532. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6533. /* Release the wait for TCP ready. */
  6534. signal_ready(opts->signal);
  6535. goto done;
  6536. }
  6537. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6538. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6539. /*err_sys("can't load server cert chain file, "
  6540. "Please run from wolfSSL home dir");*/
  6541. /* Release the wait for TCP ready. */
  6542. signal_ready(opts->signal);
  6543. goto done;
  6544. }
  6545. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6546. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6547. /*err_sys("can't load server key file, "
  6548. "Please run from wolfSSL home dir");*/
  6549. /* Release the wait for TCP ready. */
  6550. signal_ready(opts->signal);
  6551. goto done;
  6552. }
  6553. /* call ctx setup callback */
  6554. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6555. cbf->ctx_ready(ctx);
  6556. }
  6557. while (count != loop_count) {
  6558. ssl = wolfSSL_new(ctx);
  6559. if (ssl == NULL) {
  6560. signal_ready(opts->signal);
  6561. goto done;
  6562. }
  6563. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6564. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6565. /*err_sys("can't load server cert chain file, "
  6566. "Please run from wolfSSL home dir");*/
  6567. /* Release the wait for TCP ready. */
  6568. signal_ready(opts->signal);
  6569. goto done;
  6570. }
  6571. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6572. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6573. /*err_sys("can't load server key file, "
  6574. "Please run from wolfSSL home dir");*/
  6575. /* Release the wait for TCP ready. */
  6576. signal_ready(opts->signal);
  6577. goto done;
  6578. }
  6579. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6580. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6581. #elif !defined(NO_DH)
  6582. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6583. #endif
  6584. /* call ssl setup callback */
  6585. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6586. cbf->ssl_ready(ssl);
  6587. }
  6588. /* do it here to detect failure */
  6589. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6590. 0);
  6591. CloseSocket(sockfd);
  6592. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6593. /*err_sys("SSL_set_fd failed");*/
  6594. goto done;
  6595. }
  6596. #ifdef WOLFSSL_ASYNC_CRYPT
  6597. err = 0; /* Reset error */
  6598. #endif
  6599. do {
  6600. #ifdef WOLFSSL_ASYNC_CRYPT
  6601. if (err == WC_PENDING_E) {
  6602. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6603. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6604. }
  6605. #endif
  6606. ret = wolfSSL_accept(ssl);
  6607. err = wolfSSL_get_error(ssl, 0);
  6608. } while (err == WC_PENDING_E);
  6609. if (ret != WOLFSSL_SUCCESS) {
  6610. char buff[WOLFSSL_MAX_ERROR_SZ];
  6611. fprintf(stderr, "error = %d, %s\n", err,
  6612. wolfSSL_ERR_error_string(err, buff));
  6613. /*err_sys("SSL_accept failed");*/
  6614. goto done;
  6615. }
  6616. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6617. if (idx > 0) {
  6618. input[idx] = '\0';
  6619. fprintf(stderr, "Client message: %s\n", input);
  6620. }
  6621. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6622. /*err_sys("SSL_write failed");*/
  6623. goto done;
  6624. }
  6625. /* free ssl for this connection */
  6626. wolfSSL_shutdown(ssl);
  6627. wolfSSL_free(ssl); ssl = NULL;
  6628. CloseSocket(clientfd);
  6629. clientfd = -1;
  6630. count++;
  6631. }
  6632. #ifdef WOLFSSL_TIRTOS
  6633. Task_yield();
  6634. #endif
  6635. opts->return_code = TEST_SUCCESS;
  6636. done:
  6637. if (ssl != NULL) {
  6638. wolfSSL_shutdown(ssl);
  6639. wolfSSL_free(ssl);
  6640. }
  6641. if (!sharedCtx)
  6642. wolfSSL_CTX_free(ctx);
  6643. if (clientfd >= 0)
  6644. CloseSocket(clientfd);
  6645. #ifdef WOLFSSL_TIRTOS
  6646. fdCloseSession(Task_self());
  6647. #endif
  6648. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6649. && defined(HAVE_THREAD_LS)
  6650. wc_ecc_fp_free(); /* free per thread cache */
  6651. #endif
  6652. WOLFSSL_RETURN_FROM_THREAD(0);
  6653. }
  6654. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6655. static int test_client_nofail(void* args, cbType cb)
  6656. {
  6657. #if !defined(NO_WOLFSSL_CLIENT)
  6658. SOCKET_T sockfd = 0;
  6659. callback_functions* cbf;
  6660. WOLFSSL_CTX* ctx = 0;
  6661. WOLFSSL* ssl = 0;
  6662. WOLFSSL_CIPHER* cipher;
  6663. char msg[64] = "hello wolfssl!";
  6664. char reply[1024];
  6665. int input;
  6666. int msgSz = (int)XSTRLEN(msg);
  6667. int ret, err = 0;
  6668. int cipherSuite;
  6669. int sharedCtx = 0;
  6670. int doUdp = 0;
  6671. const char* cipherName1, *cipherName2;
  6672. wolfSSL_SetLoggingPrefix("client");
  6673. #ifdef WOLFSSL_TIRTOS
  6674. fdOpenSession(Task_self());
  6675. #endif
  6676. ((func_args*)args)->return_code = TEST_FAIL;
  6677. cbf = ((func_args*)args)->callbacks;
  6678. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6679. if (cbf != NULL && cbf->ctx) {
  6680. ctx = cbf->ctx;
  6681. sharedCtx = cbf->isSharedCtx;
  6682. }
  6683. else
  6684. #endif
  6685. {
  6686. WOLFSSL_METHOD* method = NULL;
  6687. if (cbf != NULL && cbf->method != NULL) {
  6688. method = cbf->method();
  6689. }
  6690. else {
  6691. method = wolfSSLv23_client_method();
  6692. }
  6693. ctx = wolfSSL_CTX_new(method);
  6694. }
  6695. if (cbf != NULL)
  6696. doUdp = cbf->doUdp;
  6697. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6698. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6699. #endif
  6700. /* Do connect here so server detects failures */
  6701. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6702. doUdp, 0, NULL);
  6703. /* Connect the socket so that we don't have to set the peer later on */
  6704. if (doUdp)
  6705. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6706. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6707. {
  6708. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6709. goto done;
  6710. }
  6711. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6712. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6713. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6714. #else
  6715. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6716. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6717. #endif
  6718. /*err_sys("can't load client cert file, "
  6719. "Please run from wolfSSL home dir");*/
  6720. goto done;
  6721. }
  6722. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6723. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6724. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6725. #else
  6726. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6727. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6728. #endif
  6729. /*err_sys("can't load client key file, "
  6730. "Please run from wolfSSL home dir");*/
  6731. goto done;
  6732. }
  6733. #ifdef HAVE_CRL
  6734. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6735. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6736. goto done;
  6737. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6738. != WOLFSSL_SUCCESS)
  6739. goto done;
  6740. }
  6741. #endif
  6742. /* call ctx setup callback */
  6743. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6744. cbf->ctx_ready(ctx);
  6745. }
  6746. ssl = wolfSSL_new(ctx);
  6747. if (ssl == NULL) {
  6748. goto done;
  6749. }
  6750. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6751. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6752. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6753. #else
  6754. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6755. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6756. #endif
  6757. /*err_sys("can't load client cert file, "
  6758. "Please run from wolfSSL home dir");*/
  6759. goto done;
  6760. }
  6761. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6762. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6763. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6764. #else
  6765. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6766. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6767. #endif
  6768. /*err_sys("can't load client key file, "
  6769. "Please run from wolfSSL home dir");*/
  6770. goto done;
  6771. }
  6772. if (!doUdp) {
  6773. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6774. /*err_sys("SSL_set_fd failed");*/
  6775. goto done;
  6776. }
  6777. }
  6778. else {
  6779. #ifdef WOLFSSL_DTLS
  6780. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6781. /*err_sys("SSL_set_fd failed");*/
  6782. goto done;
  6783. }
  6784. #else
  6785. goto done;
  6786. #endif
  6787. }
  6788. /* call ssl setup callback */
  6789. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6790. cbf->ssl_ready(ssl);
  6791. }
  6792. #ifdef WOLFSSL_ASYNC_CRYPT
  6793. err = 0; /* Reset error */
  6794. #endif
  6795. do {
  6796. #ifdef WOLFSSL_ASYNC_CRYPT
  6797. if (err == WC_PENDING_E) {
  6798. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6799. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6800. }
  6801. #endif
  6802. ret = wolfSSL_negotiate(ssl);
  6803. err = wolfSSL_get_error(ssl, 0);
  6804. } while (err == WC_PENDING_E);
  6805. if (ret != WOLFSSL_SUCCESS) {
  6806. char buff[WOLFSSL_MAX_ERROR_SZ];
  6807. fprintf(stderr, "error = %d, %s\n", err,
  6808. wolfSSL_ERR_error_string(err, buff));
  6809. /*err_sys("SSL_connect failed");*/
  6810. goto done;
  6811. }
  6812. /* test the various get cipher methods */
  6813. /* Internal cipher suite names */
  6814. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6815. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6816. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6817. (cipherSuite >> 8), cipherSuite & 0xFF);
  6818. AssertStrEQ(cipherName1, cipherName2);
  6819. /* IANA Cipher Suites Names */
  6820. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6821. then it's the internal cipher suite name */
  6822. cipher = wolfSSL_get_current_cipher(ssl);
  6823. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6824. cipherName2 = wolfSSL_get_cipher(ssl);
  6825. AssertStrEQ(cipherName1, cipherName2);
  6826. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6827. !defined(WOLFSSL_QT)
  6828. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6829. (cipherSuite >> 8), cipherSuite & 0xFF);
  6830. AssertStrEQ(cipherName1, cipherName2);
  6831. #endif
  6832. if (cb != NULL)
  6833. (cb)(ctx, ssl);
  6834. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6835. /*err_sys("SSL_write failed");*/
  6836. goto done;
  6837. }
  6838. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6839. if (input > 0) {
  6840. reply[input] = '\0';
  6841. fprintf(stderr, "Server response: %s\n", reply);
  6842. }
  6843. if (cbf != NULL && cbf->on_result != NULL)
  6844. cbf->on_result(ssl);
  6845. ((func_args*)args)->return_code = TEST_SUCCESS;
  6846. done:
  6847. if (cbf != NULL)
  6848. cbf->last_err = err;
  6849. if (cbf != NULL && cbf->on_cleanup != NULL)
  6850. cbf->on_cleanup(ssl);
  6851. wolfSSL_free(ssl);
  6852. if (!sharedCtx)
  6853. wolfSSL_CTX_free(ctx);
  6854. CloseSocket(sockfd);
  6855. #ifdef WOLFSSL_TIRTOS
  6856. fdCloseSession(Task_self());
  6857. #endif
  6858. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6859. && defined(HAVE_THREAD_LS)
  6860. wc_ecc_fp_free(); /* free per thread cache */
  6861. #endif
  6862. #else
  6863. (void)args;
  6864. (void)cb;
  6865. #endif /* !NO_WOLFSSL_CLIENT */
  6866. wolfSSL_SetLoggingPrefix(NULL);
  6867. return 0;
  6868. }
  6869. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6870. callback_functions* server_cb, cbType client_on_handshake)
  6871. {
  6872. func_args client_args;
  6873. func_args server_args;
  6874. tcp_ready ready;
  6875. THREAD_TYPE serverThread;
  6876. XMEMSET(&client_args, 0, sizeof(func_args));
  6877. XMEMSET(&server_args, 0, sizeof(func_args));
  6878. #ifdef WOLFSSL_TIRTOS
  6879. fdOpenSession(Task_self());
  6880. #endif
  6881. StartTCP();
  6882. InitTcpReady(&ready);
  6883. #if defined(USE_WINDOWS_API)
  6884. /* use RNG to get random port if using windows */
  6885. ready.port = GetRandomPort();
  6886. #endif
  6887. server_args.signal = &ready;
  6888. server_args.callbacks = server_cb;
  6889. client_args.signal = &ready;
  6890. client_args.callbacks = client_cb;
  6891. start_thread(test_server_nofail, &server_args, &serverThread);
  6892. wait_tcp_ready(&server_args);
  6893. test_client_nofail(&client_args, client_on_handshake);
  6894. join_thread(serverThread);
  6895. client_cb->return_code = client_args.return_code;
  6896. server_cb->return_code = server_args.return_code;
  6897. FreeTcpReady(&ready);
  6898. #ifdef WOLFSSL_TIRTOS
  6899. fdOpenSession(Task_self());
  6900. #endif
  6901. }
  6902. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6903. callback_functions* server_cb)
  6904. {
  6905. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6906. }
  6907. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6908. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6909. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6910. void* server_args)
  6911. {
  6912. SOCKET_T sockfd = 0;
  6913. callback_functions* cbf;
  6914. WOLFSSL_CTX* ctx = 0;
  6915. WOLFSSL* ssl = 0;
  6916. WOLFSSL_SESSION* session = NULL;
  6917. char msg[64] = "hello wolfssl!";
  6918. char reply[1024];
  6919. int input;
  6920. int msgSz = (int)XSTRLEN(msg);
  6921. int ret, err = 0;
  6922. int sharedCtx = 0;
  6923. #ifdef WOLFSSL_TIRTOS
  6924. fdOpenSession(Task_self());
  6925. #endif
  6926. ((func_args*)args)->return_code = TEST_FAIL;
  6927. cbf = ((func_args*)args)->callbacks;
  6928. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6929. if (cbf != NULL && cbf->ctx) {
  6930. ctx = cbf->ctx;
  6931. sharedCtx = 1;
  6932. }
  6933. else
  6934. #endif
  6935. {
  6936. WOLFSSL_METHOD* method = NULL;
  6937. if (cbf != NULL && cbf->method != NULL) {
  6938. method = cbf->method();
  6939. }
  6940. else {
  6941. method = wolfSSLv23_client_method();
  6942. }
  6943. ctx = wolfSSL_CTX_new(method);
  6944. }
  6945. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6946. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6947. #endif
  6948. /* Do connect here so server detects failures */
  6949. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6950. 0, 0, NULL);
  6951. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6952. WOLFSSL_SUCCESS) {
  6953. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6954. goto done;
  6955. }
  6956. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6957. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6958. /*err_sys("can't load client cert file, "
  6959. "Please run from wolfSSL home dir");*/
  6960. goto done;
  6961. }
  6962. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6963. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6964. /*err_sys("can't load client key file, "
  6965. "Please run from wolfSSL home dir");*/
  6966. goto done;
  6967. }
  6968. /* call ctx setup callback */
  6969. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6970. cbf->ctx_ready(ctx);
  6971. }
  6972. ssl = wolfSSL_new(ctx);
  6973. if (ssl == NULL) {
  6974. goto done;
  6975. }
  6976. /* keep handshake resources for re-using WOLFSSL obj */
  6977. wolfSSL_KeepArrays(ssl);
  6978. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6979. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6980. goto done;
  6981. }
  6982. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6983. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6984. /*err_sys("can't load client cert file, "
  6985. "Please run from wolfSSL home dir");*/
  6986. goto done;
  6987. }
  6988. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6989. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6990. /*err_sys("can't load client key file, "
  6991. "Please run from wolfSSL home dir");*/
  6992. goto done;
  6993. }
  6994. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6995. /*err_sys("SSL_set_fd failed");*/
  6996. goto done;
  6997. }
  6998. /* call ssl setup callback */
  6999. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7000. cbf->ssl_ready(ssl);
  7001. }
  7002. #ifdef WOLFSSL_ASYNC_CRYPT
  7003. err = 0; /* Reset error */
  7004. #endif
  7005. do {
  7006. #ifdef WOLFSSL_ASYNC_CRYPT
  7007. if (err == WC_PENDING_E) {
  7008. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7009. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7010. }
  7011. #endif
  7012. ret = wolfSSL_connect(ssl);
  7013. err = wolfSSL_get_error(ssl, 0);
  7014. } while (err == WC_PENDING_E);
  7015. if (ret != WOLFSSL_SUCCESS) {
  7016. char buff[WOLFSSL_MAX_ERROR_SZ];
  7017. fprintf(stderr, "error = %d, %s\n", err,
  7018. wolfSSL_ERR_error_string(err, buff));
  7019. /*err_sys("SSL_connect failed");*/
  7020. goto done;
  7021. }
  7022. /* Build first session */
  7023. if (cb != NULL)
  7024. cb(ctx, ssl);
  7025. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7026. /*err_sys("SSL_write failed");*/
  7027. goto done;
  7028. }
  7029. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7030. if (input > 0) {
  7031. reply[input] = '\0';
  7032. fprintf(stderr, "Server response: %s\n", reply);
  7033. }
  7034. /* Session Resumption by re-using WOLFSSL object */
  7035. wolfSSL_set_quiet_shutdown(ssl, 1);
  7036. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7037. /* err_sys ("SSL shutdown failed"); */
  7038. goto done;
  7039. }
  7040. session = wolfSSL_get1_session(ssl);
  7041. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7042. wolfSSL_SESSION_free(session);
  7043. /* err_sys ("SSL_clear failed"); */
  7044. goto done;
  7045. }
  7046. wolfSSL_set_session(ssl, session);
  7047. wolfSSL_SESSION_free(session);
  7048. session = NULL;
  7049. /* close socket once */
  7050. CloseSocket(sockfd);
  7051. sockfd = 0;
  7052. /* wait until server ready */
  7053. wait_tcp_ready((func_args*)server_args);
  7054. fprintf(stderr, "session resumption\n");
  7055. /* Do re-connect */
  7056. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7057. 0, 0, NULL);
  7058. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7059. /*err_sys("SSL_set_fd failed");*/
  7060. goto done;
  7061. }
  7062. #ifdef WOLFSSL_ASYNC_CRYPT
  7063. err = 0; /* Reset error */
  7064. #endif
  7065. do {
  7066. #ifdef WOLFSSL_ASYNC_CRYPT
  7067. if (err == WC_PENDING_E) {
  7068. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7069. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7070. }
  7071. #endif
  7072. ret = wolfSSL_connect(ssl);
  7073. err = wolfSSL_get_error(ssl, 0);
  7074. } while (err == WC_PENDING_E);
  7075. if (ret != WOLFSSL_SUCCESS) {
  7076. char buff[WOLFSSL_MAX_ERROR_SZ];
  7077. fprintf(stderr, "error = %d, %s\n", err,
  7078. wolfSSL_ERR_error_string(err, buff));
  7079. /*err_sys("SSL_connect failed");*/
  7080. goto done;
  7081. }
  7082. /* Build first session */
  7083. if (cb != NULL)
  7084. cb(ctx, ssl);
  7085. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7086. /*err_sys("SSL_write failed");*/
  7087. goto done;
  7088. }
  7089. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7090. if (input > 0) {
  7091. reply[input] = '\0';
  7092. fprintf(stderr, "Server response: %s\n", reply);
  7093. }
  7094. ((func_args*)args)->return_code = TEST_SUCCESS;
  7095. done:
  7096. wolfSSL_free(ssl);
  7097. if (!sharedCtx)
  7098. wolfSSL_CTX_free(ctx);
  7099. CloseSocket(sockfd);
  7100. #ifdef WOLFSSL_TIRTOS
  7101. fdCloseSession(Task_self());
  7102. #endif
  7103. return;
  7104. }
  7105. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7106. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7107. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7108. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7109. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7110. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7111. #define HAVE_ALPN_PROTOS_SUPPORT
  7112. #endif
  7113. /* Generic TLS client / server with callbacks for API unit tests
  7114. * Used by SNI / ALPN / crypto callback helper functions */
  7115. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7116. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7117. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7118. #define ENABLE_TLS_CALLBACK_TEST
  7119. #endif
  7120. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7121. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7122. /* TLS server for API unit testing - generic */
  7123. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7124. {
  7125. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7126. WOLFSSL_CTX* ctx = NULL;
  7127. WOLFSSL* ssl = NULL;
  7128. SOCKET_T sfd = 0;
  7129. SOCKET_T cfd = 0;
  7130. word16 port;
  7131. char msg[] = "I hear you fa shizzle!";
  7132. int len = (int) XSTRLEN(msg);
  7133. char input[1024];
  7134. int idx;
  7135. int ret, err = 0;
  7136. ((func_args*)args)->return_code = TEST_FAIL;
  7137. #if defined(USE_WINDOWS_API)
  7138. port = ((func_args*)args)->signal->port;
  7139. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7140. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7141. /* Let tcp_listen assign port */
  7142. port = 0;
  7143. #else
  7144. /* Use default port */
  7145. port = wolfSSLPort;
  7146. #endif
  7147. #ifdef WOLFSSL_DTLS
  7148. if (callbacks->method == wolfDTLS_server_method
  7149. #ifdef WOLFSSL_STATIC_MEMORY
  7150. || callbacks->method_ex == wolfDTLS_server_method_ex
  7151. #endif
  7152. #ifndef NO_OLD_TLS
  7153. || callbacks->method == wolfDTLSv1_server_method
  7154. #ifdef WOLFSSL_STATIC_MEMORY
  7155. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7156. #endif
  7157. #endif
  7158. #ifndef WOLFSSL_NO_TLS12
  7159. || callbacks->method == wolfDTLSv1_2_server_method
  7160. #ifdef WOLFSSL_STATIC_MEMORY
  7161. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7162. #endif
  7163. #endif
  7164. #ifdef WOLFSSL_DTLS13
  7165. || callbacks->method == wolfDTLSv1_3_server_method
  7166. #ifdef WOLFSSL_STATIC_MEMORY
  7167. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7168. #endif
  7169. #endif
  7170. ) {
  7171. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7172. }
  7173. else
  7174. #endif
  7175. {
  7176. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7177. }
  7178. #ifdef WOLFSSL_STATIC_MEMORY
  7179. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7180. callbacks->memSz > 0) {
  7181. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7182. callbacks->mem, callbacks->memSz, 0, 1);
  7183. if (ret != WOLFSSL_SUCCESS) {
  7184. fprintf(stderr, "CTX static new failed %d\n", ret);
  7185. goto cleanup;
  7186. }
  7187. }
  7188. #else
  7189. ctx = wolfSSL_CTX_new(callbacks->method());
  7190. #endif
  7191. if (ctx == NULL) {
  7192. fprintf(stderr, "CTX new failed\n");
  7193. goto cleanup;
  7194. }
  7195. /* set defaults */
  7196. if (callbacks->caPemFile == NULL)
  7197. callbacks->caPemFile = cliCertFile;
  7198. if (callbacks->certPemFile == NULL)
  7199. callbacks->certPemFile = svrCertFile;
  7200. if (callbacks->keyPemFile == NULL)
  7201. callbacks->keyPemFile = svrKeyFile;
  7202. #ifdef WOLFSSL_TIRTOS
  7203. fdOpenSession(Task_self());
  7204. #endif
  7205. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7206. wolfSSL_CTX_set_verify(ctx,
  7207. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7208. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7209. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7210. #endif
  7211. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7212. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7213. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7214. goto cleanup;
  7215. }
  7216. #endif
  7217. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7218. WOLFSSL_SUCCESS) {
  7219. goto cleanup;
  7220. }
  7221. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7222. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7223. goto cleanup;
  7224. }
  7225. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7226. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7227. goto cleanup;
  7228. }
  7229. #ifdef HAVE_CRL
  7230. if (callbacks->crlPemFile != NULL) {
  7231. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7232. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7233. goto cleanup;
  7234. }
  7235. }
  7236. #endif
  7237. if (callbacks->ctx_ready)
  7238. callbacks->ctx_ready(ctx);
  7239. ssl = wolfSSL_new(ctx);
  7240. if (ssl == NULL) {
  7241. fprintf(stderr, "SSL new failed\n");
  7242. goto cleanup;
  7243. }
  7244. if (wolfSSL_dtls(ssl)) {
  7245. SOCKADDR_IN_T cliAddr;
  7246. socklen_t cliLen;
  7247. cliLen = sizeof(cliAddr);
  7248. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7249. (struct sockaddr*)&cliAddr, &cliLen);
  7250. if (idx <= 0) {
  7251. goto cleanup;
  7252. }
  7253. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7254. }
  7255. else {
  7256. CloseSocket(sfd);
  7257. }
  7258. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7259. goto cleanup;
  7260. }
  7261. if (callbacks->loadToSSL) {
  7262. wolfSSL_SetDevId(ssl, callbacks->devId);
  7263. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7264. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7265. goto cleanup;
  7266. }
  7267. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7268. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7269. goto cleanup;
  7270. }
  7271. }
  7272. #ifdef NO_PSK
  7273. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7274. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7275. #elif !defined(NO_DH)
  7276. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7277. #endif
  7278. #endif
  7279. if (callbacks->ssl_ready)
  7280. callbacks->ssl_ready(ssl);
  7281. #ifdef WOLFSSL_ASYNC_CRYPT
  7282. err = 0; /* Reset error */
  7283. #endif
  7284. do {
  7285. #ifdef WOLFSSL_ASYNC_CRYPT
  7286. if (err == WC_PENDING_E) {
  7287. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7288. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7289. }
  7290. #endif
  7291. ret = wolfSSL_accept(ssl);
  7292. err = wolfSSL_get_error(ssl, ret);
  7293. } while (err == WC_PENDING_E);
  7294. if (ret != WOLFSSL_SUCCESS) {
  7295. char buff[WOLFSSL_MAX_ERROR_SZ];
  7296. fprintf(stderr, "accept error = %d, %s\n", err,
  7297. wolfSSL_ERR_error_string(err, buff));
  7298. /*err_sys("SSL_accept failed");*/
  7299. }
  7300. else {
  7301. #ifdef WOLFSSL_ASYNC_CRYPT
  7302. err = 0; /* Reset error */
  7303. #endif
  7304. do {
  7305. #ifdef WOLFSSL_ASYNC_CRYPT
  7306. if (err == WC_PENDING_E) {
  7307. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7308. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7309. }
  7310. #endif
  7311. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7312. err = wolfSSL_get_error(ssl, idx);
  7313. } while (err == WC_PENDING_E);
  7314. if (idx > 0) {
  7315. input[idx] = 0;
  7316. fprintf(stderr, "Client message: %s\n", input);
  7317. }
  7318. #ifdef WOLFSSL_ASYNC_CRYPT
  7319. err = 0; /* Reset error */
  7320. #endif
  7321. do {
  7322. #ifdef WOLFSSL_ASYNC_CRYPT
  7323. if (err == WC_PENDING_E) {
  7324. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7325. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7326. }
  7327. #endif
  7328. ret = wolfSSL_write(ssl, msg, len);
  7329. err = wolfSSL_get_error(ssl, ret);
  7330. } while (err == WC_PENDING_E);
  7331. if (len != ret) {
  7332. goto cleanup;
  7333. }
  7334. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7335. defined(WOLFSSL_DTLS)
  7336. if (wolfSSL_dtls(ssl)) {
  7337. byte* import;
  7338. word32 sz;
  7339. wolfSSL_dtls_export(ssl, NULL, &sz);
  7340. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7341. if (import == NULL) {
  7342. goto cleanup;
  7343. }
  7344. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7345. if (idx < 0) {
  7346. goto cleanup;
  7347. }
  7348. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7349. goto cleanup;
  7350. }
  7351. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7352. }
  7353. #endif
  7354. #ifdef WOLFSSL_TIRTOS
  7355. Task_yield();
  7356. #endif
  7357. ((func_args*)args)->return_code = TEST_SUCCESS;
  7358. }
  7359. if (callbacks->on_result)
  7360. callbacks->on_result(ssl);
  7361. wolfSSL_shutdown(ssl);
  7362. cleanup:
  7363. wolfSSL_free(ssl);
  7364. wolfSSL_CTX_free(ctx);
  7365. CloseSocket(cfd);
  7366. #ifdef WOLFSSL_TIRTOS
  7367. fdCloseSession(Task_self());
  7368. #endif
  7369. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7370. && defined(HAVE_THREAD_LS)
  7371. wc_ecc_fp_free(); /* free per thread cache */
  7372. #endif
  7373. WOLFSSL_RETURN_FROM_THREAD(0);
  7374. }
  7375. /* TLS Client for API unit testing - generic */
  7376. static void run_wolfssl_client(void* args)
  7377. {
  7378. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7379. WOLFSSL_CTX* ctx = NULL;
  7380. WOLFSSL* ssl = NULL;
  7381. SOCKET_T sfd = 0;
  7382. char msg[] = "hello wolfssl server!";
  7383. int len = (int) XSTRLEN(msg);
  7384. char input[1024];
  7385. int ret, err = 0;
  7386. ((func_args*)args)->return_code = TEST_FAIL;
  7387. /* set defaults */
  7388. if (callbacks->caPemFile == NULL)
  7389. callbacks->caPemFile = caCertFile;
  7390. if (callbacks->certPemFile == NULL)
  7391. callbacks->certPemFile = cliCertFile;
  7392. if (callbacks->keyPemFile == NULL)
  7393. callbacks->keyPemFile = cliKeyFile;
  7394. #ifdef WOLFSSL_STATIC_MEMORY
  7395. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7396. callbacks->memSz > 0) {
  7397. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7398. callbacks->mem, callbacks->memSz, 0, 1);
  7399. if (ret != WOLFSSL_SUCCESS) {
  7400. fprintf(stderr, "CTX static new failed %d\n", ret);
  7401. goto cleanup;
  7402. }
  7403. }
  7404. #else
  7405. ctx = wolfSSL_CTX_new(callbacks->method());
  7406. #endif
  7407. if (ctx == NULL) {
  7408. fprintf(stderr, "CTX new failed\n");
  7409. goto cleanup;
  7410. }
  7411. #ifdef WOLFSSL_TIRTOS
  7412. fdOpenSession(Task_self());
  7413. #endif
  7414. if (!callbacks->loadToSSL) {
  7415. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7416. }
  7417. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7418. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7419. #endif
  7420. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7421. WOLFSSL_SUCCESS) {
  7422. goto cleanup;
  7423. }
  7424. if (!callbacks->loadToSSL) {
  7425. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7426. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7427. goto cleanup;
  7428. }
  7429. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7430. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7431. goto cleanup;
  7432. }
  7433. }
  7434. #ifdef HAVE_CRL
  7435. if (callbacks->crlPemFile != NULL) {
  7436. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7437. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7438. goto cleanup;
  7439. }
  7440. }
  7441. #endif
  7442. if (callbacks->ctx_ready)
  7443. callbacks->ctx_ready(ctx);
  7444. ssl = wolfSSL_new(ctx);
  7445. if (wolfSSL_dtls(ssl)) {
  7446. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7447. 1, 0, ssl);
  7448. }
  7449. else {
  7450. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7451. 0, 0, ssl);
  7452. }
  7453. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7454. goto cleanup;
  7455. }
  7456. if (callbacks->loadToSSL) {
  7457. wolfSSL_SetDevId(ssl, callbacks->devId);
  7458. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7459. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7460. goto cleanup;
  7461. }
  7462. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7463. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7464. goto cleanup;
  7465. }
  7466. }
  7467. if (callbacks->ssl_ready)
  7468. callbacks->ssl_ready(ssl);
  7469. #ifdef WOLFSSL_ASYNC_CRYPT
  7470. err = 0; /* Reset error */
  7471. #endif
  7472. do {
  7473. #ifdef WOLFSSL_ASYNC_CRYPT
  7474. if (err == WC_PENDING_E) {
  7475. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7476. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7477. }
  7478. #endif
  7479. ret = wolfSSL_connect(ssl);
  7480. err = wolfSSL_get_error(ssl, ret);
  7481. } while (err == WC_PENDING_E);
  7482. if (ret != WOLFSSL_SUCCESS) {
  7483. char buff[WOLFSSL_MAX_ERROR_SZ];
  7484. fprintf(stderr, "error = %d, %s\n", err,
  7485. wolfSSL_ERR_error_string(err, buff));
  7486. /*err_sys("SSL_connect failed");*/
  7487. }
  7488. else {
  7489. #ifdef WOLFSSL_ASYNC_CRYPT
  7490. err = 0; /* Reset error */
  7491. #endif
  7492. do {
  7493. #ifdef WOLFSSL_ASYNC_CRYPT
  7494. if (err == WC_PENDING_E) {
  7495. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7496. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7497. }
  7498. #endif
  7499. ret = wolfSSL_write(ssl, msg, len);
  7500. err = wolfSSL_get_error(ssl, ret);
  7501. } while (err == WC_PENDING_E);
  7502. if (len != ret)
  7503. goto cleanup;
  7504. #ifdef WOLFSSL_ASYNC_CRYPT
  7505. err = 0; /* Reset error */
  7506. #endif
  7507. do {
  7508. #ifdef WOLFSSL_ASYNC_CRYPT
  7509. if (err == WC_PENDING_E) {
  7510. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7511. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7512. }
  7513. #endif
  7514. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7515. err = wolfSSL_get_error(ssl, ret);
  7516. } while (err == WC_PENDING_E);
  7517. if (ret > 0) {
  7518. input[ret] = '\0'; /* null term */
  7519. fprintf(stderr, "Server response: %s\n", input);
  7520. }
  7521. ((func_args*)args)->return_code = TEST_SUCCESS;
  7522. }
  7523. if (callbacks->on_result)
  7524. callbacks->on_result(ssl);
  7525. cleanup:
  7526. wolfSSL_free(ssl);
  7527. wolfSSL_CTX_free(ctx);
  7528. CloseSocket(sfd);
  7529. #ifdef WOLFSSL_TIRTOS
  7530. fdCloseSession(Task_self());
  7531. #endif
  7532. }
  7533. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7534. static int test_wolfSSL_read_write(void)
  7535. {
  7536. /* The unit testing for read and write shall happen simultaneously, since
  7537. * one can't do anything with one without the other. (Except for a failure
  7538. * test case.) This function will call all the others that will set up,
  7539. * execute, and report their test findings.
  7540. *
  7541. * Set up the success case first. This function will become the template
  7542. * for the other tests. This should eventually be renamed
  7543. *
  7544. * The success case isn't interesting, how can this fail?
  7545. * - Do not give the client context a CA certificate. The connect should
  7546. * fail. Do not need server for this?
  7547. * - Using NULL for the ssl object on server. Do not need client for this.
  7548. * - Using NULL for the ssl object on client. Do not need server for this.
  7549. * - Good ssl objects for client and server. Client write() without server
  7550. * read().
  7551. * - Good ssl objects for client and server. Server write() without client
  7552. * read().
  7553. * - Forgetting the password callback?
  7554. */
  7555. tcp_ready ready;
  7556. func_args client_args;
  7557. func_args server_args;
  7558. THREAD_TYPE serverThread;
  7559. EXPECT_DECLS;
  7560. XMEMSET(&client_args, 0, sizeof(func_args));
  7561. XMEMSET(&server_args, 0, sizeof(func_args));
  7562. #ifdef WOLFSSL_TIRTOS
  7563. fdOpenSession(Task_self());
  7564. #endif
  7565. StartTCP();
  7566. InitTcpReady(&ready);
  7567. #if defined(USE_WINDOWS_API)
  7568. /* use RNG to get random port if using windows */
  7569. ready.port = GetRandomPort();
  7570. #endif
  7571. server_args.signal = &ready;
  7572. client_args.signal = &ready;
  7573. start_thread(test_server_nofail, &server_args, &serverThread);
  7574. wait_tcp_ready(&server_args);
  7575. test_client_nofail(&client_args, NULL);
  7576. join_thread(serverThread);
  7577. ExpectTrue(client_args.return_code);
  7578. ExpectTrue(server_args.return_code);
  7579. FreeTcpReady(&ready);
  7580. #ifdef WOLFSSL_TIRTOS
  7581. fdOpenSession(Task_self());
  7582. #endif
  7583. return EXPECT_RESULT();
  7584. }
  7585. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7586. {
  7587. EXPECT_DECLS;
  7588. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7589. !defined(WOLFSSL_NO_TLS12)
  7590. /* The unit test for session resumption by re-using WOLFSSL object.
  7591. * WOLFSSL object is not cleared after first session. It reuse the object
  7592. * for second connection.
  7593. */
  7594. tcp_ready ready;
  7595. func_args client_args;
  7596. func_args server_args;
  7597. THREAD_TYPE serverThread;
  7598. callback_functions client_cbf;
  7599. callback_functions server_cbf;
  7600. XMEMSET(&client_args, 0, sizeof(func_args));
  7601. XMEMSET(&server_args, 0, sizeof(func_args));
  7602. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7603. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7604. #ifdef WOLFSSL_TIRTOS
  7605. fdOpenSession(Task_self());
  7606. #endif
  7607. StartTCP();
  7608. InitTcpReady(&ready);
  7609. #if defined(USE_WINDOWS_API)
  7610. /* use RNG to get random port if using windows */
  7611. ready.port = GetRandomPort();
  7612. #endif
  7613. client_cbf.method = wolfTLSv1_2_client_method;
  7614. server_cbf.method = wolfTLSv1_2_server_method;
  7615. client_args.callbacks = &client_cbf;
  7616. server_args.callbacks = &server_cbf;
  7617. server_args.signal = &ready;
  7618. client_args.signal = &ready;
  7619. /* the var is used for loop number */
  7620. server_args.argc = 2;
  7621. start_thread(test_server_loop, &server_args, &serverThread);
  7622. wait_tcp_ready(&server_args);
  7623. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7624. join_thread(serverThread);
  7625. ExpectTrue(client_args.return_code);
  7626. ExpectTrue(server_args.return_code);
  7627. FreeTcpReady(&ready);
  7628. #ifdef WOLFSSL_TIRTOS
  7629. fdOpenSession(Task_self());
  7630. #endif
  7631. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7632. * !defined(WOLFSSL_TLS13) */
  7633. return EXPECT_RESULT();
  7634. }
  7635. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7636. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7637. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7638. WOLFSSL_CTX* ctx)
  7639. {
  7640. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7641. myVerifyAction = VERIFY_USE_PREVERFIY;
  7642. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7643. return TEST_SUCCESS;
  7644. }
  7645. #endif
  7646. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7647. {
  7648. EXPECT_DECLS;
  7649. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7650. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7651. test_ssl_cbf client_cbf;
  7652. test_ssl_cbf server_cbf;
  7653. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7654. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7655. #ifdef WOLFSSL_TLS13
  7656. client_cbf.method = wolfTLSv1_3_client_method;
  7657. #endif /* WOLFSSL_TLS13 */
  7658. client_cbf.ctx_ready =
  7659. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7660. /* test case 1 verify depth is equal to peer chain */
  7661. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7662. &server_cbf, NULL), TEST_SUCCESS);
  7663. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7664. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7665. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7666. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7667. return EXPECT_RESULT();
  7668. }
  7669. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7670. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7671. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7672. WOLFSSL_CTX* ctx)
  7673. {
  7674. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7675. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7676. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7677. return TEST_SUCCESS;
  7678. }
  7679. #endif
  7680. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7681. {
  7682. EXPECT_DECLS;
  7683. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7684. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7685. test_ssl_cbf client_cbf;
  7686. test_ssl_cbf server_cbf;
  7687. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7688. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7689. #ifdef WOLFSSL_TLS13
  7690. client_cbf.method = wolfTLSv1_3_client_method;
  7691. #endif /* WOLFSSL_TLS13 */
  7692. client_cbf.ctx_ready =
  7693. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7694. /* test case 2
  7695. * verify depth is zero, number of peer's chain is 2.
  7696. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7697. * callback.
  7698. */
  7699. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7700. &server_cbf, NULL), TEST_SUCCESS);
  7701. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7702. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7703. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7704. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7705. return EXPECT_RESULT();
  7706. }
  7707. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7708. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7709. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7710. WOLFSSL_CTX* ctx)
  7711. {
  7712. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7713. myVerifyAction = VERIFY_USE_PREVERFIY;
  7714. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7715. return TEST_SUCCESS;
  7716. }
  7717. #endif
  7718. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7719. {
  7720. EXPECT_DECLS;
  7721. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7722. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7723. test_ssl_cbf client_cbf;
  7724. test_ssl_cbf server_cbf;
  7725. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7726. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7727. #ifdef WOLFSSL_TLS13
  7728. client_cbf.method = wolfTLSv1_3_client_method;
  7729. #endif /* WOLFSSL_TLS13 */
  7730. client_cbf.ctx_ready =
  7731. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7732. /* test case 3
  7733. * verify depth is zero, number of peer's chain is 2
  7734. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7735. * therefore, handshake becomes failure.
  7736. */
  7737. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7738. &server_cbf, NULL), TEST_FAIL);
  7739. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7740. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7741. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7742. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7743. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7744. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7745. return EXPECT_RESULT();
  7746. }
  7747. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7748. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7749. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7750. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7751. {
  7752. EXPECT_DECLS;
  7753. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7754. return EXPECT_RESULT();
  7755. }
  7756. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7757. {
  7758. EXPECT_DECLS;
  7759. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7760. return EXPECT_RESULT();
  7761. }
  7762. #endif
  7763. static int test_wolfSSL_CTX_set_cipher_list(void)
  7764. {
  7765. EXPECT_DECLS;
  7766. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7767. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7768. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7769. WOLFSSL_CTX* ctxClient = NULL;
  7770. WOLFSSL* sslClient = NULL;
  7771. test_ssl_cbf client_cbf;
  7772. test_ssl_cbf server_cbf;
  7773. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7774. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7775. server_cbf.method = wolfTLSv1_2_server_method;
  7776. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7777. client_cbf.method = wolfTLSv1_2_client_method;
  7778. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7779. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7780. &server_cbf, NULL), TEST_SUCCESS);
  7781. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7782. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7783. /* check with cipher string that has '+' */
  7784. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7785. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7786. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7787. /* check for the existence of an ECDHE ECDSA cipher suite */
  7788. if (EXPECT_SUCCESS()) {
  7789. int i = 0;
  7790. int found = 0;
  7791. const char* suite;
  7792. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7793. WOLFSSL_CIPHER* current;
  7794. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7795. do {
  7796. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7797. if (current) {
  7798. suite = wolfSSL_CIPHER_get_name(current);
  7799. if (suite && XSTRSTR(suite, "ECDSA")) {
  7800. found = 1;
  7801. break;
  7802. }
  7803. }
  7804. } while (current);
  7805. ExpectIntEQ(found, 1);
  7806. }
  7807. wolfSSL_free(sslClient);
  7808. wolfSSL_CTX_free(ctxClient);
  7809. #endif
  7810. return EXPECT_RESULT();
  7811. }
  7812. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7813. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7814. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7815. WOLFSSL* ssl)
  7816. {
  7817. EXPECT_DECLS;
  7818. size_t msg_len;
  7819. (void)ctx;
  7820. /* get_finished test */
  7821. /* 1. get own sent message */
  7822. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7823. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7824. ExpectIntGE(msg_len, 0);
  7825. /* 2. get peer message */
  7826. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7827. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7828. ExpectIntGE(msg_len, 0);
  7829. return EXPECT_RESULT();
  7830. }
  7831. #endif
  7832. static int test_wolfSSL_get_finished(void)
  7833. {
  7834. EXPECT_DECLS;
  7835. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7836. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7837. test_ssl_cbf client_cbf;
  7838. test_ssl_cbf server_cbf;
  7839. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7840. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7841. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7842. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7843. TEST_SUCCESS);
  7844. /* test received msg vs sent msg */
  7845. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7846. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7847. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7848. return EXPECT_RESULT();
  7849. }
  7850. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7851. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7852. !defined(NO_SESSION_CACHE)
  7853. /* Sessions to restore/store */
  7854. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7855. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7856. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7857. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7858. {
  7859. /* Don't store sessions. Lookup is still enabled. */
  7860. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7861. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7862. #ifdef OPENSSL_EXTRA
  7863. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7864. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7865. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7866. #endif
  7867. /* Require both peers to provide certs */
  7868. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7869. }
  7870. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7871. {
  7872. WOLFSSL_SESSION** sess;
  7873. if (wolfSSL_is_server(ssl))
  7874. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7875. else
  7876. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7877. if (*sess == NULL) {
  7878. #ifdef NO_SESSION_CACHE_REF
  7879. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7880. #else
  7881. /* Test for backwards compatibility */
  7882. if (wolfSSL_is_server(ssl)) {
  7883. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7884. }
  7885. else {
  7886. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7887. }
  7888. #endif
  7889. /* Now save the session in the internal store to make it available
  7890. * for lookup. For TLS 1.3, we can't save the session without
  7891. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7892. * session from cache. */
  7893. if (wolfSSL_is_server(ssl)
  7894. #ifndef WOLFSSL_TICKET_HAVE_ID
  7895. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7896. #endif
  7897. )
  7898. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7899. *sess), WOLFSSL_SUCCESS);
  7900. }
  7901. else {
  7902. /* If we have a session retrieved then remaining connections should be
  7903. * resuming on that session */
  7904. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7905. }
  7906. /* Save CTX to be able to decrypt tickets */
  7907. if (wolfSSL_is_server(ssl) &&
  7908. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7909. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7910. = wolfSSL_get_SSL_CTX(ssl));
  7911. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7912. WOLFSSL_SUCCESS);
  7913. }
  7914. #ifdef SESSION_CERTS
  7915. #ifndef WOLFSSL_TICKET_HAVE_ID
  7916. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7917. wolfSSL_session_reused(ssl))
  7918. #endif
  7919. {
  7920. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7921. * for all connections. TLS 1.3 only has tickets so if we don't
  7922. * include the session id in the ticket then the certificates
  7923. * will not be available on resumption. */
  7924. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7925. AssertNotNull(peer);
  7926. wolfSSL_X509_free(peer);
  7927. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7928. #ifdef OPENSSL_EXTRA
  7929. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7930. #endif
  7931. }
  7932. #endif /* SESSION_CERTS */
  7933. }
  7934. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7935. {
  7936. /* Set the session to reuse for the client */
  7937. AssertIntEQ(wolfSSL_set_session(ssl,
  7938. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7939. }
  7940. #endif
  7941. static int test_wolfSSL_CTX_add_session(void)
  7942. {
  7943. EXPECT_DECLS;
  7944. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7945. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7946. !defined(NO_SESSION_CACHE)
  7947. tcp_ready ready;
  7948. func_args client_args;
  7949. func_args server_args;
  7950. THREAD_TYPE serverThread;
  7951. callback_functions client_cb;
  7952. callback_functions server_cb;
  7953. method_provider methods[][2] = {
  7954. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7955. !defined(NO_DES3))
  7956. /* Without AES there are almost no ciphersuites available. This leads
  7957. * to no ciphersuites being available and an error. */
  7958. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7959. #endif
  7960. #ifndef WOLFSSL_NO_TLS12
  7961. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7962. #endif
  7963. /* Needs the default ticket callback since it is tied to the
  7964. * connection context and this makes it easy to carry over the ticket
  7965. * crypto context between connections */
  7966. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7967. defined(HAVE_SESSION_TICKET)
  7968. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7969. #endif
  7970. };
  7971. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7972. size_t i, j;
  7973. for (i = 0; i < methodsLen; i++) {
  7974. /* First run creates a connection while the second+ run will attempt
  7975. * to resume the connection. The trick is that the internal cache
  7976. * is turned off. wolfSSL_CTX_add_session should put the session in
  7977. * the cache anyway. */
  7978. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7979. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7980. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7981. #ifdef NO_SESSION_CACHE_REF
  7982. for (j = 0; j < 4; j++) {
  7983. #else
  7984. /* The session may be overwritten in this case. Do only one resumption
  7985. * to stop this test from failing intermittently. */
  7986. for (j = 0; j < 2; j++) {
  7987. #endif
  7988. #ifdef WOLFSSL_TIRTOS
  7989. fdOpenSession(Task_self());
  7990. #endif
  7991. StartTCP();
  7992. InitTcpReady(&ready);
  7993. XMEMSET(&client_args, 0, sizeof(func_args));
  7994. XMEMSET(&server_args, 0, sizeof(func_args));
  7995. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7996. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7997. client_cb.method = methods[i][0];
  7998. server_cb.method = methods[i][1];
  7999. server_args.signal = &ready;
  8000. server_args.callbacks = &server_cb;
  8001. client_args.signal = &ready;
  8002. client_args.callbacks = &client_cb;
  8003. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  8004. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  8005. server_cb.isSharedCtx = 1;
  8006. }
  8007. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8008. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8009. if (j != 0)
  8010. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  8011. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8012. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8013. server_cb.ticNoInit = 1; /* Use default builtin */
  8014. start_thread(test_server_nofail, &server_args, &serverThread);
  8015. wait_tcp_ready(&server_args);
  8016. test_client_nofail(&client_args, NULL);
  8017. join_thread(serverThread);
  8018. ExpectTrue(client_args.return_code);
  8019. ExpectTrue(server_args.return_code);
  8020. FreeTcpReady(&ready);
  8021. if (EXPECT_FAIL())
  8022. break;
  8023. }
  8024. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8025. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8026. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8027. if (EXPECT_FAIL())
  8028. break;
  8029. }
  8030. #endif
  8031. return EXPECT_RESULT();
  8032. }
  8033. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8034. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8035. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8036. defined(HAVE_SESSION_TICKET) && \
  8037. !defined(TITAN_SESSION_CACHE) && \
  8038. !defined(HUGE_SESSION_CACHE) && \
  8039. !defined(BIG_SESSION_CACHE) && \
  8040. !defined(MEDIUM_SESSION_CACHE)
  8041. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8042. /* Sessions to restore/store */
  8043. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8044. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8045. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8046. static int twcase_new_session_called = 0;
  8047. static int twcase_remove_session_called = 0;
  8048. static int twcase_get_session_called = 0;
  8049. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8050. #define SESSION_CACHE_SIZE 33
  8051. typedef struct {
  8052. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8053. WOLFSSL_SESSION* value;
  8054. } hashTable_entry;
  8055. typedef struct {
  8056. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8057. size_t capacity; /* size of entries */
  8058. size_t length; /* number of items in the hash table */
  8059. wolfSSL_Mutex htLock; /* lock */
  8060. }hashTable;
  8061. static hashTable server_sessionCache;
  8062. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8063. {
  8064. int i;
  8065. unsigned int len;
  8066. (void)ssl;
  8067. /*
  8068. * This example uses a hash table.
  8069. * Steps you should take for a non-demo code:
  8070. * - acquire a lock for the file named according to the session id
  8071. * - open the file
  8072. * - encrypt and write the SSL_SESSION object to the file
  8073. * - release the lock
  8074. *
  8075. * Return:
  8076. * 0: The callback does not wish to hold a reference of the sess
  8077. * 1: The callback wants to hold a reference of the sess. The callback is
  8078. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8079. */
  8080. if (sess == NULL)
  8081. return 0;
  8082. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8083. return 0;
  8084. }
  8085. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8086. if (server_sessionCache.entries[i].value == NULL) {
  8087. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8088. server_sessionCache.entries[i].value = sess;
  8089. server_sessionCache.length++;
  8090. break;
  8091. }
  8092. }
  8093. ++twcase_new_session_called;
  8094. wc_UnLockMutex(&server_sessionCache.htLock);
  8095. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8096. twcase_new_session_called);
  8097. return 1;
  8098. }
  8099. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8100. {
  8101. int i;
  8102. (void)ctx;
  8103. (void)sess;
  8104. if (sess == NULL)
  8105. return;
  8106. /*
  8107. * This example uses a hash table.
  8108. * Steps you should take for a non-demo code:
  8109. * - acquire a lock for the file named according to the session id
  8110. * - remove the file
  8111. * - release the lock
  8112. */
  8113. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8114. return;
  8115. }
  8116. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8117. if (server_sessionCache.entries[i].key != NULL &&
  8118. XMEMCMP(server_sessionCache.entries[i].key,
  8119. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8120. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8121. server_sessionCache.entries[i].value = NULL;
  8122. server_sessionCache.entries[i].key = NULL;
  8123. server_sessionCache.length--;
  8124. break;
  8125. }
  8126. }
  8127. ++twcase_remove_session_called;
  8128. wc_UnLockMutex(&server_sessionCache.htLock);
  8129. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8130. twcase_remove_session_called);
  8131. }
  8132. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8133. const unsigned char *id, int len, int *ref)
  8134. {
  8135. int i;
  8136. (void)ssl;
  8137. (void)id;
  8138. (void)len;
  8139. /*
  8140. * This example uses a hash table.
  8141. * Steps you should take for a non-demo code:
  8142. * - acquire a lock for the file named according to the session id in the
  8143. * 2nd arg
  8144. * - read and decrypt contents of file and create a new SSL_SESSION
  8145. * - object release the lock
  8146. * - return the new session object
  8147. */
  8148. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8149. ++twcase_get_session_called);
  8150. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8151. * be responsible for the pointer then set to 0. */
  8152. *ref = 1;
  8153. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8154. if (server_sessionCache.entries[i].key != NULL &&
  8155. XMEMCMP(server_sessionCache.entries[i].key, id,
  8156. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8157. return server_sessionCache.entries[i].value;
  8158. }
  8159. }
  8160. return NULL;
  8161. }
  8162. static int twcase_get_sessionCb_cleanup(void)
  8163. {
  8164. int i;
  8165. int cnt = 0;
  8166. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8167. * for freeing sessions */
  8168. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8169. if (server_sessionCache.entries[i].value != NULL) {
  8170. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8171. cnt++;
  8172. }
  8173. }
  8174. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8175. cnt);
  8176. return TEST_SUCCESS;
  8177. }
  8178. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8179. {
  8180. EXPECT_DECLS;
  8181. /* off - Disable internal cache */
  8182. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8183. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8184. #ifdef OPENSSL_EXTRA
  8185. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8186. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8187. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8188. #endif
  8189. /* off - Do not setup external cache */
  8190. /* Require both peers to provide certs */
  8191. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8192. return EXPECT_RESULT();
  8193. }
  8194. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8195. {
  8196. /* on - internal cache is on by default */
  8197. /* off - Do not setup external cache */
  8198. /* Require both peers to provide certs */
  8199. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8200. return TEST_SUCCESS;
  8201. }
  8202. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8203. {
  8204. EXPECT_DECLS;
  8205. /* off - Disable internal cache */
  8206. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8207. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8208. #ifdef OPENSSL_EXTRA
  8209. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8210. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8211. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8212. #endif
  8213. /* on - Enable external cache */
  8214. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8215. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8216. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8217. /* Require both peers to provide certs */
  8218. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8219. return EXPECT_RESULT();
  8220. }
  8221. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8222. {
  8223. /* on - internal cache is on by default */
  8224. /* on - Enable external cache */
  8225. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8226. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8227. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8228. /* Require both peers to provide certs */
  8229. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8230. return TEST_SUCCESS;
  8231. }
  8232. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8233. {
  8234. /* on - internal cache is on by default */
  8235. /* on - Enable external cache */
  8236. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8237. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8238. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8239. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8240. /* Require both peers to provide certs */
  8241. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8242. return TEST_SUCCESS;
  8243. }
  8244. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8245. {
  8246. EXPECT_DECLS;
  8247. WOLFSSL_SESSION** sess;
  8248. if (wolfSSL_is_server(ssl))
  8249. sess = &twcase_server_first_session_ptr;
  8250. else
  8251. return TEST_SUCCESS;
  8252. if (*sess == NULL) {
  8253. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8254. /* Now save the session in the internal store to make it available
  8255. * for lookup. For TLS 1.3, we can't save the session without
  8256. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8257. * session from cache. */
  8258. if (wolfSSL_is_server(ssl)
  8259. #ifndef WOLFSSL_TICKET_HAVE_ID
  8260. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8261. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8262. #endif
  8263. ) {
  8264. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8265. *sess), WOLFSSL_SUCCESS);
  8266. }
  8267. }
  8268. /* Save CTX to be able to decrypt tickets */
  8269. if (twcase_server_current_ctx_ptr == NULL) {
  8270. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8271. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8272. WOLFSSL_SUCCESS);
  8273. }
  8274. #ifdef SESSION_CERTS
  8275. #ifndef WOLFSSL_TICKET_HAVE_ID
  8276. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8277. wolfSSL_session_reused(ssl))
  8278. #endif
  8279. {
  8280. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8281. * for all connections. TLS 1.3 only has tickets so if we don't
  8282. * include the session id in the ticket then the certificates
  8283. * will not be available on resumption. */
  8284. WOLFSSL_X509* peer = NULL;
  8285. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8286. wolfSSL_X509_free(peer);
  8287. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8288. }
  8289. #endif
  8290. return EXPECT_RESULT();
  8291. }
  8292. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8293. {
  8294. EXPECT_DECLS;
  8295. WOLFSSL_SESSION** sess;
  8296. sess = &twcase_client_first_session_ptr;
  8297. if (*sess == NULL) {
  8298. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8299. }
  8300. else {
  8301. /* If we have a session retrieved then remaining connections should be
  8302. * resuming on that session */
  8303. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8304. }
  8305. #ifdef SESSION_CERTS
  8306. #ifndef WOLFSSL_TICKET_HAVE_ID
  8307. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8308. wolfSSL_session_reused(ssl))
  8309. #endif
  8310. {
  8311. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8312. ExpectNotNull(peer);
  8313. wolfSSL_X509_free(peer);
  8314. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8315. #ifdef OPENSSL_EXTRA
  8316. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8317. #endif
  8318. }
  8319. #endif
  8320. return EXPECT_RESULT();
  8321. }
  8322. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8323. {
  8324. EXPECT_DECLS;
  8325. /* Set the session to reuse for the client */
  8326. ExpectNotNull(ssl);
  8327. ExpectNotNull(twcase_client_first_session_ptr);
  8328. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8329. WOLFSSL_SUCCESS);
  8330. return EXPECT_RESULT();
  8331. }
  8332. struct test_add_session_ext_params {
  8333. method_provider client_meth;
  8334. method_provider server_meth;
  8335. const char* tls_version;
  8336. };
  8337. static int test_wolfSSL_CTX_add_session_ext(
  8338. struct test_add_session_ext_params* param)
  8339. {
  8340. EXPECT_DECLS;
  8341. /* Test the default 33 sessions */
  8342. int j;
  8343. /* Clear cache before starting */
  8344. wolfSSL_CTX_flush_sessions(NULL, -1);
  8345. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8346. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8347. return BAD_MUTEX_E;
  8348. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8349. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8350. for (j = 0; j < 5; j++) {
  8351. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8352. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8353. test_ssl_cbf client_cb;
  8354. test_ssl_cbf server_cb;
  8355. (void)dtls;
  8356. /* Test five cache configurations */
  8357. twcase_client_first_session_ptr = NULL;
  8358. twcase_server_first_session_ptr = NULL;
  8359. twcase_server_current_ctx_ptr = NULL;
  8360. twcase_new_session_called = 0;
  8361. twcase_remove_session_called = 0;
  8362. twcase_get_session_called = 0;
  8363. /* connection 1 - first connection */
  8364. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8365. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8366. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8367. client_cb.method = param->client_meth;
  8368. server_cb.method = param->server_meth;
  8369. if (dtls)
  8370. client_cb.doUdp = server_cb.doUdp = 1;
  8371. /* Setup internal and external cache */
  8372. switch (j) {
  8373. case 0:
  8374. /* SSL_OP_NO_TICKET stateful ticket case */
  8375. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8376. break;
  8377. case 1:
  8378. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8379. break;
  8380. case 2:
  8381. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8382. break;
  8383. case 3:
  8384. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8385. break;
  8386. case 4:
  8387. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8388. break;
  8389. }
  8390. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8391. /* Add session to internal cache and save SSL session for testing */
  8392. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8393. /* Save client SSL session for testing */
  8394. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8395. server_cb.ticNoInit = 1; /* Use default builtin */
  8396. /* Don't free/release ctx */
  8397. server_cb.ctx = twcase_server_current_ctx_ptr;
  8398. server_cb.isSharedCtx = 1;
  8399. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8400. &server_cb, NULL), TEST_SUCCESS);
  8401. ExpectIntEQ(twcase_get_session_called, 0);
  8402. if (EXPECT_FAIL()) {
  8403. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8404. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8405. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8406. break;
  8407. }
  8408. switch (j) {
  8409. case 0:
  8410. case 1:
  8411. case 2:
  8412. /* cache cannot be searched with out a connection */
  8413. /* Add a new session */
  8414. ExpectIntEQ(twcase_new_session_called, 1);
  8415. /* In twcase_server_sess_ctx_pre_shutdown
  8416. * wolfSSL_CTX_add_session which evicts the existing session
  8417. * in cache and adds it back in */
  8418. ExpectIntLE(twcase_remove_session_called, 1);
  8419. break;
  8420. case 3:
  8421. case 4:
  8422. /* no external cache */
  8423. ExpectIntEQ(twcase_new_session_called, 0);
  8424. ExpectIntEQ(twcase_remove_session_called, 0);
  8425. break;
  8426. }
  8427. /* connection 2 - session resume */
  8428. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8429. twcase_new_session_called = 0;
  8430. twcase_remove_session_called = 0;
  8431. twcase_get_session_called = 0;
  8432. server_cb.on_result = 0;
  8433. client_cb.on_result = 0;
  8434. server_cb.ticNoInit = 1; /* Use default builtin */
  8435. server_cb.ctx = twcase_server_current_ctx_ptr;
  8436. /* try session resumption */
  8437. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8438. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8439. &server_cb, NULL), TEST_SUCCESS);
  8440. /* Clear cache before checking */
  8441. wolfSSL_CTX_flush_sessions(NULL, -1);
  8442. switch (j) {
  8443. case 0:
  8444. if (tls13) {
  8445. /* (D)TLSv1.3 stateful case */
  8446. /* cache hit */
  8447. /* DTLS accesses cache once for stateless parsing and
  8448. * once for stateful parsing */
  8449. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8450. /* (D)TLSv1.3 creates a new ticket,
  8451. * updates both internal and external cache */
  8452. ExpectIntEQ(twcase_new_session_called, 1);
  8453. /* A new session ID is created for a new ticket */
  8454. ExpectIntEQ(twcase_remove_session_called, 2);
  8455. }
  8456. else {
  8457. /* non (D)TLSv1.3 case, no update */
  8458. /* DTLS accesses cache once for stateless parsing and
  8459. * once for stateful parsing */
  8460. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8461. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8462. #else
  8463. ExpectIntEQ(twcase_get_session_called, 1);
  8464. #endif
  8465. ExpectIntEQ(twcase_new_session_called, 0);
  8466. /* Called on session added in
  8467. * twcase_server_sess_ctx_pre_shutdown */
  8468. ExpectIntEQ(twcase_remove_session_called, 1);
  8469. }
  8470. break;
  8471. case 1:
  8472. if (tls13) {
  8473. /* (D)TLSv1.3 case */
  8474. /* cache hit */
  8475. ExpectIntEQ(twcase_get_session_called, 1);
  8476. /* (D)TLSv1.3 creates a new ticket,
  8477. * updates both internal and external cache */
  8478. ExpectIntEQ(twcase_new_session_called, 1);
  8479. /* Called on session added in
  8480. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8481. ExpectIntEQ(twcase_remove_session_called, 1);
  8482. }
  8483. else {
  8484. /* non (D)TLSv1.3 case */
  8485. /* cache hit */
  8486. /* DTLS accesses cache once for stateless parsing and
  8487. * once for stateful parsing */
  8488. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8489. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8490. #else
  8491. ExpectIntEQ(twcase_get_session_called, 1);
  8492. #endif
  8493. ExpectIntEQ(twcase_new_session_called, 0);
  8494. /* Called on session added in
  8495. * twcase_server_sess_ctx_pre_shutdown */
  8496. ExpectIntEQ(twcase_remove_session_called, 1);
  8497. }
  8498. break;
  8499. case 2:
  8500. if (tls13) {
  8501. /* (D)TLSv1.3 case */
  8502. /* cache hit */
  8503. ExpectIntEQ(twcase_get_session_called, 1);
  8504. /* (D)TLSv1.3 creates a new ticket,
  8505. * updates both internal and external cache */
  8506. ExpectIntEQ(twcase_new_session_called, 1);
  8507. /* Called on session added in
  8508. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8509. ExpectIntEQ(twcase_remove_session_called, 1);
  8510. }
  8511. else {
  8512. /* non (D)TLSv1.3 case */
  8513. /* cache hit */
  8514. /* DTLS accesses cache once for stateless parsing and
  8515. * once for stateful parsing */
  8516. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8517. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8518. #else
  8519. ExpectIntEQ(twcase_get_session_called, 1);
  8520. #endif
  8521. ExpectIntEQ(twcase_new_session_called, 0);
  8522. /* Called on session added in
  8523. * twcase_server_sess_ctx_pre_shutdown */
  8524. ExpectIntEQ(twcase_remove_session_called, 1);
  8525. }
  8526. break;
  8527. case 3:
  8528. case 4:
  8529. /* no external cache */
  8530. ExpectIntEQ(twcase_get_session_called, 0);
  8531. ExpectIntEQ(twcase_new_session_called, 0);
  8532. ExpectIntEQ(twcase_remove_session_called, 0);
  8533. break;
  8534. }
  8535. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8536. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8537. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8538. if (EXPECT_FAIL())
  8539. break;
  8540. }
  8541. twcase_get_sessionCb_cleanup();
  8542. XMEMSET(&server_sessionCache.entries, 0,
  8543. sizeof(server_sessionCache.entries));
  8544. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8545. wc_FreeMutex(&server_sessionCache.htLock);
  8546. return EXPECT_RESULT();
  8547. }
  8548. #endif
  8549. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8550. {
  8551. EXPECT_DECLS;
  8552. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8553. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8554. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8555. defined(HAVE_SESSION_TICKET) && \
  8556. !defined(TITAN_SESSION_CACHE) && \
  8557. !defined(HUGE_SESSION_CACHE) && \
  8558. !defined(BIG_SESSION_CACHE) && \
  8559. !defined(MEDIUM_SESSION_CACHE)
  8560. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8561. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8562. struct test_add_session_ext_params param[1] = {
  8563. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8564. };
  8565. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8566. #endif
  8567. #endif
  8568. return EXPECT_RESULT();
  8569. }
  8570. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8571. {
  8572. EXPECT_DECLS;
  8573. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8574. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8575. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8576. defined(HAVE_SESSION_TICKET) && \
  8577. !defined(TITAN_SESSION_CACHE) && \
  8578. !defined(HUGE_SESSION_CACHE) && \
  8579. !defined(BIG_SESSION_CACHE) && \
  8580. !defined(MEDIUM_SESSION_CACHE)
  8581. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8582. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8583. #ifdef WOLFSSL_DTLS13
  8584. struct test_add_session_ext_params param[1] = {
  8585. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8586. };
  8587. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8588. #endif
  8589. #endif
  8590. #endif
  8591. return EXPECT_RESULT();
  8592. }
  8593. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8594. {
  8595. EXPECT_DECLS;
  8596. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8597. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8598. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8599. defined(HAVE_SESSION_TICKET) && \
  8600. !defined(TITAN_SESSION_CACHE) && \
  8601. !defined(HUGE_SESSION_CACHE) && \
  8602. !defined(BIG_SESSION_CACHE) && \
  8603. !defined(MEDIUM_SESSION_CACHE)
  8604. #ifndef WOLFSSL_NO_TLS12
  8605. struct test_add_session_ext_params param[1] = {
  8606. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8607. };
  8608. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8609. #endif
  8610. #endif
  8611. return EXPECT_RESULT();
  8612. }
  8613. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8614. {
  8615. EXPECT_DECLS;
  8616. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8617. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8618. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8619. defined(HAVE_SESSION_TICKET) && \
  8620. !defined(TITAN_SESSION_CACHE) && \
  8621. !defined(HUGE_SESSION_CACHE) && \
  8622. !defined(BIG_SESSION_CACHE) && \
  8623. !defined(MEDIUM_SESSION_CACHE)
  8624. #ifndef WOLFSSL_NO_TLS12
  8625. #ifdef WOLFSSL_DTLS
  8626. struct test_add_session_ext_params param[1] = {
  8627. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8628. };
  8629. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8630. #endif
  8631. #endif
  8632. #endif
  8633. return EXPECT_RESULT();
  8634. }
  8635. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8636. {
  8637. EXPECT_DECLS;
  8638. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8639. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8640. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8641. defined(HAVE_SESSION_TICKET) && \
  8642. !defined(TITAN_SESSION_CACHE) && \
  8643. !defined(HUGE_SESSION_CACHE) && \
  8644. !defined(BIG_SESSION_CACHE) && \
  8645. !defined(MEDIUM_SESSION_CACHE)
  8646. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8647. !defined(NO_DES3))
  8648. struct test_add_session_ext_params param[1] = {
  8649. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8650. };
  8651. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8652. #endif
  8653. #endif
  8654. return EXPECT_RESULT();
  8655. }
  8656. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8657. {
  8658. EXPECT_DECLS;
  8659. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8660. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8661. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8662. defined(HAVE_SESSION_TICKET) && \
  8663. !defined(TITAN_SESSION_CACHE) && \
  8664. !defined(HUGE_SESSION_CACHE) && \
  8665. !defined(BIG_SESSION_CACHE) && \
  8666. !defined(MEDIUM_SESSION_CACHE)
  8667. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8668. !defined(NO_DES3))
  8669. #ifdef WOLFSSL_DTLS
  8670. struct test_add_session_ext_params param[1] = {
  8671. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8672. };
  8673. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8674. #endif
  8675. #endif
  8676. #endif
  8677. return EXPECT_RESULT();
  8678. }
  8679. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8680. /* canned export of a session using older version 3 */
  8681. static unsigned char version_3[] = {
  8682. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8683. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8684. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8685. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8686. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8687. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8688. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8689. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8690. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8691. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8692. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8693. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8695. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8696. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8697. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8698. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8699. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8700. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8701. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8702. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8703. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8704. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8705. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8709. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8713. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8714. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8715. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8716. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8717. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8718. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8719. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8720. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8721. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8722. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8723. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8724. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8725. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8726. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8727. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8728. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8729. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8730. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8731. 0x2E, 0x31, 0xED, 0x4F
  8732. };
  8733. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8734. static int test_wolfSSL_dtls_export(void)
  8735. {
  8736. EXPECT_DECLS;
  8737. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8738. tcp_ready ready;
  8739. func_args client_args;
  8740. func_args server_args;
  8741. THREAD_TYPE serverThread;
  8742. callback_functions server_cbf;
  8743. callback_functions client_cbf;
  8744. #ifdef WOLFSSL_TIRTOS
  8745. fdOpenSession(Task_self());
  8746. #endif
  8747. InitTcpReady(&ready);
  8748. #if defined(USE_WINDOWS_API)
  8749. /* use RNG to get random port if using windows */
  8750. ready.port = GetRandomPort();
  8751. #endif
  8752. /* set using dtls */
  8753. XMEMSET(&client_args, 0, sizeof(func_args));
  8754. XMEMSET(&server_args, 0, sizeof(func_args));
  8755. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8756. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8757. server_cbf.method = wolfDTLSv1_2_server_method;
  8758. client_cbf.method = wolfDTLSv1_2_client_method;
  8759. server_args.callbacks = &server_cbf;
  8760. client_args.callbacks = &client_cbf;
  8761. server_args.signal = &ready;
  8762. client_args.signal = &ready;
  8763. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8764. wait_tcp_ready(&server_args);
  8765. run_wolfssl_client(&client_args);
  8766. join_thread(serverThread);
  8767. ExpectTrue(client_args.return_code);
  8768. ExpectTrue(server_args.return_code);
  8769. FreeTcpReady(&ready);
  8770. #ifdef WOLFSSL_TIRTOS
  8771. fdOpenSession(Task_self());
  8772. #endif
  8773. if (EXPECT_SUCCESS()) {
  8774. SOCKET_T sockfd = 0;
  8775. WOLFSSL_CTX* ctx = NULL;
  8776. WOLFSSL* ssl = NULL;
  8777. char msg[64] = "hello wolfssl!";
  8778. char reply[1024];
  8779. int msgSz = (int)XSTRLEN(msg);
  8780. byte *session, *window;
  8781. unsigned int sessionSz = 0;
  8782. unsigned int windowSz = 0;
  8783. #ifndef TEST_IPV6
  8784. struct sockaddr_in peerAddr;
  8785. #else
  8786. struct sockaddr_in6 peerAddr;
  8787. #endif /* TEST_IPV6 */
  8788. int i;
  8789. /* Set ctx to DTLS 1.2 */
  8790. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8791. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8792. /* test importing version 3 */
  8793. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8794. /* test importing bad length and bad version */
  8795. version_3[2] += 1;
  8796. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8797. version_3[2] -= 1; version_3[1] = 0XA0;
  8798. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8799. wolfSSL_free(ssl);
  8800. wolfSSL_CTX_free(ctx);
  8801. /* check storing client state after connection and storing window only */
  8802. #ifdef WOLFSSL_TIRTOS
  8803. fdOpenSession(Task_self());
  8804. #endif
  8805. InitTcpReady(&ready);
  8806. #if defined(USE_WINDOWS_API)
  8807. /* use RNG to get random port if using windows */
  8808. ready.port = GetRandomPort();
  8809. #endif
  8810. /* set using dtls */
  8811. XMEMSET(&server_args, 0, sizeof(func_args));
  8812. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8813. server_cbf.method = wolfDTLSv1_2_server_method;
  8814. server_cbf.doUdp = 1;
  8815. server_args.callbacks = &server_cbf;
  8816. server_args.argc = 3; /* set loop_count to 3 */
  8817. server_args.signal = &ready;
  8818. start_thread(test_server_nofail, &server_args, &serverThread);
  8819. wait_tcp_ready(&server_args);
  8820. /* create and connect with client */
  8821. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8822. ExpectIntEQ(WOLFSSL_SUCCESS,
  8823. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8824. ExpectIntEQ(WOLFSSL_SUCCESS,
  8825. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8826. ExpectIntEQ(WOLFSSL_SUCCESS,
  8827. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8828. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8829. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8830. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8831. /* store server information connected too */
  8832. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8833. #ifndef TEST_IPV6
  8834. peerAddr.sin_family = AF_INET;
  8835. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8836. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8837. #else
  8838. peerAddr.sin6_family = AF_INET6;
  8839. ExpectIntEQ(
  8840. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8841. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8842. #endif
  8843. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8844. WOLFSSL_SUCCESS);
  8845. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8846. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8847. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8848. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8849. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8850. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8851. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8852. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8853. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8854. wolfSSL_free(ssl);
  8855. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8856. /* restore state */
  8857. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8858. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8859. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8860. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8861. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8862. WOLFSSL_SUCCESS);
  8863. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8864. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8865. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8866. wolfSSL_free(ssl);
  8867. }
  8868. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8869. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8870. wolfSSL_CTX_free(ctx);
  8871. fprintf(stderr, "done and waiting for server\n");
  8872. join_thread(serverThread);
  8873. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8874. FreeTcpReady(&ready);
  8875. #ifdef WOLFSSL_TIRTOS
  8876. fdOpenSession(Task_self());
  8877. #endif
  8878. }
  8879. #endif
  8880. return EXPECT_RESULT();
  8881. }
  8882. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8883. #ifdef WOLFSSL_TLS13
  8884. static const byte canned_client_tls13_session[] = {
  8885. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8886. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8887. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8888. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8889. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8890. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8891. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8892. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8893. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8894. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8895. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8896. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8897. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8898. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8899. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8900. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8901. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8902. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8903. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8904. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8905. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8906. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8907. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8908. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8909. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8910. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8911. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8912. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8913. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8914. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8915. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8916. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8917. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8918. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8919. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8920. 0x00, 0x03
  8921. };
  8922. static const byte canned_server_tls13_session[] = {
  8923. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8924. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8925. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8926. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8928. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8929. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8930. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8931. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8932. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8933. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8934. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8935. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8936. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8937. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8938. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8939. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8940. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8941. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8942. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8943. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8944. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8945. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8946. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8947. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8948. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8949. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8950. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8951. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8952. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8953. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8954. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8955. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8956. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8957. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8958. 0x00, 0x04
  8959. };
  8960. #endif /* WOLFSSL_TLS13 */
  8961. static const byte canned_client_session[] = {
  8962. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8963. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8964. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8965. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8966. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8967. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8968. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8969. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8970. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8971. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8972. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8973. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8974. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8975. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8976. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8977. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8978. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8979. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8980. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8981. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8982. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8983. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8984. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8985. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8986. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8987. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8988. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8989. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8990. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8991. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8992. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8993. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8995. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8996. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8997. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8998. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8999. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9000. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9001. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9002. 0x00, 0x03
  9003. };
  9004. static const byte canned_server_session[] = {
  9005. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9006. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9007. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9008. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9009. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9010. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9011. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9012. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  9013. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9014. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9015. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9016. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9017. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9018. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9019. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9020. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9021. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9022. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9023. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9024. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9025. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9026. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9027. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9028. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9029. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9030. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9031. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9032. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9033. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9034. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9035. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9038. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9039. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9040. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9041. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9042. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9043. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9044. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9045. 0x00, 0x04
  9046. };
  9047. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9048. {
  9049. SOCKET_T sockfd = 0;
  9050. SOCKET_T clientfd = 0;
  9051. word16 port;
  9052. callback_functions* cbf;
  9053. WOLFSSL_CTX* ctx = 0;
  9054. WOLFSSL* ssl = 0;
  9055. char msg[] = "I hear you fa shizzle!";
  9056. char input[1024];
  9057. int idx;
  9058. #ifdef WOLFSSL_TIRTOS
  9059. fdOpenSession(Task_self());
  9060. #endif
  9061. ((func_args*)args)->return_code = TEST_FAIL;
  9062. cbf = ((func_args*)args)->callbacks;
  9063. #if defined(USE_WINDOWS_API)
  9064. port = ((func_args*)args)->signal->port;
  9065. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9066. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9067. /* Let tcp_listen assign port */
  9068. port = 0;
  9069. #else
  9070. /* Use default port */
  9071. port = wolfSSLPort;
  9072. #endif
  9073. /* do it here to detect failure */
  9074. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9075. CloseSocket(sockfd);
  9076. {
  9077. WOLFSSL_METHOD* method = NULL;
  9078. if (cbf != NULL && cbf->method != NULL) {
  9079. method = cbf->method();
  9080. }
  9081. else {
  9082. method = wolfTLSv1_2_server_method();
  9083. }
  9084. ctx = wolfSSL_CTX_new(method);
  9085. }
  9086. if (ctx == NULL) {
  9087. goto done;
  9088. }
  9089. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9090. /* call ctx setup callback */
  9091. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9092. cbf->ctx_ready(ctx);
  9093. }
  9094. ssl = wolfSSL_new(ctx);
  9095. if (ssl == NULL) {
  9096. goto done;
  9097. }
  9098. wolfSSL_set_fd(ssl, clientfd);
  9099. /* call ssl setup callback */
  9100. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9101. cbf->ssl_ready(ssl);
  9102. }
  9103. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9104. if (idx > 0) {
  9105. input[idx] = '\0';
  9106. fprintf(stderr, "Client message export/import: %s\n", input);
  9107. }
  9108. else {
  9109. fprintf(stderr, "ret = %d error = %d\n", idx,
  9110. wolfSSL_get_error(ssl, idx));
  9111. goto done;
  9112. }
  9113. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9114. /*err_sys("SSL_write failed");*/
  9115. WOLFSSL_RETURN_FROM_THREAD(0);
  9116. }
  9117. #ifdef WOLFSSL_TIRTOS
  9118. Task_yield();
  9119. #endif
  9120. ((func_args*)args)->return_code = TEST_SUCCESS;
  9121. done:
  9122. wolfSSL_shutdown(ssl);
  9123. wolfSSL_free(ssl);
  9124. wolfSSL_CTX_free(ctx);
  9125. CloseSocket(clientfd);
  9126. #ifdef WOLFSSL_TIRTOS
  9127. fdCloseSession(Task_self());
  9128. #endif
  9129. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9130. && defined(HAVE_THREAD_LS)
  9131. wc_ecc_fp_free(); /* free per thread cache */
  9132. #endif
  9133. #if defined(HAVE_SESSION_TICKET) && \
  9134. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9135. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9136. OpenSSLTicketCleanup();
  9137. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9138. TicketCleanup();
  9139. #endif
  9140. #endif
  9141. WOLFSSL_RETURN_FROM_THREAD(0);
  9142. }
  9143. static void load_tls12_canned_server(WOLFSSL* ssl)
  9144. {
  9145. int clientfd = wolfSSL_get_fd(ssl);
  9146. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9147. sizeof(canned_server_session)), sizeof(canned_server_session));
  9148. wolfSSL_set_fd(ssl, clientfd);
  9149. }
  9150. #ifdef WOLFSSL_TLS13
  9151. static void load_tls13_canned_server(WOLFSSL* ssl)
  9152. {
  9153. int clientfd = wolfSSL_get_fd(ssl);
  9154. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9155. sizeof(canned_server_tls13_session)),
  9156. sizeof(canned_server_tls13_session));
  9157. wolfSSL_set_fd(ssl, clientfd);
  9158. }
  9159. #endif
  9160. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9161. static int test_wolfSSL_tls_export_run(int v)
  9162. {
  9163. EXPECT_DECLS;
  9164. SOCKET_T sockfd = 0;
  9165. WOLFSSL_CTX* ctx = 0;
  9166. WOLFSSL* ssl = 0;
  9167. char msg[64] = "hello wolfssl!";
  9168. char reply[1024];
  9169. word32 replySz;
  9170. int msgSz = (int)XSTRLEN(msg);
  9171. const byte* clientSession = NULL;
  9172. int clientSessionSz = 0;
  9173. tcp_ready ready;
  9174. func_args server_args;
  9175. THREAD_TYPE serverThread;
  9176. callback_functions server_cbf;
  9177. #ifdef WOLFSSL_TIRTOS
  9178. fdOpenSession(Task_self());
  9179. #endif
  9180. InitTcpReady(&ready);
  9181. #if defined(USE_WINDOWS_API)
  9182. /* use RNG to get random port if using windows */
  9183. ready.port = GetRandomPort();
  9184. #endif
  9185. XMEMSET(&server_args, 0, sizeof(func_args));
  9186. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9187. switch (v) {
  9188. case WOLFSSL_TLSV1_2:
  9189. server_cbf.method = wolfTLSv1_2_server_method;
  9190. server_cbf.ssl_ready = load_tls12_canned_server;
  9191. /* setup the client side */
  9192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9193. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9194. clientSession = canned_client_session;
  9195. clientSessionSz = sizeof(canned_client_session);
  9196. break;
  9197. #ifdef WOLFSSL_TLS13
  9198. case WOLFSSL_TLSV1_3:
  9199. server_cbf.method = wolfTLSv1_3_server_method;
  9200. server_cbf.ssl_ready = load_tls13_canned_server;
  9201. /* setup the client side */
  9202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9203. clientSession = canned_client_tls13_session;
  9204. clientSessionSz = sizeof(canned_client_tls13_session);
  9205. break;
  9206. #endif
  9207. }
  9208. server_args.callbacks = &server_cbf;
  9209. server_args.signal = &ready;
  9210. start_thread(tls_export_server, &server_args, &serverThread);
  9211. wait_tcp_ready(&server_args);
  9212. #ifdef WOLFSSL_TIRTOS
  9213. fdOpenSession(Task_self());
  9214. #endif
  9215. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9216. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9217. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9218. clientSessionSz);
  9219. replySz = sizeof(reply);
  9220. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9221. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9222. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9223. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9224. #endif
  9225. wolfSSL_set_fd(ssl, sockfd);
  9226. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9227. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9228. wolfSSL_free(ssl);
  9229. wolfSSL_CTX_free(ctx);
  9230. CloseSocket(sockfd);
  9231. #ifdef WOLFSSL_TIRTOS
  9232. fdCloseSession(Task_self());
  9233. #endif
  9234. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9235. && defined(HAVE_THREAD_LS)
  9236. wc_ecc_fp_free(); /* free per thread cache */
  9237. #endif
  9238. join_thread(serverThread);
  9239. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9240. FreeTcpReady(&ready);
  9241. #ifdef WOLFSSL_TIRTOS
  9242. fdOpenSession(Task_self());
  9243. #endif
  9244. return EXPECT_RESULT();
  9245. }
  9246. #endif
  9247. static int test_wolfSSL_tls_export(void)
  9248. {
  9249. int res = TEST_SKIPPED;
  9250. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9251. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9252. #ifdef WOLFSSL_TLS13
  9253. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9254. #endif
  9255. res = TEST_RES_CHECK(1);
  9256. #endif
  9257. return res;
  9258. }
  9259. /*----------------------------------------------------------------------------*
  9260. | TLS extensions tests
  9261. *----------------------------------------------------------------------------*/
  9262. #ifdef ENABLE_TLS_CALLBACK_TEST
  9263. /* Connection test runner - generic */
  9264. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9265. callback_functions* server_callbacks)
  9266. {
  9267. tcp_ready ready;
  9268. func_args client_args;
  9269. func_args server_args;
  9270. THREAD_TYPE serverThread;
  9271. XMEMSET(&client_args, 0, sizeof(func_args));
  9272. XMEMSET(&server_args, 0, sizeof(func_args));
  9273. StartTCP();
  9274. client_args.callbacks = client_callbacks;
  9275. server_args.callbacks = server_callbacks;
  9276. #ifdef WOLFSSL_TIRTOS
  9277. fdOpenSession(Task_self());
  9278. #endif
  9279. /* RUN Server side */
  9280. InitTcpReady(&ready);
  9281. #if defined(USE_WINDOWS_API)
  9282. /* use RNG to get random port if using windows */
  9283. ready.port = GetRandomPort();
  9284. #endif
  9285. server_args.signal = &ready;
  9286. client_args.signal = &ready;
  9287. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9288. wait_tcp_ready(&server_args);
  9289. /* RUN Client side */
  9290. run_wolfssl_client(&client_args);
  9291. join_thread(serverThread);
  9292. FreeTcpReady(&ready);
  9293. #ifdef WOLFSSL_TIRTOS
  9294. fdCloseSession(Task_self());
  9295. #endif
  9296. client_callbacks->return_code = client_args.return_code;
  9297. server_callbacks->return_code = server_args.return_code;
  9298. }
  9299. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9300. #ifdef HAVE_SNI
  9301. static int test_wolfSSL_UseSNI_params(void)
  9302. {
  9303. EXPECT_DECLS;
  9304. #if !defined(NO_WOLFSSL_CLIENT)
  9305. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9306. WOLFSSL *ssl = wolfSSL_new(ctx);
  9307. ExpectNotNull(ctx);
  9308. ExpectNotNull(ssl);
  9309. /* invalid [ctx|ssl] */
  9310. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9311. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9312. /* invalid type */
  9313. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9315. /* invalid data */
  9316. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9317. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9318. /* success case */
  9319. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9320. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9321. wolfSSL_free(ssl);
  9322. wolfSSL_CTX_free(ctx);
  9323. #endif /* !NO_WOLFSSL_CLIENT */
  9324. return EXPECT_RESULT();
  9325. }
  9326. /* BEGIN of connection tests callbacks */
  9327. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9328. {
  9329. AssertIntEQ(WOLFSSL_SUCCESS,
  9330. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9331. }
  9332. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9333. {
  9334. AssertIntEQ(WOLFSSL_SUCCESS,
  9335. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9336. }
  9337. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9338. {
  9339. AssertIntEQ(WOLFSSL_SUCCESS,
  9340. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9341. }
  9342. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9343. {
  9344. use_SNI_at_ssl(ssl);
  9345. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9346. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9347. }
  9348. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9349. {
  9350. use_SNI_at_ssl(ssl);
  9351. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9352. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9353. }
  9354. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9355. {
  9356. use_SNI_at_ctx(ctx);
  9357. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9358. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9359. }
  9360. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9361. {
  9362. use_SNI_at_ssl(ssl);
  9363. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9364. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9365. }
  9366. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9367. {
  9368. use_SNI_at_ctx(ctx);
  9369. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9370. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9371. }
  9372. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9373. {
  9374. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9375. }
  9376. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9377. {
  9378. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9379. }
  9380. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9381. {
  9382. byte type = WOLFSSL_SNI_HOST_NAME;
  9383. void* request = (void*) &type; /* to be overwritten */
  9384. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9385. AssertNotNull(request);
  9386. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9387. AssertNull(request);
  9388. }
  9389. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9390. {
  9391. byte type = WOLFSSL_SNI_HOST_NAME;
  9392. void* request = NULL;
  9393. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9394. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9395. AssertNotNull(request);
  9396. AssertStrEQ("www.wolfssl.com", (char*)request);
  9397. }
  9398. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9399. {
  9400. byte type = WOLFSSL_SNI_HOST_NAME;
  9401. void* request = NULL;
  9402. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9403. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9404. AssertNotNull(request);
  9405. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9406. }
  9407. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9408. {
  9409. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9410. }
  9411. /* END of connection tests callbacks */
  9412. static int test_wolfSSL_UseSNI_connection(void)
  9413. {
  9414. int res = TEST_SKIPPED;
  9415. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9416. callback_functions client_cb;
  9417. callback_functions server_cb;
  9418. size_t i;
  9419. #ifdef WOLFSSL_STATIC_MEMORY
  9420. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9421. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9422. #endif
  9423. struct {
  9424. method_provider client_meth;
  9425. method_provider server_meth;
  9426. #ifdef WOLFSSL_STATIC_MEMORY
  9427. wolfSSL_method_func client_meth_ex;
  9428. wolfSSL_method_func server_meth_ex;
  9429. #endif
  9430. } methods[] = {
  9431. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9432. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9433. #ifdef WOLFSSL_STATIC_MEMORY
  9434. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9435. #endif
  9436. },
  9437. #endif
  9438. #ifndef WOLFSSL_NO_TLS12
  9439. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9440. #ifdef WOLFSSL_STATIC_MEMORY
  9441. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9442. #endif
  9443. },
  9444. #endif
  9445. #ifdef WOLFSSL_TLS13
  9446. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9447. #ifdef WOLFSSL_STATIC_MEMORY
  9448. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9449. #endif
  9450. },
  9451. #endif
  9452. };
  9453. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9454. for (i = 0; i < methodsSz; i++) {
  9455. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9456. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9457. client_cb.method = methods[i].client_meth;
  9458. server_cb.method = methods[i].server_meth;
  9459. client_cb.devId = testDevId;
  9460. server_cb.devId = testDevId;
  9461. #ifdef WOLFSSL_STATIC_MEMORY
  9462. client_cb.method_ex = methods[i].client_meth_ex;
  9463. server_cb.method_ex = methods[i].server_meth_ex;
  9464. client_cb.mem = cliMem;
  9465. client_cb.memSz = (word32)sizeof(cliMem);
  9466. server_cb.mem = svrMem;
  9467. server_cb.memSz = (word32)sizeof(svrMem);;
  9468. #endif
  9469. /* success case at ctx */
  9470. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9471. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9472. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9473. test_wolfSSL_client_server(&client_cb, &server_cb);
  9474. /* success case at ssl */
  9475. fprintf(stderr, "\tsuccess case at ssl\n");
  9476. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9477. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9478. test_wolfSSL_client_server(&client_cb, &server_cb);
  9479. /* default mismatch behavior */
  9480. fprintf(stderr, "\tdefault mismatch behavior\n");
  9481. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9482. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9483. test_wolfSSL_client_server(&client_cb, &server_cb);
  9484. /* continue on mismatch */
  9485. fprintf(stderr, "\tcontinue on mismatch\n");
  9486. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9487. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9488. test_wolfSSL_client_server(&client_cb, &server_cb);
  9489. /* fake answer on mismatch */
  9490. fprintf(stderr, "\tfake answer on mismatch\n");
  9491. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9492. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9493. test_wolfSSL_client_server(&client_cb, &server_cb);
  9494. /* sni abort - success */
  9495. fprintf(stderr, "\tsni abort - success\n");
  9496. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9497. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9498. test_wolfSSL_client_server(&client_cb, &server_cb);
  9499. /* sni abort - abort when absent (ctx) */
  9500. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9501. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9502. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9503. test_wolfSSL_client_server(&client_cb, &server_cb);
  9504. /* sni abort - abort when absent (ssl) */
  9505. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9506. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9507. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9508. test_wolfSSL_client_server(&client_cb, &server_cb);
  9509. /* sni abort - success when overwritten */
  9510. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9511. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9512. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9513. test_wolfSSL_client_server(&client_cb, &server_cb);
  9514. /* sni abort - success when allowing mismatches */
  9515. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9516. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9517. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9518. test_wolfSSL_client_server(&client_cb, &server_cb);
  9519. }
  9520. res = TEST_RES_CHECK(1);
  9521. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9522. return res;
  9523. }
  9524. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9525. {
  9526. EXPECT_DECLS;
  9527. byte buff[] = { /* www.paypal.com */
  9528. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9529. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9530. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9531. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9532. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9533. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9534. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9535. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9536. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9537. };
  9538. byte buff2[] = { /* api.textmate.org */
  9539. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9540. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9541. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9542. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9543. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9544. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9545. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9546. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9547. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9548. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9549. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9550. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9551. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9552. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9553. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9554. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9555. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9556. };
  9557. byte buff3[] = { /* no sni extension */
  9558. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9559. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9560. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9561. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9562. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9563. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9564. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9565. };
  9566. byte buff4[] = { /* last extension has zero size */
  9567. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9568. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9569. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9570. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9571. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9572. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9573. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9574. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9575. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9576. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9577. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9578. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9579. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9580. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9581. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9582. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9583. 0x12, 0x00, 0x00
  9584. };
  9585. byte buff5[] = { /* SSL v2.0 client hello */
  9586. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9587. /* dummy bytes below, just to pass size check */
  9588. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9589. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9590. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9591. };
  9592. byte result[32] = {0};
  9593. word32 length = 32;
  9594. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9595. 0, result, &length));
  9596. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9597. 0, result, &length));
  9598. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9599. 1, result, &length));
  9600. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9601. 0, result, &length));
  9602. buff[0] = 0x16;
  9603. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9604. 0, result, &length));
  9605. buff[1] = 0x03;
  9606. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9607. sizeof(buff), 0, result, &length));
  9608. buff[2] = 0x03;
  9609. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9610. sizeof(buff), 0, result, &length));
  9611. buff[4] = 0x64;
  9612. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9613. 0, result, &length));
  9614. if (EXPECT_SUCCESS())
  9615. result[length] = 0;
  9616. ExpectStrEQ("www.paypal.com", (const char*) result);
  9617. length = 32;
  9618. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9619. 0, result, &length));
  9620. if (EXPECT_SUCCESS())
  9621. result[length] = 0;
  9622. ExpectStrEQ("api.textmate.org", (const char*) result);
  9623. /* SSL v2.0 tests */
  9624. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9625. sizeof(buff5), 0, result, &length));
  9626. buff5[2] = 0x02;
  9627. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9628. sizeof(buff5), 0, result, &length));
  9629. buff5[2] = 0x01; buff5[6] = 0x08;
  9630. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9631. sizeof(buff5), 0, result, &length));
  9632. buff5[6] = 0x09; buff5[8] = 0x01;
  9633. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9634. sizeof(buff5), 0, result, &length));
  9635. return EXPECT_RESULT();
  9636. }
  9637. #endif /* HAVE_SNI */
  9638. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9639. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9640. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9641. /* Dummy peer functions to satisfy the exporter/importer */
  9642. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9643. int* ipSz, unsigned short* port, int* fam)
  9644. {
  9645. (void)ssl;
  9646. ip[0] = -1;
  9647. *ipSz = 1;
  9648. *port = 1;
  9649. *fam = 2;
  9650. return 1;
  9651. }
  9652. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9653. int ipSz, unsigned short port, int fam)
  9654. {
  9655. (void)ssl;
  9656. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9657. return 0;
  9658. return 1;
  9659. }
  9660. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9661. WOLFSSL **ssl)
  9662. {
  9663. EXPECT_DECLS;
  9664. unsigned char* sessionBuf = NULL;
  9665. unsigned int sessionSz = 0;
  9666. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9667. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9668. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9669. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9670. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9671. ExpectNotNull(sessionBuf =
  9672. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9673. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9674. wolfSSL_free(*ssl);
  9675. *ssl = NULL;
  9676. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9677. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9678. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9679. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9680. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9681. return EXPECT_RESULT();
  9682. }
  9683. #endif
  9684. static int test_wolfSSL_dtls_export_peers(void)
  9685. {
  9686. EXPECT_DECLS;
  9687. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9688. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9689. test_ssl_cbf client_cbf;
  9690. test_ssl_cbf server_cbf;
  9691. size_t i, j;
  9692. struct test_params {
  9693. method_provider client_meth;
  9694. method_provider server_meth;
  9695. const char* dtls_version;
  9696. } params[] = {
  9697. #ifndef NO_OLD_TLS
  9698. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9699. #endif
  9700. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9701. /* TODO DTLS 1.3 exporting not supported
  9702. #ifdef WOLFSSL_DTLS13
  9703. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9704. #endif
  9705. */
  9706. };
  9707. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9708. for (j = 0; j <= 0b11; j++) {
  9709. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9710. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9711. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9712. client_cbf.method = params[i].client_meth;
  9713. server_cbf.method = params[i].server_meth;
  9714. if (j & 0b01) {
  9715. client_cbf.on_handshake =
  9716. test_wolfSSL_dtls_export_peers_on_handshake;
  9717. printf(" With client export;");
  9718. }
  9719. if (j & 0b10) {
  9720. server_cbf.on_handshake =
  9721. test_wolfSSL_dtls_export_peers_on_handshake;
  9722. printf(" With server export;");
  9723. }
  9724. printf("\n");
  9725. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9726. &server_cbf, NULL), TEST_SUCCESS);
  9727. if (!EXPECT_SUCCESS())
  9728. break;
  9729. }
  9730. }
  9731. #endif
  9732. return EXPECT_RESULT();
  9733. }
  9734. static int test_wolfSSL_UseTrustedCA(void)
  9735. {
  9736. EXPECT_DECLS;
  9737. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9738. && !defined(NO_RSA)
  9739. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9740. WOLFSSL_CTX *ctx = NULL;
  9741. WOLFSSL *ssl = NULL;
  9742. byte id[20];
  9743. #ifndef NO_WOLFSSL_SERVER
  9744. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9745. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9746. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9747. #else
  9748. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9749. #endif
  9750. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9751. XMEMSET(id, 0, sizeof(id));
  9752. /* error cases */
  9753. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9754. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9755. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9756. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9757. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9758. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9759. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9760. #ifdef NO_SHA
  9761. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9762. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9763. #endif
  9764. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9765. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9766. /* success cases */
  9767. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9768. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9769. #ifndef NO_SHA
  9770. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9771. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9772. #endif
  9773. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9774. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9775. wolfSSL_free(ssl);
  9776. wolfSSL_CTX_free(ctx);
  9777. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9778. #endif /* HAVE_TRUSTED_CA */
  9779. return EXPECT_RESULT();
  9780. }
  9781. static int test_wolfSSL_UseMaxFragment(void)
  9782. {
  9783. EXPECT_DECLS;
  9784. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9785. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9786. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9787. #ifndef NO_WOLFSSL_SERVER
  9788. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9789. #else
  9790. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9791. #endif
  9792. WOLFSSL *ssl = NULL;
  9793. #ifdef OPENSSL_EXTRA
  9794. int (*UseMaxFragment)(SSL *s, unsigned char mode);
  9795. int (*CTX_UseMaxFragment)(SSL_CTX *c, unsigned char mode);
  9796. #else
  9797. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9798. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9799. #endif
  9800. #ifndef NO_WOLFSSL_SERVER
  9801. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9802. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9803. #endif
  9804. ExpectNotNull(ctx);
  9805. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9806. #ifdef OPENSSL_EXTRA
  9807. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9808. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9809. #else
  9810. UseMaxFragment = wolfSSL_UseMaxFragment;
  9811. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9812. #endif
  9813. /* error cases */
  9814. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9815. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9816. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9817. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9818. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9819. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9820. /* success case */
  9821. #ifdef OPENSSL_EXTRA
  9822. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9823. #else
  9824. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9825. #endif
  9826. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9827. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9828. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9829. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9830. #ifdef OPENSSL_EXTRA
  9831. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9832. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9833. #else
  9834. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9835. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9836. #endif
  9837. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9838. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9839. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9840. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9841. #ifdef OPENSSL_EXTRA
  9842. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9843. #else
  9844. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9845. #endif
  9846. wolfSSL_free(ssl);
  9847. wolfSSL_CTX_free(ctx);
  9848. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9849. #endif
  9850. return EXPECT_RESULT();
  9851. }
  9852. static int test_wolfSSL_UseTruncatedHMAC(void)
  9853. {
  9854. EXPECT_DECLS;
  9855. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9856. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9857. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9858. #ifndef NO_WOLFSSL_SERVER
  9859. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9860. #else
  9861. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9862. #endif
  9863. WOLFSSL *ssl = NULL;
  9864. ExpectNotNull(ctx);
  9865. #ifndef NO_WOLFSSL_SERVER
  9866. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9867. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9868. #endif
  9869. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9870. /* error cases */
  9871. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9872. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9873. /* success case */
  9874. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9875. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9876. wolfSSL_free(ssl);
  9877. wolfSSL_CTX_free(ctx);
  9878. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9879. #endif
  9880. return EXPECT_RESULT();
  9881. }
  9882. static int test_wolfSSL_UseSupportedCurve(void)
  9883. {
  9884. EXPECT_DECLS;
  9885. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9886. !defined(NO_TLS)
  9887. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9888. WOLFSSL *ssl = wolfSSL_new(ctx);
  9889. ExpectNotNull(ctx);
  9890. ExpectNotNull(ssl);
  9891. /* error cases */
  9892. ExpectIntNE(WOLFSSL_SUCCESS,
  9893. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9894. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9895. ExpectIntNE(WOLFSSL_SUCCESS,
  9896. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9897. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9898. /* success case */
  9899. ExpectIntEQ(WOLFSSL_SUCCESS,
  9900. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9901. ExpectIntEQ(WOLFSSL_SUCCESS,
  9902. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9903. wolfSSL_free(ssl);
  9904. wolfSSL_CTX_free(ctx);
  9905. #endif
  9906. return EXPECT_RESULT();
  9907. }
  9908. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9909. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9910. {
  9911. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9912. }
  9913. static void use_ALPN_all(WOLFSSL* ssl)
  9914. {
  9915. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9916. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9917. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9918. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9919. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9920. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9921. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9922. }
  9923. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9924. {
  9925. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9926. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9927. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9928. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9929. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9930. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9931. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9932. }
  9933. static void use_ALPN_one(WOLFSSL* ssl)
  9934. {
  9935. /* spdy/2 */
  9936. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9937. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9938. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9939. }
  9940. static void use_ALPN_unknown(WOLFSSL* ssl)
  9941. {
  9942. /* http/2.0 */
  9943. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9944. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9945. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9946. }
  9947. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9948. {
  9949. /* http/2.0 */
  9950. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9951. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9952. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9953. }
  9954. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9955. {
  9956. /* spdy/3 */
  9957. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9958. char *proto = NULL;
  9959. word16 protoSz = 0;
  9960. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9961. /* check value */
  9962. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9963. if (proto) {
  9964. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9965. }
  9966. }
  9967. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9968. {
  9969. char *proto = NULL;
  9970. word16 protoSz = 0;
  9971. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9972. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9973. /* check value */
  9974. AssertIntEQ(1, (0 == protoSz));
  9975. AssertIntEQ(1, (NULL == proto));
  9976. }
  9977. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9978. {
  9979. /* http/1.1 */
  9980. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9981. char *proto;
  9982. word16 protoSz = 0;
  9983. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9984. /* check value */
  9985. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9986. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9987. }
  9988. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9989. {
  9990. /* spdy/2 */
  9991. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9992. char *proto;
  9993. word16 protoSz = 0;
  9994. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9995. /* check value */
  9996. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9997. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9998. }
  9999. static void verify_ALPN_client_list(WOLFSSL* ssl)
  10000. {
  10001. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10002. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10003. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10004. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10005. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10006. char *clist = NULL;
  10007. word16 clistSz = 0;
  10008. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  10009. &clistSz));
  10010. /* check value */
  10011. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  10012. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  10013. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  10014. }
  10015. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10016. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10017. /* ALPN select callback, success with spdy/2 */
  10018. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10019. unsigned char *outlen, const unsigned char *in,
  10020. unsigned int inlen, void *arg)
  10021. {
  10022. /* spdy/2 */
  10023. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10024. (void)ssl;
  10025. (void)arg;
  10026. /* adding +1 since LEN byte comes first */
  10027. if (inlen < sizeof(proto) + 1) {
  10028. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10029. }
  10030. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10031. *out = in + 1;
  10032. *outlen = (unsigned char)sizeof(proto);
  10033. return SSL_TLSEXT_ERR_OK;
  10034. }
  10035. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10036. }
  10037. /* ALPN select callback, force failure */
  10038. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10039. unsigned char *outlen, const unsigned char *in,
  10040. unsigned int inlen, void *arg)
  10041. {
  10042. (void)ssl;
  10043. (void)out;
  10044. (void)outlen;
  10045. (void)in;
  10046. (void)inlen;
  10047. (void)arg;
  10048. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10049. }
  10050. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10051. {
  10052. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10053. }
  10054. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10055. {
  10056. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10057. }
  10058. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10059. static int test_wolfSSL_UseALPN_connection(void)
  10060. {
  10061. int res = TEST_SKIPPED;
  10062. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10063. callback_functions client_cb;
  10064. callback_functions server_cb;
  10065. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10066. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10067. client_cb.method = wolfSSLv23_client_method;
  10068. server_cb.method = wolfSSLv23_server_method;
  10069. client_cb.devId = testDevId;
  10070. server_cb.devId = testDevId;
  10071. /* success case same list */
  10072. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10073. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10074. test_wolfSSL_client_server(&client_cb, &server_cb);
  10075. /* success case only one for server */
  10076. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10077. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10078. test_wolfSSL_client_server(&client_cb, &server_cb);
  10079. /* success case only one for client */
  10080. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10081. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10082. test_wolfSSL_client_server(&client_cb, &server_cb);
  10083. /* success case none for client */
  10084. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10085. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10086. test_wolfSSL_client_server(&client_cb, &server_cb);
  10087. /* success case mismatch behavior but option 'continue' set */
  10088. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10089. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10090. test_wolfSSL_client_server(&client_cb, &server_cb);
  10091. /* success case read protocol send by client */
  10092. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10093. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10094. test_wolfSSL_client_server(&client_cb, &server_cb);
  10095. /* mismatch behavior with same list
  10096. * the first and only this one must be taken */
  10097. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10098. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10099. test_wolfSSL_client_server(&client_cb, &server_cb);
  10100. /* default mismatch behavior */
  10101. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10102. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10103. test_wolfSSL_client_server(&client_cb, &server_cb);
  10104. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10105. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10106. /* WOLFSSL-level ALPN select callback tests */
  10107. /* Callback: success (one protocol, spdy/2) */
  10108. client_cb.ctx_ready = NULL;
  10109. client_cb.ssl_ready = use_ALPN_one;
  10110. client_cb.on_result = verify_ALPN_matching_spdy2;
  10111. server_cb.ctx_ready = NULL;
  10112. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10113. server_cb.on_result = verify_ALPN_matching_spdy2;
  10114. test_wolfSSL_client_server(&client_cb, &server_cb);
  10115. /* Callback: failure (one client protocol, spdy/2) */
  10116. client_cb.ctx_ready = NULL;
  10117. client_cb.ssl_ready = use_ALPN_one;
  10118. client_cb.on_result = NULL;
  10119. server_cb.ctx_ready = NULL;
  10120. server_cb.ssl_ready = use_ALPN_failure_callback;
  10121. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10122. test_wolfSSL_client_server(&client_cb, &server_cb);
  10123. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10124. res = TEST_RES_CHECK(1);
  10125. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10126. return res;
  10127. }
  10128. static int test_wolfSSL_UseALPN_params(void)
  10129. {
  10130. EXPECT_DECLS;
  10131. #ifndef NO_WOLFSSL_CLIENT
  10132. /* "http/1.1" */
  10133. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10134. /* "spdy/1" */
  10135. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10136. /* "spdy/2" */
  10137. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10138. /* "spdy/3" */
  10139. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10140. char buff[256];
  10141. word32 idx;
  10142. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10143. WOLFSSL *ssl = wolfSSL_new(ctx);
  10144. ExpectNotNull(ctx);
  10145. ExpectNotNull(ssl);
  10146. /* error cases */
  10147. ExpectIntNE(WOLFSSL_SUCCESS,
  10148. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10149. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10150. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10151. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10152. /* success case */
  10153. /* http1 only */
  10154. ExpectIntEQ(WOLFSSL_SUCCESS,
  10155. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10156. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10157. /* http1, spdy1 */
  10158. XMEMCPY(buff, http1, sizeof(http1));
  10159. idx = sizeof(http1);
  10160. buff[idx++] = ',';
  10161. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10162. idx += sizeof(spdy1);
  10163. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10164. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10165. /* http1, spdy2, spdy1 */
  10166. XMEMCPY(buff, http1, sizeof(http1));
  10167. idx = sizeof(http1);
  10168. buff[idx++] = ',';
  10169. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10170. idx += sizeof(spdy2);
  10171. buff[idx++] = ',';
  10172. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10173. idx += sizeof(spdy1);
  10174. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10175. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10176. /* spdy3, http1, spdy2, spdy1 */
  10177. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10178. idx = sizeof(spdy3);
  10179. buff[idx++] = ',';
  10180. XMEMCPY(buff+idx, http1, sizeof(http1));
  10181. idx += sizeof(http1);
  10182. buff[idx++] = ',';
  10183. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10184. idx += sizeof(spdy2);
  10185. buff[idx++] = ',';
  10186. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10187. idx += sizeof(spdy1);
  10188. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10189. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10190. wolfSSL_free(ssl);
  10191. wolfSSL_CTX_free(ctx);
  10192. #endif
  10193. return EXPECT_RESULT();
  10194. }
  10195. #endif /* HAVE_ALPN */
  10196. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10197. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10198. {
  10199. unsigned char p[] = {
  10200. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10201. 6, 's', 'p', 'd', 'y', '/', '2',
  10202. 6, 's', 'p', 'd', 'y', '/', '1',
  10203. };
  10204. unsigned char p_len = sizeof(p);
  10205. int ret;
  10206. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10207. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10208. AssertIntEQ(ret, 0);
  10209. #else
  10210. AssertIntEQ(ret, SSL_SUCCESS);
  10211. #endif
  10212. }
  10213. static void set_alpn_protos(SSL* ssl)
  10214. {
  10215. unsigned char p[] = {
  10216. 6, 's', 'p', 'd', 'y', '/', '3',
  10217. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10218. 6, 's', 'p', 'd', 'y', '/', '2',
  10219. 6, 's', 'p', 'd', 'y', '/', '1',
  10220. };
  10221. unsigned char p_len = sizeof(p);
  10222. int ret;
  10223. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10224. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10225. AssertIntEQ(ret, 0);
  10226. #else
  10227. AssertIntEQ(ret, SSL_SUCCESS);
  10228. #endif
  10229. }
  10230. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10231. {
  10232. /* "spdy/3" */
  10233. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10234. const unsigned char *proto;
  10235. unsigned int protoSz = 0;
  10236. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10237. /* check value */
  10238. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10239. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10240. }
  10241. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10242. {
  10243. /* "http/1.1" */
  10244. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10245. const unsigned char *proto;
  10246. unsigned int protoSz = 0;
  10247. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10248. /* check value */
  10249. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10250. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10251. }
  10252. static int test_wolfSSL_set_alpn_protos(void)
  10253. {
  10254. int res = TEST_SKIPPED;
  10255. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10256. callback_functions client_cb;
  10257. callback_functions server_cb;
  10258. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10259. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10260. client_cb.method = wolfSSLv23_client_method;
  10261. server_cb.method = wolfSSLv23_server_method;
  10262. client_cb.devId = testDevId;
  10263. server_cb.devId = testDevId;
  10264. /* use CTX_alpn_protos */
  10265. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10266. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10267. test_wolfSSL_client_server(&client_cb, &server_cb);
  10268. /* use set_alpn_protos */
  10269. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10270. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10271. test_wolfSSL_client_server(&client_cb, &server_cb);
  10272. res = TEST_SUCCESS;
  10273. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10274. return res;
  10275. }
  10276. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10277. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10278. {
  10279. EXPECT_DECLS;
  10280. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10281. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10282. WOLFSSL *ssl = wolfSSL_new(ctx);
  10283. ExpectNotNull(ctx);
  10284. ExpectNotNull(ssl);
  10285. /* error cases */
  10286. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10287. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10288. /* success cases */
  10289. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10290. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10291. wolfSSL_free(ssl);
  10292. wolfSSL_CTX_free(ctx);
  10293. #endif
  10294. return EXPECT_RESULT();
  10295. }
  10296. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10297. {
  10298. EXPECT_DECLS;
  10299. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10300. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10301. WOLFSSL *ssl = wolfSSL_new(ctx);
  10302. ExpectNotNull(ctx);
  10303. ExpectNotNull(ssl);
  10304. /* error cases */
  10305. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10306. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10307. /* success cases */
  10308. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10309. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10310. wolfSSL_free(ssl);
  10311. wolfSSL_CTX_free(ctx);
  10312. #endif
  10313. return EXPECT_RESULT();
  10314. }
  10315. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10316. static int test_wolfSSL_SCR_Reconnect(void)
  10317. {
  10318. EXPECT_DECLS;
  10319. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10320. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10321. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10322. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10323. struct test_memio_ctx test_ctx;
  10324. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10325. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10326. byte data;
  10327. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10328. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10329. test_ctx.s_ciphers =
  10330. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10331. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10332. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10333. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10334. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10335. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10336. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10337. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10338. /* WOLFSSL_FATAL_ERROR since it will block */
  10339. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10340. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10341. WOLFSSL_ERROR_WANT_READ);
  10342. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10343. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10344. WOLFSSL_ERROR_WANT_READ);
  10345. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10346. wolfSSL_free(ssl_c);
  10347. ssl_c = NULL;
  10348. wolfSSL_free(ssl_s);
  10349. ssl_s = NULL;
  10350. wolfSSL_CTX_free(ctx_c);
  10351. ctx_c = NULL;
  10352. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10353. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10354. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10355. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10356. wolfSSL_free(ssl_s);
  10357. wolfSSL_free(ssl_c);
  10358. wolfSSL_CTX_free(ctx_s);
  10359. wolfSSL_CTX_free(ctx_c);
  10360. #endif
  10361. return EXPECT_RESULT();
  10362. }
  10363. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10364. (!defined(NO_RSA) || defined(HAVE_ECC))
  10365. /* Called when writing. */
  10366. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10367. {
  10368. (void)ssl;
  10369. (void)buf;
  10370. (void)sz;
  10371. (void)ctx;
  10372. /* Force error return from wolfSSL_accept_TLSv13(). */
  10373. return WANT_WRITE;
  10374. }
  10375. /* Called when reading. */
  10376. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10377. {
  10378. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10379. int len = (int)msg->length;
  10380. (void)ssl;
  10381. (void)sz;
  10382. /* Pass back as much of message as will fit in buffer. */
  10383. if (len > sz)
  10384. len = sz;
  10385. XMEMCPY(buf, msg->buffer, len);
  10386. /* Move over returned data. */
  10387. msg->buffer += len;
  10388. msg->length -= len;
  10389. /* Amount actually copied. */
  10390. return len;
  10391. }
  10392. #endif
  10393. /* Test the detection of duplicate known TLS extensions.
  10394. * Specifically in a ClientHello.
  10395. */
  10396. static int test_tls_ext_duplicate(void)
  10397. {
  10398. EXPECT_DECLS;
  10399. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10400. !defined(NO_FILESYSTEM)
  10401. const unsigned char clientHelloDupTlsExt[] = {
  10402. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10403. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10404. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10405. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10406. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10407. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10408. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10409. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10412. 0x00, 0x9e, 0x01, 0x00,
  10413. /* Extensions - duplicate signature algorithms. */
  10414. 0x00, 0x19, 0x00, 0x0d,
  10415. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10416. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10417. /* Supported Versions extension for TLS 1.3. */
  10418. 0x00, 0x2b,
  10419. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10420. };
  10421. WOLFSSL_BUFFER_INFO msg;
  10422. const char* testCertFile;
  10423. const char* testKeyFile;
  10424. WOLFSSL_CTX *ctx = NULL;
  10425. WOLFSSL *ssl = NULL;
  10426. #ifndef NO_RSA
  10427. testCertFile = svrCertFile;
  10428. testKeyFile = svrKeyFile;
  10429. #elif defined(HAVE_ECC)
  10430. testCertFile = eccCertFile;
  10431. testKeyFile = eccKeyFile;
  10432. #endif
  10433. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10434. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10435. WOLFSSL_FILETYPE_PEM));
  10436. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10437. WOLFSSL_FILETYPE_PEM));
  10438. /* Read from 'msg'. */
  10439. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10440. /* No where to send to - dummy sender. */
  10441. wolfSSL_SetIOSend(ctx, DummySend);
  10442. ssl = wolfSSL_new(ctx);
  10443. ExpectNotNull(ssl);
  10444. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10445. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10446. wolfSSL_SetIOReadCtx(ssl, &msg);
  10447. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10448. /* can return duplicate ext error or socket error if the peer closed down
  10449. * while sending alert */
  10450. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10451. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10452. }
  10453. wolfSSL_free(ssl);
  10454. wolfSSL_CTX_free(ctx);
  10455. #endif
  10456. return EXPECT_RESULT();
  10457. }
  10458. /*----------------------------------------------------------------------------*
  10459. | X509 Tests
  10460. *----------------------------------------------------------------------------*/
  10461. static int test_wolfSSL_X509_NAME_get_entry(void)
  10462. {
  10463. EXPECT_DECLS;
  10464. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10465. #if defined(OPENSSL_ALL) || \
  10466. (defined(OPENSSL_EXTRA) && \
  10467. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10468. /* use openssl like name to test mapping */
  10469. X509_NAME_ENTRY* ne = NULL;
  10470. X509_NAME* name = NULL;
  10471. X509* x509 = NULL;
  10472. #ifndef NO_FILESYSTEM
  10473. ASN1_STRING* asn = NULL;
  10474. char* subCN = NULL;
  10475. #endif
  10476. int idx = 0;
  10477. ASN1_OBJECT *object = NULL;
  10478. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10479. defined(WOLFSSL_NGINX)
  10480. #ifndef NO_BIO
  10481. BIO* bio = NULL;
  10482. #endif
  10483. #endif
  10484. #ifndef NO_FILESYSTEM
  10485. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10486. WOLFSSL_FILETYPE_PEM));
  10487. ExpectNotNull(name = X509_get_subject_name(x509));
  10488. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10489. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10490. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10491. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10492. wolfSSL_FreeX509(x509);
  10493. x509 = NULL;
  10494. #endif
  10495. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10496. WOLFSSL_FILETYPE_PEM));
  10497. ExpectNotNull(name = X509_get_subject_name(x509));
  10498. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10499. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10500. defined(WOLFSSL_NGINX)
  10501. #ifndef NO_BIO
  10502. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10503. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10504. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10505. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10506. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10507. BIO_free(bio);
  10508. #endif
  10509. #endif
  10510. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10511. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10512. wolfSSL_FreeX509(x509);
  10513. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10514. #endif /* !NO_CERTS && !NO_RSA */
  10515. return EXPECT_RESULT();
  10516. }
  10517. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10518. static int test_wolfSSL_PKCS12(void)
  10519. {
  10520. EXPECT_DECLS;
  10521. /* .p12 file is encrypted with DES3 */
  10522. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10523. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10524. * Password Key
  10525. */
  10526. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10527. !defined(NO_STDIO_FILESYSTEM) && \
  10528. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10529. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10530. byte buf[6000];
  10531. char file[] = "./certs/test-servercert.p12";
  10532. char order[] = "./certs/ecc-rsa-server.p12";
  10533. #ifdef WC_RC2
  10534. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10535. #endif
  10536. char pass[] = "a password";
  10537. const char goodPsw[] = "wolfSSL test";
  10538. const char badPsw[] = "bad";
  10539. #ifdef HAVE_ECC
  10540. WOLFSSL_X509_NAME *subject = NULL;
  10541. WOLFSSL_X509 *x509 = NULL;
  10542. #endif
  10543. XFILE f = XBADFILE;
  10544. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10545. WOLFSSL_BIO *bio = NULL;
  10546. WOLFSSL_EVP_PKEY *pkey = NULL;
  10547. WC_PKCS12 *pkcs12 = NULL;
  10548. WC_PKCS12 *pkcs12_2 = NULL;
  10549. WOLFSSL_X509 *cert = NULL;
  10550. WOLFSSL_X509 *tmp = NULL;
  10551. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10552. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10553. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10554. WOLFSSL_CTX *ctx = NULL;
  10555. WOLFSSL *ssl = NULL;
  10556. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10557. #endif
  10558. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10559. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10560. if (f != XBADFILE) {
  10561. XFCLOSE(f);
  10562. f = XBADFILE;
  10563. }
  10564. goodPswLen = (int)XSTRLEN(goodPsw);
  10565. badPswLen = (int)XSTRLEN(badPsw);
  10566. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10567. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10568. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10569. ExpectNotNull(pkcs12);
  10570. BIO_free(bio);
  10571. bio = NULL;
  10572. /* check verify MAC directly */
  10573. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10574. /* check verify MAC fail case directly */
  10575. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10576. /* check verify MAC fail case */
  10577. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10578. ExpectNull(pkey);
  10579. ExpectNull(cert);
  10580. /* check parse with no extra certs kept */
  10581. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10582. 1);
  10583. ExpectNotNull(pkey);
  10584. ExpectNotNull(cert);
  10585. wolfSSL_EVP_PKEY_free(pkey);
  10586. pkey = NULL;
  10587. wolfSSL_X509_free(cert);
  10588. cert = NULL;
  10589. /* check parse with extra certs kept */
  10590. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10591. 1);
  10592. ExpectNotNull(pkey);
  10593. ExpectNotNull(cert);
  10594. ExpectNotNull(ca);
  10595. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10596. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10597. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10598. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10599. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10600. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10601. #else
  10602. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10603. #endif
  10604. /* Copy stack structure */
  10605. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10606. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10607. /* CTX now owns the tmp_ca stack structure */
  10608. tmp_ca = NULL;
  10609. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10610. ExpectNotNull(tmp_ca);
  10611. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10612. /* Check that the main cert is also set */
  10613. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10614. ExpectNotNull(ssl = SSL_new(ctx));
  10615. ExpectNotNull(SSL_get_certificate(ssl));
  10616. SSL_free(ssl);
  10617. SSL_CTX_free(ctx);
  10618. ctx = NULL;
  10619. #endif
  10620. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10621. /* should be 2 other certs on stack */
  10622. ExpectNotNull(tmp = sk_X509_pop(ca));
  10623. X509_free(tmp);
  10624. ExpectNotNull(tmp = sk_X509_pop(ca));
  10625. X509_free(tmp);
  10626. ExpectNull(sk_X509_pop(ca));
  10627. EVP_PKEY_free(pkey);
  10628. pkey = NULL;
  10629. X509_free(cert);
  10630. cert = NULL;
  10631. sk_X509_pop_free(ca, X509_free);
  10632. ca = NULL;
  10633. /* check PKCS12_create */
  10634. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10635. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10636. SSL_SUCCESS);
  10637. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10638. -1, -1, 100, -1, 0)));
  10639. EVP_PKEY_free(pkey);
  10640. pkey = NULL;
  10641. X509_free(cert);
  10642. cert = NULL;
  10643. sk_X509_pop_free(ca, NULL);
  10644. ca = NULL;
  10645. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10646. SSL_SUCCESS);
  10647. PKCS12_free(pkcs12_2);
  10648. pkcs12_2 = NULL;
  10649. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10650. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10651. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10652. 2000, 1, 0)));
  10653. EVP_PKEY_free(pkey);
  10654. pkey = NULL;
  10655. X509_free(cert);
  10656. cert = NULL;
  10657. sk_X509_pop_free(ca, NULL);
  10658. ca = NULL;
  10659. /* convert to DER then back and parse */
  10660. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10661. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10662. PKCS12_free(pkcs12_2);
  10663. pkcs12_2 = NULL;
  10664. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10665. BIO_free(bio);
  10666. bio = NULL;
  10667. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10668. SSL_SUCCESS);
  10669. /* should be 2 other certs on stack */
  10670. ExpectNotNull(tmp = sk_X509_pop(ca));
  10671. X509_free(tmp);
  10672. ExpectNotNull(tmp = sk_X509_pop(ca));
  10673. X509_free(tmp);
  10674. ExpectNull(sk_X509_pop(ca));
  10675. #ifndef NO_RC4
  10676. PKCS12_free(pkcs12_2);
  10677. pkcs12_2 = NULL;
  10678. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10679. NID_pbe_WithSHA1And128BitRC4,
  10680. NID_pbe_WithSHA1And128BitRC4,
  10681. 2000, 1, 0)));
  10682. EVP_PKEY_free(pkey);
  10683. pkey = NULL;
  10684. X509_free(cert);
  10685. cert = NULL;
  10686. sk_X509_pop_free(ca, NULL);
  10687. ca = NULL;
  10688. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10689. SSL_SUCCESS);
  10690. #endif /* NO_RC4 */
  10691. EVP_PKEY_free(pkey);
  10692. pkey = NULL;
  10693. X509_free(cert);
  10694. cert = NULL;
  10695. PKCS12_free(pkcs12);
  10696. pkcs12 = NULL;
  10697. PKCS12_free(pkcs12_2);
  10698. pkcs12_2 = NULL;
  10699. sk_X509_pop_free(ca, NULL);
  10700. ca = NULL;
  10701. #ifdef HAVE_ECC
  10702. /* test order of parsing */
  10703. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10704. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10705. if (f != XBADFILE) {
  10706. XFCLOSE(f);
  10707. f = XBADFILE;
  10708. }
  10709. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10710. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10711. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10712. WOLFSSL_SUCCESS);
  10713. /* check use of pkey after parse */
  10714. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10715. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10716. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10717. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10718. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10719. #else
  10720. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10721. #endif
  10722. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10723. SSL_CTX_free(ctx);
  10724. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10725. #endif
  10726. ExpectNotNull(pkey);
  10727. ExpectNotNull(cert);
  10728. ExpectNotNull(ca);
  10729. /* compare subject lines of certificates */
  10730. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10731. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10732. SSL_FILETYPE_PEM));
  10733. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10734. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10735. X509_free(x509);
  10736. x509 = NULL;
  10737. /* test expected fail case */
  10738. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10739. SSL_FILETYPE_PEM));
  10740. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10741. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10742. X509_free(x509);
  10743. x509 = NULL;
  10744. X509_free(cert);
  10745. cert = NULL;
  10746. /* get subject line from ca stack */
  10747. ExpectNotNull(cert = sk_X509_pop(ca));
  10748. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10749. /* compare subject from certificate in ca to expected */
  10750. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10751. SSL_FILETYPE_PEM));
  10752. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10753. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10754. /* modify case and compare subject from certificate in ca to expected.
  10755. * The first bit of the name is:
  10756. * /C=US/ST=Washington
  10757. * So we'll change subject->name[1] to 'c' (lower case) */
  10758. if (subject != NULL) {
  10759. subject->name[1] = 'c';
  10760. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10761. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10762. }
  10763. EVP_PKEY_free(pkey);
  10764. pkey = NULL;
  10765. X509_free(x509);
  10766. x509 = NULL;
  10767. X509_free(cert);
  10768. cert = NULL;
  10769. BIO_free(bio);
  10770. bio = NULL;
  10771. PKCS12_free(pkcs12);
  10772. pkcs12 = NULL;
  10773. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10774. ca = NULL;
  10775. /* test order of parsing */
  10776. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10777. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10778. if (f != XBADFILE) {
  10779. XFCLOSE(f);
  10780. f = XBADFILE;
  10781. }
  10782. /* check verify MAC fail case */
  10783. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10784. ExpectNull(pkey);
  10785. ExpectNull(cert);
  10786. /* check parse with no extra certs kept */
  10787. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10788. 1);
  10789. ExpectNotNull(pkey);
  10790. ExpectNotNull(cert);
  10791. wolfSSL_EVP_PKEY_free(pkey);
  10792. pkey = NULL;
  10793. wolfSSL_X509_free(cert);
  10794. cert = NULL;
  10795. /* check parse with extra certs kept */
  10796. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10797. 1);
  10798. ExpectNotNull(pkey);
  10799. ExpectNotNull(cert);
  10800. ExpectNotNull(ca);
  10801. wolfSSL_EVP_PKEY_free(pkey);
  10802. pkey = NULL;
  10803. wolfSSL_X509_free(cert);
  10804. cert = NULL;
  10805. sk_X509_pop_free(ca, NULL);
  10806. ca = NULL;
  10807. PKCS12_free(pkcs12);
  10808. pkcs12 = NULL;
  10809. #endif /* HAVE_ECC */
  10810. #ifdef WC_RC2
  10811. /* test PKCS#12 with RC2 encryption */
  10812. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10813. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10814. if (f != XBADFILE) {
  10815. XFCLOSE(f);
  10816. f = XBADFILE;
  10817. }
  10818. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10819. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10820. /* check verify MAC fail case */
  10821. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10822. ExpectNull(pkey);
  10823. ExpectNull(cert);
  10824. /* check parse with not extra certs kept */
  10825. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10826. WOLFSSL_SUCCESS);
  10827. ExpectNotNull(pkey);
  10828. ExpectNotNull(cert);
  10829. wolfSSL_EVP_PKEY_free(pkey);
  10830. pkey = NULL;
  10831. wolfSSL_X509_free(cert);
  10832. cert = NULL;
  10833. /* check parse with extra certs kept */
  10834. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10835. WOLFSSL_SUCCESS);
  10836. ExpectNotNull(pkey);
  10837. ExpectNotNull(cert);
  10838. ExpectNotNull(ca);
  10839. wolfSSL_EVP_PKEY_free(pkey);
  10840. wolfSSL_X509_free(cert);
  10841. sk_X509_pop_free(ca, NULL);
  10842. BIO_free(bio);
  10843. bio = NULL;
  10844. PKCS12_free(pkcs12);
  10845. pkcs12 = NULL;
  10846. #endif /* WC_RC2 */
  10847. /* Test i2d_PKCS12_bio */
  10848. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10849. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10850. if (f != XBADFILE)
  10851. XFCLOSE(f);
  10852. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10853. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10854. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10855. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10856. PKCS12_free(pkcs12);
  10857. BIO_free(bio);
  10858. (void)order;
  10859. #endif /* OPENSSL_EXTRA */
  10860. #endif /* HAVE_FIPS */
  10861. return EXPECT_RESULT();
  10862. }
  10863. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10864. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10865. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10866. #define TEST_PKCS8_ENC
  10867. #endif
  10868. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10869. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10870. /* used to keep track if FailTestCallback was called */
  10871. static int failTestCallbackCalled = 0;
  10872. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10873. {
  10874. (void)passwd;
  10875. (void)sz;
  10876. (void)rw;
  10877. (void)userdata;
  10878. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10879. failTestCallbackCalled = 1;
  10880. return -1;
  10881. }
  10882. #endif
  10883. static int test_wolfSSL_no_password_cb(void)
  10884. {
  10885. EXPECT_DECLS;
  10886. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10887. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10888. WOLFSSL_CTX* ctx = NULL;
  10889. byte buff[FOURK_BUF];
  10890. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10891. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10892. XFILE f = XBADFILE;
  10893. int bytes = 0;
  10894. #ifndef NO_WOLFSSL_CLIENT
  10895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10896. #else
  10897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10898. #endif
  10899. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10900. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10901. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10902. if (f != XBADFILE) {
  10903. XFCLOSE(f);
  10904. f = XBADFILE;
  10905. }
  10906. ExpectIntLE(bytes, sizeof(buff));
  10907. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10908. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10909. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10910. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10911. if (f != XBADFILE)
  10912. XFCLOSE(f);
  10913. ExpectIntLE(bytes, sizeof(buff));
  10914. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10915. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10916. wolfSSL_CTX_free(ctx);
  10917. /* Password callback should not be called by default */
  10918. ExpectIntEQ(failTestCallbackCalled, 0);
  10919. #endif
  10920. return EXPECT_RESULT();
  10921. }
  10922. #ifdef TEST_PKCS8_ENC
  10923. /* for PKCS8 test case */
  10924. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10925. {
  10926. int flag = 0;
  10927. (void)rw;
  10928. if (userdata != NULL) {
  10929. flag = *((int*)userdata); /* user set data */
  10930. }
  10931. switch (flag) {
  10932. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10933. * can be anything the user wishes to be passed to the callback
  10934. * associated with the WOLFSSL_CTX */
  10935. XSTRNCPY(passwd, "yassl123", sz);
  10936. return 8;
  10937. default:
  10938. return BAD_FUNC_ARG;
  10939. }
  10940. }
  10941. #endif /* TEST_PKCS8_ENC */
  10942. /* Testing functions dealing with PKCS8 */
  10943. static int test_wolfSSL_PKCS8(void)
  10944. {
  10945. EXPECT_DECLS;
  10946. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10947. !defined(WOLFCRYPT_ONLY)
  10948. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10949. byte buff[FOURK_BUF];
  10950. byte der[FOURK_BUF];
  10951. #ifndef NO_RSA
  10952. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10953. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10954. #endif
  10955. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10956. #ifdef HAVE_ECC
  10957. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10958. #endif
  10959. XFILE f = XBADFILE;
  10960. int bytes = 0;
  10961. WOLFSSL_CTX* ctx = NULL;
  10962. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10963. int ret;
  10964. ecc_key key;
  10965. word32 x = 0;
  10966. #endif
  10967. #ifdef TEST_PKCS8_ENC
  10968. #if !defined(NO_RSA) && !defined(NO_SHA)
  10969. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10970. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10971. #endif
  10972. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10973. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10974. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10975. #endif
  10976. int flag;
  10977. #endif
  10978. (void)der;
  10979. #ifndef NO_WOLFSSL_CLIENT
  10980. #ifndef WOLFSSL_NO_TLS12
  10981. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10982. #else
  10983. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10984. #endif
  10985. #else
  10986. #ifndef WOLFSSL_NO_TLS12
  10987. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10988. #else
  10989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10990. #endif
  10991. #endif
  10992. #ifdef TEST_PKCS8_ENC
  10993. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10994. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10995. flag = 1; /* used by password callback as return code */
  10996. #if !defined(NO_RSA) && !defined(NO_SHA)
  10997. /* test loading PEM PKCS8 encrypted file */
  10998. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10999. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11000. if (f != XBADFILE) {
  11001. XFCLOSE(f);
  11002. f = XBADFILE;
  11003. }
  11004. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11005. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11006. /* this next case should fail because of password callback return code */
  11007. flag = 0; /* used by password callback as return code */
  11008. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11009. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11010. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11011. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11012. "yassl123"), 0);
  11013. /* test that error value is returned with a bad password */
  11014. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11015. "bad"), 0);
  11016. /* test loading PEM PKCS8 encrypted file */
  11017. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11018. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11019. if (f != XBADFILE) {
  11020. XFCLOSE(f);
  11021. f = XBADFILE;
  11022. }
  11023. flag = 1; /* used by password callback as return code */
  11024. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11025. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11026. /* this next case should fail because of password callback return code */
  11027. flag = 0; /* used by password callback as return code */
  11028. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11029. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11030. #endif /* !NO_RSA && !NO_SHA */
  11031. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11032. /* test loading PEM PKCS8 encrypted ECC Key file */
  11033. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11034. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11035. if (f != XBADFILE) {
  11036. XFCLOSE(f);
  11037. f = XBADFILE;
  11038. }
  11039. flag = 1; /* used by password callback as return code */
  11040. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11041. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11042. /* this next case should fail because of password callback return code */
  11043. flag = 0; /* used by password callback as return code */
  11044. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11045. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11046. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11047. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11048. "yassl123"), 0);
  11049. /* test that error value is returned with a bad password */
  11050. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11051. "bad"), 0);
  11052. /* test loading DER PKCS8 encrypted ECC Key file */
  11053. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11054. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11055. if (f != XBADFILE) {
  11056. XFCLOSE(f);
  11057. f = XBADFILE;
  11058. }
  11059. flag = 1; /* used by password callback as return code */
  11060. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11061. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11062. /* this next case should fail because of password callback return code */
  11063. flag = 0; /* used by password callback as return code */
  11064. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11065. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11066. /* leave flag as "okay" */
  11067. flag = 1;
  11068. #endif /* HAVE_ECC && !NO_SHA */
  11069. #endif /* TEST_PKCS8_ENC */
  11070. #ifndef NO_RSA
  11071. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11072. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11073. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11074. if (f != XBADFILE) {
  11075. XFCLOSE(f);
  11076. f = XBADFILE;
  11077. }
  11078. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11079. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11080. /* test loading PEM PKCS8 private key file (not encrypted) */
  11081. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11082. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11083. if (f != XBADFILE) {
  11084. XFCLOSE(f);
  11085. f = XBADFILE;
  11086. }
  11087. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11088. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11089. #endif /* !NO_RSA */
  11090. /* Test PKCS8 PEM ECC key no crypt */
  11091. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11092. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11093. if (f != XBADFILE) {
  11094. XFCLOSE(f);
  11095. f = XBADFILE;
  11096. }
  11097. #ifdef HAVE_ECC
  11098. /* Test PKCS8 PEM ECC key no crypt */
  11099. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11100. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11101. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11102. /* decrypt PKCS8 PEM to key in DER format */
  11103. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11104. (word32)sizeof(der), NULL)), 0);
  11105. ret = wc_ecc_init(&key);
  11106. if (ret == 0) {
  11107. ret = wc_EccPrivateKeyDecode(der, &x, &key, (word32)bytes);
  11108. wc_ecc_free(&key);
  11109. }
  11110. ExpectIntEQ(ret, 0);
  11111. #endif
  11112. /* Test PKCS8 DER ECC key no crypt */
  11113. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11114. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11115. if (f != XBADFILE)
  11116. XFCLOSE(f);
  11117. /* Test using a PKCS8 ECC PEM */
  11118. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11119. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11120. #else
  11121. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11122. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11123. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11124. #endif /* HAVE_ECC */
  11125. wolfSSL_CTX_free(ctx);
  11126. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11127. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11128. return EXPECT_RESULT();
  11129. }
  11130. static int test_wolfSSL_PKCS8_ED25519(void)
  11131. {
  11132. EXPECT_DECLS;
  11133. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11134. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11135. defined(HAVE_ED25519_KEY_IMPORT)
  11136. const byte encPrivKey[] = \
  11137. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11138. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11139. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11140. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11141. "2L9W4v7swXkV+X+a1ww=\n"
  11142. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11143. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11144. byte der[FOURK_BUF];
  11145. WOLFSSL_CTX* ctx = NULL;
  11146. int bytes;
  11147. XMEMSET(der, 0, sizeof(der));
  11148. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11149. (word32)sizeof(der), password)), 0);
  11150. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11151. #ifndef NO_WOLFSSL_SERVER
  11152. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11153. #else
  11154. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11155. #endif
  11156. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11157. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11158. wolfSSL_CTX_free(ctx);
  11159. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11160. #endif
  11161. return EXPECT_RESULT();
  11162. }
  11163. static int test_wolfSSL_PKCS8_ED448(void)
  11164. {
  11165. EXPECT_DECLS;
  11166. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11167. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11168. defined(HAVE_ED448_KEY_IMPORT)
  11169. const byte encPrivKey[] = \
  11170. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11171. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11172. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11173. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11174. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11175. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11176. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11177. byte der[FOURK_BUF];
  11178. WOLFSSL_CTX* ctx = NULL;
  11179. int bytes;
  11180. XMEMSET(der, 0, sizeof(der));
  11181. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11182. (word32)sizeof(der), password)), 0);
  11183. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11184. #ifndef NO_WOLFSSL_SERVER
  11185. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11186. #else
  11187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11188. #endif
  11189. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11190. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11191. wolfSSL_CTX_free(ctx);
  11192. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11193. #endif
  11194. return EXPECT_RESULT();
  11195. }
  11196. /* Testing functions dealing with PKCS5 */
  11197. static int test_wolfSSL_PKCS5(void)
  11198. {
  11199. EXPECT_DECLS;
  11200. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11201. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11202. const char* passwd = "myfipsPa$$W0rd";
  11203. #else
  11204. const char *passwd = "pass1234";
  11205. #endif
  11206. const unsigned char *salt = (unsigned char *)"salt1234";
  11207. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11208. DYNAMIC_TYPE_TMP_BUFFER);
  11209. int ret = 0;
  11210. ExpectNotNull(out);
  11211. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11212. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11213. WOLFSSL_SUCCESS);
  11214. #ifdef WOLFSSL_SHA512
  11215. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11216. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11217. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11218. #endif
  11219. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11220. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11221. return EXPECT_RESULT();
  11222. }
  11223. /* test parsing URI from certificate */
  11224. static int test_wolfSSL_URI(void)
  11225. {
  11226. EXPECT_DECLS;
  11227. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11228. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11229. defined(OPENSSL_EXTRA))
  11230. WOLFSSL_X509* x509 = NULL;
  11231. const char uri[] = "./certs/client-uri-cert.pem";
  11232. const char urn[] = "./certs/client-absolute-urn.pem";
  11233. const char badUri[] = "./certs/client-relative-uri.pem";
  11234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11235. WOLFSSL_FILETYPE_PEM));
  11236. wolfSSL_FreeX509(x509);
  11237. x509 = NULL;
  11238. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11239. WOLFSSL_FILETYPE_PEM));
  11240. wolfSSL_FreeX509(x509);
  11241. x509 = NULL;
  11242. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11243. && !defined(WOLFSSL_FPKI)
  11244. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11245. WOLFSSL_FILETYPE_PEM));
  11246. #else
  11247. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11248. WOLFSSL_FILETYPE_PEM));
  11249. #endif
  11250. wolfSSL_FreeX509(x509);
  11251. #endif
  11252. return EXPECT_RESULT();
  11253. }
  11254. static int test_wolfSSL_TBS(void)
  11255. {
  11256. EXPECT_DECLS;
  11257. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11258. && defined(OPENSSL_EXTRA)
  11259. WOLFSSL_X509* x509 = NULL;
  11260. const unsigned char* tbs;
  11261. int tbsSz;
  11262. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11263. WOLFSSL_FILETYPE_PEM));
  11264. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11265. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11266. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11267. ExpectIntEQ(tbsSz, 1003);
  11268. wolfSSL_FreeX509(x509);
  11269. #endif
  11270. return EXPECT_RESULT();
  11271. }
  11272. static int test_wolfSSL_X509_verify(void)
  11273. {
  11274. EXPECT_DECLS;
  11275. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11276. defined(OPENSSL_EXTRA)
  11277. WOLFSSL_X509* ca = NULL;
  11278. WOLFSSL_X509* serv = NULL;
  11279. WOLFSSL_EVP_PKEY* pkey = NULL;
  11280. unsigned char buf[2048];
  11281. const unsigned char* pt = NULL;
  11282. int bufSz;
  11283. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11284. WOLFSSL_FILETYPE_PEM));
  11285. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11286. WOLFSSL_SUCCESS);
  11287. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11288. WOLFSSL_SUCCESS);
  11289. ExpectIntEQ(bufSz, 294);
  11290. bufSz = 2048;
  11291. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11292. WOLFSSL_SUCCESS);
  11293. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11294. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11295. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11296. WOLFSSL_FILETYPE_PEM));
  11297. /* success case */
  11298. pt = buf;
  11299. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11300. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11301. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11302. wolfSSL_EVP_PKEY_free(pkey);
  11303. pkey = NULL;
  11304. /* fail case */
  11305. bufSz = 2048;
  11306. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11307. WOLFSSL_SUCCESS);
  11308. pt = buf;
  11309. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11310. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11311. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11312. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11313. wolfSSL_EVP_PKEY_free(pkey);
  11314. wolfSSL_FreeX509(ca);
  11315. wolfSSL_FreeX509(serv);
  11316. #endif
  11317. return EXPECT_RESULT();
  11318. }
  11319. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11320. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11321. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11322. /* create certificate with version 2 */
  11323. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11324. {
  11325. EXPECT_DECLS;
  11326. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11327. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11328. unsigned char *der = NULL, *key = NULL, *pt;
  11329. char *header = NULL, *name = NULL;
  11330. int derSz;
  11331. long keySz;
  11332. XFILE fp = XBADFILE;
  11333. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11334. time_t t;
  11335. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11336. WOLFSSL_FILETYPE_PEM));
  11337. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11338. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11339. WOLFSSL_SUCCESS);
  11340. if (fp != XBADFILE)
  11341. XFCLOSE(fp);
  11342. pt = key;
  11343. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11344. (const unsigned char**)&pt, keySz));
  11345. /* create the version 2 certificate */
  11346. ExpectNotNull(x509v2 = X509_new());
  11347. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11348. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11349. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11350. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11351. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11352. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11353. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11354. t = time(NULL);
  11355. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11356. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11357. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11358. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11359. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11360. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11361. ExpectIntGT(derSz, 0);
  11362. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11363. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11364. /* TODO: Replace with API call */
  11365. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11366. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11367. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11368. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11369. wolfSSL_X509_free(x509);
  11370. wolfSSL_X509_free(x509v2);
  11371. wolfSSL_EVP_PKEY_free(priv);
  11372. wolfSSL_EVP_PKEY_free(pub);
  11373. wolfSSL_ASN1_TIME_free(notBefore);
  11374. wolfSSL_ASN1_TIME_free(notAfter);
  11375. return EXPECT_RESULT();
  11376. }
  11377. /* override certificate version error */
  11378. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11379. {
  11380. EXPECT_DECLS;
  11381. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11382. ExpectIntEQ(store->error, ASN_VERSION_E);
  11383. #else
  11384. ExpectIntEQ(store->error, 0);
  11385. #endif
  11386. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11387. (void)preverify;
  11388. return EXPECT_RESULT() == TEST_SUCCESS;
  11389. }
  11390. /* set verify callback that will override bad certificate version */
  11391. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11392. {
  11393. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11394. return TEST_SUCCESS;
  11395. }
  11396. #endif
  11397. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11398. {
  11399. EXPECT_DECLS;
  11400. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11401. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11402. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11403. test_ssl_cbf func_cb_client;
  11404. test_ssl_cbf func_cb_server;
  11405. /* test server rejects a client certificate that is not version 3 */
  11406. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11407. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11408. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11409. #ifndef WOLFSSL_NO_TLS12
  11410. func_cb_client.method = wolfTLSv1_2_client_method;
  11411. #else
  11412. func_cb_client.method = wolfTLSv1_3_client_method;
  11413. #endif
  11414. #ifndef WOLFSSL_NO_TLS12
  11415. func_cb_server.method = wolfTLSv1_2_server_method;
  11416. #else
  11417. func_cb_server.method = wolfTLSv1_3_server_method;
  11418. #endif
  11419. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11420. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11421. &func_cb_server, NULL), TEST_FAIL);
  11422. #else
  11423. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11424. &func_cb_server, NULL), TEST_SUCCESS);
  11425. #endif
  11426. #endif
  11427. return EXPECT_RESULT();
  11428. }
  11429. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11430. {
  11431. EXPECT_DECLS;
  11432. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11433. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11434. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11435. test_ssl_cbf func_cb_client;
  11436. test_ssl_cbf func_cb_server;
  11437. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11438. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11439. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11440. func_cb_server.ctx_ready = &test_set_override_x509;
  11441. #ifndef WOLFSSL_NO_TLS12
  11442. func_cb_client.method = wolfTLSv1_2_client_method;
  11443. #else
  11444. func_cb_client.method = wolfTLSv1_3_client_method;
  11445. #endif
  11446. #ifndef WOLFSSL_NO_TLS12
  11447. func_cb_server.method = wolfTLSv1_2_server_method;
  11448. #else
  11449. func_cb_server.method = wolfTLSv1_3_server_method;
  11450. #endif
  11451. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11452. &func_cb_server, NULL), TEST_SUCCESS);
  11453. #endif
  11454. return EXPECT_RESULT();
  11455. }
  11456. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11457. * version allowed.
  11458. * POST: 1 on success.
  11459. */
  11460. static int test_wolfSSL_CTX_SetMinVersion(void)
  11461. {
  11462. int res = TEST_SKIPPED;
  11463. #ifndef NO_WOLFSSL_CLIENT
  11464. int failFlag = WOLFSSL_SUCCESS;
  11465. WOLFSSL_CTX* ctx;
  11466. int itr;
  11467. #ifndef NO_OLD_TLS
  11468. const int versions[] = {
  11469. #ifdef WOLFSSL_ALLOW_TLSV10
  11470. WOLFSSL_TLSV1,
  11471. #endif
  11472. WOLFSSL_TLSV1_1,
  11473. WOLFSSL_TLSV1_2 };
  11474. #elif !defined(WOLFSSL_NO_TLS12)
  11475. const int versions[] = { WOLFSSL_TLSV1_2 };
  11476. #elif defined(WOLFSSL_TLS13)
  11477. const int versions[] = { WOLFSSL_TLSV1_3 };
  11478. #else
  11479. const int versions[0];
  11480. #endif
  11481. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11482. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11483. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11484. != WOLFSSL_SUCCESS) {
  11485. failFlag = WOLFSSL_FAILURE;
  11486. }
  11487. }
  11488. wolfSSL_CTX_free(ctx);
  11489. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11490. #endif
  11491. return res;
  11492. } /* END test_wolfSSL_CTX_SetMinVersion */
  11493. /*----------------------------------------------------------------------------*
  11494. | OCSP Stapling
  11495. *----------------------------------------------------------------------------*/
  11496. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11497. * need to contact the CA, lowering the cost of cert revocation checking.
  11498. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11499. * POST: 1 returned for success.
  11500. */
  11501. static int test_wolfSSL_UseOCSPStapling(void)
  11502. {
  11503. EXPECT_DECLS;
  11504. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11505. !defined(NO_WOLFSSL_CLIENT)
  11506. WOLFSSL_CTX* ctx = NULL;
  11507. WOLFSSL* ssl = NULL;
  11508. #ifndef NO_WOLFSSL_CLIENT
  11509. #ifndef WOLFSSL_NO_TLS12
  11510. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11511. #else
  11512. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11513. #endif
  11514. #else
  11515. #ifndef WOLFSSL_NO_TLS12
  11516. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11517. #else
  11518. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11519. #endif
  11520. #endif
  11521. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11522. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11523. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11524. #ifndef NO_WOLFSSL_CLIENT
  11525. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11526. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11527. #else
  11528. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11529. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11530. #endif
  11531. wolfSSL_free(ssl);
  11532. wolfSSL_CTX_free(ctx);
  11533. #endif
  11534. return EXPECT_RESULT();
  11535. } /* END test_wolfSSL_UseOCSPStapling */
  11536. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11537. * stapling eliminates the need to contact the CA and lowers cert revocation
  11538. * check.
  11539. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11540. */
  11541. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11542. {
  11543. EXPECT_DECLS;
  11544. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11545. !defined(NO_WOLFSSL_CLIENT)
  11546. WOLFSSL_CTX* ctx = NULL;
  11547. WOLFSSL* ssl = NULL;
  11548. #ifndef NO_WOLFSSL_CLIENT
  11549. #ifndef WOLFSSL_NO_TLS12
  11550. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11551. #else
  11552. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11553. #endif
  11554. #else
  11555. #ifndef WOLFSSL_NO_TLS12
  11556. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11557. #else
  11558. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11559. #endif
  11560. #endif
  11561. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11562. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11563. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11564. #ifndef NO_WOLFSSL_CLIENT
  11565. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11566. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11567. #else
  11568. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11569. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11570. #endif
  11571. wolfSSL_free(ssl);
  11572. wolfSSL_CTX_free(ctx);
  11573. #endif
  11574. return EXPECT_RESULT();
  11575. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11576. /*----------------------------------------------------------------------------*
  11577. | Multicast Tests
  11578. *----------------------------------------------------------------------------*/
  11579. static int test_wolfSSL_mcast(void)
  11580. {
  11581. EXPECT_DECLS;
  11582. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11583. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11584. WOLFSSL_CTX* ctx = NULL;
  11585. WOLFSSL* ssl = NULL;
  11586. byte preMasterSecret[512];
  11587. byte clientRandom[32];
  11588. byte serverRandom[32];
  11589. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11590. byte buf[256];
  11591. word16 newId;
  11592. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11593. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11594. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11595. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11596. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11597. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11598. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11599. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11600. WOLFSSL_SUCCESS);
  11601. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11602. ExpectIntLE(newId, 100);
  11603. wolfSSL_free(ssl);
  11604. wolfSSL_CTX_free(ctx);
  11605. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11606. * WOLFSSL_SNIFFER) */
  11607. return EXPECT_RESULT();
  11608. }
  11609. /*----------------------------------------------------------------------------*
  11610. | Wolfcrypt
  11611. *----------------------------------------------------------------------------*/
  11612. /*
  11613. * Unit test for the wc_InitBlake2b()
  11614. */
  11615. static int test_wc_InitBlake2b(void)
  11616. {
  11617. EXPECT_DECLS;
  11618. #ifdef HAVE_BLAKE2
  11619. Blake2b blake;
  11620. /* Test good arg. */
  11621. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11622. /* Test bad arg. */
  11623. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11624. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11625. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11626. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11627. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11628. #endif
  11629. return EXPECT_RESULT();
  11630. } /* END test_wc_InitBlake2b*/
  11631. /*
  11632. * Unit test for the wc_InitBlake2b_WithKey()
  11633. */
  11634. static int test_wc_InitBlake2b_WithKey(void)
  11635. {
  11636. EXPECT_DECLS;
  11637. #ifdef HAVE_BLAKE2
  11638. Blake2b blake;
  11639. word32 digestSz = BLAKE2B_KEYBYTES;
  11640. byte key[BLAKE2B_KEYBYTES];
  11641. word32 keylen = BLAKE2B_KEYBYTES;
  11642. XMEMSET(key, 0, sizeof(key));
  11643. /* Test good arg. */
  11644. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11645. /* Test bad args. */
  11646. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11647. BAD_FUNC_ARG);
  11648. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11649. BAD_FUNC_ARG);
  11650. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11651. #endif
  11652. return EXPECT_RESULT();
  11653. } /* END wc_InitBlake2b_WithKey*/
  11654. /*
  11655. * Unit test for the wc_InitBlake2s_WithKey()
  11656. */
  11657. static int test_wc_InitBlake2s_WithKey(void)
  11658. {
  11659. EXPECT_DECLS;
  11660. #ifdef HAVE_BLAKE2S
  11661. Blake2s blake;
  11662. word32 digestSz = BLAKE2S_KEYBYTES;
  11663. byte *key = (byte*)"01234567890123456789012345678901";
  11664. word32 keylen = BLAKE2S_KEYBYTES;
  11665. /* Test good arg. */
  11666. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11667. /* Test bad args. */
  11668. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11669. BAD_FUNC_ARG);
  11670. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11671. BAD_FUNC_ARG);
  11672. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11673. #endif
  11674. return EXPECT_RESULT();
  11675. } /* END wc_InitBlake2s_WithKey*/
  11676. /*
  11677. * Unit test for the wc_InitMd5()
  11678. */
  11679. static int test_wc_InitMd5(void)
  11680. {
  11681. EXPECT_DECLS;
  11682. #ifndef NO_MD5
  11683. wc_Md5 md5;
  11684. /* Test good arg. */
  11685. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11686. /* Test bad arg. */
  11687. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11688. wc_Md5Free(&md5);
  11689. #endif
  11690. return EXPECT_RESULT();
  11691. } /* END test_wc_InitMd5 */
  11692. /*
  11693. * Testing wc_UpdateMd5()
  11694. */
  11695. static int test_wc_Md5Update(void)
  11696. {
  11697. EXPECT_DECLS;
  11698. #ifndef NO_MD5
  11699. wc_Md5 md5;
  11700. byte hash[WC_MD5_DIGEST_SIZE];
  11701. testVector a, b, c;
  11702. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11703. /* Input */
  11704. a.input = "a";
  11705. a.inLen = XSTRLEN(a.input);
  11706. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11707. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11708. /* Update input. */
  11709. a.input = "abc";
  11710. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11711. "\x72";
  11712. a.inLen = XSTRLEN(a.input);
  11713. a.outLen = XSTRLEN(a.output);
  11714. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11715. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11716. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11717. /* Pass in bad values. */
  11718. b.input = NULL;
  11719. b.inLen = 0;
  11720. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11721. c.input = NULL;
  11722. c.inLen = WC_MD5_DIGEST_SIZE;
  11723. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11724. BAD_FUNC_ARG);
  11725. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11726. BAD_FUNC_ARG);
  11727. wc_Md5Free(&md5);
  11728. #endif
  11729. return EXPECT_RESULT();
  11730. } /* END test_wc_Md5Update() */
  11731. /*
  11732. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11733. */
  11734. static int test_wc_Md5Final(void)
  11735. {
  11736. EXPECT_DECLS;
  11737. #ifndef NO_MD5
  11738. /* Instantiate */
  11739. wc_Md5 md5;
  11740. byte* hash_test[3];
  11741. byte hash1[WC_MD5_DIGEST_SIZE];
  11742. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11743. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11744. int times, i;
  11745. /* Initialize */
  11746. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11747. hash_test[0] = hash1;
  11748. hash_test[1] = hash2;
  11749. hash_test[2] = hash3;
  11750. times = sizeof(hash_test)/sizeof(byte*);
  11751. for (i = 0; i < times; i++) {
  11752. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11753. }
  11754. /* Test bad args. */
  11755. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11756. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11757. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11758. wc_Md5Free(&md5);
  11759. #endif
  11760. return EXPECT_RESULT();
  11761. }
  11762. /*
  11763. * Unit test for the wc_InitSha()
  11764. */
  11765. static int test_wc_InitSha(void)
  11766. {
  11767. EXPECT_DECLS;
  11768. #ifndef NO_SHA
  11769. wc_Sha sha;
  11770. /* Test good arg. */
  11771. ExpectIntEQ(wc_InitSha(&sha), 0);
  11772. /* Test bad arg. */
  11773. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11774. wc_ShaFree(&sha);
  11775. #endif
  11776. return EXPECT_RESULT();
  11777. } /* END test_wc_InitSha */
  11778. /*
  11779. * Tesing wc_ShaUpdate()
  11780. */
  11781. static int test_wc_ShaUpdate(void)
  11782. {
  11783. EXPECT_DECLS;
  11784. #ifndef NO_SHA
  11785. wc_Sha sha;
  11786. byte hash[WC_SHA_DIGEST_SIZE];
  11787. testVector a, b, c;
  11788. ExpectIntEQ(wc_InitSha(&sha), 0);
  11789. /* Input. */
  11790. a.input = "a";
  11791. a.inLen = XSTRLEN(a.input);
  11792. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11793. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11794. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11795. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11796. /* Update input. */
  11797. a.input = "abc";
  11798. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11799. "\x6C\x9C\xD0\xD8\x9D";
  11800. a.inLen = XSTRLEN(a.input);
  11801. a.outLen = XSTRLEN(a.output);
  11802. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11803. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11804. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11805. /* Try passing in bad values. */
  11806. b.input = NULL;
  11807. b.inLen = 0;
  11808. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11809. c.input = NULL;
  11810. c.inLen = WC_SHA_DIGEST_SIZE;
  11811. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11812. BAD_FUNC_ARG);
  11813. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11814. BAD_FUNC_ARG);
  11815. wc_ShaFree(&sha);
  11816. #endif
  11817. return EXPECT_RESULT();
  11818. } /* END test_wc_ShaUpdate() */
  11819. /*
  11820. * Unit test on wc_ShaFinal
  11821. */
  11822. static int test_wc_ShaFinal(void)
  11823. {
  11824. EXPECT_DECLS;
  11825. #ifndef NO_SHA
  11826. wc_Sha sha;
  11827. byte* hash_test[3];
  11828. byte hash1[WC_SHA_DIGEST_SIZE];
  11829. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11830. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11831. int times, i;
  11832. /* Initialize*/
  11833. ExpectIntEQ(wc_InitSha(&sha), 0);
  11834. hash_test[0] = hash1;
  11835. hash_test[1] = hash2;
  11836. hash_test[2] = hash3;
  11837. times = sizeof(hash_test)/sizeof(byte*);
  11838. for (i = 0; i < times; i++) {
  11839. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11840. }
  11841. /* Test bad args. */
  11842. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11843. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11844. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11845. wc_ShaFree(&sha);
  11846. #endif
  11847. return EXPECT_RESULT();
  11848. } /* END test_wc_ShaFinal */
  11849. /*
  11850. * Unit test for wc_InitSha256()
  11851. */
  11852. static int test_wc_InitSha256(void)
  11853. {
  11854. EXPECT_DECLS;
  11855. #ifndef NO_SHA256
  11856. wc_Sha256 sha256;
  11857. /* Test good arg. */
  11858. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11859. /* Test bad arg. */
  11860. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11861. wc_Sha256Free(&sha256);
  11862. #endif
  11863. return EXPECT_RESULT();
  11864. } /* END test_wc_InitSha256 */
  11865. /*
  11866. * Unit test for wc_Sha256Update()
  11867. */
  11868. static int test_wc_Sha256Update(void)
  11869. {
  11870. EXPECT_DECLS;
  11871. #ifndef NO_SHA256
  11872. wc_Sha256 sha256;
  11873. byte hash[WC_SHA256_DIGEST_SIZE];
  11874. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11875. testVector a, b, c;
  11876. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11877. /* Input. */
  11878. a.input = "a";
  11879. a.inLen = XSTRLEN(a.input);
  11880. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11881. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11882. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11883. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11884. /* Update input. */
  11885. a.input = "abc";
  11886. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11887. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11888. "\x15\xAD";
  11889. a.inLen = XSTRLEN(a.input);
  11890. a.outLen = XSTRLEN(a.output);
  11891. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11892. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11893. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11894. /* Unaligned check. */
  11895. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11896. 0);
  11897. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11898. /* Try passing in bad values */
  11899. b.input = NULL;
  11900. b.inLen = 0;
  11901. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11902. c.input = NULL;
  11903. c.inLen = WC_SHA256_DIGEST_SIZE;
  11904. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11905. BAD_FUNC_ARG);
  11906. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11907. BAD_FUNC_ARG);
  11908. wc_Sha256Free(&sha256);
  11909. #endif
  11910. return EXPECT_RESULT();
  11911. } /* END test_wc_Sha256Update */
  11912. /*
  11913. * Unit test function for wc_Sha256Final()
  11914. */
  11915. static int test_wc_Sha256Final(void)
  11916. {
  11917. EXPECT_DECLS;
  11918. #ifndef NO_SHA256
  11919. wc_Sha256 sha256;
  11920. byte* hash_test[3];
  11921. byte hash1[WC_SHA256_DIGEST_SIZE];
  11922. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11923. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11924. int times, i;
  11925. /* Initialize */
  11926. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11927. hash_test[0] = hash1;
  11928. hash_test[1] = hash2;
  11929. hash_test[2] = hash3;
  11930. times = sizeof(hash_test) / sizeof(byte*);
  11931. for (i = 0; i < times; i++) {
  11932. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11933. }
  11934. /* Test bad args. */
  11935. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11936. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11937. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11938. wc_Sha256Free(&sha256);
  11939. #endif
  11940. return EXPECT_RESULT();
  11941. } /* END test_wc_Sha256Final */
  11942. /*
  11943. * Unit test function for wc_Sha256FinalRaw()
  11944. */
  11945. static int test_wc_Sha256FinalRaw(void)
  11946. {
  11947. EXPECT_DECLS;
  11948. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11949. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11950. !defined(WOLFSSL_NO_HASH_RAW)
  11951. wc_Sha256 sha256;
  11952. byte* hash_test[3];
  11953. byte hash1[WC_SHA256_DIGEST_SIZE];
  11954. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11955. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11956. int times, i;
  11957. /* Initialize */
  11958. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11959. hash_test[0] = hash1;
  11960. hash_test[1] = hash2;
  11961. hash_test[2] = hash3;
  11962. times = sizeof(hash_test) / sizeof(byte*);
  11963. for (i = 0; i < times; i++) {
  11964. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11965. }
  11966. /* Test bad args. */
  11967. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11968. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11969. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11970. wc_Sha256Free(&sha256);
  11971. #endif
  11972. return EXPECT_RESULT();
  11973. } /* END test_wc_Sha256FinalRaw */
  11974. /*
  11975. * Unit test function for wc_Sha256GetFlags()
  11976. */
  11977. static int test_wc_Sha256GetFlags(void)
  11978. {
  11979. EXPECT_DECLS;
  11980. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11981. wc_Sha256 sha256;
  11982. word32 flags = 0;
  11983. /* Initialize */
  11984. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11985. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11986. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11987. wc_Sha256Free(&sha256);
  11988. #endif
  11989. return EXPECT_RESULT();
  11990. } /* END test_wc_Sha256GetFlags */
  11991. /*
  11992. * Unit test function for wc_Sha256Free()
  11993. */
  11994. static int test_wc_Sha256Free(void)
  11995. {
  11996. EXPECT_DECLS;
  11997. #ifndef NO_SHA256
  11998. wc_Sha256Free(NULL);
  11999. /* Set result to SUCCESS. */
  12000. ExpectTrue(1);
  12001. #endif
  12002. return EXPECT_RESULT();
  12003. } /* END test_wc_Sha256Free */
  12004. /*
  12005. * Unit test function for wc_Sha256GetHash()
  12006. */
  12007. static int test_wc_Sha256GetHash(void)
  12008. {
  12009. EXPECT_DECLS;
  12010. #ifndef NO_SHA256
  12011. wc_Sha256 sha256;
  12012. byte hash1[WC_SHA256_DIGEST_SIZE];
  12013. /* Initialize */
  12014. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12015. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12016. /* test bad arguments*/
  12017. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12018. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12019. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12020. wc_Sha256Free(&sha256);
  12021. #endif
  12022. return EXPECT_RESULT();
  12023. } /* END test_wc_Sha256GetHash */
  12024. /*
  12025. * Unit test function for wc_Sha256Copy()
  12026. */
  12027. static int test_wc_Sha256Copy(void)
  12028. {
  12029. EXPECT_DECLS;
  12030. #ifndef NO_SHA256
  12031. wc_Sha256 sha256;
  12032. wc_Sha256 temp;
  12033. XMEMSET(&sha256, 0, sizeof(sha256));
  12034. XMEMSET(&temp, 0, sizeof(temp));
  12035. /* Initialize */
  12036. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12037. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12038. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12039. /* test bad arguments*/
  12040. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12041. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12042. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12043. wc_Sha256Free(&sha256);
  12044. wc_Sha256Free(&temp);
  12045. #endif
  12046. return EXPECT_RESULT();
  12047. } /* END test_wc_Sha256Copy */
  12048. /*
  12049. * Testing wc_InitSha512()
  12050. */
  12051. static int test_wc_InitSha512(void)
  12052. {
  12053. EXPECT_DECLS;
  12054. #ifdef WOLFSSL_SHA512
  12055. wc_Sha512 sha512;
  12056. /* Test good arg. */
  12057. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12058. /* Test bad arg. */
  12059. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12060. wc_Sha512Free(&sha512);
  12061. #endif
  12062. return EXPECT_RESULT();
  12063. } /* END test_wc_InitSha512 */
  12064. /*
  12065. * wc_Sha512Update() test.
  12066. */
  12067. static int test_wc_Sha512Update(void)
  12068. {
  12069. EXPECT_DECLS;
  12070. #ifdef WOLFSSL_SHA512
  12071. wc_Sha512 sha512;
  12072. byte hash[WC_SHA512_DIGEST_SIZE];
  12073. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12074. testVector a, b, c;
  12075. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12076. /* Input. */
  12077. a.input = "a";
  12078. a.inLen = XSTRLEN(a.input);
  12079. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12080. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12081. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12082. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12083. /* Update input. */
  12084. a.input = "abc";
  12085. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12086. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12087. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12088. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12089. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12090. a.inLen = XSTRLEN(a.input);
  12091. a.outLen = XSTRLEN(a.output);
  12092. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12093. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12094. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12095. /* Unaligned check. */
  12096. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12097. 0);
  12098. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12099. /* Try passing in bad values */
  12100. b.input = NULL;
  12101. b.inLen = 0;
  12102. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12103. c.input = NULL;
  12104. c.inLen = WC_SHA512_DIGEST_SIZE;
  12105. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12106. BAD_FUNC_ARG);
  12107. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12108. BAD_FUNC_ARG);
  12109. wc_Sha512Free(&sha512);
  12110. #endif
  12111. return EXPECT_RESULT();
  12112. } /* END test_wc_Sha512Update */
  12113. #ifdef WOLFSSL_SHA512
  12114. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12115. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12116. /* Performs test for
  12117. * - wc_Sha512Final/wc_Sha512FinalRaw
  12118. * - wc_Sha512_224Final/wc_Sha512_224Final
  12119. * - wc_Sha512_256Final/wc_Sha512_256Final
  12120. * parameter:
  12121. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12122. * WC_HASH_TYPE_SHA512_256
  12123. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12124. *return 0 on success
  12125. */
  12126. static int test_Sha512_Family_Final(int type, int isRaw)
  12127. {
  12128. EXPECT_DECLS;
  12129. wc_Sha512 sha512;
  12130. byte* hash_test[3];
  12131. byte hash1[WC_SHA512_DIGEST_SIZE];
  12132. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12133. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12134. int times, i;
  12135. int(*initFp)(wc_Sha512*);
  12136. int(*finalFp)(wc_Sha512*, byte*);
  12137. void(*freeFp)(wc_Sha512*);
  12138. if (type == WC_HASH_TYPE_SHA512) {
  12139. initFp = wc_InitSha512;
  12140. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12141. !defined(WOLFSSL_NO_HASH_RAW)
  12142. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12143. #else
  12144. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12145. #endif
  12146. freeFp = wc_Sha512Free;
  12147. }
  12148. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12149. #if !defined(WOLFSSL_NOSHA512_224)
  12150. else if (type == WC_HASH_TYPE_SHA512_224) {
  12151. initFp = wc_InitSha512_224;
  12152. #if !defined(WOLFSSL_NO_HASH_RAW)
  12153. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12154. #else
  12155. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12156. #endif
  12157. freeFp = wc_Sha512_224Free;
  12158. }
  12159. #endif
  12160. #if !defined(WOLFSSL_NOSHA512_256)
  12161. else if (type == WC_HASH_TYPE_SHA512_256) {
  12162. initFp = wc_InitSha512_256;
  12163. #if !defined(WOLFSSL_NO_HASH_RAW)
  12164. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12165. #else
  12166. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12167. #endif
  12168. freeFp = wc_Sha512_256Free;
  12169. }
  12170. #endif
  12171. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12172. else
  12173. return TEST_FAIL;
  12174. /* Initialize */
  12175. ExpectIntEQ(initFp(&sha512), 0);
  12176. hash_test[0] = hash1;
  12177. hash_test[1] = hash2;
  12178. hash_test[2] = hash3;
  12179. times = sizeof(hash_test) / sizeof(byte *);
  12180. /* Good test args. */
  12181. for (i = 0; i < times; i++) {
  12182. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12183. }
  12184. /* Test bad args. */
  12185. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12186. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12187. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12188. freeFp(&sha512);
  12189. return EXPECT_RESULT();
  12190. }
  12191. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12192. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12193. #endif /* WOLFSSL_SHA512 */
  12194. /*
  12195. * Unit test function for wc_Sha512Final()
  12196. */
  12197. static int test_wc_Sha512Final(void)
  12198. {
  12199. EXPECT_DECLS;
  12200. #ifdef WOLFSSL_SHA512
  12201. wc_Sha512 sha512;
  12202. byte* hash_test[3];
  12203. byte hash1[WC_SHA512_DIGEST_SIZE];
  12204. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12205. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12206. int times, i;
  12207. /* Initialize */
  12208. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12209. hash_test[0] = hash1;
  12210. hash_test[1] = hash2;
  12211. hash_test[2] = hash3;
  12212. times = sizeof(hash_test) / sizeof(byte *);
  12213. for (i = 0; i < times; i++) {
  12214. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12215. }
  12216. /* Test bad args. */
  12217. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12218. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12219. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12220. wc_Sha512Free(&sha512);
  12221. #endif
  12222. return EXPECT_RESULT();
  12223. } /* END test_wc_Sha512Final */
  12224. /*
  12225. * Unit test function for wc_Sha512GetFlags()
  12226. */
  12227. static int test_wc_Sha512GetFlags(void)
  12228. {
  12229. EXPECT_DECLS;
  12230. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12231. wc_Sha512 sha512;
  12232. word32 flags = 0;
  12233. /* Initialize */
  12234. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12235. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12236. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12237. wc_Sha512Free(&sha512);
  12238. #endif
  12239. return EXPECT_RESULT();
  12240. } /* END test_wc_Sha512GetFlags */
  12241. /*
  12242. * Unit test function for wc_Sha512FinalRaw()
  12243. */
  12244. static int test_wc_Sha512FinalRaw(void)
  12245. {
  12246. EXPECT_DECLS;
  12247. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12248. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12249. !defined(WOLFSSL_NO_HASH_RAW)
  12250. wc_Sha512 sha512;
  12251. byte* hash_test[3];
  12252. byte hash1[WC_SHA512_DIGEST_SIZE];
  12253. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12254. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12255. int times, i;
  12256. /* Initialize */
  12257. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12258. hash_test[0] = hash1;
  12259. hash_test[1] = hash2;
  12260. hash_test[2] = hash3;
  12261. times = sizeof(hash_test) / sizeof(byte*);
  12262. /* Good test args. */
  12263. for (i = 0; i < times; i++) {
  12264. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12265. }
  12266. /* Test bad args. */
  12267. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12268. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12269. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12270. wc_Sha512Free(&sha512);
  12271. #endif
  12272. return EXPECT_RESULT();
  12273. } /* END test_wc_Sha512FinalRaw */
  12274. /*
  12275. * Unit test function for wc_Sha512Free()
  12276. */
  12277. static int test_wc_Sha512Free(void)
  12278. {
  12279. EXPECT_DECLS;
  12280. #ifdef WOLFSSL_SHA512
  12281. wc_Sha512Free(NULL);
  12282. /* Set result to SUCCESS. */
  12283. ExpectTrue(1);
  12284. #endif
  12285. return EXPECT_RESULT();
  12286. } /* END test_wc_Sha512Free */
  12287. #ifdef WOLFSSL_SHA512
  12288. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12289. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12290. static int test_Sha512_Family_GetHash(int type )
  12291. {
  12292. EXPECT_DECLS;
  12293. int(*initFp)(wc_Sha512*);
  12294. int(*ghashFp)(wc_Sha512*, byte*);
  12295. wc_Sha512 sha512;
  12296. byte hash1[WC_SHA512_DIGEST_SIZE];
  12297. if (type == WC_HASH_TYPE_SHA512) {
  12298. initFp = wc_InitSha512;
  12299. ghashFp = wc_Sha512GetHash;
  12300. }
  12301. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12302. #if !defined(WOLFSSL_NOSHA512_224)
  12303. else if (type == WC_HASH_TYPE_SHA512_224) {
  12304. initFp = wc_InitSha512_224;
  12305. ghashFp = wc_Sha512_224GetHash;
  12306. }
  12307. #endif
  12308. #if !defined(WOLFSSL_NOSHA512_256)
  12309. else if (type == WC_HASH_TYPE_SHA512_256) {
  12310. initFp = wc_InitSha512_256;
  12311. ghashFp = wc_Sha512_256GetHash;
  12312. }
  12313. #endif
  12314. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12315. else {
  12316. initFp = NULL;
  12317. ghashFp = NULL;
  12318. }
  12319. if (initFp == NULL || ghashFp == NULL)
  12320. return TEST_FAIL;
  12321. ExpectIntEQ(initFp(&sha512), 0);
  12322. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12323. /* test bad arguments*/
  12324. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12325. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12326. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12327. wc_Sha512Free(&sha512);
  12328. return EXPECT_RESULT();
  12329. }
  12330. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12331. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12332. #endif /* WOLFSSL_SHA512 */
  12333. /*
  12334. * Unit test function for wc_Sha512GetHash()
  12335. */
  12336. static int test_wc_Sha512GetHash(void)
  12337. {
  12338. EXPECT_DECLS;
  12339. #ifdef WOLFSSL_SHA512
  12340. wc_Sha512 sha512;
  12341. byte hash1[WC_SHA512_DIGEST_SIZE];
  12342. /* Initialize */
  12343. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12344. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12345. /* test bad arguments*/
  12346. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12347. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12348. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12349. wc_Sha512Free(&sha512);
  12350. #endif
  12351. return EXPECT_RESULT();
  12352. } /* END test_wc_Sha512GetHash */
  12353. /*
  12354. * Unit test function for wc_Sha512Copy()
  12355. */
  12356. static int test_wc_Sha512Copy(void)
  12357. {
  12358. EXPECT_DECLS;
  12359. #ifdef WOLFSSL_SHA512
  12360. wc_Sha512 sha512;
  12361. wc_Sha512 temp;
  12362. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12363. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12364. /* Initialize */
  12365. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12366. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12367. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12368. /* test bad arguments*/
  12369. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12370. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12371. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12372. wc_Sha512Free(&sha512);
  12373. wc_Sha512Free(&temp);
  12374. #endif
  12375. return EXPECT_RESULT();
  12376. } /* END test_wc_Sha512Copy */
  12377. static int test_wc_InitSha512_224(void)
  12378. {
  12379. EXPECT_DECLS;
  12380. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12381. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12382. wc_Sha512 sha512;
  12383. /* Test good arg. */
  12384. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12385. /* Test bad arg. */
  12386. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12387. wc_Sha512_224Free(&sha512);
  12388. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12389. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12390. return EXPECT_RESULT();
  12391. }
  12392. static int test_wc_Sha512_224Update(void)
  12393. {
  12394. EXPECT_DECLS;
  12395. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12396. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12397. wc_Sha512 sha512;
  12398. byte hash[WC_SHA512_DIGEST_SIZE];
  12399. testVector a, c;
  12400. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12401. /* Input. */
  12402. a.input = "a";
  12403. a.inLen = XSTRLEN(a.input);
  12404. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12405. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12406. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12407. 0);
  12408. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12409. /* Update input. */
  12410. a.input = "abc";
  12411. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12412. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12413. a.inLen = XSTRLEN(a.input);
  12414. a.outLen = XSTRLEN(a.output);
  12415. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12416. 0);
  12417. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12418. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12419. c.input = NULL;
  12420. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12421. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12422. BAD_FUNC_ARG);
  12423. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12424. BAD_FUNC_ARG);
  12425. wc_Sha512_224Free(&sha512);
  12426. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12427. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12428. return EXPECT_RESULT();
  12429. }
  12430. static int test_wc_Sha512_224Final(void)
  12431. {
  12432. EXPECT_DECLS;
  12433. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12434. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12435. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12436. TEST_SUCCESS);
  12437. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12438. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12439. return EXPECT_RESULT();
  12440. }
  12441. static int test_wc_Sha512_224GetFlags(void)
  12442. {
  12443. EXPECT_DECLS;
  12444. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12445. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12446. wc_Sha512 sha512;
  12447. wc_Sha512 copy;
  12448. word32 flags = 0;
  12449. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12450. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12451. /* Initialize */
  12452. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12453. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12454. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12455. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12456. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12457. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12458. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12459. wc_Sha512_224Free(&copy);
  12460. wc_Sha512_224Free(&sha512);
  12461. #endif
  12462. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12463. return EXPECT_RESULT();
  12464. }
  12465. static int test_wc_Sha512_224FinalRaw(void)
  12466. {
  12467. EXPECT_DECLS;
  12468. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12469. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12470. !defined(WOLFSSL_NO_HASH_RAW)
  12471. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12472. TEST_SUCCESS);
  12473. #endif
  12474. return EXPECT_RESULT();
  12475. }
  12476. static int test_wc_Sha512_224Free(void)
  12477. {
  12478. EXPECT_DECLS;
  12479. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12480. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12481. wc_Sha512_224Free(NULL);
  12482. /* Set result to SUCCESS. */
  12483. ExpectTrue(1);
  12484. #endif
  12485. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12486. return EXPECT_RESULT();
  12487. }
  12488. static int test_wc_Sha512_224GetHash(void)
  12489. {
  12490. EXPECT_DECLS;
  12491. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12492. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12493. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12494. TEST_SUCCESS);
  12495. #endif
  12496. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12497. return EXPECT_RESULT();
  12498. }
  12499. static int test_wc_Sha512_224Copy(void)
  12500. {
  12501. EXPECT_DECLS;
  12502. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12503. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12504. wc_Sha512 sha512;
  12505. wc_Sha512 temp;
  12506. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12507. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12508. /* Initialize */
  12509. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12510. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12511. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12512. /* test bad arguments*/
  12513. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12514. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12515. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12516. wc_Sha512_224Free(&sha512);
  12517. wc_Sha512_224Free(&temp);
  12518. #endif
  12519. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12520. return EXPECT_RESULT();
  12521. }
  12522. static int test_wc_InitSha512_256(void)
  12523. {
  12524. EXPECT_DECLS;
  12525. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12526. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12527. wc_Sha512 sha512;
  12528. /* Test good arg. */
  12529. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12530. /* Test bad arg. */
  12531. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12532. wc_Sha512_256Free(&sha512);
  12533. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12534. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12535. return EXPECT_RESULT();
  12536. }
  12537. static int test_wc_Sha512_256Update(void)
  12538. {
  12539. EXPECT_DECLS;
  12540. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12541. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12542. wc_Sha512 sha512;
  12543. byte hash[WC_SHA512_DIGEST_SIZE];
  12544. testVector a, c;
  12545. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12546. /* Input. */
  12547. a.input = "a";
  12548. a.inLen = XSTRLEN(a.input);
  12549. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12550. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12551. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12552. 0);
  12553. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12554. /* Update input. */
  12555. a.input = "abc";
  12556. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12557. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12558. "\x07\xe7\xaf\x23";
  12559. a.inLen = XSTRLEN(a.input);
  12560. a.outLen = XSTRLEN(a.output);
  12561. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12562. 0);
  12563. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12564. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12565. c.input = NULL;
  12566. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12567. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12568. BAD_FUNC_ARG);
  12569. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12570. BAD_FUNC_ARG);
  12571. wc_Sha512_256Free(&sha512);
  12572. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12573. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12574. return EXPECT_RESULT();
  12575. }
  12576. static int test_wc_Sha512_256Final(void)
  12577. {
  12578. EXPECT_DECLS;
  12579. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12580. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12581. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12582. TEST_SUCCESS);
  12583. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12584. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12585. return EXPECT_RESULT();
  12586. }
  12587. static int test_wc_Sha512_256GetFlags(void)
  12588. {
  12589. EXPECT_DECLS;
  12590. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12591. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12592. wc_Sha512 sha512, copy;
  12593. word32 flags = 0;
  12594. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12595. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12596. /* Initialize */
  12597. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12598. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12599. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12600. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12601. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12602. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12603. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12604. wc_Sha512_256Free(&sha512);
  12605. #endif
  12606. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12607. return EXPECT_RESULT();
  12608. }
  12609. static int test_wc_Sha512_256FinalRaw(void)
  12610. {
  12611. EXPECT_DECLS;
  12612. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12613. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12614. !defined(WOLFSSL_NO_HASH_RAW)
  12615. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12616. TEST_SUCCESS);
  12617. #endif
  12618. return EXPECT_RESULT();
  12619. }
  12620. static int test_wc_Sha512_256Free(void)
  12621. {
  12622. EXPECT_DECLS;
  12623. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12624. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12625. wc_Sha512_256Free(NULL);
  12626. /* Set result to SUCCESS. */
  12627. ExpectTrue(1);
  12628. #endif
  12629. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12630. return EXPECT_RESULT();
  12631. }
  12632. static int test_wc_Sha512_256GetHash(void)
  12633. {
  12634. EXPECT_DECLS;
  12635. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12636. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12637. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12638. TEST_SUCCESS);
  12639. #endif
  12640. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12641. return EXPECT_RESULT();
  12642. }
  12643. static int test_wc_Sha512_256Copy(void)
  12644. {
  12645. EXPECT_DECLS;
  12646. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12647. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12648. wc_Sha512 sha512;
  12649. wc_Sha512 temp;
  12650. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12651. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12652. /* Initialize */
  12653. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12654. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12655. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12656. /* test bad arguments*/
  12657. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12658. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12659. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12660. wc_Sha512_256Free(&sha512);
  12661. wc_Sha512_256Free(&temp);
  12662. #endif
  12663. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12664. return EXPECT_RESULT();
  12665. }
  12666. /*
  12667. * Testing wc_InitSha384()
  12668. */
  12669. static int test_wc_InitSha384(void)
  12670. {
  12671. EXPECT_DECLS;
  12672. #ifdef WOLFSSL_SHA384
  12673. wc_Sha384 sha384;
  12674. /* Test good arg. */
  12675. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12676. /* Test bad arg. */
  12677. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12678. wc_Sha384Free(&sha384);
  12679. #endif
  12680. return EXPECT_RESULT();
  12681. } /* END test_wc_InitSha384 */
  12682. /*
  12683. * test wc_Sha384Update()
  12684. */
  12685. static int test_wc_Sha384Update(void)
  12686. {
  12687. EXPECT_DECLS;
  12688. #ifdef WOLFSSL_SHA384
  12689. wc_Sha384 sha384;
  12690. byte hash[WC_SHA384_DIGEST_SIZE];
  12691. testVector a, b, c;
  12692. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12693. /* Input */
  12694. a.input = "a";
  12695. a.inLen = XSTRLEN(a.input);
  12696. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12697. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12698. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12699. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12700. /* Update input. */
  12701. a.input = "abc";
  12702. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12703. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12704. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12705. "\xc8\x25\xa7";
  12706. a.inLen = XSTRLEN(a.input);
  12707. a.outLen = XSTRLEN(a.output);
  12708. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12709. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12710. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12711. /* Pass in bad values. */
  12712. b.input = NULL;
  12713. b.inLen = 0;
  12714. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12715. c.input = NULL;
  12716. c.inLen = WC_SHA384_DIGEST_SIZE;
  12717. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12718. BAD_FUNC_ARG);
  12719. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12720. BAD_FUNC_ARG);
  12721. wc_Sha384Free(&sha384);
  12722. #endif
  12723. return EXPECT_RESULT();
  12724. } /* END test_wc_Sha384Update */
  12725. /*
  12726. * Unit test function for wc_Sha384Final();
  12727. */
  12728. static int test_wc_Sha384Final(void)
  12729. {
  12730. EXPECT_DECLS;
  12731. #ifdef WOLFSSL_SHA384
  12732. wc_Sha384 sha384;
  12733. byte* hash_test[3];
  12734. byte hash1[WC_SHA384_DIGEST_SIZE];
  12735. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12736. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12737. int times, i;
  12738. /* Initialize */
  12739. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12740. hash_test[0] = hash1;
  12741. hash_test[1] = hash2;
  12742. hash_test[2] = hash3;
  12743. times = sizeof(hash_test) / sizeof(byte*);
  12744. /* Good test args. */
  12745. for (i = 0; i < times; i++) {
  12746. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12747. }
  12748. /* Test bad args. */
  12749. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12750. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12751. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12752. wc_Sha384Free(&sha384);
  12753. #endif
  12754. return EXPECT_RESULT();
  12755. } /* END test_wc_Sha384Final */
  12756. /*
  12757. * Unit test function for wc_Sha384GetFlags()
  12758. */
  12759. static int test_wc_Sha384GetFlags(void)
  12760. {
  12761. EXPECT_DECLS;
  12762. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12763. wc_Sha384 sha384;
  12764. word32 flags = 0;
  12765. /* Initialize */
  12766. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12767. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12768. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12769. wc_Sha384Free(&sha384);
  12770. #endif
  12771. return EXPECT_RESULT();
  12772. } /* END test_wc_Sha384GetFlags */
  12773. /*
  12774. * Unit test function for wc_Sha384FinalRaw()
  12775. */
  12776. static int test_wc_Sha384FinalRaw(void)
  12777. {
  12778. EXPECT_DECLS;
  12779. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12780. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12781. !defined(WOLFSSL_NO_HASH_RAW)
  12782. wc_Sha384 sha384;
  12783. byte* hash_test[3];
  12784. byte hash1[WC_SHA384_DIGEST_SIZE];
  12785. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12786. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12787. int times, i;
  12788. /* Initialize */
  12789. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12790. hash_test[0] = hash1;
  12791. hash_test[1] = hash2;
  12792. hash_test[2] = hash3;
  12793. times = sizeof(hash_test) / sizeof(byte*);
  12794. /* Good test args. */
  12795. for (i = 0; i < times; i++) {
  12796. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12797. }
  12798. /* Test bad args. */
  12799. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12800. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12801. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12802. wc_Sha384Free(&sha384);
  12803. #endif
  12804. return EXPECT_RESULT();
  12805. } /* END test_wc_Sha384FinalRaw */
  12806. /*
  12807. * Unit test function for wc_Sha384Free()
  12808. */
  12809. static int test_wc_Sha384Free(void)
  12810. {
  12811. EXPECT_DECLS;
  12812. #ifdef WOLFSSL_SHA384
  12813. wc_Sha384Free(NULL);
  12814. /* Set result to SUCCESS. */
  12815. ExpectTrue(1);
  12816. #endif
  12817. return EXPECT_RESULT();
  12818. } /* END test_wc_Sha384Free */
  12819. /*
  12820. * Unit test function for wc_Sha384GetHash()
  12821. */
  12822. static int test_wc_Sha384GetHash(void)
  12823. {
  12824. EXPECT_DECLS;
  12825. #ifdef WOLFSSL_SHA384
  12826. wc_Sha384 sha384;
  12827. byte hash1[WC_SHA384_DIGEST_SIZE];
  12828. /* Initialize */
  12829. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12830. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12831. /* test bad arguments*/
  12832. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12833. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12834. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12835. wc_Sha384Free(&sha384);
  12836. #endif
  12837. return EXPECT_RESULT();
  12838. } /* END test_wc_Sha384GetHash */
  12839. /*
  12840. * Unit test function for wc_Sha384Copy()
  12841. */
  12842. static int test_wc_Sha384Copy(void)
  12843. {
  12844. EXPECT_DECLS;
  12845. #ifdef WOLFSSL_SHA384
  12846. wc_Sha384 sha384;
  12847. wc_Sha384 temp;
  12848. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12849. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12850. /* Initialize */
  12851. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12852. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12853. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12854. /* test bad arguments*/
  12855. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12856. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12857. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12858. wc_Sha384Free(&sha384);
  12859. wc_Sha384Free(&temp);
  12860. #endif
  12861. return EXPECT_RESULT();
  12862. } /* END test_wc_Sha384Copy */
  12863. /*
  12864. * Testing wc_InitSha224();
  12865. */
  12866. static int test_wc_InitSha224(void)
  12867. {
  12868. EXPECT_DECLS;
  12869. #ifdef WOLFSSL_SHA224
  12870. wc_Sha224 sha224;
  12871. /* Test good arg. */
  12872. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12873. /* Test bad arg. */
  12874. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12875. wc_Sha224Free(&sha224);
  12876. #endif
  12877. return EXPECT_RESULT();
  12878. } /* END test_wc_InitSha224 */
  12879. /*
  12880. * Unit test on wc_Sha224Update
  12881. */
  12882. static int test_wc_Sha224Update(void)
  12883. {
  12884. EXPECT_DECLS;
  12885. #ifdef WOLFSSL_SHA224
  12886. wc_Sha224 sha224;
  12887. byte hash[WC_SHA224_DIGEST_SIZE];
  12888. testVector a, b, c;
  12889. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12890. /* Input. */
  12891. a.input = "a";
  12892. a.inLen = XSTRLEN(a.input);
  12893. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12894. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12895. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12896. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12897. /* Update input. */
  12898. a.input = "abc";
  12899. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12900. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12901. a.inLen = XSTRLEN(a.input);
  12902. a.outLen = XSTRLEN(a.output);
  12903. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12904. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12905. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12906. /* Pass in bad values. */
  12907. b.input = NULL;
  12908. b.inLen = 0;
  12909. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12910. c.input = NULL;
  12911. c.inLen = WC_SHA224_DIGEST_SIZE;
  12912. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12913. BAD_FUNC_ARG);
  12914. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12915. BAD_FUNC_ARG);
  12916. wc_Sha224Free(&sha224);
  12917. #endif
  12918. return EXPECT_RESULT();
  12919. } /* END test_wc_Sha224Update */
  12920. /*
  12921. * Unit test for wc_Sha224Final();
  12922. */
  12923. static int test_wc_Sha224Final(void)
  12924. {
  12925. EXPECT_DECLS;
  12926. #ifdef WOLFSSL_SHA224
  12927. wc_Sha224 sha224;
  12928. byte* hash_test[3];
  12929. byte hash1[WC_SHA224_DIGEST_SIZE];
  12930. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12931. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12932. int times, i;
  12933. /* Initialize */
  12934. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12935. hash_test[0] = hash1;
  12936. hash_test[1] = hash2;
  12937. hash_test[2] = hash3;
  12938. times = sizeof(hash_test) / sizeof(byte*);
  12939. /* Good test args. */
  12940. /* Testing oversized buffers. */
  12941. for (i = 0; i < times; i++) {
  12942. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12943. }
  12944. /* Test bad args. */
  12945. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12946. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12947. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12948. wc_Sha224Free(&sha224);
  12949. #endif
  12950. return EXPECT_RESULT();
  12951. } /* END test_wc_Sha224Final */
  12952. /*
  12953. * Unit test function for wc_Sha224SetFlags()
  12954. */
  12955. static int test_wc_Sha224SetFlags(void)
  12956. {
  12957. EXPECT_DECLS;
  12958. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12959. wc_Sha224 sha224;
  12960. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12961. /* Initialize */
  12962. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12963. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12964. flags = 0;
  12965. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12966. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12967. wc_Sha224Free(&sha224);
  12968. #endif
  12969. return EXPECT_RESULT();
  12970. } /* END test_wc_Sha224SetFlags */
  12971. /*
  12972. * Unit test function for wc_Sha224GetFlags()
  12973. */
  12974. static int test_wc_Sha224GetFlags(void)
  12975. {
  12976. EXPECT_DECLS;
  12977. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12978. wc_Sha224 sha224;
  12979. word32 flags = 0;
  12980. /* Initialize */
  12981. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12982. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12983. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12984. wc_Sha224Free(&sha224);
  12985. #endif
  12986. return EXPECT_RESULT();
  12987. } /* END test_wc_Sha224GetFlags */
  12988. /*
  12989. * Unit test function for wc_Sha224Free()
  12990. */
  12991. static int test_wc_Sha224Free(void)
  12992. {
  12993. EXPECT_DECLS;
  12994. #ifdef WOLFSSL_SHA224
  12995. wc_Sha224Free(NULL);
  12996. /* Set result to SUCCESS. */
  12997. ExpectTrue(1);
  12998. #endif
  12999. return EXPECT_RESULT();
  13000. } /* END test_wc_Sha224Free */
  13001. /*
  13002. * Unit test function for wc_Sha224GetHash()
  13003. */
  13004. static int test_wc_Sha224GetHash(void)
  13005. {
  13006. EXPECT_DECLS;
  13007. #ifdef WOLFSSL_SHA224
  13008. wc_Sha224 sha224;
  13009. byte hash1[WC_SHA224_DIGEST_SIZE];
  13010. /* Initialize */
  13011. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13012. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13013. /* test bad arguments*/
  13014. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13015. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13016. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13017. wc_Sha224Free(&sha224);
  13018. #endif
  13019. return EXPECT_RESULT();
  13020. } /* END test_wc_Sha224GetHash */
  13021. /*
  13022. * Unit test function for wc_Sha224Copy()
  13023. */
  13024. static int test_wc_Sha224Copy(void)
  13025. {
  13026. EXPECT_DECLS;
  13027. #ifdef WOLFSSL_SHA224
  13028. wc_Sha224 sha224;
  13029. wc_Sha224 temp;
  13030. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13031. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13032. /* Initialize */
  13033. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13034. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13035. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13036. /* test bad arguments*/
  13037. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13038. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13039. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13040. wc_Sha224Free(&sha224);
  13041. wc_Sha224Free(&temp);
  13042. #endif
  13043. return EXPECT_RESULT();
  13044. } /* END test_wc_Sha224Copy */
  13045. /*
  13046. * Testing wc_InitRipeMd()
  13047. */
  13048. static int test_wc_InitRipeMd(void)
  13049. {
  13050. EXPECT_DECLS;
  13051. #ifdef WOLFSSL_RIPEMD
  13052. RipeMd ripemd;
  13053. /* Test good arg. */
  13054. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13055. /* Test bad arg. */
  13056. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13057. #endif
  13058. return EXPECT_RESULT();
  13059. } /* END test_wc_InitRipeMd */
  13060. /*
  13061. * Testing wc_RipeMdUpdate()
  13062. */
  13063. static int test_wc_RipeMdUpdate(void)
  13064. {
  13065. EXPECT_DECLS;
  13066. #ifdef WOLFSSL_RIPEMD
  13067. RipeMd ripemd;
  13068. byte hash[RIPEMD_DIGEST_SIZE];
  13069. testVector a, b, c;
  13070. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13071. /* Input */
  13072. a.input = "a";
  13073. a.inLen = XSTRLEN(a.input);
  13074. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13075. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13076. /* Update input. */
  13077. a.input = "abc";
  13078. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13079. "\xb0\x87\xf1\x5a\x0b\xfc";
  13080. a.inLen = XSTRLEN(a.input);
  13081. a.outLen = XSTRLEN(a.output);
  13082. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13083. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13084. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13085. /* Pass in bad values. */
  13086. b.input = NULL;
  13087. b.inLen = 0;
  13088. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13089. c.input = NULL;
  13090. c.inLen = RIPEMD_DIGEST_SIZE;
  13091. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13092. BAD_FUNC_ARG);
  13093. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13094. BAD_FUNC_ARG);
  13095. #endif
  13096. return EXPECT_RESULT();
  13097. } /* END test_wc_RipeMdUdpate */
  13098. /*
  13099. * Unit test function for wc_RipeMdFinal()
  13100. */
  13101. static int test_wc_RipeMdFinal(void)
  13102. {
  13103. EXPECT_DECLS;
  13104. #ifdef WOLFSSL_RIPEMD
  13105. RipeMd ripemd;
  13106. byte* hash_test[3];
  13107. byte hash1[RIPEMD_DIGEST_SIZE];
  13108. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13109. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13110. int times, i;
  13111. /* Initialize */
  13112. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13113. hash_test[0] = hash1;
  13114. hash_test[1] = hash2;
  13115. hash_test[2] = hash3;
  13116. times = sizeof(hash_test) / sizeof(byte*);
  13117. /* Testing oversized buffers. */
  13118. for (i = 0; i < times; i++) {
  13119. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13120. }
  13121. /* Test bad args. */
  13122. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13123. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13124. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13125. #endif
  13126. return EXPECT_RESULT();
  13127. } /* END test_wc_RipeMdFinal */
  13128. /*
  13129. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13130. * wc_InitSha3_512
  13131. */
  13132. static int test_wc_InitSha3(void)
  13133. {
  13134. EXPECT_DECLS;
  13135. #if defined(WOLFSSL_SHA3)
  13136. wc_Sha3 sha3;
  13137. (void)sha3;
  13138. #if !defined(WOLFSSL_NOSHA3_224)
  13139. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13140. /* Test bad args. */
  13141. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13142. wc_Sha3_224_Free(&sha3);
  13143. #endif /* NOSHA3_224 */
  13144. #if !defined(WOLFSSL_NOSHA3_256)
  13145. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13146. /* Test bad args. */
  13147. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13148. wc_Sha3_256_Free(&sha3);
  13149. #endif /* NOSHA3_256 */
  13150. #if !defined(WOLFSSL_NOSHA3_384)
  13151. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13152. /* Test bad args. */
  13153. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13154. wc_Sha3_384_Free(&sha3);
  13155. #endif /* NOSHA3_384 */
  13156. #if !defined(WOLFSSL_NOSHA3_512)
  13157. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13158. /* Test bad args. */
  13159. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13160. wc_Sha3_512_Free(&sha3);
  13161. #endif /* NOSHA3_512 */
  13162. #endif
  13163. return EXPECT_RESULT();
  13164. } /* END test_wc_InitSha3 */
  13165. /*
  13166. * Testing wc_Sha3_Update()
  13167. */
  13168. static int testing_wc_Sha3_Update(void)
  13169. {
  13170. EXPECT_DECLS;
  13171. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13172. !defined(WOLFSSL_AFALG_XILINX)
  13173. wc_Sha3 sha3;
  13174. byte msg[] = "Everybody's working for the weekend.";
  13175. byte msg2[] = "Everybody gets Friday off.";
  13176. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13177. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13178. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13179. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13180. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13181. word32 msglen = sizeof(msg) - 1;
  13182. word32 msg2len = sizeof(msg2);
  13183. word32 msgCmplen = sizeof(msgCmp);
  13184. #if !defined(WOLFSSL_NOSHA3_224)
  13185. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13186. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13187. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13188. ExpectTrue(sha3.i == msglen);
  13189. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13190. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13191. /* Pass bad args. */
  13192. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13193. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13194. wc_Sha3_224_Free(&sha3);
  13195. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13196. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13197. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13198. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13199. wc_Sha3_224_Free(&sha3);
  13200. #endif /* SHA3_224 */
  13201. #if !defined(WOLFSSL_NOSHA3_256)
  13202. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13203. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13204. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13205. ExpectTrue(sha3.i == msglen);
  13206. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13207. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13208. /* Pass bad args. */
  13209. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13210. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13211. wc_Sha3_256_Free(&sha3);
  13212. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13213. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13214. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13215. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13216. wc_Sha3_256_Free(&sha3);
  13217. #endif /* SHA3_256 */
  13218. #if !defined(WOLFSSL_NOSHA3_384)
  13219. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13220. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13221. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13222. ExpectTrue(sha3.i == msglen);
  13223. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13224. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13225. /* Pass bad args. */
  13226. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13227. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13228. wc_Sha3_384_Free(&sha3);
  13229. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13230. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13231. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13232. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13233. wc_Sha3_384_Free(&sha3);
  13234. #endif /* SHA3_384 */
  13235. #if !defined(WOLFSSL_NOSHA3_512)
  13236. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13237. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13238. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13239. ExpectTrue(sha3.i == msglen);
  13240. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13241. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13242. /* Pass bad args. */
  13243. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13244. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13245. wc_Sha3_512_Free(&sha3);
  13246. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13247. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13248. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13249. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13250. wc_Sha3_512_Free(&sha3);
  13251. #endif /* SHA3_512 */
  13252. #endif /* WOLFSSL_SHA3 */
  13253. return EXPECT_RESULT();
  13254. } /* END testing_wc_Sha3_Update */
  13255. /*
  13256. * Testing wc_Sha3_224_Final()
  13257. */
  13258. static int test_wc_Sha3_224_Final(void)
  13259. {
  13260. EXPECT_DECLS;
  13261. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13262. wc_Sha3 sha3;
  13263. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13264. "nopnopq";
  13265. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13266. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13267. "\x64\xea\xd0\xfc\xce\x33";
  13268. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13269. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13270. /* Init stack variables. */
  13271. XMEMSET(hash, 0, sizeof(hash));
  13272. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13273. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13274. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13275. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13276. /* Test bad args. */
  13277. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13278. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13279. wc_Sha3_224_Free(&sha3);
  13280. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13281. /* Init stack variables. */
  13282. XMEMSET(hash, 0, sizeof(hash));
  13283. XMEMSET(hashRet, 0, sizeof(hashRet));
  13284. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13285. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13286. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13287. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13288. /* Test bad args. */
  13289. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13290. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13291. wc_Sha3_224_Free(&sha3);
  13292. #endif
  13293. return EXPECT_RESULT();
  13294. } /* END test_wc_Sha3_224_Final */
  13295. /*
  13296. * Testing wc_Sha3_256_Final()
  13297. */
  13298. static int test_wc_Sha3_256_Final(void)
  13299. {
  13300. EXPECT_DECLS;
  13301. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13302. wc_Sha3 sha3;
  13303. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13304. "nopnopq";
  13305. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13306. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13307. "\xdd\x97\x49\x6d\x33\x76";
  13308. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13309. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13310. /* Init stack variables. */
  13311. XMEMSET(hash, 0, sizeof(hash));
  13312. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13313. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13314. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13315. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13316. /* Test bad args. */
  13317. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13318. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13319. wc_Sha3_256_Free(&sha3);
  13320. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13321. /* Init stack variables. */
  13322. XMEMSET(hash, 0, sizeof(hash));
  13323. XMEMSET(hashRet, 0, sizeof(hashRet));
  13324. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13325. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13326. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13327. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13328. /* Test bad args. */
  13329. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13330. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13331. wc_Sha3_256_Free(&sha3);
  13332. #endif
  13333. return EXPECT_RESULT();
  13334. } /* END test_wc_Sha3_256_Final */
  13335. /*
  13336. * Testing wc_Sha3_384_Final()
  13337. */
  13338. static int test_wc_Sha3_384_Final(void)
  13339. {
  13340. EXPECT_DECLS;
  13341. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13342. wc_Sha3 sha3;
  13343. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13344. "nopnopq";
  13345. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13346. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13347. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13348. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13349. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13350. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13351. /* Init stack variables. */
  13352. XMEMSET(hash, 0, sizeof(hash));
  13353. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13354. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13355. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13356. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13357. /* Test bad args. */
  13358. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13359. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13360. wc_Sha3_384_Free(&sha3);
  13361. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13362. /* Init stack variables. */
  13363. XMEMSET(hash, 0, sizeof(hash));
  13364. XMEMSET(hashRet, 0, sizeof(hashRet));
  13365. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13366. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13367. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13368. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13369. /* Test bad args. */
  13370. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13371. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13372. wc_Sha3_384_Free(&sha3);
  13373. #endif
  13374. return EXPECT_RESULT();
  13375. } /* END test_wc_Sha3_384_Final */
  13376. /*
  13377. * Testing wc_Sha3_512_Final()
  13378. */
  13379. static int test_wc_Sha3_512_Final(void)
  13380. {
  13381. EXPECT_DECLS;
  13382. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13383. !defined(WOLFSSL_NOSHA3_384)
  13384. wc_Sha3 sha3;
  13385. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13386. "nopnopq";
  13387. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13388. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13389. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13390. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13391. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13392. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13393. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13394. /* Init stack variables. */
  13395. XMEMSET(hash, 0, sizeof(hash));
  13396. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13397. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13398. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13399. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13400. /* Test bad args. */
  13401. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13402. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13403. wc_Sha3_512_Free(&sha3);
  13404. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13405. /* Init stack variables. */
  13406. XMEMSET(hash, 0, sizeof(hash));
  13407. XMEMSET(hashRet, 0, sizeof(hashRet));
  13408. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13409. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13410. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13411. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13412. /* Test bad args. */
  13413. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13414. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13415. wc_Sha3_512_Free(&sha3);
  13416. #endif
  13417. return EXPECT_RESULT();
  13418. } /* END test_wc_Sha3_512_Final */
  13419. /*
  13420. * Testing wc_Sha3_224_Copy()
  13421. */
  13422. static int test_wc_Sha3_224_Copy(void)
  13423. {
  13424. EXPECT_DECLS;
  13425. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13426. wc_Sha3 sha3, sha3Cpy;
  13427. const char* msg = TEST_STRING;
  13428. word32 msglen = (word32)TEST_STRING_SZ;
  13429. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13430. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13431. XMEMSET(hash, 0, sizeof(hash));
  13432. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13433. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13434. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13435. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13436. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13437. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13438. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13439. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13440. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13441. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13442. /* Test bad args. */
  13443. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13444. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13445. wc_Sha3_224_Free(&sha3);
  13446. wc_Sha3_224_Free(&sha3Cpy);
  13447. #endif
  13448. return EXPECT_RESULT();
  13449. } /* END test_wc_Sha3_224_Copy */
  13450. /*
  13451. * Testing wc_Sha3_256_Copy()
  13452. */
  13453. static int test_wc_Sha3_256_Copy(void)
  13454. {
  13455. EXPECT_DECLS;
  13456. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13457. wc_Sha3 sha3, sha3Cpy;
  13458. const char* msg = TEST_STRING;
  13459. word32 msglen = (word32)TEST_STRING_SZ;
  13460. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13461. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13462. XMEMSET(hash, 0, sizeof(hash));
  13463. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13464. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13465. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13466. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13467. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13468. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13469. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13470. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13471. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13472. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13473. /* Test bad args. */
  13474. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13475. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13476. wc_Sha3_256_Free(&sha3);
  13477. wc_Sha3_256_Free(&sha3Cpy);
  13478. #endif
  13479. return EXPECT_RESULT();
  13480. } /* END test_wc_Sha3_256_Copy */
  13481. /*
  13482. * Testing wc_Sha3_384_Copy()
  13483. */
  13484. static int test_wc_Sha3_384_Copy(void)
  13485. {
  13486. EXPECT_DECLS;
  13487. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13488. wc_Sha3 sha3, sha3Cpy;
  13489. const char* msg = TEST_STRING;
  13490. word32 msglen = (word32)TEST_STRING_SZ;
  13491. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13492. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13493. XMEMSET(hash, 0, sizeof(hash));
  13494. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13495. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13496. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13497. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13498. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13499. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13500. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13501. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13502. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13503. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13504. /* Test bad args. */
  13505. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13506. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13507. wc_Sha3_384_Free(&sha3);
  13508. wc_Sha3_384_Free(&sha3Cpy);
  13509. #endif
  13510. return EXPECT_RESULT();
  13511. } /* END test_wc_Sha3_384_Copy */
  13512. /*
  13513. * Testing wc_Sha3_512_Copy()
  13514. */
  13515. static int test_wc_Sha3_512_Copy(void)
  13516. {
  13517. EXPECT_DECLS;
  13518. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13519. wc_Sha3 sha3, sha3Cpy;
  13520. const char* msg = TEST_STRING;
  13521. word32 msglen = (word32)TEST_STRING_SZ;
  13522. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13523. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13524. XMEMSET(hash, 0, sizeof(hash));
  13525. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13526. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13527. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13528. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13529. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13530. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13531. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13532. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13533. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13534. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13535. /* Test bad args. */
  13536. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13537. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13538. wc_Sha3_512_Free(&sha3);
  13539. wc_Sha3_512_Free(&sha3Cpy);
  13540. #endif
  13541. return EXPECT_RESULT();
  13542. } /* END test_wc_Sha3_512_Copy */
  13543. /*
  13544. * Unit test function for wc_Sha3_GetFlags()
  13545. */
  13546. static int test_wc_Sha3_GetFlags(void)
  13547. {
  13548. EXPECT_DECLS;
  13549. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13550. wc_Sha3 sha3;
  13551. word32 flags = 0;
  13552. /* Initialize */
  13553. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13554. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13555. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13556. wc_Sha3_224_Free(&sha3);
  13557. #endif
  13558. return EXPECT_RESULT();
  13559. } /* END test_wc_Sha3_GetFlags */
  13560. static int test_wc_InitShake256(void)
  13561. {
  13562. EXPECT_DECLS;
  13563. #ifdef WOLFSSL_SHAKE256
  13564. wc_Shake shake;
  13565. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13566. /* Test bad args. */
  13567. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13568. wc_Shake256_Free(&shake);
  13569. #endif
  13570. return EXPECT_RESULT();
  13571. }
  13572. static int testing_wc_Shake256_Update(void)
  13573. {
  13574. EXPECT_DECLS;
  13575. #ifdef WOLFSSL_SHAKE256
  13576. wc_Shake shake;
  13577. byte msg[] = "Everybody's working for the weekend.";
  13578. byte msg2[] = "Everybody gets Friday off.";
  13579. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13580. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13581. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13582. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13583. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13584. word32 msglen = sizeof(msg) - 1;
  13585. word32 msg2len = sizeof(msg2);
  13586. word32 msgCmplen = sizeof(msgCmp);
  13587. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13588. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13589. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13590. ExpectTrue(shake.i == msglen);
  13591. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13592. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13593. /* Pass bad args. */
  13594. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13595. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13596. wc_Shake256_Free(&shake);
  13597. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13598. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13599. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13600. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13601. wc_Shake256_Free(&shake);
  13602. #endif /* WOLFSSL_SHAKE256 */
  13603. return EXPECT_RESULT();
  13604. }
  13605. static int test_wc_Shake256_Final(void)
  13606. {
  13607. EXPECT_DECLS;
  13608. #ifdef WOLFSSL_SHAKE256
  13609. wc_Shake shake;
  13610. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13611. "nopnopq";
  13612. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13613. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13614. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13615. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13616. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13617. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13618. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13619. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13620. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13621. byte hash[114];
  13622. /* Init stack variables. */
  13623. XMEMSET(hash, 0, sizeof(hash));
  13624. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13625. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13626. 0);
  13627. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13628. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13629. /* Test bad args. */
  13630. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13631. BAD_FUNC_ARG);
  13632. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13633. BAD_FUNC_ARG);
  13634. wc_Shake256_Free(&shake);
  13635. #endif
  13636. return EXPECT_RESULT();
  13637. }
  13638. /*
  13639. * Testing wc_Shake256_Copy()
  13640. */
  13641. static int test_wc_Shake256_Copy(void)
  13642. {
  13643. EXPECT_DECLS;
  13644. #ifdef WOLFSSL_SHAKE256
  13645. wc_Shake shake, shakeCpy;
  13646. const char* msg = TEST_STRING;
  13647. word32 msglen = (word32)TEST_STRING_SZ;
  13648. byte hash[144];
  13649. byte hashCpy[144];
  13650. word32 hashLen = sizeof(hash);
  13651. word32 hashLenCpy = sizeof(hashCpy);
  13652. XMEMSET(hash, 0, sizeof(hash));
  13653. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13654. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13655. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13656. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13657. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13658. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13659. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13660. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13661. /* Test bad args. */
  13662. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13663. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13664. wc_Shake256_Free(&shake);
  13665. wc_Shake256_Free(&shakeCpy);
  13666. #endif
  13667. return EXPECT_RESULT();
  13668. } /* END test_wc_Shake256_Copy */
  13669. /*
  13670. * Unit test function for wc_Shake256Hash()
  13671. */
  13672. static int test_wc_Shake256Hash(void)
  13673. {
  13674. EXPECT_DECLS;
  13675. #ifdef WOLFSSL_SHAKE256
  13676. const byte data[] = { /* Hello World */
  13677. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13678. 0x72,0x6c,0x64
  13679. };
  13680. word32 len = sizeof(data);
  13681. byte hash[144];
  13682. word32 hashLen = sizeof(hash);
  13683. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13684. #endif
  13685. return EXPECT_RESULT();
  13686. } /* END test_wc_Shake256Hash */
  13687. /*
  13688. * Testing wc_InitSm3(), wc_Sm3Free()
  13689. */
  13690. static int test_wc_InitSm3Free(void)
  13691. {
  13692. EXPECT_DECLS;
  13693. #ifdef WOLFSSL_SM3
  13694. wc_Sm3 sm3;
  13695. /* Invalid Parameters */
  13696. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13697. /* Valid Parameters */
  13698. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13699. wc_Sm3Free(NULL);
  13700. wc_Sm3Free(&sm3);
  13701. #endif
  13702. return EXPECT_RESULT();
  13703. } /* END test_wc_InitSm3 */
  13704. /*
  13705. * Testing wc_Sm3Update(), wc_Sm3Final()
  13706. */
  13707. static int test_wc_Sm3UpdateFinal(void)
  13708. {
  13709. EXPECT_DECLS;
  13710. #ifdef WOLFSSL_SM3
  13711. wc_Sm3 sm3;
  13712. byte data[WC_SM3_BLOCK_SIZE * 4];
  13713. byte hash[WC_SM3_DIGEST_SIZE];
  13714. byte calcHash[WC_SM3_DIGEST_SIZE];
  13715. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13716. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13717. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13718. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13719. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13720. };
  13721. word32 chunk;
  13722. word32 i;
  13723. XMEMSET(data, 0, sizeof(data));
  13724. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13725. /* Invalid Parameters */
  13726. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13727. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13728. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13729. /* Valid Parameters */
  13730. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13731. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13732. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13733. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13734. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13735. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13736. /* Ensure too many bytes for lengths. */
  13737. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13738. /* Invalid Parameters */
  13739. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13740. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13741. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13742. /* Valid Parameters */
  13743. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13744. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13745. /* Chunk tests. */
  13746. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13747. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13748. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13749. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13750. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13751. }
  13752. if (i < (word32)sizeof(data)) {
  13753. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13754. 0);
  13755. }
  13756. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13757. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13758. }
  13759. /* Not testing when the low 32-bit length overflows. */
  13760. wc_Sm3Free(&sm3);
  13761. #endif
  13762. return EXPECT_RESULT();
  13763. } /* END test_wc_Sm3Update */
  13764. /*
  13765. * Testing wc_Sm3GetHash()
  13766. */
  13767. static int test_wc_Sm3GetHash(void)
  13768. {
  13769. EXPECT_DECLS;
  13770. #ifdef WOLFSSL_SM3
  13771. wc_Sm3 sm3;
  13772. byte hash[WC_SM3_DIGEST_SIZE];
  13773. byte calcHash[WC_SM3_DIGEST_SIZE];
  13774. byte data[WC_SM3_BLOCK_SIZE];
  13775. XMEMSET(data, 0, sizeof(data));
  13776. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13777. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13778. /* Invalid Parameters */
  13779. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13780. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13781. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13782. /* Valid Parameters */
  13783. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13784. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13785. /* With update. */
  13786. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13787. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13788. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13789. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13790. wc_Sm3Free(&sm3);
  13791. #endif
  13792. return EXPECT_RESULT();
  13793. } /* END test_wc_Sm3Update */
  13794. /*
  13795. * Testing wc_Sm3Copy()
  13796. */
  13797. static int test_wc_Sm3Copy(void)
  13798. {
  13799. EXPECT_DECLS;
  13800. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13801. wc_Sm3 sm3;
  13802. wc_Sm3 sm3Copy;
  13803. byte hash[WC_SM3_DIGEST_SIZE];
  13804. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13805. byte data[WC_SM3_BLOCK_SIZE + 1];
  13806. int i;
  13807. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13808. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13809. /* Invalid Parameters */
  13810. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13811. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13812. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13813. /* Valid Parameters */
  13814. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13815. /* Ensure all parts of data updated during hashing are copied. */
  13816. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13817. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13818. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13819. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13820. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13821. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13822. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13823. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13824. }
  13825. wc_Sm3Free(&sm3Copy);
  13826. wc_Sm3Free(&sm3);
  13827. #endif
  13828. return EXPECT_RESULT();
  13829. } /* END test_wc_Sm3Copy */
  13830. /*
  13831. * Testing wc_Sm3FinalRaw()
  13832. */
  13833. static int test_wc_Sm3FinalRaw(void)
  13834. {
  13835. EXPECT_DECLS;
  13836. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13837. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13838. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13839. !defined(WOLFSSL_NO_HASH_RAW)
  13840. wc_Sm3 sm3;
  13841. byte hash1[WC_SM3_DIGEST_SIZE];
  13842. byte hash2[WC_SM3_DIGEST_SIZE];
  13843. byte hash3[WC_SM3_DIGEST_SIZE];
  13844. byte* hash_test[3] = { hash1, hash2, hash3 };
  13845. int times;
  13846. int i;
  13847. XMEMSET(&sm3, 0, sizeof(sm3));
  13848. /* Initialize */
  13849. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13850. /* Invalid Parameters */
  13851. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13852. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13853. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13854. times = sizeof(hash_test) / sizeof(byte*);
  13855. for (i = 0; i < times; i++) {
  13856. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13857. }
  13858. wc_Sm3Free(&sm3);
  13859. #endif
  13860. return EXPECT_RESULT();
  13861. } /* END test_wc_Sm3FinalRaw */
  13862. /*
  13863. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13864. */
  13865. static int test_wc_Sm3GetSetFlags(void)
  13866. {
  13867. EXPECT_DECLS;
  13868. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13869. wc_Sm3 sm3;
  13870. wc_Sm3 sm3Copy;
  13871. word32 flags = 0;
  13872. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13873. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13874. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13875. ExpectIntEQ(flags, 0);
  13876. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13877. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13878. ExpectIntEQ(flags, 0);
  13879. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13880. ExpectIntEQ(flags, 0);
  13881. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13882. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13883. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13884. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13885. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13886. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13887. wc_Sm3Free(&sm3Copy);
  13888. wc_Sm3Free(&sm3);
  13889. #endif
  13890. return EXPECT_RESULT();
  13891. } /* END test_wc_Sm3Update */
  13892. /*
  13893. * Testing wc_Sm3Hash()
  13894. */
  13895. static int test_wc_Sm3Hash(void)
  13896. {
  13897. EXPECT_DECLS;
  13898. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13899. byte data[WC_SM3_BLOCK_SIZE];
  13900. byte hash[WC_SM3_DIGEST_SIZE];
  13901. /* Invalid parameters. */
  13902. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13903. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13904. /* Valid parameters. */
  13905. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13906. #endif
  13907. return EXPECT_RESULT();
  13908. } /* END test_wc_Sm3Hash */
  13909. /*
  13910. * Test function for wc_HmacSetKey
  13911. */
  13912. static int test_wc_Md5HmacSetKey(void)
  13913. {
  13914. EXPECT_DECLS;
  13915. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13916. Hmac hmac;
  13917. int ret, times, itr;
  13918. const char* keys[]=
  13919. {
  13920. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13921. #ifndef HAVE_FIPS
  13922. "Jefe", /* smaller than minimum FIPS key size */
  13923. #endif
  13924. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13925. };
  13926. times = sizeof(keys) / sizeof(char*);
  13927. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13928. for (itr = 0; itr < times; itr++) {
  13929. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13930. (word32)XSTRLEN(keys[itr]));
  13931. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13932. wc_HmacFree(&hmac);
  13933. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13934. #else
  13935. ExpectIntEQ(ret, 0);
  13936. #endif
  13937. }
  13938. /* Bad args. */
  13939. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13940. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13941. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13942. BAD_FUNC_ARG);
  13943. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13944. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13945. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13946. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13947. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13948. #elif defined(HAVE_FIPS)
  13949. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13950. #else
  13951. ExpectIntEQ(ret, 0);
  13952. #endif
  13953. wc_HmacFree(&hmac);
  13954. #endif
  13955. return EXPECT_RESULT();
  13956. } /* END test_wc_Md5HmacSetKey */
  13957. /*
  13958. * testing wc_HmacSetKey() on wc_Sha hash.
  13959. */
  13960. static int test_wc_ShaHmacSetKey(void)
  13961. {
  13962. EXPECT_DECLS;
  13963. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13964. Hmac hmac;
  13965. int ret, times, itr;
  13966. const char* keys[]=
  13967. {
  13968. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13969. "\x0b\x0b\x0b",
  13970. #ifndef HAVE_FIPS
  13971. "Jefe", /* smaller than minimum FIPS key size */
  13972. #endif
  13973. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13974. "\xAA\xAA\xAA"
  13975. };
  13976. times = sizeof(keys) / sizeof(char*);
  13977. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13978. for (itr = 0; itr < times; itr++) {
  13979. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13980. (word32)XSTRLEN(keys[itr])), 0);
  13981. }
  13982. /* Bad args. */
  13983. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13984. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13985. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13986. BAD_FUNC_ARG);
  13987. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13988. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13989. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13990. #ifdef HAVE_FIPS
  13991. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13992. #else
  13993. ExpectIntEQ(ret, 0);
  13994. #endif
  13995. wc_HmacFree(&hmac);
  13996. #endif
  13997. return EXPECT_RESULT();
  13998. } /* END test_wc_ShaHmacSetKey() */
  13999. /*
  14000. * testing wc_HmacSetKey() on Sha224 hash.
  14001. */
  14002. static int test_wc_Sha224HmacSetKey(void)
  14003. {
  14004. EXPECT_DECLS;
  14005. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14006. Hmac hmac;
  14007. int ret, times, itr;
  14008. const char* keys[]=
  14009. {
  14010. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14011. "\x0b\x0b\x0b",
  14012. #ifndef HAVE_FIPS
  14013. "Jefe", /* smaller than minimum FIPS key size */
  14014. #endif
  14015. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14016. "\xAA\xAA\xAA"
  14017. };
  14018. times = sizeof(keys) / sizeof(char*);
  14019. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14020. for (itr = 0; itr < times; itr++) {
  14021. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14022. (word32)XSTRLEN(keys[itr])), 0);
  14023. }
  14024. /* Bad args. */
  14025. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14026. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14027. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14028. BAD_FUNC_ARG);
  14029. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14030. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14031. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14032. #ifdef HAVE_FIPS
  14033. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14034. #else
  14035. ExpectIntEQ(ret, 0);
  14036. #endif
  14037. wc_HmacFree(&hmac);
  14038. #endif
  14039. return EXPECT_RESULT();
  14040. } /* END test_wc_Sha224HmacSetKey() */
  14041. /*
  14042. * testing wc_HmacSetKey() on Sha256 hash
  14043. */
  14044. static int test_wc_Sha256HmacSetKey(void)
  14045. {
  14046. EXPECT_DECLS;
  14047. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14048. Hmac hmac;
  14049. int ret, times, itr;
  14050. const char* keys[]=
  14051. {
  14052. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14053. "\x0b\x0b\x0b",
  14054. #ifndef HAVE_FIPS
  14055. "Jefe", /* smaller than minimum FIPS key size */
  14056. #endif
  14057. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14058. "\xAA\xAA\xAA"
  14059. };
  14060. times = sizeof(keys) / sizeof(char*);
  14061. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14062. for (itr = 0; itr < times; itr++) {
  14063. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14064. (word32)XSTRLEN(keys[itr])), 0);
  14065. }
  14066. /* Bad args. */
  14067. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14068. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14069. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14070. BAD_FUNC_ARG);
  14071. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14072. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14073. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14074. #ifdef HAVE_FIPS
  14075. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14076. #else
  14077. ExpectIntEQ(ret, 0);
  14078. #endif
  14079. wc_HmacFree(&hmac);
  14080. #endif
  14081. return EXPECT_RESULT();
  14082. } /* END test_wc_Sha256HmacSetKey() */
  14083. /*
  14084. * testing wc_HmacSetKey on Sha384 hash.
  14085. */
  14086. static int test_wc_Sha384HmacSetKey(void)
  14087. {
  14088. EXPECT_DECLS;
  14089. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14090. Hmac hmac;
  14091. int ret, times, itr;
  14092. const char* keys[]=
  14093. {
  14094. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14095. "\x0b\x0b\x0b",
  14096. #ifndef HAVE_FIPS
  14097. "Jefe", /* smaller than minimum FIPS key size */
  14098. #endif
  14099. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14100. "\xAA\xAA\xAA"
  14101. };
  14102. times = sizeof(keys) / sizeof(char*);
  14103. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14104. for (itr = 0; itr < times; itr++) {
  14105. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14106. (word32)XSTRLEN(keys[itr])), 0);
  14107. }
  14108. /* Bad args. */
  14109. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14110. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14111. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14112. BAD_FUNC_ARG);
  14113. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14114. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14115. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14116. #ifdef HAVE_FIPS
  14117. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14118. #else
  14119. ExpectIntEQ(ret, 0);
  14120. #endif
  14121. wc_HmacFree(&hmac);
  14122. #endif
  14123. return EXPECT_RESULT();
  14124. } /* END test_wc_Sha384HmacSetKey() */
  14125. /*
  14126. * testing wc_HmacUpdate on wc_Md5 hash.
  14127. */
  14128. static int test_wc_Md5HmacUpdate(void)
  14129. {
  14130. EXPECT_DECLS;
  14131. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14132. Hmac hmac;
  14133. testVector a, b;
  14134. #ifdef HAVE_FIPS
  14135. const char* keys =
  14136. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14137. #else
  14138. const char* keys = "Jefe";
  14139. #endif
  14140. a.input = "what do ya want for nothing?";
  14141. a.inLen = XSTRLEN(a.input);
  14142. b.input = "Hi There";
  14143. b.inLen = XSTRLEN(b.input);
  14144. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14145. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14146. (word32)XSTRLEN(keys)), 0);
  14147. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14148. /* Update Hmac. */
  14149. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14150. /* Test bad args. */
  14151. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14152. BAD_FUNC_ARG);
  14153. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14154. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14155. wc_HmacFree(&hmac);
  14156. #endif
  14157. return EXPECT_RESULT();
  14158. } /* END test_wc_Md5HmacUpdate */
  14159. /*
  14160. * testing wc_HmacUpdate on SHA hash.
  14161. */
  14162. static int test_wc_ShaHmacUpdate(void)
  14163. {
  14164. EXPECT_DECLS;
  14165. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14166. Hmac hmac;
  14167. testVector a, b;
  14168. #ifdef HAVE_FIPS
  14169. const char* keys =
  14170. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14171. #else
  14172. const char* keys = "Jefe";
  14173. #endif
  14174. a.input = "what do ya want for nothing?";
  14175. a.inLen = XSTRLEN(a.input);
  14176. b.input = "Hi There";
  14177. b.inLen = XSTRLEN(b.input);
  14178. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14179. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14180. (word32)XSTRLEN(keys)), 0);
  14181. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14182. /* Update Hmac. */
  14183. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14184. /* Test bad args. */
  14185. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14186. BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14188. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14189. wc_HmacFree(&hmac);
  14190. #endif
  14191. return EXPECT_RESULT();
  14192. } /* END test_wc_ShaHmacUpdate */
  14193. /*
  14194. * testing wc_HmacUpdate on SHA224 hash.
  14195. */
  14196. static int test_wc_Sha224HmacUpdate(void)
  14197. {
  14198. EXPECT_DECLS;
  14199. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14200. Hmac hmac;
  14201. testVector a, b;
  14202. #ifdef HAVE_FIPS
  14203. const char* keys =
  14204. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14205. #else
  14206. const char* keys = "Jefe";
  14207. #endif
  14208. a.input = "what do ya want for nothing?";
  14209. a.inLen = XSTRLEN(a.input);
  14210. b.input = "Hi There";
  14211. b.inLen = XSTRLEN(b.input);
  14212. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14213. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14214. (word32)XSTRLEN(keys)), 0);
  14215. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14216. /* Update Hmac. */
  14217. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14218. /* Test bad args. */
  14219. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14220. BAD_FUNC_ARG);
  14221. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14222. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14223. wc_HmacFree(&hmac);
  14224. #endif
  14225. return EXPECT_RESULT();
  14226. } /* END test_wc_Sha224HmacUpdate */
  14227. /*
  14228. * testing wc_HmacUpdate on SHA256 hash.
  14229. */
  14230. static int test_wc_Sha256HmacUpdate(void)
  14231. {
  14232. EXPECT_DECLS;
  14233. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14234. Hmac hmac;
  14235. testVector a, b;
  14236. #ifdef HAVE_FIPS
  14237. const char* keys =
  14238. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14239. #else
  14240. const char* keys = "Jefe";
  14241. #endif
  14242. a.input = "what do ya want for nothing?";
  14243. a.inLen = XSTRLEN(a.input);
  14244. b.input = "Hi There";
  14245. b.inLen = XSTRLEN(b.input);
  14246. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14247. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14248. (word32)XSTRLEN(keys)), 0);
  14249. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14250. /* Update Hmac. */
  14251. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14252. /* Test bad args. */
  14253. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14254. BAD_FUNC_ARG);
  14255. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14256. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14257. wc_HmacFree(&hmac);
  14258. #endif
  14259. return EXPECT_RESULT();
  14260. } /* END test_wc_Sha256HmacUpdate */
  14261. /*
  14262. * testing wc_HmacUpdate on SHA384 hash.
  14263. */
  14264. static int test_wc_Sha384HmacUpdate(void)
  14265. {
  14266. EXPECT_DECLS;
  14267. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14268. Hmac hmac;
  14269. testVector a, b;
  14270. #ifdef HAVE_FIPS
  14271. const char* keys =
  14272. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14273. #else
  14274. const char* keys = "Jefe";
  14275. #endif
  14276. a.input = "what do ya want for nothing?";
  14277. a.inLen = XSTRLEN(a.input);
  14278. b.input = "Hi There";
  14279. b.inLen = XSTRLEN(b.input);
  14280. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14281. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14282. (word32)XSTRLEN(keys)), 0);
  14283. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14284. /* Update Hmac. */
  14285. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14286. /* Test bad args. */
  14287. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14288. BAD_FUNC_ARG);
  14289. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14290. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14291. wc_HmacFree(&hmac);
  14292. #endif
  14293. return EXPECT_RESULT();
  14294. } /* END test_wc_Sha384HmacUpdate */
  14295. /*
  14296. * Testing wc_HmacFinal() with MD5
  14297. */
  14298. static int test_wc_Md5HmacFinal(void)
  14299. {
  14300. EXPECT_DECLS;
  14301. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14302. Hmac hmac;
  14303. byte hash[WC_MD5_DIGEST_SIZE];
  14304. testVector a;
  14305. const char* key;
  14306. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14307. a.input = "Hi There";
  14308. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14309. "\x9d";
  14310. a.inLen = XSTRLEN(a.input);
  14311. a.outLen = XSTRLEN(a.output);
  14312. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14313. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14314. 0);
  14315. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14316. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14317. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14318. /* Try bad parameters. */
  14319. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14320. #ifndef HAVE_FIPS
  14321. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14322. #endif
  14323. wc_HmacFree(&hmac);
  14324. #endif
  14325. return EXPECT_RESULT();
  14326. } /* END test_wc_Md5HmacFinal */
  14327. /*
  14328. * Testing wc_HmacFinal() with SHA
  14329. */
  14330. static int test_wc_ShaHmacFinal(void)
  14331. {
  14332. EXPECT_DECLS;
  14333. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14334. Hmac hmac;
  14335. byte hash[WC_SHA_DIGEST_SIZE];
  14336. testVector a;
  14337. const char* key;
  14338. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14339. "\x0b\x0b\x0b";
  14340. a.input = "Hi There";
  14341. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14342. "\x8e\xf1\x46\xbe\x00";
  14343. a.inLen = XSTRLEN(a.input);
  14344. a.outLen = XSTRLEN(a.output);
  14345. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14346. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14347. 0);
  14348. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14349. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14350. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14351. /* Try bad parameters. */
  14352. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14353. #ifndef HAVE_FIPS
  14354. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14355. #endif
  14356. wc_HmacFree(&hmac);
  14357. #endif
  14358. return EXPECT_RESULT();
  14359. } /* END test_wc_ShaHmacFinal */
  14360. /*
  14361. * Testing wc_HmacFinal() with SHA224
  14362. */
  14363. static int test_wc_Sha224HmacFinal(void)
  14364. {
  14365. EXPECT_DECLS;
  14366. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14367. Hmac hmac;
  14368. byte hash[WC_SHA224_DIGEST_SIZE];
  14369. testVector a;
  14370. const char* key;
  14371. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14372. "\x0b\x0b\x0b";
  14373. a.input = "Hi There";
  14374. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14375. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14376. a.inLen = XSTRLEN(a.input);
  14377. a.outLen = XSTRLEN(a.output);
  14378. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14379. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14380. (word32)XSTRLEN(key)), 0);
  14381. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14382. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14383. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14384. /* Try bad parameters. */
  14385. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14386. #ifndef HAVE_FIPS
  14387. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14388. #endif
  14389. wc_HmacFree(&hmac);
  14390. #endif
  14391. return EXPECT_RESULT();
  14392. } /* END test_wc_Sha224HmacFinal */
  14393. /*
  14394. * Testing wc_HmacFinal() with SHA256
  14395. */
  14396. static int test_wc_Sha256HmacFinal(void)
  14397. {
  14398. EXPECT_DECLS;
  14399. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14400. Hmac hmac;
  14401. byte hash[WC_SHA256_DIGEST_SIZE];
  14402. testVector a;
  14403. const char* key;
  14404. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14405. "\x0b\x0b\x0b";
  14406. a.input = "Hi There";
  14407. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14408. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14409. "\xcf\xf7";
  14410. a.inLen = XSTRLEN(a.input);
  14411. a.outLen = XSTRLEN(a.output);
  14412. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14413. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14414. (word32)XSTRLEN(key)), 0);
  14415. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14416. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14417. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14418. /* Try bad parameters. */
  14419. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14420. #ifndef HAVE_FIPS
  14421. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14422. #endif
  14423. wc_HmacFree(&hmac);
  14424. #endif
  14425. return EXPECT_RESULT();
  14426. } /* END test_wc_Sha256HmacFinal */
  14427. /*
  14428. * Testing wc_HmacFinal() with SHA384
  14429. */
  14430. static int test_wc_Sha384HmacFinal(void)
  14431. {
  14432. EXPECT_DECLS;
  14433. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14434. Hmac hmac;
  14435. byte hash[WC_SHA384_DIGEST_SIZE];
  14436. testVector a;
  14437. const char* key;
  14438. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14439. "\x0b\x0b\x0b";
  14440. a.input = "Hi There";
  14441. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14442. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14443. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14444. "\xfa\x9c\xb6";
  14445. a.inLen = XSTRLEN(a.input);
  14446. a.outLen = XSTRLEN(a.output);
  14447. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14448. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14449. (word32)XSTRLEN(key)), 0);
  14450. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14451. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14452. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14453. /* Try bad parameters. */
  14454. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14455. #ifndef HAVE_FIPS
  14456. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14457. #endif
  14458. wc_HmacFree(&hmac);
  14459. #endif
  14460. return EXPECT_RESULT();
  14461. } /* END test_wc_Sha384HmacFinal */
  14462. /*
  14463. * Testing wc_InitCmac()
  14464. */
  14465. static int test_wc_InitCmac(void)
  14466. {
  14467. EXPECT_DECLS;
  14468. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14469. Cmac cmac1;
  14470. Cmac cmac2;
  14471. Cmac cmac3;
  14472. /* AES 128 key. */
  14473. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14474. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14475. /* AES 192 key. */
  14476. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14477. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14478. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14479. /* AES 256 key. */
  14480. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14481. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14482. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14483. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14484. word32 key1Sz = (word32)sizeof(key1) - 1;
  14485. word32 key2Sz = (word32)sizeof(key2) - 1;
  14486. word32 key3Sz = (word32)sizeof(key3) - 1;
  14487. int type = WC_CMAC_AES;
  14488. (void)key1;
  14489. (void)key1Sz;
  14490. (void)key2;
  14491. (void)key2Sz;
  14492. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14493. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14494. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14495. #ifdef WOLFSSL_AES_128
  14496. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14497. #endif
  14498. #ifdef WOLFSSL_AES_192
  14499. wc_AesFree(&cmac1.aes);
  14500. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14501. #endif
  14502. #ifdef WOLFSSL_AES_256
  14503. wc_AesFree(&cmac2.aes);
  14504. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14505. #endif
  14506. wc_AesFree(&cmac3.aes);
  14507. /* Test bad args. */
  14508. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14509. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14510. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14511. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14512. #endif
  14513. return EXPECT_RESULT();
  14514. } /* END test_wc_InitCmac */
  14515. /*
  14516. * Testing wc_CmacUpdate()
  14517. */
  14518. static int test_wc_CmacUpdate(void)
  14519. {
  14520. EXPECT_DECLS;
  14521. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14522. Cmac cmac;
  14523. byte key[] = {
  14524. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14525. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14526. };
  14527. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14528. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14529. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14530. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14531. "\xf4";
  14532. word32 inSz = (word32)sizeof(in) - 1;
  14533. word32 keySz = (word32)sizeof(key);
  14534. int type = WC_CMAC_AES;
  14535. XMEMSET(&cmac, 0, sizeof(Cmac));
  14536. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14537. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14538. /* Test bad args. */
  14539. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14540. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14541. wc_AesFree(&cmac.aes);
  14542. #endif
  14543. return EXPECT_RESULT();
  14544. } /* END test_wc_CmacUpdate */
  14545. /*
  14546. * Testing wc_CmacFinal()
  14547. */
  14548. static int test_wc_CmacFinal(void)
  14549. {
  14550. EXPECT_DECLS;
  14551. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14552. Cmac cmac;
  14553. byte key[] = {
  14554. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14555. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14556. };
  14557. byte msg[] = {
  14558. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14559. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14560. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14561. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14562. 0xf4
  14563. };
  14564. /* Test vectors from CMACGenAES128.rsp from
  14565. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14566. * Per RFC4493 truncation of lsb is possible.
  14567. */
  14568. byte expMac[] = {
  14569. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14570. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14571. };
  14572. byte mac[AES_BLOCK_SIZE];
  14573. word32 msgSz = (word32)sizeof(msg);
  14574. word32 keySz = (word32)sizeof(key);
  14575. word32 macSz = sizeof(mac);
  14576. word32 badMacSz = 17;
  14577. int expMacSz = sizeof(expMac);
  14578. int type = WC_CMAC_AES;
  14579. XMEMSET(&cmac, 0, sizeof(Cmac));
  14580. XMEMSET(mac, 0, macSz);
  14581. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14582. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14583. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14584. /* Pass in bad args. */
  14585. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14586. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14587. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14588. /* For the last call, use the API with implicit wc_CmacFree(). */
  14589. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14590. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14591. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14592. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14593. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14594. /* Pass in bad args. */
  14595. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14596. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14598. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14599. #endif
  14600. return EXPECT_RESULT();
  14601. } /* END test_wc_CmacFinal */
  14602. /*
  14603. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14604. */
  14605. static int test_wc_AesCmacGenerate(void)
  14606. {
  14607. EXPECT_DECLS;
  14608. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14609. byte key[] = {
  14610. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14611. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14612. };
  14613. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14614. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14615. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14616. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14617. byte mac[AES_BLOCK_SIZE];
  14618. word32 keySz = sizeof(key);
  14619. word32 macSz = sizeof(mac);
  14620. word32 msgSz = sizeof(msg) - 1;
  14621. word32 expMacSz = sizeof(expMac) - 1;
  14622. XMEMSET(mac, 0, macSz);
  14623. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14624. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14625. /* Pass in bad args. */
  14626. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14627. BAD_FUNC_ARG);
  14628. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14629. BAD_FUNC_ARG);
  14630. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14631. BAD_FUNC_ARG);
  14632. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14633. BAD_FUNC_ARG);
  14634. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14635. /* Test bad args. */
  14636. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14637. BAD_FUNC_ARG);
  14638. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14639. BAD_FUNC_ARG);
  14640. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14641. BAD_FUNC_ARG);
  14642. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14643. BAD_FUNC_ARG);
  14644. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14645. BAD_FUNC_ARG);
  14646. #endif
  14647. return EXPECT_RESULT();
  14648. } /* END test_wc_AesCmacGenerate */
  14649. /*
  14650. * Testing streaming AES-GCM API.
  14651. */
  14652. static int test_wc_AesGcmStream(void)
  14653. {
  14654. EXPECT_DECLS;
  14655. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14656. defined(WOLFSSL_AESGCM_STREAM)
  14657. int i;
  14658. WC_RNG rng[1];
  14659. Aes aesEnc[1];
  14660. Aes aesDec[1];
  14661. byte tag[AES_BLOCK_SIZE];
  14662. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14663. byte out[AES_BLOCK_SIZE * 3 + 2];
  14664. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14665. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14666. byte key[AES_128_KEY_SIZE] = { 0, };
  14667. byte iv[AES_IV_SIZE] = { 1, };
  14668. byte ivOut[AES_IV_SIZE];
  14669. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14670. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14671. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14672. };
  14673. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14674. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14675. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14676. };
  14677. static const byte expTag[AES_BLOCK_SIZE] = {
  14678. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14679. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14680. };
  14681. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14682. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14683. XMEMSET(&aesDec, 0, sizeof(Aes));
  14684. /* Create a random for generating IV/nonce. */
  14685. ExpectIntEQ(wc_InitRng(rng), 0);
  14686. /* Initialize data structures. */
  14687. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14688. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14689. /* BadParameters to streaming init. */
  14690. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14691. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14692. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14693. BAD_FUNC_ARG);
  14694. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14695. BAD_FUNC_ARG);
  14696. /* Bad parameters to encrypt update. */
  14697. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14698. BAD_FUNC_ARG);
  14699. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14700. BAD_FUNC_ARG);
  14701. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14702. BAD_FUNC_ARG);
  14703. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14704. BAD_FUNC_ARG);
  14705. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14706. BAD_FUNC_ARG);
  14707. /* Bad parameters to decrypt update. */
  14708. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14709. BAD_FUNC_ARG);
  14710. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14711. BAD_FUNC_ARG);
  14712. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14713. BAD_FUNC_ARG);
  14714. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14715. BAD_FUNC_ARG);
  14716. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14717. BAD_FUNC_ARG);
  14718. /* Bad parameters to encrypt final. */
  14719. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14720. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14721. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14722. BAD_FUNC_ARG);
  14723. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14724. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14725. BAD_FUNC_ARG);
  14726. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14727. BAD_FUNC_ARG);
  14728. /* Bad parameters to decrypt final. */
  14729. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14730. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14731. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14732. BAD_FUNC_ARG);
  14733. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14734. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14735. BAD_FUNC_ARG);
  14736. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14737. BAD_FUNC_ARG);
  14738. /* Check calling final before setting key fails. */
  14739. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14740. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14741. /* Check calling update before setting key else fails. */
  14742. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14743. MISSING_KEY);
  14744. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14745. MISSING_KEY);
  14746. /* Set key but not IV. */
  14747. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14748. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14749. /* Check calling final before setting IV fails. */
  14750. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14751. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14752. /* Check calling update before setting IV else fails. */
  14753. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14754. MISSING_IV);
  14755. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14756. MISSING_IV);
  14757. /* Set IV using fixed part IV and external IV APIs. */
  14758. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14759. rng), 0);
  14760. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14761. GCM_NONCE_MID_SZ), 0);
  14762. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14763. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14764. /* Encrypt and decrypt data. */
  14765. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14766. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14767. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14768. /* Finalize and check tag matches. */
  14769. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14770. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14771. /* Set key and IV through streaming init API. */
  14772. wc_AesFree(aesEnc);
  14773. wc_AesFree(aesDec);
  14774. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14775. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14776. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14777. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14778. /* Encrypt/decrypt one block and AAD of one block. */
  14779. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14780. AES_BLOCK_SIZE), 0);
  14781. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14782. AES_BLOCK_SIZE), 0);
  14783. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14784. /* Finalize and check tag matches. */
  14785. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14786. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14787. /* Set key and IV through streaming init API. */
  14788. wc_AesFree(aesEnc);
  14789. wc_AesFree(aesDec);
  14790. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14791. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14792. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14793. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14794. /* No data to encrypt/decrypt one byte of AAD. */
  14795. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14796. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14797. /* Finalize and check tag matches. */
  14798. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14799. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14800. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14801. /* Set key and IV through streaming init API. */
  14802. wc_AesFree(aesEnc);
  14803. wc_AesFree(aesDec);
  14804. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14805. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14806. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14807. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14808. /* Encrypt/decrypt one byte and no AAD. */
  14809. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14810. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14811. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14812. /* Finalize and check tag matches. */
  14813. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14814. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14815. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14816. /* Set key and IV through streaming init API. */
  14817. wc_AesFree(aesEnc);
  14818. wc_AesFree(aesDec);
  14819. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14820. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14821. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14822. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14823. /* Encryption AES is one byte at a time */
  14824. for (i = 0; i < (int)sizeof(aad); i++) {
  14825. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14826. 0);
  14827. }
  14828. for (i = 0; i < (int)sizeof(in); i++) {
  14829. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14830. 0);
  14831. }
  14832. /* Decryption AES is two bytes at a time */
  14833. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14834. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14835. 0);
  14836. }
  14837. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14838. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14839. 0), 0);
  14840. }
  14841. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14842. /* Finalize and check tag matches. */
  14843. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14844. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14845. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14846. /* Check streaming encryption can be decrypted with one shot. */
  14847. wc_AesFree(aesDec);
  14848. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14849. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14850. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14851. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14852. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14853. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14854. wc_AesFree(aesEnc);
  14855. wc_AesFree(aesDec);
  14856. wc_FreeRng(rng);
  14857. #endif
  14858. return EXPECT_RESULT();
  14859. } /* END test_wc_AesGcmStream */
  14860. /*
  14861. * Testing streaming SM4 API.
  14862. */
  14863. static int test_wc_Sm4(void)
  14864. {
  14865. int res = TEST_SKIPPED;
  14866. #ifdef WOLFSSL_SM4
  14867. EXPECT_DECLS;
  14868. wc_Sm4 sm4;
  14869. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14870. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14871. unsigned char key[SM4_KEY_SIZE];
  14872. #endif
  14873. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14874. unsigned char iv[SM4_IV_SIZE];
  14875. #endif
  14876. /* Invalid parameters - wc_Sm4Init */
  14877. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14878. /* Valid cases - wc_Sm4Init */
  14879. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14880. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14881. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14882. XMEMSET(key, 0, sizeof(key));
  14883. /* Invalid parameters - wc_Sm4SetKey. */
  14884. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14885. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14886. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14887. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14888. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14889. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14890. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14891. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14893. /* Valid cases - wc_Sm4SetKey. */
  14894. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14895. #endif
  14896. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14897. XMEMSET(iv, 0, sizeof(iv));
  14898. /* Invalid parameters - wc_Sm4SetIV. */
  14899. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14901. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14902. /* Valid cases - wc_Sm4SetIV. */
  14903. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14904. #endif
  14905. /* Valid cases - wc_Sm4Free */
  14906. wc_Sm4Free(NULL);
  14907. wc_Sm4Free(&sm4);
  14908. res = EXPECT_RESULT();
  14909. #endif
  14910. return res;
  14911. } /* END test_wc_Sm4 */
  14912. /*
  14913. * Testing block based SM4-ECB API.
  14914. */
  14915. static int test_wc_Sm4Ecb(void)
  14916. {
  14917. int res = TEST_SKIPPED;
  14918. #ifdef WOLFSSL_SM4_ECB
  14919. EXPECT_DECLS;
  14920. wc_Sm4 sm4;
  14921. unsigned char key[SM4_KEY_SIZE];
  14922. unsigned char in[SM4_BLOCK_SIZE * 2];
  14923. unsigned char out[SM4_BLOCK_SIZE * 2];
  14924. unsigned char out2[SM4_BLOCK_SIZE];
  14925. XMEMSET(key, 0, sizeof(key));
  14926. XMEMSET(in, 0, sizeof(in));
  14927. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14928. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14929. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14930. /* Tested in test_wc_Sm4. */
  14931. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14932. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14933. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14934. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14935. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14936. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14937. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14938. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14939. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14940. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14941. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14942. /* Valid cases - wc_Sm4EcbEncrypt. */
  14943. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14944. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14945. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14946. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14947. /* In and out are same pointer. */
  14948. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14949. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14950. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14951. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14952. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14953. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14954. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14955. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14956. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14957. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14958. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14959. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14960. /* Valid cases - wc_Sm4EcbDecrypt. */
  14961. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14962. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14963. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14964. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14965. /* In and out are same pointer. */
  14966. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14967. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14968. wc_Sm4Free(&sm4);
  14969. res = EXPECT_RESULT();
  14970. #endif
  14971. return res;
  14972. } /* END test_wc_Sm4Ecb */
  14973. /*
  14974. * Testing block based SM4-CBC API.
  14975. */
  14976. static int test_wc_Sm4Cbc(void)
  14977. {
  14978. int res = TEST_SKIPPED;
  14979. #ifdef WOLFSSL_SM4_CBC
  14980. EXPECT_DECLS;
  14981. wc_Sm4 sm4;
  14982. unsigned char key[SM4_KEY_SIZE];
  14983. unsigned char iv[SM4_IV_SIZE];
  14984. unsigned char in[SM4_BLOCK_SIZE * 2];
  14985. unsigned char out[SM4_BLOCK_SIZE * 2];
  14986. unsigned char out2[SM4_BLOCK_SIZE];
  14987. XMEMSET(key, 0, sizeof(key));
  14988. XMEMSET(iv, 0, sizeof(iv));
  14989. XMEMSET(in, 0, sizeof(in));
  14990. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14991. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14992. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14993. /* Tested in test_wc_Sm4. */
  14994. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14995. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14996. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14997. /* Tested in test_wc_Sm4. */
  14998. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14999. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  15000. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15001. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15002. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15003. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15004. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15005. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15006. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15007. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15008. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15009. /* Valid cases - wc_Sm4CbcEncrypt. */
  15010. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15011. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15012. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15013. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15014. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15015. /* In and out are same pointer. */
  15016. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15017. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15018. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15019. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15020. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15021. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15022. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15023. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15024. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15025. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15026. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15027. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15028. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15029. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15030. /* Valid cases - wc_Sm4CbcDecrypt. */
  15031. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15032. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15033. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15034. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15035. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15036. /* In and out are same pointer. */
  15037. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15038. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15039. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15040. wc_Sm4Free(&sm4);
  15041. res = EXPECT_RESULT();
  15042. #endif
  15043. return res;
  15044. } /* END test_wc_Sm4Cbc */
  15045. /*
  15046. * Testing streaming SM4-CTR API.
  15047. */
  15048. static int test_wc_Sm4Ctr(void)
  15049. {
  15050. int res = TEST_SKIPPED;
  15051. #ifdef WOLFSSL_SM4_CTR
  15052. EXPECT_DECLS;
  15053. wc_Sm4 sm4;
  15054. unsigned char key[SM4_KEY_SIZE];
  15055. unsigned char iv[SM4_IV_SIZE];
  15056. unsigned char in[SM4_BLOCK_SIZE * 4];
  15057. unsigned char out[SM4_BLOCK_SIZE * 4];
  15058. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15059. word32 chunk;
  15060. word32 i;
  15061. XMEMSET(key, 0, sizeof(key));
  15062. XMEMSET(iv, 0, sizeof(iv));
  15063. XMEMSET(in, 0, sizeof(in));
  15064. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15065. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15066. /* Tested in test_wc_Sm4. */
  15067. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15068. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15069. /* Tested in test_wc_Sm4. */
  15070. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15071. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15072. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15073. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15074. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15075. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15077. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15078. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15079. /* Valid cases - wc_Sm4CtrEncrypt. */
  15080. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15081. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15082. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15083. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15084. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15085. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15086. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15087. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15088. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15089. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15090. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15091. /* In and out are same pointer. Also check encrypt of cipher text produces
  15092. * plaintext.
  15093. */
  15094. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15095. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15096. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15097. /* Chunking tests. */
  15098. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15099. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15100. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15101. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15102. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15103. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15104. }
  15105. if (i < (word32)sizeof(in)) {
  15106. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15107. (word32)sizeof(in) - i), 0);
  15108. }
  15109. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15110. }
  15111. for (i = 0; i < (word32)sizeof(iv); i++) {
  15112. iv[i] = 0xff;
  15113. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15114. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15115. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15116. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15117. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15118. }
  15119. wc_Sm4Free(&sm4);
  15120. res = EXPECT_RESULT();
  15121. #endif
  15122. return res;
  15123. } /* END test_wc_Sm4Ctr */
  15124. /*
  15125. * Testing stream SM4-GCM API.
  15126. */
  15127. static int test_wc_Sm4Gcm(void)
  15128. {
  15129. int res = TEST_SKIPPED;
  15130. #ifdef WOLFSSL_SM4_GCM
  15131. EXPECT_DECLS;
  15132. wc_Sm4 sm4;
  15133. unsigned char key[SM4_KEY_SIZE];
  15134. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15135. unsigned char in[SM4_BLOCK_SIZE * 2];
  15136. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15137. unsigned char out[SM4_BLOCK_SIZE * 2];
  15138. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15139. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15140. unsigned char tag[SM4_BLOCK_SIZE];
  15141. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15142. word32 i;
  15143. XMEMSET(key, 0, sizeof(key));
  15144. XMEMSET(nonce, 0, sizeof(nonce));
  15145. XMEMSET(in, 0, sizeof(in));
  15146. XMEMSET(in2, 0, sizeof(in2));
  15147. XMEMSET(aad, 0, sizeof(aad));
  15148. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15149. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15150. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15151. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15152. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15153. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15154. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15155. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15156. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15157. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15158. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15159. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15160. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15161. /* Valid parameters - wc_Sm4GcmSetKey. */
  15162. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15163. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15164. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15165. 0), BAD_FUNC_ARG);
  15166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15167. 0), BAD_FUNC_ARG);
  15168. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15169. 0), BAD_FUNC_ARG);
  15170. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15171. 0), BAD_FUNC_ARG);
  15172. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15173. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15174. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15175. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15176. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15177. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15178. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15179. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15180. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15181. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15182. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15183. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15184. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15185. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15186. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15187. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15188. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15189. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15190. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15191. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15192. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15193. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15194. 0), BAD_FUNC_ARG);
  15195. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15196. 0), BAD_FUNC_ARG);
  15197. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15198. 0), BAD_FUNC_ARG);
  15199. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15200. 0), BAD_FUNC_ARG);
  15201. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15202. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15203. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15204. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15205. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15206. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15207. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15208. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15209. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15210. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15211. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15212. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15213. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15214. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15215. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15216. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15217. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15218. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15219. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15220. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15221. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15222. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15223. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15224. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15225. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15226. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15227. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15228. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15229. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15230. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15231. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15232. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15233. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15234. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15235. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15236. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15237. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15238. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15239. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15240. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15241. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15242. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15243. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15244. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15245. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15246. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15247. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15248. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15249. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15250. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15251. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15252. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15253. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15254. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15255. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15256. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15258. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15259. SM4_GCM_AUTH_E);
  15260. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15261. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15262. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15263. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15264. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15265. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15266. }
  15267. /* Check different in/out sizes. */
  15268. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15269. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15270. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15271. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15272. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15273. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15274. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15275. XMEMCPY(out2, out, i - 1);
  15276. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15277. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15278. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15279. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15280. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15281. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15282. }
  15283. /* Force the counter to roll over in first byte. */
  15284. {
  15285. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15286. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15287. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15288. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15289. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15290. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15291. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15292. }
  15293. wc_Sm4Free(&sm4);
  15294. res = EXPECT_RESULT();
  15295. #endif
  15296. return res;
  15297. } /* END test_wc_Sm4Gcm */
  15298. /*
  15299. * Testing stream SM4-CCM API.
  15300. */
  15301. static int test_wc_Sm4Ccm(void)
  15302. {
  15303. int res = TEST_SKIPPED;
  15304. #ifdef WOLFSSL_SM4_CCM
  15305. EXPECT_DECLS;
  15306. wc_Sm4 sm4;
  15307. unsigned char key[SM4_KEY_SIZE];
  15308. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15309. unsigned char in[SM4_BLOCK_SIZE * 2];
  15310. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15311. unsigned char out[SM4_BLOCK_SIZE * 2];
  15312. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15313. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15314. unsigned char tag[SM4_BLOCK_SIZE];
  15315. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15316. word32 i;
  15317. XMEMSET(key, 0, sizeof(key));
  15318. XMEMSET(nonce, 0, sizeof(nonce));
  15319. XMEMSET(in, 0, sizeof(in));
  15320. XMEMSET(in2, 0, sizeof(in2));
  15321. XMEMSET(aad, 0, sizeof(aad));
  15322. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15323. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15324. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15325. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15326. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15327. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15328. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15329. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15330. 0), BAD_FUNC_ARG);
  15331. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15332. 0), BAD_FUNC_ARG);
  15333. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15334. 0), BAD_FUNC_ARG);
  15335. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15336. 0), BAD_FUNC_ARG);
  15337. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15338. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15339. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15340. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15341. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15342. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15343. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15344. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15345. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15346. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15347. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15348. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15349. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15350. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15351. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15352. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15353. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15354. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15355. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15356. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15357. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15358. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15359. 0), BAD_FUNC_ARG);
  15360. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15361. 0), BAD_FUNC_ARG);
  15362. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15363. 0), BAD_FUNC_ARG);
  15364. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15365. 0), BAD_FUNC_ARG);
  15366. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15367. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15368. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15369. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15370. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15371. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15372. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15373. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15374. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15375. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15376. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15377. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15378. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15379. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15380. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15381. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15382. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15383. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15384. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15385. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15386. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15387. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15388. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15389. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15390. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15391. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15392. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15393. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15394. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15395. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15396. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15397. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15398. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15399. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15400. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15401. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15402. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15403. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15404. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15405. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15406. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15407. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15408. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15409. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15410. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15411. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15412. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15413. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15414. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15415. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15416. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15417. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15418. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15419. }
  15420. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15421. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15422. SM4_CCM_AUTH_E);
  15423. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15424. for (i = 0; i < 4; i++) {
  15425. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15426. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15427. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15428. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15429. }
  15430. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15431. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15432. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15433. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15434. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15435. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15436. }
  15437. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15438. * Even values in range 4..SM4_BLOCK_SIZE.
  15439. */
  15440. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15441. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15442. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15443. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15444. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15445. }
  15446. /* Check different in/out sizes. */
  15447. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15448. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15449. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15450. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15451. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15452. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15453. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15454. XMEMCPY(out2, out, i - 1);
  15455. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15456. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15457. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15458. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15459. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15460. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15461. }
  15462. /* Force the counter to roll over in first byte. */
  15463. {
  15464. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15465. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15466. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15467. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15468. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15469. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15470. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15471. }
  15472. wc_Sm4Free(&sm4);
  15473. res = EXPECT_RESULT();
  15474. #endif
  15475. return res;
  15476. } /* END test_wc_Sm4Ccm */
  15477. /*
  15478. * unit test for wc_Des3_SetIV()
  15479. */
  15480. static int test_wc_Des3_SetIV(void)
  15481. {
  15482. EXPECT_DECLS;
  15483. #ifndef NO_DES3
  15484. Des3 des;
  15485. const byte key[] = {
  15486. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15487. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15488. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15489. };
  15490. const byte iv[] = {
  15491. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15492. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15493. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15494. };
  15495. XMEMSET(&des, 0, sizeof(Des3));
  15496. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15497. /* DES_ENCRYPTION or DES_DECRYPTION */
  15498. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15499. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15500. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15501. /* Test explicitly wc_Des3_SetIV() */
  15502. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15503. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15504. #endif
  15505. wc_Des3Free(&des);
  15506. #endif
  15507. return EXPECT_RESULT();
  15508. } /* END test_wc_Des3_SetIV */
  15509. /*
  15510. * unit test for wc_Des3_SetKey()
  15511. */
  15512. static int test_wc_Des3_SetKey(void)
  15513. {
  15514. EXPECT_DECLS;
  15515. #ifndef NO_DES3
  15516. Des3 des;
  15517. const byte key[] = {
  15518. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15519. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15520. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15521. };
  15522. const byte iv[] = {
  15523. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15524. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15525. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15526. };
  15527. XMEMSET(&des, 0, sizeof(Des3));
  15528. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15529. /* DES_ENCRYPTION or DES_DECRYPTION */
  15530. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15531. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15532. /* Test bad args. */
  15533. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15534. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15535. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15536. /* Default case. Should return 0. */
  15537. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15538. wc_Des3Free(&des);
  15539. #endif
  15540. return EXPECT_RESULT();
  15541. } /* END test_wc_Des3_SetKey */
  15542. /*
  15543. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15544. */
  15545. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15546. {
  15547. EXPECT_DECLS;
  15548. #ifndef NO_DES3
  15549. Des3 des;
  15550. byte cipher[24];
  15551. byte plain[24];
  15552. const byte key[] = {
  15553. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15554. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15555. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15556. };
  15557. const byte iv[] = {
  15558. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15559. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15560. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15561. };
  15562. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15563. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15564. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15565. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15566. };
  15567. XMEMSET(&des, 0, sizeof(Des3));
  15568. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15569. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15570. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15571. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15572. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15573. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15574. /* Pass in bad args. */
  15575. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15576. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15577. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15578. BAD_FUNC_ARG);
  15579. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15580. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15581. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15582. wc_Des3Free(&des);
  15583. #endif
  15584. return EXPECT_RESULT();
  15585. } /* END wc_Des3_CbcEncrypt */
  15586. /*
  15587. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15588. */
  15589. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15590. {
  15591. EXPECT_DECLS;
  15592. #ifndef NO_DES3
  15593. word32 vectorSz, cipherSz;
  15594. byte cipher[24];
  15595. byte plain[24];
  15596. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15597. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15598. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15599. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15600. };
  15601. byte key[] = {
  15602. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15603. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15604. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15605. };
  15606. byte iv[] = {
  15607. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15608. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15609. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15610. };
  15611. vectorSz = sizeof(byte) * 24;
  15612. cipherSz = sizeof(byte) * 24;
  15613. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15614. 0);
  15615. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15616. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15617. /* pass in bad args. */
  15618. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15619. BAD_FUNC_ARG);
  15620. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15621. BAD_FUNC_ARG);
  15622. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15623. BAD_FUNC_ARG);
  15624. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15625. 0);
  15626. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15627. BAD_FUNC_ARG);
  15628. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15629. BAD_FUNC_ARG);
  15630. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15631. BAD_FUNC_ARG);
  15632. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15633. 0);
  15634. #endif
  15635. return EXPECT_RESULT();
  15636. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15637. /*
  15638. * Unit test for wc_Des3_EcbEncrypt
  15639. */
  15640. static int test_wc_Des3_EcbEncrypt(void)
  15641. {
  15642. EXPECT_DECLS;
  15643. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15644. Des3 des;
  15645. byte cipher[24];
  15646. word32 cipherSz = sizeof(cipher);
  15647. const byte key[] = {
  15648. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15649. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15650. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15651. };
  15652. const byte iv[] = {
  15653. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15654. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15655. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15656. };
  15657. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15658. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15659. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15660. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15661. };
  15662. XMEMSET(&des, 0, sizeof(Des3));
  15663. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15664. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15665. /* Bad Cases */
  15666. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15667. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15668. BAD_FUNC_ARG);
  15669. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15670. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15671. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15672. /* Good Cases */
  15673. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15674. wc_Des3Free(&des);
  15675. #endif
  15676. return EXPECT_RESULT();
  15677. } /* END test_wc_Des3_EcbEncrypt */
  15678. /*
  15679. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15680. */
  15681. static int test_wc_Chacha_SetKey(void)
  15682. {
  15683. EXPECT_DECLS;
  15684. #ifdef HAVE_CHACHA
  15685. ChaCha ctx;
  15686. const byte key[] = {
  15687. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15688. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15689. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15691. };
  15692. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15693. byte cipher[128];
  15694. XMEMSET(cipher, 0, sizeof(cipher));
  15695. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15696. /* Test bad args. */
  15697. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15698. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15699. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15700. /* Test bad args. */
  15701. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15702. #endif
  15703. return EXPECT_RESULT();
  15704. } /* END test_wc_Chacha_SetKey */
  15705. /*
  15706. * unit test for wc_Poly1305SetKey()
  15707. */
  15708. static int test_wc_Poly1305SetKey(void)
  15709. {
  15710. EXPECT_DECLS;
  15711. #ifdef HAVE_POLY1305
  15712. Poly1305 ctx;
  15713. const byte key[] =
  15714. {
  15715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15719. };
  15720. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15721. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15722. /* Test bad args. */
  15723. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15724. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15725. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15726. #endif
  15727. return EXPECT_RESULT();
  15728. } /* END test_wc_Poly1305_SetKey() */
  15729. /*
  15730. * Testing wc_Chacha_Process()
  15731. */
  15732. static int test_wc_Chacha_Process(void)
  15733. {
  15734. EXPECT_DECLS;
  15735. #ifdef HAVE_CHACHA
  15736. ChaCha enc, dec;
  15737. byte cipher[128];
  15738. byte plain[128];
  15739. const byte key[] =
  15740. {
  15741. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15744. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15745. };
  15746. const char* input = "Everybody gets Friday off.";
  15747. word32 keySz = sizeof(key)/sizeof(byte);
  15748. unsigned long int inlen = XSTRLEN(input);
  15749. /* Initialize stack variables. */
  15750. XMEMSET(cipher, 0, 128);
  15751. XMEMSET(plain, 0, 128);
  15752. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15753. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15754. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15755. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15756. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15757. 0);
  15758. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15759. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15760. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15761. /* test checking and using leftovers, currently just in C code */
  15762. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15763. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15764. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15765. (word32)inlen - 2), 0);
  15766. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15767. (byte*)input + (inlen - 2), 2), 0);
  15768. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15769. (word32)inlen - 2), 0);
  15770. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15771. (byte*)input + (inlen - 2), 2), 0);
  15772. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15773. /* check edge cases with counter increment */
  15774. {
  15775. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15776. const byte expected[] = {
  15777. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15778. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15779. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15780. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15781. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15782. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15783. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15784. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15785. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15786. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15787. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15788. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15789. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15790. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15791. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15792. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15793. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15794. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15795. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15796. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15797. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15798. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15799. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15800. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15801. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15802. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15803. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15804. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15805. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15806. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15807. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15808. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15809. };
  15810. const byte iv2[] = {
  15811. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15812. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15813. };
  15814. byte input2[256];
  15815. int i;
  15816. for (i = 0; i < 256; i++)
  15817. input2[i] = i;
  15818. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15819. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15820. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15821. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15822. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15823. /* partial */
  15824. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15825. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15826. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15827. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15828. }
  15829. #endif
  15830. /* Test bad args. */
  15831. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15832. BAD_FUNC_ARG);
  15833. #endif
  15834. return EXPECT_RESULT();
  15835. } /* END test_wc_Chacha_Process */
  15836. /*
  15837. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15838. */
  15839. static int test_wc_ChaCha20Poly1305_aead(void)
  15840. {
  15841. EXPECT_DECLS;
  15842. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15843. const byte key[] = {
  15844. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15845. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15846. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15847. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15848. };
  15849. const byte plaintext[] = {
  15850. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15851. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15852. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15853. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15854. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15855. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15856. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15857. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15858. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15859. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15860. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15861. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15862. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15863. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15864. 0x74, 0x2e
  15865. };
  15866. const byte iv[] = {
  15867. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15868. 0x44, 0x45, 0x46, 0x47
  15869. };
  15870. const byte aad[] = { /* additional data */
  15871. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15872. 0xc4, 0xc5, 0xc6, 0xc7
  15873. };
  15874. const byte cipher[] = { /* expected output from operation */
  15875. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15876. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15877. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15878. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15879. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15880. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15881. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15882. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15883. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15884. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15885. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15886. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15887. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15888. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15889. 0x61, 0x16
  15890. };
  15891. const byte authTag[] = { /* expected output from operation */
  15892. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15893. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15894. };
  15895. byte generatedCiphertext[272];
  15896. byte generatedPlaintext[272];
  15897. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15898. /* Initialize stack variables. */
  15899. XMEMSET(generatedCiphertext, 0, 272);
  15900. XMEMSET(generatedPlaintext, 0, 272);
  15901. /* Test Encrypt */
  15902. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15903. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15904. 0);
  15905. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15906. sizeof(cipher)/sizeof(byte)), 0);
  15907. /* Test bad args. */
  15908. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15909. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15910. BAD_FUNC_ARG);
  15911. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15912. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15913. BAD_FUNC_ARG);
  15914. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15915. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15916. BAD_FUNC_ARG);
  15917. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15918. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15919. BAD_FUNC_ARG);
  15920. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15921. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15922. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15923. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15924. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15925. sizeof(cipher), authTag, generatedPlaintext), 0);
  15926. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15927. sizeof(plaintext)/sizeof(byte)), 0);
  15928. /* Test bad args. */
  15929. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15930. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15931. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15932. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15933. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15934. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15935. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15936. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15937. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15938. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15939. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15940. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15941. #endif
  15942. return EXPECT_RESULT();
  15943. } /* END test_wc_ChaCha20Poly1305_aead */
  15944. /*
  15945. * Testing function for wc_Rc2SetKey().
  15946. */
  15947. static int test_wc_Rc2SetKey(void)
  15948. {
  15949. EXPECT_DECLS;
  15950. #ifdef WC_RC2
  15951. Rc2 rc2;
  15952. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15953. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15954. /* valid key and IV */
  15955. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15956. iv, 40), 0);
  15957. /* valid key, no IV */
  15958. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15959. NULL, 40), 0);
  15960. /* bad arguments */
  15961. /* null Rc2 struct */
  15962. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15963. iv, 40), BAD_FUNC_ARG);
  15964. /* null key */
  15965. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15966. iv, 40), BAD_FUNC_ARG);
  15967. /* key size == 0 */
  15968. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15969. /* key size > 128 */
  15970. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15971. /* effective bits == 0 */
  15972. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15973. iv, 0), WC_KEY_SIZE_E);
  15974. /* effective bits > 1024 */
  15975. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15976. iv, 1025), WC_KEY_SIZE_E);
  15977. #endif
  15978. return EXPECT_RESULT();
  15979. } /* END test_wc_Rc2SetKey */
  15980. /*
  15981. * Testing function for wc_Rc2SetIV().
  15982. */
  15983. static int test_wc_Rc2SetIV(void)
  15984. {
  15985. EXPECT_DECLS;
  15986. #ifdef WC_RC2
  15987. Rc2 rc2;
  15988. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15989. /* valid IV */
  15990. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15991. /* valid NULL IV */
  15992. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15993. /* bad arguments */
  15994. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15995. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15996. #endif
  15997. return EXPECT_RESULT();
  15998. } /* END test_wc_Rc2SetIV */
  15999. /*
  16000. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  16001. */
  16002. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16003. {
  16004. EXPECT_DECLS;
  16005. #ifdef WC_RC2
  16006. Rc2 rc2;
  16007. int effectiveKeyBits = 63;
  16008. byte cipher[RC2_BLOCK_SIZE];
  16009. byte plain[RC2_BLOCK_SIZE];
  16010. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16011. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16012. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16013. XMEMSET(cipher, 0, sizeof(cipher));
  16014. XMEMSET(plain, 0, sizeof(plain));
  16015. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16016. NULL, effectiveKeyBits), 0);
  16017. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16018. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16019. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16020. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16021. /* Rc2EcbEncrypt bad arguments */
  16022. /* null Rc2 struct */
  16023. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16024. BAD_FUNC_ARG);
  16025. /* null out buffer */
  16026. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16027. BAD_FUNC_ARG);
  16028. /* null input buffer */
  16029. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16030. BAD_FUNC_ARG);
  16031. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16032. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16033. /* Rc2EcbDecrypt bad arguments */
  16034. /* null Rc2 struct */
  16035. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16036. BAD_FUNC_ARG);
  16037. /* null out buffer */
  16038. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16039. BAD_FUNC_ARG);
  16040. /* null input buffer */
  16041. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16042. BAD_FUNC_ARG);
  16043. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16044. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16045. #endif
  16046. return EXPECT_RESULT();
  16047. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16048. /*
  16049. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16050. */
  16051. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16052. {
  16053. EXPECT_DECLS;
  16054. #ifdef WC_RC2
  16055. Rc2 rc2;
  16056. int effectiveKeyBits = 63;
  16057. byte cipher[RC2_BLOCK_SIZE*2];
  16058. byte plain[RC2_BLOCK_SIZE*2];
  16059. /* vector taken from test.c */
  16060. byte key[] = {
  16061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16062. };
  16063. byte iv[] = {
  16064. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16065. };
  16066. byte input[] = {
  16067. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16068. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16069. };
  16070. byte output[] = {
  16071. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16072. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16073. };
  16074. XMEMSET(cipher, 0, sizeof(cipher));
  16075. XMEMSET(plain, 0, sizeof(plain));
  16076. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16077. iv, effectiveKeyBits), 0);
  16078. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16079. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16080. /* reset IV for decrypt */
  16081. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16082. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16083. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16084. /* Rc2CbcEncrypt bad arguments */
  16085. /* null Rc2 struct */
  16086. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16087. BAD_FUNC_ARG);
  16088. /* null out buffer */
  16089. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16090. BAD_FUNC_ARG);
  16091. /* null input buffer */
  16092. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16093. BAD_FUNC_ARG);
  16094. /* Rc2CbcDecrypt bad arguments */
  16095. /* in size is 0 */
  16096. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16097. /* null Rc2 struct */
  16098. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16099. BAD_FUNC_ARG);
  16100. /* null out buffer */
  16101. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16102. BAD_FUNC_ARG);
  16103. /* null input buffer */
  16104. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16105. BAD_FUNC_ARG);
  16106. #endif
  16107. return EXPECT_RESULT();
  16108. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16109. /*
  16110. * Testing function for wc_AesSetIV
  16111. */
  16112. static int test_wc_AesSetIV(void)
  16113. {
  16114. int res = TEST_SKIPPED;
  16115. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16116. Aes aes;
  16117. int ret = 0;
  16118. byte key16[] =
  16119. {
  16120. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16121. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16122. };
  16123. byte iv1[] = "1234567890abcdef";
  16124. byte iv2[] = "0987654321fedcba";
  16125. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16126. if (ret != 0)
  16127. return ret;
  16128. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16129. iv1, AES_ENCRYPTION);
  16130. if (ret == 0) {
  16131. ret = wc_AesSetIV(&aes, iv2);
  16132. }
  16133. /* Test bad args. */
  16134. if (ret == 0) {
  16135. ret = wc_AesSetIV(NULL, iv1);
  16136. if (ret == BAD_FUNC_ARG) {
  16137. /* NULL iv should return 0. */
  16138. ret = wc_AesSetIV(&aes, NULL);
  16139. }
  16140. else {
  16141. ret = WOLFSSL_FATAL_ERROR;
  16142. }
  16143. }
  16144. wc_AesFree(&aes);
  16145. res = TEST_RES_CHECK(ret == 0);
  16146. #endif
  16147. return res;
  16148. } /* test_wc_AesSetIV */
  16149. /*
  16150. * Testing function for wc_AesSetKey().
  16151. */
  16152. static int test_wc_AesSetKey(void)
  16153. {
  16154. EXPECT_DECLS;
  16155. #ifndef NO_AES
  16156. Aes aes;
  16157. byte key16[] = {
  16158. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16159. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16160. };
  16161. #ifdef WOLFSSL_AES_192
  16162. byte key24[] = {
  16163. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16164. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16165. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16166. };
  16167. #endif
  16168. #ifdef WOLFSSL_AES_256
  16169. byte key32[] = {
  16170. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16171. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16172. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16173. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16174. };
  16175. #endif
  16176. byte badKey16[] = {
  16177. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16178. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16179. };
  16180. byte iv[] = "1234567890abcdef";
  16181. XMEMSET(&aes, 0, sizeof(Aes));
  16182. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16183. #ifdef WOLFSSL_AES_128
  16184. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16185. iv, AES_ENCRYPTION), 0);
  16186. #endif
  16187. #ifdef WOLFSSL_AES_192
  16188. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16189. iv, AES_ENCRYPTION), 0);
  16190. #endif
  16191. #ifdef WOLFSSL_AES_256
  16192. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16193. iv, AES_ENCRYPTION), 0);
  16194. #endif
  16195. /* Pass in bad args. */
  16196. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16197. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16198. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16199. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16200. BAD_FUNC_ARG);
  16201. wc_AesFree(&aes);
  16202. #endif
  16203. return EXPECT_RESULT();
  16204. } /* END test_wc_AesSetKey */
  16205. /*
  16206. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16207. * and wc_AesCbcDecryptWithKey()
  16208. */
  16209. static int test_wc_AesCbcEncryptDecrypt(void)
  16210. {
  16211. EXPECT_DECLS;
  16212. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16213. defined(WOLFSSL_AES_256)
  16214. Aes aes;
  16215. byte key32[] = {
  16216. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16217. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16218. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16219. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16220. };
  16221. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16222. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16223. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16224. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16225. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16226. };
  16227. byte iv[] = "1234567890abcdef";
  16228. byte enc[sizeof(vector)];
  16229. byte dec[sizeof(vector)];
  16230. byte dec2[sizeof(vector)];
  16231. /* Init stack variables. */
  16232. XMEMSET(&aes, 0, sizeof(Aes));
  16233. XMEMSET(enc, 0, sizeof(enc));
  16234. XMEMSET(dec, 0, sizeof(vector));
  16235. XMEMSET(dec2, 0, sizeof(vector));
  16236. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16237. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16238. AES_ENCRYPTION), 0);
  16239. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16240. /* Re init for decrypt and set flag. */
  16241. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16242. AES_DECRYPTION), 0);
  16243. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16244. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16245. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16246. sizeof(key32)/sizeof(byte), iv), 0);
  16247. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16248. /* Pass in bad args */
  16249. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16250. BAD_FUNC_ARG);
  16251. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16252. BAD_FUNC_ARG);
  16253. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16254. BAD_FUNC_ARG);
  16255. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16256. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16257. BAD_LENGTH_E);
  16258. #endif
  16259. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16260. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16261. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16262. "mode (v2), skip test");
  16263. #else
  16264. /* Test passing in size of 0 */
  16265. XMEMSET(enc, 0, sizeof(enc));
  16266. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16267. /* Check enc was not modified */
  16268. {
  16269. int i;
  16270. for (i = 0; i < (int)sizeof(enc); i++)
  16271. ExpectIntEQ(enc[i], 0);
  16272. }
  16273. #endif
  16274. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16275. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16276. BAD_FUNC_ARG);
  16277. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16278. BAD_FUNC_ARG);
  16279. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16280. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16281. BAD_LENGTH_E);
  16282. #else
  16283. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16284. BAD_FUNC_ARG);
  16285. #endif
  16286. /* Test passing in size of 0 */
  16287. XMEMSET(dec, 0, sizeof(dec));
  16288. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16289. /* Check dec was not modified */
  16290. {
  16291. int i;
  16292. for (i = 0; i < (int)sizeof(dec); i++)
  16293. ExpectIntEQ(dec[i], 0);
  16294. }
  16295. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16296. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16297. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16298. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16299. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16300. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16301. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16302. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16303. wc_AesFree(&aes);
  16304. #endif
  16305. return EXPECT_RESULT();
  16306. } /* END test_wc_AesCbcEncryptDecrypt */
  16307. /*
  16308. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16309. */
  16310. static int test_wc_AesCtrEncryptDecrypt(void)
  16311. {
  16312. EXPECT_DECLS;
  16313. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16314. Aes aesEnc;
  16315. Aes aesDec;
  16316. byte key32[] = {
  16317. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16318. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16319. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16320. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16321. };
  16322. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16323. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16324. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16325. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16326. };
  16327. byte iv[] = "1234567890abcdef";
  16328. byte enc[AES_BLOCK_SIZE * 2];
  16329. byte dec[AES_BLOCK_SIZE * 2];
  16330. /* Init stack variables. */
  16331. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16332. XMEMSET(&aesDec, 0, sizeof(Aes));
  16333. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16334. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16335. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16336. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16337. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16338. AES_ENCRYPTION), 0);
  16339. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16340. sizeof(vector)/sizeof(byte)), 0);
  16341. /* Decrypt with wc_AesCtrEncrypt() */
  16342. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16343. AES_ENCRYPTION), 0);
  16344. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16345. 0);
  16346. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16347. /* Test bad args. */
  16348. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16349. BAD_FUNC_ARG);
  16350. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16351. BAD_FUNC_ARG);
  16352. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16353. BAD_FUNC_ARG);
  16354. wc_AesFree(&aesEnc);
  16355. wc_AesFree(&aesDec);
  16356. #endif
  16357. return EXPECT_RESULT();
  16358. } /* END test_wc_AesCtrEncryptDecrypt */
  16359. /*
  16360. * test function for wc_AesGcmSetKey()
  16361. */
  16362. static int test_wc_AesGcmSetKey(void)
  16363. {
  16364. EXPECT_DECLS;
  16365. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16366. Aes aes;
  16367. #ifdef WOLFSSL_AES_128
  16368. byte key16[] = {
  16369. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16370. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16371. };
  16372. #endif
  16373. #ifdef WOLFSSL_AES_192
  16374. byte key24[] = {
  16375. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16376. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16377. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16378. };
  16379. #endif
  16380. #ifdef WOLFSSL_AES_256
  16381. byte key32[] = {
  16382. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16383. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16384. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16385. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16386. };
  16387. #endif
  16388. byte badKey16[] = {
  16389. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16390. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16391. };
  16392. byte badKey24[] = {
  16393. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16394. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16395. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16396. };
  16397. byte badKey32[] = {
  16398. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16399. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16400. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16401. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16402. };
  16403. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16404. #ifdef WOLFSSL_AES_128
  16405. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16406. #endif
  16407. #ifdef WOLFSSL_AES_192
  16408. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16409. #endif
  16410. #ifdef WOLFSSL_AES_256
  16411. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16412. #endif
  16413. /* Pass in bad args. */
  16414. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16415. BAD_FUNC_ARG);
  16416. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16417. BAD_FUNC_ARG);
  16418. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16419. BAD_FUNC_ARG);
  16420. wc_AesFree(&aes);
  16421. #endif
  16422. return EXPECT_RESULT();
  16423. } /* END test_wc_AesGcmSetKey */
  16424. /*
  16425. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16426. */
  16427. static int test_wc_AesGcmEncryptDecrypt(void)
  16428. {
  16429. EXPECT_DECLS;
  16430. /* WOLFSSL_AFALG requires 12 byte IV */
  16431. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16432. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16433. Aes aes;
  16434. byte key32[] = {
  16435. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16436. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16437. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16438. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16439. };
  16440. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16441. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16442. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16443. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16444. };
  16445. const byte a[] = {
  16446. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16447. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16448. 0xab, 0xad, 0xda, 0xd2
  16449. };
  16450. byte iv[] = "1234567890a";
  16451. byte longIV[] = "1234567890abcdefghij";
  16452. byte enc[sizeof(vector)];
  16453. byte resultT[AES_BLOCK_SIZE];
  16454. byte dec[sizeof(vector)];
  16455. /* Init stack variables. */
  16456. XMEMSET(&aes, 0, sizeof(Aes));
  16457. XMEMSET(enc, 0, sizeof(vector));
  16458. XMEMSET(dec, 0, sizeof(vector));
  16459. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16460. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16461. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16462. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16463. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16464. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16465. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16466. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16467. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16468. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16469. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16470. BAD_FUNC_ARG);
  16471. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16472. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16473. BAD_FUNC_ARG);
  16474. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16475. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16476. BAD_FUNC_ARG);
  16477. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16478. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16479. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16480. /* FIPS does not check the lower bound of ivSz */
  16481. #else
  16482. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16483. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16484. #endif
  16485. /* This case is now considered good. Long IVs are now allowed.
  16486. * Except for the original FIPS release, it still has an upper
  16487. * bound on the IV length. */
  16488. #if (!defined(HAVE_FIPS) || \
  16489. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16490. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16491. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16492. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16493. 0);
  16494. #else
  16495. (void)longIV;
  16496. #endif /* Old FIPS */
  16497. /* END wc_AesGcmEncrypt */
  16498. #ifdef HAVE_AES_DECRYPT
  16499. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16500. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16501. BAD_FUNC_ARG);
  16502. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16503. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16504. BAD_FUNC_ARG);
  16505. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16506. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16507. BAD_FUNC_ARG);
  16508. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16509. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16510. BAD_FUNC_ARG);
  16511. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16512. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16513. BAD_FUNC_ARG);
  16514. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16515. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16516. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16517. AES_GCM_AUTH_E);
  16518. #else
  16519. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16520. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16521. BAD_FUNC_ARG);
  16522. #endif
  16523. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16524. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16525. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16526. /* FIPS does not check the lower bound of ivSz */
  16527. #else
  16528. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16529. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16530. #endif
  16531. #endif /* HAVE_AES_DECRYPT */
  16532. wc_AesFree(&aes);
  16533. #endif
  16534. return EXPECT_RESULT();
  16535. } /* END test_wc_AesGcmEncryptDecrypt */
  16536. /*
  16537. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16538. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16539. */
  16540. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16541. {
  16542. EXPECT_DECLS;
  16543. #if (!defined(HAVE_FIPS) || \
  16544. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16545. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16546. const byte key[] = {
  16547. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16548. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16549. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16550. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16551. };
  16552. const byte in[] = {
  16553. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16554. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16555. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16556. };
  16557. const byte aad[] = {
  16558. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16559. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16560. 0xab, 0xad, 0xda, 0xd2
  16561. };
  16562. Aes aesEnc;
  16563. Aes aesDec;
  16564. byte iv[] = "1234567890abcdefghij";
  16565. byte out[sizeof(in)];
  16566. byte plain[sizeof(in)];
  16567. byte tag[AES_BLOCK_SIZE];
  16568. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16569. XMEMSET(&aesDec, 0, sizeof(Aes));
  16570. XMEMSET(out, 0, sizeof(out));
  16571. XMEMSET(plain, 0, sizeof(plain));
  16572. XMEMSET(tag, 0, sizeof(tag));
  16573. /* Perform one-shot encryption using long IV */
  16574. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16575. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16576. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16577. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16578. /* Perform streaming decryption using long IV */
  16579. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16580. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16581. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16582. sizeof(aad)), 0);
  16583. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16584. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16585. /* Free resources */
  16586. wc_AesFree(&aesEnc);
  16587. wc_AesFree(&aesDec);
  16588. #endif
  16589. return EXPECT_RESULT();
  16590. } /* END wc_AesGcmMixedEncDecLongIV */
  16591. /*
  16592. * unit test for wc_GmacSetKey()
  16593. */
  16594. static int test_wc_GmacSetKey(void)
  16595. {
  16596. EXPECT_DECLS;
  16597. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16598. Gmac gmac;
  16599. byte key16[] = {
  16600. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16601. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16602. };
  16603. #ifdef WOLFSSL_AES_192
  16604. byte key24[] = {
  16605. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16606. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16607. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16608. };
  16609. #endif
  16610. #ifdef WOLFSSL_AES_256
  16611. byte key32[] = {
  16612. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16613. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16614. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16615. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16616. };
  16617. #endif
  16618. byte badKey16[] = {
  16619. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16620. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16621. };
  16622. byte badKey24[] = {
  16623. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16624. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16625. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16626. };
  16627. byte badKey32[] = {
  16628. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16629. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16630. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16631. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16632. };
  16633. XMEMSET(&gmac, 0, sizeof(Gmac));
  16634. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16635. #ifdef WOLFSSL_AES_128
  16636. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16637. #endif
  16638. #ifdef WOLFSSL_AES_192
  16639. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16640. #endif
  16641. #ifdef WOLFSSL_AES_256
  16642. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16643. #endif
  16644. /* Pass in bad args. */
  16645. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16646. BAD_FUNC_ARG);
  16647. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16648. BAD_FUNC_ARG);
  16649. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16650. BAD_FUNC_ARG);
  16651. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16652. BAD_FUNC_ARG);
  16653. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16654. BAD_FUNC_ARG);
  16655. wc_AesFree(&gmac.aes);
  16656. #endif
  16657. return EXPECT_RESULT();
  16658. } /* END test_wc_GmacSetKey */
  16659. /*
  16660. * unit test for wc_GmacUpdate
  16661. */
  16662. static int test_wc_GmacUpdate(void)
  16663. {
  16664. EXPECT_DECLS;
  16665. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16666. Gmac gmac;
  16667. #ifdef WOLFSSL_AES_128
  16668. const byte key16[] = {
  16669. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16670. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16671. };
  16672. #endif
  16673. #ifdef WOLFSSL_AES_192
  16674. byte key24[] = {
  16675. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16676. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16677. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16678. };
  16679. #endif
  16680. #ifdef WOLFSSL_AES_256
  16681. byte key32[] = {
  16682. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16683. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16684. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16685. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16686. };
  16687. #endif
  16688. #ifdef WOLFSSL_AES_128
  16689. const byte authIn[] = {
  16690. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16691. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16692. };
  16693. #endif
  16694. #ifdef WOLFSSL_AES_192
  16695. const byte authIn2[] = {
  16696. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16697. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16698. };
  16699. #endif
  16700. const byte authIn3[] = {
  16701. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16702. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16703. };
  16704. #ifdef WOLFSSL_AES_128
  16705. const byte tag1[] = { /* Known. */
  16706. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16707. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16708. };
  16709. #endif
  16710. #ifdef WOLFSSL_AES_192
  16711. const byte tag2[] = { /* Known */
  16712. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16713. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16714. };
  16715. #endif
  16716. const byte tag3[] = { /* Known */
  16717. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16718. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16719. };
  16720. #ifdef WOLFSSL_AES_128
  16721. const byte iv[] = {
  16722. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16723. 0xe2, 0x8c, 0x8f, 0x16
  16724. };
  16725. #endif
  16726. #ifdef WOLFSSL_AES_192
  16727. const byte iv2[] = {
  16728. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16729. 0x7e, 0x1a, 0x6f, 0xbc
  16730. };
  16731. #endif
  16732. const byte iv3[] = {
  16733. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16734. 0xc3, 0xfb, 0x6c, 0x8a
  16735. };
  16736. byte tagOut[16];
  16737. byte tagOut2[24];
  16738. byte tagOut3[32];
  16739. /* Init stack variables. */
  16740. XMEMSET(&gmac, 0, sizeof(Gmac));
  16741. XMEMSET(tagOut, 0, sizeof(tagOut));
  16742. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16743. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16744. #ifdef WOLFSSL_AES_128
  16745. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16746. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16747. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16748. tagOut, sizeof(tag1)), 0);
  16749. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16750. wc_AesFree(&gmac.aes);
  16751. #endif
  16752. #ifdef WOLFSSL_AES_192
  16753. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16754. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16755. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16756. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16757. tagOut2, sizeof(tag2)), 0);
  16758. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16759. wc_AesFree(&gmac.aes);
  16760. #endif
  16761. #ifdef WOLFSSL_AES_256
  16762. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16763. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16764. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16765. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16766. tagOut3, sizeof(tag3)), 0);
  16767. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16768. wc_AesFree(&gmac.aes);
  16769. #endif
  16770. /* Pass bad args. */
  16771. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16772. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16773. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16774. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16775. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16776. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16777. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16778. wc_AesFree(&gmac.aes);
  16779. #endif
  16780. return EXPECT_RESULT();
  16781. } /* END test_wc_GmacUpdate */
  16782. /*
  16783. * testing wc_CamelliaSetKey
  16784. */
  16785. static int test_wc_CamelliaSetKey(void)
  16786. {
  16787. EXPECT_DECLS;
  16788. #ifdef HAVE_CAMELLIA
  16789. Camellia camellia;
  16790. /*128-bit key*/
  16791. static const byte key16[] = {
  16792. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16793. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16794. };
  16795. /* 192-bit key */
  16796. static const byte key24[] = {
  16797. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16798. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16799. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16800. };
  16801. /* 256-bit key */
  16802. static const byte key32[] = {
  16803. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16804. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16805. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16806. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16807. };
  16808. static const byte iv[] = {
  16809. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16810. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16811. };
  16812. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16813. 0);
  16814. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16815. NULL), 0);
  16816. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16817. 0);
  16818. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16819. NULL), 0);
  16820. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16821. 0);
  16822. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16823. NULL), 0);
  16824. /* Bad args. */
  16825. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16826. BAD_FUNC_ARG);
  16827. #endif
  16828. return EXPECT_RESULT();
  16829. } /* END test_wc_CammeliaSetKey */
  16830. /*
  16831. * Testing wc_CamelliaSetIV()
  16832. */
  16833. static int test_wc_CamelliaSetIV(void)
  16834. {
  16835. EXPECT_DECLS;
  16836. #ifdef HAVE_CAMELLIA
  16837. Camellia camellia;
  16838. static const byte iv[] = {
  16839. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16840. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16841. };
  16842. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16843. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16844. /* Bad args. */
  16845. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16846. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16847. #endif
  16848. return EXPECT_RESULT();
  16849. } /* END test_wc_CamelliaSetIV*/
  16850. /*
  16851. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16852. */
  16853. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16854. {
  16855. EXPECT_DECLS;
  16856. #ifdef HAVE_CAMELLIA
  16857. Camellia camellia;
  16858. static const byte key24[] = {
  16859. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16860. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16861. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16862. };
  16863. static const byte iv[] = {
  16864. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16865. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16866. };
  16867. static const byte plainT[] = {
  16868. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16869. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16870. };
  16871. byte enc[sizeof(plainT)];
  16872. byte dec[sizeof(enc)];
  16873. /* Init stack variables.*/
  16874. XMEMSET(enc, 0, 16);
  16875. XMEMSET(enc, 0, 16);
  16876. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16877. 0);
  16878. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16879. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16880. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16881. /* Pass bad args. */
  16882. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16883. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16884. BAD_FUNC_ARG);
  16885. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16886. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16887. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16888. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16889. #endif
  16890. return EXPECT_RESULT();
  16891. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16892. /*
  16893. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16894. */
  16895. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16896. {
  16897. EXPECT_DECLS;
  16898. #ifdef HAVE_CAMELLIA
  16899. Camellia camellia;
  16900. static const byte key24[] = {
  16901. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16902. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16903. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16904. };
  16905. static const byte plainT[] = {
  16906. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16907. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16908. };
  16909. byte enc[CAMELLIA_BLOCK_SIZE];
  16910. byte dec[CAMELLIA_BLOCK_SIZE];
  16911. /* Init stack variables. */
  16912. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16913. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16914. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16915. NULL), 0);
  16916. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16917. CAMELLIA_BLOCK_SIZE), 0);
  16918. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16919. NULL), 0);
  16920. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16921. 0);
  16922. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16923. /* Pass in bad args. */
  16924. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16925. BAD_FUNC_ARG);
  16926. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16927. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16928. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16929. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16930. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16931. BAD_FUNC_ARG);
  16932. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16933. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16934. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16935. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16936. #endif
  16937. return EXPECT_RESULT();
  16938. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16939. /*
  16940. * Testing wc_Arc4SetKey()
  16941. */
  16942. static int test_wc_Arc4SetKey(void)
  16943. {
  16944. EXPECT_DECLS;
  16945. #ifndef NO_RC4
  16946. Arc4 arc;
  16947. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16948. int keyLen = 8;
  16949. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, (word32)keyLen), 0);
  16950. /* Test bad args. */
  16951. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, (word32)keyLen), BAD_FUNC_ARG);
  16952. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , (word32)keyLen), BAD_FUNC_ARG);
  16953. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16954. #endif
  16955. return EXPECT_RESULT();
  16956. } /* END test_wc_Arc4SetKey */
  16957. /*
  16958. * Testing wc_Arc4Process for ENC/DEC.
  16959. */
  16960. static int test_wc_Arc4Process(void)
  16961. {
  16962. EXPECT_DECLS;
  16963. #ifndef NO_RC4
  16964. Arc4 enc;
  16965. Arc4 dec;
  16966. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16967. int keyLen = 8;
  16968. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16969. byte cipher[8];
  16970. byte plain[8];
  16971. /* Init stack variables */
  16972. XMEMSET(&enc, 0, sizeof(Arc4));
  16973. XMEMSET(&dec, 0, sizeof(Arc4));
  16974. XMEMSET(cipher, 0, sizeof(cipher));
  16975. XMEMSET(plain, 0, sizeof(plain));
  16976. /* Use for async. */
  16977. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16978. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16979. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, (word32)keyLen), 0);
  16980. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, (word32)keyLen), 0);
  16981. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, (word32)keyLen), 0);
  16982. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, (word32)keyLen), 0);
  16983. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16984. /* Bad args. */
  16985. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, (word32)keyLen), BAD_FUNC_ARG);
  16986. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, (word32)keyLen), BAD_FUNC_ARG);
  16987. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, (word32)keyLen), BAD_FUNC_ARG);
  16988. wc_Arc4Free(&enc);
  16989. wc_Arc4Free(&dec);
  16990. #endif
  16991. return EXPECT_RESULT();
  16992. } /* END test_wc_Arc4Process */
  16993. /*
  16994. * Testing wc_Init RsaKey()
  16995. */
  16996. static int test_wc_InitRsaKey(void)
  16997. {
  16998. EXPECT_DECLS;
  16999. #ifndef NO_RSA
  17000. RsaKey key;
  17001. XMEMSET(&key, 0, sizeof(RsaKey));
  17002. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17003. /* Test bad args. */
  17004. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  17005. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17006. #endif
  17007. return EXPECT_RESULT();
  17008. } /* END test_wc_InitRsaKey */
  17009. /*
  17010. * Testing wc_RsaPrivateKeyDecode()
  17011. */
  17012. static int test_wc_RsaPrivateKeyDecode(void)
  17013. {
  17014. EXPECT_DECLS;
  17015. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17016. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17017. RsaKey key;
  17018. byte* tmp = NULL;
  17019. word32 idx = 0;
  17020. int bytes = 0;
  17021. XMEMSET(&key, 0, sizeof(RsaKey));
  17022. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17023. DYNAMIC_TYPE_TMP_BUFFER));
  17024. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17025. if (tmp != NULL) {
  17026. #ifdef USE_CERT_BUFFERS_1024
  17027. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17028. bytes = sizeof_client_key_der_1024;
  17029. #else
  17030. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17031. bytes = sizeof_client_key_der_2048;
  17032. #endif /* Use cert buffers. */
  17033. }
  17034. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17035. /* Test bad args. */
  17036. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17037. BAD_FUNC_ARG);
  17038. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17039. BAD_FUNC_ARG);
  17040. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17041. BAD_FUNC_ARG);
  17042. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17043. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17044. #endif
  17045. return EXPECT_RESULT();
  17046. } /* END test_wc_RsaPrivateKeyDecode */
  17047. /*
  17048. * Testing wc_RsaPublicKeyDecode()
  17049. */
  17050. static int test_wc_RsaPublicKeyDecode(void)
  17051. {
  17052. EXPECT_DECLS;
  17053. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17054. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17055. RsaKey keyPub;
  17056. byte* tmp = NULL;
  17057. word32 idx = 0;
  17058. int bytes = 0;
  17059. word32 keySz = 0;
  17060. word32 tstKeySz = 0;
  17061. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17062. XFILE f = XBADFILE;
  17063. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17064. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17065. byte buf[4096];
  17066. #endif
  17067. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17068. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17069. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17070. if (tmp != NULL) {
  17071. #ifdef USE_CERT_BUFFERS_1024
  17072. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17073. bytes = sizeof_client_keypub_der_1024;
  17074. keySz = 1024;
  17075. #else
  17076. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17077. bytes = sizeof_client_keypub_der_2048;
  17078. keySz = 2048;
  17079. #endif
  17080. }
  17081. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17082. /* Pass in bad args. */
  17083. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17084. BAD_FUNC_ARG);
  17085. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17086. BAD_FUNC_ARG);
  17087. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17088. BAD_FUNC_ARG);
  17089. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17090. /* Test for getting modulus key size */
  17091. idx = 0;
  17092. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17093. &tstKeySz, NULL, NULL), 0);
  17094. ExpectIntEQ(tstKeySz, keySz/8);
  17095. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17096. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17097. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17098. if (f != XBADFILE) {
  17099. XFCLOSE(f);
  17100. f = XBADFILE;
  17101. }
  17102. idx = 0;
  17103. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17104. NULL), 0);
  17105. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17106. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17107. if (f != XBADFILE)
  17108. XFCLOSE(f);
  17109. idx = 0;
  17110. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17111. NULL), 0);
  17112. #endif
  17113. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17114. #endif
  17115. return EXPECT_RESULT();
  17116. } /* END test_wc_RsaPublicKeyDecode */
  17117. /*
  17118. * Testing wc_RsaPublicKeyDecodeRaw()
  17119. */
  17120. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17121. {
  17122. EXPECT_DECLS;
  17123. #if !defined(NO_RSA)
  17124. RsaKey key;
  17125. const byte n = 0x23;
  17126. const byte e = 0x03;
  17127. int nSz = sizeof(n);
  17128. int eSz = sizeof(e);
  17129. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17130. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17131. /* Pass in bad args. */
  17132. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17133. BAD_FUNC_ARG);
  17134. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17135. BAD_FUNC_ARG);
  17136. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17137. BAD_FUNC_ARG);
  17138. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17139. #endif
  17140. return EXPECT_RESULT();
  17141. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17142. /*
  17143. * Testing wc_RsaPrivateKeyDecodeRaw()
  17144. */
  17145. static int test_wc_RsaPrivateKeyDecodeRaw(void)
  17146. {
  17147. EXPECT_DECLS;
  17148. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  17149. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  17150. RsaKey key;
  17151. const byte n = 33;
  17152. const byte e = 3;
  17153. const byte d = 7;
  17154. const byte u = 2;
  17155. const byte p = 3;
  17156. const byte q = 11;
  17157. const byte dp = 1;
  17158. const byte dq = 7;
  17159. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17160. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17161. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17162. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17163. NULL, 0, &key), 0);
  17164. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17165. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17166. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17167. NULL, 0, &key), 0);
  17168. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17169. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17170. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17171. &dq, sizeof(dq), &key), 0);
  17172. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17173. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17174. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17175. &dq, sizeof(dq), &key), 0);
  17176. /* Pass in bad args. */
  17177. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(NULL, sizeof(n),
  17178. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17179. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17180. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17181. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, 0,
  17182. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17183. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17184. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17185. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17186. NULL, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17187. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17188. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17189. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17190. &e, 0, &d, sizeof(d), &u, sizeof(u),
  17191. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17192. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17193. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17194. &e, sizeof(e), NULL, sizeof(d), &u, sizeof(u),
  17195. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17196. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17197. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17198. &e, sizeof(e), &d, 0, &u, sizeof(u),
  17199. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17200. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17201. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17202. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17203. NULL, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17204. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17205. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17206. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17207. &p, 0, &q, sizeof(q), &dp, sizeof(dp),
  17208. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17209. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17210. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17211. &p, sizeof(p), NULL, sizeof(q), &dp, sizeof(dp),
  17212. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17213. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17214. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17215. &p, sizeof(p), &q, 0, &dp, sizeof(dp),
  17216. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17217. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
  17218. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17219. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17220. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17221. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17222. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17223. &e, sizeof(e), &d, sizeof(d), NULL, sizeof(u),
  17224. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17225. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17226. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17227. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17228. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17229. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17230. #endif
  17231. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17232. #endif
  17233. return EXPECT_RESULT();
  17234. } /* END test_wc_RsaPrivateKeyDecodeRaw */
  17235. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17236. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17237. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17238. * trying until it gets a probable prime. */
  17239. #ifdef HAVE_FIPS
  17240. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17241. {
  17242. int ret;
  17243. for (;;) {
  17244. ret = wc_MakeRsaKey(key, size, e, rng);
  17245. if (ret != PRIME_GEN_E) break;
  17246. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17247. "trying again.\n");
  17248. }
  17249. return ret;
  17250. }
  17251. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17252. #else
  17253. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17254. #endif
  17255. #endif
  17256. /*
  17257. * Testing wc_MakeRsaKey()
  17258. */
  17259. static int test_wc_MakeRsaKey(void)
  17260. {
  17261. EXPECT_DECLS;
  17262. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17263. RsaKey genKey;
  17264. WC_RNG rng;
  17265. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17266. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17267. int bits = 1024;
  17268. #else
  17269. int bits = 2048;
  17270. #endif
  17271. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17272. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17273. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17274. ExpectIntEQ(wc_InitRng(&rng), 0);
  17275. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17276. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17277. /* Test bad args. */
  17278. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17279. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17280. BAD_FUNC_ARG);
  17281. /* e < 3 */
  17282. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17283. /* e & 1 == 0 */
  17284. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17285. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17286. #endif
  17287. return EXPECT_RESULT();
  17288. } /* END test_wc_MakeRsaKey */
  17289. /*
  17290. * Test the bounds checking on the cipher text versus the key modulus.
  17291. * 1. Make a new RSA key.
  17292. * 2. Set c to 1.
  17293. * 3. Decrypt c into k. (error)
  17294. * 4. Copy the key modulus to c and sub 1 from the copy.
  17295. * 5. Decrypt c into k. (error)
  17296. * Valid bounds test cases are covered by all the other RSA tests.
  17297. */
  17298. static int test_RsaDecryptBoundsCheck(void)
  17299. {
  17300. EXPECT_DECLS;
  17301. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17302. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17303. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17304. WC_RNG rng;
  17305. RsaKey key;
  17306. byte flatC[256];
  17307. word32 flatCSz;
  17308. byte out[256];
  17309. word32 outSz = sizeof(out);
  17310. XMEMSET(&key, 0, sizeof(RsaKey));
  17311. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17312. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17313. ExpectIntEQ(wc_InitRng(&rng), 0);
  17314. if (EXPECT_SUCCESS()) {
  17315. const byte* derKey;
  17316. word32 derKeySz;
  17317. word32 idx = 0;
  17318. #ifdef USE_CERT_BUFFERS_1024
  17319. derKey = server_key_der_1024;
  17320. derKeySz = (word32)sizeof_server_key_der_1024;
  17321. flatCSz = 128;
  17322. #else
  17323. derKey = server_key_der_2048;
  17324. derKeySz = (word32)sizeof_server_key_der_2048;
  17325. flatCSz = 256;
  17326. #endif
  17327. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17328. }
  17329. if (EXPECT_SUCCESS()) {
  17330. XMEMSET(flatC, 0, flatCSz);
  17331. flatC[flatCSz-1] = 1;
  17332. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17333. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17334. if (EXPECT_SUCCESS()) {
  17335. mp_int c;
  17336. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17337. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17338. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17339. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17340. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17341. mp_clear(&c);
  17342. }
  17343. }
  17344. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17345. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17346. #endif
  17347. return EXPECT_RESULT();
  17348. } /* END test_wc_RsaDecryptBoundsCheck */
  17349. /*
  17350. * Testing wc_SetKeyUsage()
  17351. */
  17352. static int test_wc_SetKeyUsage(void)
  17353. {
  17354. EXPECT_DECLS;
  17355. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17356. Cert myCert;
  17357. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17358. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17359. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17360. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17361. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17362. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17363. /* Test bad args. */
  17364. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17365. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17366. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17367. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17368. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17369. KEYUSAGE_E);
  17370. #endif
  17371. return EXPECT_RESULT();
  17372. } /* END test_wc_SetKeyUsage */
  17373. /*
  17374. * Testing wc_CheckProbablePrime()
  17375. */
  17376. static int test_wc_CheckProbablePrime(void)
  17377. {
  17378. EXPECT_DECLS;
  17379. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17380. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17381. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17382. RsaKey key;
  17383. WC_RNG rng;
  17384. byte e[3];
  17385. word32 eSz = (word32)sizeof(e);
  17386. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17387. word32 nSz = (word32)sizeof(n);
  17388. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17389. word32 dSz = (word32)sizeof(d);
  17390. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17391. word32 pSz = (word32)sizeof(p);
  17392. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17393. word32 qSz = (word32)sizeof(q);
  17394. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17395. int* isPrime;
  17396. int test[5];
  17397. isPrime = test;
  17398. XMEMSET(&key, 0, sizeof(RsaKey));
  17399. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17400. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17401. ExpectIntEQ(wc_InitRng(&rng), 0);
  17402. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17403. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17404. WC_RSA_EXPONENT, &rng), 0);
  17405. PRIVATE_KEY_UNLOCK();
  17406. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17407. &qSz), 0);
  17408. PRIVATE_KEY_LOCK();
  17409. /* Bad cases */
  17410. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17411. BAD_FUNC_ARG);
  17412. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17413. BAD_FUNC_ARG);
  17414. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17415. BAD_FUNC_ARG);
  17416. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17417. BAD_FUNC_ARG);
  17418. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17419. BAD_FUNC_ARG);
  17420. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17421. BAD_FUNC_ARG);
  17422. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17423. BAD_FUNC_ARG);
  17424. /* Good case */
  17425. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17426. 0);
  17427. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17428. wc_FreeRng(&rng);
  17429. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17430. #endif
  17431. return EXPECT_RESULT();
  17432. } /* END test_wc_CheckProbablePrime */
  17433. /*
  17434. * Testing wc_RsaPSS_Verify()
  17435. */
  17436. static int test_wc_RsaPSS_Verify(void)
  17437. {
  17438. EXPECT_DECLS;
  17439. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17440. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17441. RsaKey key;
  17442. WC_RNG rng;
  17443. int sz = 256;
  17444. const char* szMessage = "This is the string to be signed";
  17445. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17446. unsigned char pDecrypted[2048/8];
  17447. byte* pt = pDecrypted;
  17448. word32 outLen = sizeof(pDecrypted);
  17449. XMEMSET(&key, 0, sizeof(RsaKey));
  17450. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17451. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17452. ExpectIntEQ(wc_InitRng(&rng), 0);
  17453. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17454. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17455. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17456. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17457. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17458. /* Bad cases */
  17459. ExpectIntEQ(wc_RsaPSS_Verify(NULL, (word32)sz, pt, outLen,
  17460. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17461. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17462. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17463. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, (word32)sz, NULL, outLen,
  17464. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17465. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17466. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17467. /* Good case */
  17468. ExpectIntGT(wc_RsaPSS_Verify(pSignature, (word32)sz, pt, outLen,
  17469. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17470. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17471. wc_FreeRng(&rng);
  17472. #endif
  17473. return EXPECT_RESULT();
  17474. } /* END test_wc_RsaPSS_Verify */
  17475. /*
  17476. * Testing wc_RsaPSS_VerifyCheck()
  17477. */
  17478. static int test_wc_RsaPSS_VerifyCheck(void)
  17479. {
  17480. EXPECT_DECLS;
  17481. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17482. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17483. RsaKey key;
  17484. WC_RNG rng;
  17485. int sz = 256; /* 2048/8 */
  17486. byte digest[32];
  17487. word32 digestSz = sizeof(digest);
  17488. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17489. word32 pSignatureSz = sizeof(pSignature);
  17490. unsigned char pDecrypted[2048/8];
  17491. byte* pt = pDecrypted;
  17492. word32 outLen = sizeof(pDecrypted);
  17493. XMEMSET(&key, 0, sizeof(RsaKey));
  17494. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17495. XMEMSET(digest, 0, sizeof(digest));
  17496. XMEMSET(pSignature, 0, sizeof(pSignature));
  17497. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17498. ExpectIntEQ(wc_InitRng(&rng), 0);
  17499. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17500. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17501. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17502. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17503. 0);
  17504. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17505. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17506. /* Bad cases */
  17507. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, (word32)sz, pt, outLen, digest,
  17508. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17509. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17510. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17511. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, NULL, outLen, digest,
  17512. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17513. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17514. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17515. /* Good case */
  17516. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, pt, outLen, digest,
  17517. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17518. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17519. wc_FreeRng(&rng);
  17520. #endif
  17521. return EXPECT_RESULT();
  17522. } /* END test_wc_RsaPSS_VerifyCheck */
  17523. /*
  17524. * Testing wc_RsaPSS_VerifyCheckInline()
  17525. */
  17526. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17527. {
  17528. EXPECT_DECLS;
  17529. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17530. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17531. RsaKey key;
  17532. WC_RNG rng;
  17533. int sz = 256;
  17534. byte digest[32];
  17535. word32 digestSz = sizeof(digest);
  17536. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17537. unsigned char pDecrypted[2048/8];
  17538. byte* pt = pDecrypted;
  17539. XMEMSET(&key, 0, sizeof(RsaKey));
  17540. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17541. XMEMSET(digest, 0, sizeof(digest));
  17542. XMEMSET(pSignature, 0, sizeof(pSignature));
  17543. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17544. ExpectIntEQ(wc_InitRng(&rng), 0);
  17545. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17546. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17547. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17548. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17549. 0);
  17550. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17551. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17552. /* Bad Cases */
  17553. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, (word32)sz, &pt, digest,
  17554. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17555. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17556. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17557. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17558. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17559. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17560. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17561. /* Good case */
  17562. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17563. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17564. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17565. wc_FreeRng(&rng);
  17566. #endif
  17567. return EXPECT_RESULT();
  17568. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17569. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17570. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17571. {
  17572. (void)flag;
  17573. (void)type;
  17574. (void)file;
  17575. (void)line;
  17576. }
  17577. #endif
  17578. /*
  17579. * Testing wc_LockMutex_ex
  17580. */
  17581. static int test_wc_LockMutex_ex(void)
  17582. {
  17583. EXPECT_DECLS;
  17584. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17585. int flag = CRYPTO_LOCK;
  17586. int type = 0;
  17587. const char* file = "./test-LockMutex_ex.txt";
  17588. int line = 0;
  17589. /* without SetMutexCb */
  17590. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17591. /* with SetMutexCb */
  17592. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17593. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17594. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17595. #endif
  17596. return EXPECT_RESULT();
  17597. } /* End test_wc_LockMutex_ex*/
  17598. /*
  17599. * Testing wc_SetMutexCb
  17600. */
  17601. static int test_wc_SetMutexCb(void)
  17602. {
  17603. EXPECT_DECLS;
  17604. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17605. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17606. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17607. #endif
  17608. return EXPECT_RESULT();
  17609. } /* End test_wc_SetMutexCb*/
  17610. /*
  17611. * Testing wc_RsaKeyToDer()
  17612. */
  17613. static int test_wc_RsaKeyToDer(void)
  17614. {
  17615. EXPECT_DECLS;
  17616. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17617. RsaKey genKey;
  17618. WC_RNG rng;
  17619. byte* der = NULL;
  17620. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17621. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17622. int bits = 1024;
  17623. word32 derSz = 611;
  17624. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17625. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17626. #else
  17627. int bits = 2048;
  17628. word32 derSz = 1196;
  17629. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17630. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17631. #endif
  17632. XMEMSET(&rng, 0, sizeof(rng));
  17633. XMEMSET(&genKey, 0, sizeof(genKey));
  17634. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17635. /* Init structures. */
  17636. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17637. ExpectIntEQ(wc_InitRng(&rng), 0);
  17638. /* Make key. */
  17639. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17640. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17641. /* Pass good/bad args. */
  17642. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17643. /* Get just the output length */
  17644. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17645. /* Try Public Key. */
  17646. genKey.type = 0;
  17647. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17648. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17649. /* Put back to Private Key */
  17650. genKey.type = 1;
  17651. #endif
  17652. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17653. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17654. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17655. #endif
  17656. return EXPECT_RESULT();
  17657. } /* END test_wc_RsaKeyToDer */
  17658. /*
  17659. * Testing wc_RsaKeyToPublicDer()
  17660. */
  17661. static int test_wc_RsaKeyToPublicDer(void)
  17662. {
  17663. EXPECT_DECLS;
  17664. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17665. RsaKey key;
  17666. WC_RNG rng;
  17667. byte* der = NULL;
  17668. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17669. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17670. int bits = 1024;
  17671. word32 derLen = 162;
  17672. #else
  17673. int bits = 2048;
  17674. word32 derLen = 294;
  17675. #endif
  17676. int ret;
  17677. XMEMSET(&rng, 0, sizeof(rng));
  17678. XMEMSET(&key, 0, sizeof(key));
  17679. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17680. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17681. ExpectIntEQ(wc_InitRng(&rng), 0);
  17682. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17683. /* test getting size only */
  17684. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17685. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17686. /* test getting size only */
  17687. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17688. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17689. /* Pass in bad args. */
  17690. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17691. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17692. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17693. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17694. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17695. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17696. #endif
  17697. return EXPECT_RESULT();
  17698. } /* END test_wc_RsaKeyToPublicDer */
  17699. /*
  17700. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17701. */
  17702. static int test_wc_RsaPublicEncryptDecrypt(void)
  17703. {
  17704. EXPECT_DECLS;
  17705. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17706. RsaKey key;
  17707. WC_RNG rng;
  17708. const char inStr[] = TEST_STRING;
  17709. const word32 plainLen = (word32)TEST_STRING_SZ;
  17710. const word32 inLen = (word32)TEST_STRING_SZ;
  17711. int bits = TEST_RSA_BITS;
  17712. const word32 cipherLen = TEST_RSA_BYTES;
  17713. word32 cipherLenResult = cipherLen;
  17714. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17715. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17716. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17717. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17718. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17719. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17720. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17721. ExpectNotNull(in);
  17722. ExpectNotNull(plain);
  17723. ExpectNotNull(cipher);
  17724. #endif
  17725. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17726. /* Initialize stack structures. */
  17727. XMEMSET(&key, 0, sizeof(RsaKey));
  17728. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17729. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17730. ExpectIntEQ(wc_InitRng(&rng), 0);
  17731. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17732. /* Encrypt. */
  17733. ExpectIntGT(cipherLenResult = (word32)wc_RsaPublicEncrypt(in, inLen, cipher,
  17734. cipherLen, &key, &rng), 0);
  17735. /* Pass bad args - tested in another testing function.*/
  17736. /* Decrypt */
  17737. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17738. /* Bind rng */
  17739. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17740. #endif
  17741. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17742. &key), 0);
  17743. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17744. /* Pass bad args - tested in another testing function.*/
  17745. WC_FREE_VAR(in, NULL);
  17746. WC_FREE_VAR(plain, NULL);
  17747. WC_FREE_VAR(cipher, NULL);
  17748. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17749. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17750. #endif
  17751. return EXPECT_RESULT();
  17752. } /* END test_wc_RsaPublicEncryptDecrypt */
  17753. /*
  17754. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17755. */
  17756. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17757. {
  17758. EXPECT_DECLS;
  17759. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17760. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17761. RsaKey key;
  17762. WC_RNG rng;
  17763. const char inStr[] = TEST_STRING;
  17764. const word32 inLen = (word32)TEST_STRING_SZ;
  17765. const word32 plainSz = (word32)TEST_STRING_SZ;
  17766. byte* res = NULL;
  17767. int idx = 0;
  17768. int bits = TEST_RSA_BITS;
  17769. const word32 cipherSz = TEST_RSA_BYTES;
  17770. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17771. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17772. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17773. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17774. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17775. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17776. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17777. ExpectNotNull(in);
  17778. ExpectNotNull(plain);
  17779. ExpectNotNull(cipher);
  17780. #endif
  17781. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17782. /* Initialize stack structures. */
  17783. XMEMSET(&key, 0, sizeof(RsaKey));
  17784. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17785. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17786. ExpectIntEQ(wc_InitRng(&rng), 0);
  17787. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17788. /* Encrypt */
  17789. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17790. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17791. /* Pass bad args - tested in another testing function.*/
  17792. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17793. /* Decrypt */
  17794. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17795. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17796. #endif
  17797. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17798. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17799. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17800. /* Pass bad args - tested in another testing function.*/
  17801. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17802. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17803. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17804. #endif
  17805. WC_FREE_VAR(in, NULL);
  17806. WC_FREE_VAR(plain, NULL);
  17807. WC_FREE_VAR(cipher, NULL);
  17808. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17809. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17810. #endif
  17811. return EXPECT_RESULT();
  17812. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17813. /*
  17814. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17815. */
  17816. static int test_wc_RsaSSL_SignVerify(void)
  17817. {
  17818. EXPECT_DECLS;
  17819. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17820. RsaKey key;
  17821. WC_RNG rng;
  17822. const char inStr[] = TEST_STRING;
  17823. const word32 plainSz = (word32)TEST_STRING_SZ;
  17824. const word32 inLen = (word32)TEST_STRING_SZ;
  17825. word32 idx = 0;
  17826. int bits = TEST_RSA_BITS;
  17827. const word32 outSz = TEST_RSA_BYTES;
  17828. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17829. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17830. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17831. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17832. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17833. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17834. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17835. ExpectNotNull(in);
  17836. ExpectNotNull(out);
  17837. ExpectNotNull(plain);
  17838. #endif
  17839. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17840. XMEMSET(&key, 0, sizeof(RsaKey));
  17841. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17842. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17843. ExpectIntEQ(wc_InitRng(&rng), 0);
  17844. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17845. /* Sign. */
  17846. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17847. idx = (int)outSz;
  17848. /* Test bad args. */
  17849. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17850. BAD_FUNC_ARG);
  17851. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17852. BAD_FUNC_ARG);
  17853. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17854. BAD_FUNC_ARG);
  17855. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17856. BAD_FUNC_ARG);
  17857. /* Verify. */
  17858. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17859. /* Pass bad args. */
  17860. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17861. BAD_FUNC_ARG);
  17862. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17863. BAD_FUNC_ARG);
  17864. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17865. BAD_FUNC_ARG);
  17866. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17867. BAD_FUNC_ARG);
  17868. WC_FREE_VAR(in, NULL);
  17869. WC_FREE_VAR(out, NULL);
  17870. WC_FREE_VAR(plain, NULL);
  17871. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17872. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17873. #endif
  17874. return EXPECT_RESULT();
  17875. } /* END test_wc_RsaSSL_SignVerify */
  17876. /*
  17877. * Testing wc_RsaEncryptSize()
  17878. */
  17879. static int test_wc_RsaEncryptSize(void)
  17880. {
  17881. EXPECT_DECLS;
  17882. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17883. RsaKey key;
  17884. WC_RNG rng;
  17885. XMEMSET(&key, 0, sizeof(RsaKey));
  17886. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17887. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17888. ExpectIntEQ(wc_InitRng(&rng), 0);
  17889. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17890. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17891. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17892. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17893. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17894. #endif
  17895. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17896. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17897. /* Pass in bad arg. */
  17898. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17899. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17900. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17901. #endif
  17902. return EXPECT_RESULT();
  17903. } /* END test_wc_RsaEncryptSize*/
  17904. /*
  17905. * Testing wc_RsaFlattenPublicKey()
  17906. */
  17907. static int test_wc_RsaFlattenPublicKey(void)
  17908. {
  17909. EXPECT_DECLS;
  17910. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17911. RsaKey key;
  17912. WC_RNG rng;
  17913. byte e[256];
  17914. byte n[256];
  17915. word32 eSz = sizeof(e);
  17916. word32 nSz = sizeof(n);
  17917. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17918. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17919. int bits = 1024;
  17920. #else
  17921. int bits = 2048;
  17922. #endif
  17923. XMEMSET(&key, 0, sizeof(RsaKey));
  17924. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17925. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17926. ExpectIntEQ(wc_InitRng(&rng), 0);
  17927. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17928. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17929. /* Pass bad args. */
  17930. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17931. BAD_FUNC_ARG);
  17932. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17933. BAD_FUNC_ARG);
  17934. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17935. BAD_FUNC_ARG);
  17936. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17937. BAD_FUNC_ARG);
  17938. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17939. BAD_FUNC_ARG);
  17940. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17941. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17942. #endif
  17943. return EXPECT_RESULT();
  17944. } /* END test_wc_RsaFlattenPublicKey */
  17945. /*
  17946. * unit test for wc_AesCcmSetKey
  17947. */
  17948. static int test_wc_AesCcmSetKey(void)
  17949. {
  17950. EXPECT_DECLS;
  17951. #ifdef HAVE_AESCCM
  17952. Aes aes;
  17953. const byte key16[] = {
  17954. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17955. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17956. };
  17957. const byte key24[] = {
  17958. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17959. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17960. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17961. };
  17962. const byte key32[] = {
  17963. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17964. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17965. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17966. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17967. };
  17968. XMEMSET(&aes, 0, sizeof(Aes));
  17969. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17970. #ifdef WOLFSSL_AES_128
  17971. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17972. #endif
  17973. #ifdef WOLFSSL_AES_192
  17974. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17975. #endif
  17976. #ifdef WOLFSSL_AES_256
  17977. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17978. #endif
  17979. /* Test bad args. */
  17980. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17981. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17982. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17983. wc_AesFree(&aes);
  17984. #endif
  17985. return EXPECT_RESULT();
  17986. } /* END test_wc_AesCcmSetKey */
  17987. /*
  17988. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17989. */
  17990. static int test_wc_AesCcmEncryptDecrypt(void)
  17991. {
  17992. EXPECT_DECLS;
  17993. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17994. Aes aes;
  17995. const byte key16[] = {
  17996. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17997. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17998. };
  17999. /* plaintext */
  18000. const byte plainT[] = {
  18001. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18002. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18003. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18004. };
  18005. /* nonce */
  18006. const byte iv[] = {
  18007. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18008. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18009. };
  18010. const byte c[] = { /* cipher text. */
  18011. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18012. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18013. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18014. };
  18015. const byte t[] = { /* Auth tag */
  18016. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18017. };
  18018. const byte authIn[] = {
  18019. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18020. };
  18021. byte cipherOut[sizeof(plainT)];
  18022. byte authTag[sizeof(t)];
  18023. #ifdef HAVE_AES_DECRYPT
  18024. byte plainOut[sizeof(cipherOut)];
  18025. #endif
  18026. XMEMSET(&aes, 0, sizeof(Aes));
  18027. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18028. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18029. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18030. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  18031. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  18032. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  18033. #ifdef HAVE_AES_DECRYPT
  18034. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18035. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  18036. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  18037. #endif
  18038. /* Pass in bad args. Encrypt*/
  18039. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18040. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18041. BAD_FUNC_ARG);
  18042. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18043. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18044. BAD_FUNC_ARG);
  18045. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18046. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18047. BAD_FUNC_ARG);
  18048. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18049. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18050. BAD_FUNC_ARG);
  18051. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18052. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  18053. BAD_FUNC_ARG);
  18054. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18055. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18056. BAD_FUNC_ARG);
  18057. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18058. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18059. BAD_FUNC_ARG);
  18060. #ifdef HAVE_AES_DECRYPT
  18061. /* Pass in bad args. Decrypt*/
  18062. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18063. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18064. BAD_FUNC_ARG);
  18065. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18066. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18067. BAD_FUNC_ARG);
  18068. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18069. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18070. BAD_FUNC_ARG);
  18071. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18072. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18073. BAD_FUNC_ARG);
  18074. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18075. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  18076. BAD_FUNC_ARG);
  18077. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18078. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18079. BAD_FUNC_ARG);
  18080. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18081. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18082. BAD_FUNC_ARG);
  18083. #endif
  18084. wc_AesFree(&aes);
  18085. #endif /* HAVE_AESCCM */
  18086. return EXPECT_RESULT();
  18087. } /* END test_wc_AesCcmEncryptDecrypt */
  18088. #if defined(WOLFSSL_AES_EAX) && \
  18089. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18090. /*
  18091. * Testing test_wc_AesEaxVectors()
  18092. */
  18093. static int test_wc_AesEaxVectors(void)
  18094. {
  18095. EXPECT_DECLS;
  18096. typedef struct {
  18097. byte key[AES_256_KEY_SIZE];
  18098. int key_length;
  18099. byte iv[AES_BLOCK_SIZE];
  18100. int iv_length;
  18101. byte aad[AES_BLOCK_SIZE * 2];
  18102. int aad_length;
  18103. byte msg[AES_BLOCK_SIZE * 5];
  18104. int msg_length;
  18105. byte ct[AES_BLOCK_SIZE * 5];
  18106. int ct_length;
  18107. byte tag[AES_BLOCK_SIZE];
  18108. int tag_length;
  18109. int valid;
  18110. } AadVector;
  18111. /* Test vectors obtained from Google wycheproof project
  18112. * https://github.com/google/wycheproof
  18113. * from testvectors/aes_eax_test.json
  18114. */
  18115. const AadVector vectors[] = {
  18116. {
  18117. /* key, key length */
  18118. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18119. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18120. /* iv, iv length */
  18121. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18122. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18123. /* aad, aad length */
  18124. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18125. /* msg, msg length */
  18126. {0x00}, 0,
  18127. /* ct, ct length */
  18128. {0x00}, 0,
  18129. /* tag, tag length */
  18130. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18131. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18132. /* valid */
  18133. 1,
  18134. },
  18135. {
  18136. /* key, key length */
  18137. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18138. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18139. /* iv, iv length */
  18140. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18141. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18142. /* aad, aad length */
  18143. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18144. /* msg, msg length */
  18145. {0xf7, 0xfb}, 2,
  18146. /* ct, ct length */
  18147. {0x19, 0xdd}, 2,
  18148. /* tag, tag length */
  18149. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18150. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18151. /* valid */
  18152. 1,
  18153. },
  18154. {
  18155. /* key, key length */
  18156. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18157. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18158. /* iv, iv length */
  18159. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18160. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18161. /* aad, aad length */
  18162. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18163. /* msg, msg length */
  18164. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18165. /* ct, ct length */
  18166. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18167. /* tag, tag length */
  18168. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18169. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18170. /* valid */
  18171. 1,
  18172. },
  18173. {
  18174. /* key, key length */
  18175. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18176. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18177. /* iv, iv length */
  18178. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18179. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18180. /* aad, aad length */
  18181. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18182. /* msg, msg length */
  18183. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18184. /* ct, ct length */
  18185. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18186. /* tag, tag length */
  18187. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18188. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18189. /* valid */
  18190. 1,
  18191. },
  18192. {
  18193. /* key, key length */
  18194. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18195. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18196. /* iv, iv length */
  18197. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18198. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18199. /* aad, aad length */
  18200. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18201. /* msg, msg length */
  18202. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18203. /* ct, ct length */
  18204. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18205. /* tag, tag length */
  18206. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18207. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18208. /* valid */
  18209. 1,
  18210. },
  18211. {
  18212. /* key, key length */
  18213. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18214. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18215. /* iv, iv length */
  18216. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18217. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18218. /* aad, aad length */
  18219. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18220. /* msg, msg length */
  18221. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18222. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18223. /* ct, ct length */
  18224. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18225. 0x0e, 0x72, 0x84, 0x14}, 12,
  18226. /* tag, tag length */
  18227. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18228. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18229. /* valid */
  18230. 1,
  18231. },
  18232. {
  18233. /* key, key length */
  18234. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18235. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18236. /* iv, iv length */
  18237. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18238. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18239. /* aad, aad length */
  18240. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18241. /* msg, msg length */
  18242. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18243. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18244. 0x36}, 17,
  18245. /* ct, ct length */
  18246. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18247. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18248. 0x30}, 17,
  18249. /* tag, tag length */
  18250. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18251. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18252. /* valid */
  18253. 1,
  18254. },
  18255. {
  18256. /* key, key length */
  18257. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18258. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18259. /* iv, iv length */
  18260. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18261. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18262. /* aad, aad length */
  18263. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18264. /* msg, msg length */
  18265. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18266. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18267. 0x2d, 0x56}, 18,
  18268. /* ct, ct length */
  18269. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18270. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18271. 0x8c, 0xc3}, 18,
  18272. /* tag, tag length */
  18273. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18274. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18275. /* valid */
  18276. 1,
  18277. },
  18278. {
  18279. /* key, key length */
  18280. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18281. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18282. /* iv, iv length */
  18283. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18284. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18285. /* aad, aad length */
  18286. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18287. /* msg, msg length */
  18288. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18289. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18290. 0xef, 0x11}, 18,
  18291. /* ct, ct length */
  18292. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18293. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18294. 0xb1, 0xe8}, 18,
  18295. /* tag, tag length */
  18296. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18297. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18298. /* valid */
  18299. 1,
  18300. },
  18301. {
  18302. /* key, key length */
  18303. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18304. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18305. /* iv, iv length */
  18306. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18307. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18308. /* aad, aad length */
  18309. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18310. /* msg, msg length */
  18311. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18312. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18313. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18314. /* ct, ct length */
  18315. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18316. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18317. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18318. /* tag, tag length */
  18319. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18320. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18321. /* valid */
  18322. 1,
  18323. },
  18324. {
  18325. /* key, key length */
  18326. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18327. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18328. /* iv, iv length */
  18329. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18330. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18331. /* aad, aad length */
  18332. {0x00}, 0,
  18333. /* msg, msg length */
  18334. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18335. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18336. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18337. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18338. /* ct, ct length */
  18339. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18340. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18341. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18342. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18343. /* tag, tag length */
  18344. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18345. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18346. /* valid */
  18347. 1,
  18348. },
  18349. {
  18350. /* key, key length */
  18351. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18352. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18353. /* iv, iv length */
  18354. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18355. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18356. /* aad, aad length */
  18357. {0x00}, 0,
  18358. /* msg, msg length */
  18359. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18361. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18362. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18363. /* ct, ct length */
  18364. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18365. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18366. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18367. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18368. /* tag, tag length */
  18369. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18370. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18371. /* valid */
  18372. 1,
  18373. },
  18374. {
  18375. /* key, key length */
  18376. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18377. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18378. /* iv, iv length */
  18379. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18380. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18381. /* aad, aad length */
  18382. {0x00}, 0,
  18383. /* msg, msg length */
  18384. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18385. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18386. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18387. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18388. /* ct, ct length */
  18389. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18390. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18391. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18392. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18393. /* tag, tag length */
  18394. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18395. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18396. /* valid */
  18397. 1,
  18398. },
  18399. {
  18400. /* key, key length */
  18401. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18402. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18403. /* iv, iv length */
  18404. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18405. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18406. /* aad, aad length */
  18407. {0x00}, 0,
  18408. /* msg, msg length */
  18409. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18411. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18412. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18413. /* ct, ct length */
  18414. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18415. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18416. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18417. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18418. /* tag, tag length */
  18419. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18420. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18421. /* valid */
  18422. 1,
  18423. },
  18424. {
  18425. /* key, key length */
  18426. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18427. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18428. /* iv, iv length */
  18429. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18430. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18431. /* aad, aad length */
  18432. {0x00}, 0,
  18433. /* msg, msg length */
  18434. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18435. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18436. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18437. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18438. /* ct, ct length */
  18439. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18440. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18441. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18442. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18443. /* tag, tag length */
  18444. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18445. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18446. /* valid */
  18447. 1,
  18448. },
  18449. {
  18450. /* key, key length */
  18451. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18452. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18453. /* iv, iv length */
  18454. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18455. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18456. /* aad, aad length */
  18457. {0x00}, 0,
  18458. /* msg, msg length */
  18459. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18460. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18461. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18462. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18463. /* ct, ct length */
  18464. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18465. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18466. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18467. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18468. /* tag, tag length */
  18469. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18470. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18471. /* valid */
  18472. 1,
  18473. },
  18474. {
  18475. /* key, key length */
  18476. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18477. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18478. /* iv, iv length */
  18479. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18480. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18481. /* aad, aad length */
  18482. {0x00}, 0,
  18483. /* msg, msg length */
  18484. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18485. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18486. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18487. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18488. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18489. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18490. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18491. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18492. /* ct, ct length */
  18493. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18494. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18495. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18496. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18497. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18498. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18499. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18500. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18501. /* tag, tag length */
  18502. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18503. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18504. /* valid */
  18505. 1,
  18506. },
  18507. {
  18508. /* key, key length */
  18509. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18510. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18511. /* iv, iv length */
  18512. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18513. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18514. /* aad, aad length */
  18515. {0x00}, 0,
  18516. /* msg, msg length */
  18517. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18518. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18519. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18520. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18521. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18522. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18523. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18524. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18525. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18526. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18527. /* ct, ct length */
  18528. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18529. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18530. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18531. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18532. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18533. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18534. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18535. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18536. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18537. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18538. /* tag, tag length */
  18539. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18540. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18541. /* valid */
  18542. 1,
  18543. },
  18544. {
  18545. /* key, key length */
  18546. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18547. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18548. /* iv, iv length */
  18549. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18550. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18551. /* aad, aad length */
  18552. {0x00}, 0,
  18553. /* msg, msg length */
  18554. {0x00}, 0,
  18555. /* ct, ct length */
  18556. {0x00}, 0,
  18557. /* tag, tag length */
  18558. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18559. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18560. /* valid */
  18561. 1,
  18562. },
  18563. {
  18564. /* key, key length */
  18565. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18566. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18567. /* iv, iv length */
  18568. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18569. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18570. /* aad, aad length */
  18571. {0x00}, 0,
  18572. /* msg, msg length */
  18573. {0x01}, 1,
  18574. /* ct, ct length */
  18575. {0x1d}, 1,
  18576. /* tag, tag length */
  18577. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18578. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18579. /* valid */
  18580. 1,
  18581. },
  18582. {
  18583. /* key, key length */
  18584. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18585. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18586. /* iv, iv length */
  18587. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18588. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18589. /* aad, aad length */
  18590. {0x00}, 0,
  18591. /* msg, msg length */
  18592. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18593. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18594. /* ct, ct length */
  18595. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18596. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18597. /* tag, tag length */
  18598. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18599. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18600. /* valid */
  18601. 1,
  18602. },
  18603. {
  18604. /* key, key length */
  18605. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18606. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18607. /* iv, iv length */
  18608. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18609. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18610. /* aad, aad length */
  18611. {0x00}, 0,
  18612. /* msg, msg length */
  18613. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18614. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18615. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18616. /* ct, ct length */
  18617. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18618. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18619. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18620. /* tag, tag length */
  18621. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18622. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18623. /* valid */
  18624. 1,
  18625. },
  18626. {
  18627. /* key, key length */
  18628. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18629. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18630. /* iv, iv length */
  18631. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18632. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18633. /* aad, aad length */
  18634. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18635. /* msg, msg length */
  18636. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18637. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18638. /* ct, ct length */
  18639. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18640. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18641. /* tag, tag length */
  18642. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18643. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18644. /* valid */
  18645. 1,
  18646. },
  18647. {
  18648. /* key, key length */
  18649. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18650. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18651. /* iv, iv length */
  18652. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18653. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18654. /* aad, aad length */
  18655. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18656. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18657. /* msg, msg length */
  18658. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18659. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18660. /* ct, ct length */
  18661. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18662. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18663. /* tag, tag length */
  18664. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18665. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18666. /* valid */
  18667. 1,
  18668. },
  18669. {
  18670. /* key, key length */
  18671. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18672. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18673. /* iv, iv length */
  18674. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18675. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18676. /* aad, aad length */
  18677. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18678. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18679. /* msg, msg length */
  18680. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18681. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18682. /* ct, ct length */
  18683. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18684. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18685. /* tag, tag length */
  18686. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18687. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18688. /* valid */
  18689. 1,
  18690. },
  18691. {
  18692. /* key, key length */
  18693. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18694. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18695. /* iv, iv length */
  18696. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18697. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18698. /* aad, aad length */
  18699. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18700. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18701. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18702. /* msg, msg length */
  18703. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18704. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18705. /* ct, ct length */
  18706. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18707. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18708. /* tag, tag length */
  18709. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18710. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18711. /* valid */
  18712. 1,
  18713. },
  18714. {
  18715. /* key, key length */
  18716. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18717. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18718. /* iv, iv length */
  18719. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18720. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18721. /* aad, aad length */
  18722. {0x00}, 0,
  18723. /* msg, msg length */
  18724. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18725. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18726. /* ct, ct length */
  18727. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18728. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18729. /* tag, tag length */
  18730. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18731. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18732. /* valid */
  18733. 0,
  18734. },
  18735. {
  18736. /* key, key length */
  18737. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18738. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18739. /* iv, iv length */
  18740. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18741. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18742. /* aad, aad length */
  18743. {0x00}, 0,
  18744. /* msg, msg length */
  18745. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18746. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18747. /* ct, ct length */
  18748. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18749. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18750. /* tag, tag length */
  18751. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18752. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18753. /* valid */
  18754. 0,
  18755. },
  18756. {
  18757. /* key, key length */
  18758. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18759. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18760. /* iv, iv length */
  18761. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18762. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18763. /* aad, aad length */
  18764. {0x00}, 0,
  18765. /* msg, msg length */
  18766. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18767. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18768. /* ct, ct length */
  18769. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18770. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18771. /* tag, tag length */
  18772. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18773. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18774. /* valid */
  18775. 0,
  18776. },
  18777. {
  18778. /* key, key length */
  18779. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18780. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18781. /* iv, iv length */
  18782. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18783. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18784. /* aad, aad length */
  18785. {0x00}, 0,
  18786. /* msg, msg length */
  18787. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18788. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18789. /* ct, ct length */
  18790. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18791. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18792. /* tag, tag length */
  18793. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18794. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18795. /* valid */
  18796. 0,
  18797. },
  18798. {
  18799. /* key, key length */
  18800. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18801. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18802. /* iv, iv length */
  18803. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18804. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18805. /* aad, aad length */
  18806. {0x00}, 0,
  18807. /* msg, msg length */
  18808. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18809. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18810. /* ct, ct length */
  18811. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18812. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18813. /* tag, tag length */
  18814. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18815. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18816. /* valid */
  18817. 0,
  18818. },
  18819. {
  18820. /* key, key length */
  18821. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18822. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18823. /* iv, iv length */
  18824. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18825. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18826. /* aad, aad length */
  18827. {0x00}, 0,
  18828. /* msg, msg length */
  18829. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18830. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18831. /* ct, ct length */
  18832. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18833. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18834. /* tag, tag length */
  18835. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18836. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18837. /* valid */
  18838. 0,
  18839. },
  18840. {
  18841. /* key, key length */
  18842. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18843. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18844. /* iv, iv length */
  18845. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18846. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18847. /* aad, aad length */
  18848. {0x00}, 0,
  18849. /* msg, msg length */
  18850. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18851. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18852. /* ct, ct length */
  18853. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18854. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18855. /* tag, tag length */
  18856. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18857. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18858. /* valid */
  18859. 0,
  18860. },
  18861. {
  18862. /* key, key length */
  18863. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18864. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18865. /* iv, iv length */
  18866. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18867. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18868. /* aad, aad length */
  18869. {0x00}, 0,
  18870. /* msg, msg length */
  18871. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18872. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18873. /* ct, ct length */
  18874. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18875. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18876. /* tag, tag length */
  18877. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18878. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18879. /* valid */
  18880. 0,
  18881. },
  18882. {
  18883. /* key, key length */
  18884. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18885. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18886. /* iv, iv length */
  18887. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18888. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18889. /* aad, aad length */
  18890. {0x00}, 0,
  18891. /* msg, msg length */
  18892. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18893. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18894. /* ct, ct length */
  18895. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18896. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18897. /* tag, tag length */
  18898. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18899. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18900. /* valid */
  18901. 0,
  18902. },
  18903. {
  18904. /* key, key length */
  18905. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18906. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18907. /* iv, iv length */
  18908. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18909. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18910. /* aad, aad length */
  18911. {0x00}, 0,
  18912. /* msg, msg length */
  18913. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18914. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18915. /* ct, ct length */
  18916. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18917. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18918. /* tag, tag length */
  18919. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18920. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18921. /* valid */
  18922. 0,
  18923. },
  18924. {
  18925. /* key, key length */
  18926. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18927. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18928. /* iv, iv length */
  18929. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18930. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18931. /* aad, aad length */
  18932. {0x00}, 0,
  18933. /* msg, msg length */
  18934. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18935. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18936. /* ct, ct length */
  18937. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18938. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18939. /* tag, tag length */
  18940. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18941. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18942. /* valid */
  18943. 0,
  18944. },
  18945. {
  18946. /* key, key length */
  18947. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18948. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18949. /* iv, iv length */
  18950. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18951. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18952. /* aad, aad length */
  18953. {0x00}, 0,
  18954. /* msg, msg length */
  18955. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18956. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18957. /* ct, ct length */
  18958. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18959. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18960. /* tag, tag length */
  18961. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18962. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18963. /* valid */
  18964. 0,
  18965. },
  18966. {
  18967. /* key, key length */
  18968. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18969. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18970. /* iv, iv length */
  18971. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18972. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18973. /* aad, aad length */
  18974. {0x00}, 0,
  18975. /* msg, msg length */
  18976. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18977. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18978. /* ct, ct length */
  18979. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18980. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18981. /* tag, tag length */
  18982. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18983. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18984. /* valid */
  18985. 0,
  18986. },
  18987. {
  18988. /* key, key length */
  18989. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18990. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18991. /* iv, iv length */
  18992. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18993. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18994. /* aad, aad length */
  18995. {0x00}, 0,
  18996. /* msg, msg length */
  18997. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18998. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18999. /* ct, ct length */
  19000. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19001. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19002. /* tag, tag length */
  19003. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19004. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  19005. /* valid */
  19006. 0,
  19007. },
  19008. {
  19009. /* key, key length */
  19010. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19011. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19012. /* iv, iv length */
  19013. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19014. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19015. /* aad, aad length */
  19016. {0x00}, 0,
  19017. /* msg, msg length */
  19018. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19019. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19020. /* ct, ct length */
  19021. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19022. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19023. /* tag, tag length */
  19024. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19025. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  19026. /* valid */
  19027. 0,
  19028. },
  19029. {
  19030. /* key, key length */
  19031. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19032. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19033. /* iv, iv length */
  19034. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19035. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19036. /* aad, aad length */
  19037. {0x00}, 0,
  19038. /* msg, msg length */
  19039. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19040. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19041. /* ct, ct length */
  19042. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19043. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19044. /* tag, tag length */
  19045. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19046. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  19047. /* valid */
  19048. 0,
  19049. },
  19050. {
  19051. /* key, key length */
  19052. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19053. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19054. /* iv, iv length */
  19055. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19056. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19057. /* aad, aad length */
  19058. {0x00}, 0,
  19059. /* msg, msg length */
  19060. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19061. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19062. /* ct, ct length */
  19063. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19064. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19065. /* tag, tag length */
  19066. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19067. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  19068. /* valid */
  19069. 0,
  19070. },
  19071. {
  19072. /* key, key length */
  19073. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19074. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19075. /* iv, iv length */
  19076. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19077. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19078. /* aad, aad length */
  19079. {0x00}, 0,
  19080. /* msg, msg length */
  19081. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19082. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19083. /* ct, ct length */
  19084. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19085. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19086. /* tag, tag length */
  19087. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19088. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  19089. /* valid */
  19090. 0,
  19091. },
  19092. {
  19093. /* key, key length */
  19094. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19095. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19096. /* iv, iv length */
  19097. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19098. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19099. /* aad, aad length */
  19100. {0x00}, 0,
  19101. /* msg, msg length */
  19102. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19103. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19104. /* ct, ct length */
  19105. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19106. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19107. /* tag, tag length */
  19108. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19109. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19110. /* valid */
  19111. 0,
  19112. },
  19113. {
  19114. /* key, key length */
  19115. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19116. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19117. /* iv, iv length */
  19118. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19119. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19120. /* aad, aad length */
  19121. {0x00}, 0,
  19122. /* msg, msg length */
  19123. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19124. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19125. /* ct, ct length */
  19126. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19127. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19128. /* tag, tag length */
  19129. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19130. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19131. /* valid */
  19132. 0,
  19133. },
  19134. {
  19135. /* key, key length */
  19136. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19137. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19138. /* iv, iv length */
  19139. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19140. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19141. /* aad, aad length */
  19142. {0x00}, 0,
  19143. /* msg, msg length */
  19144. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19145. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19146. /* ct, ct length */
  19147. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19148. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19149. /* tag, tag length */
  19150. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19151. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19152. /* valid */
  19153. 0,
  19154. },
  19155. {
  19156. /* key, key length */
  19157. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19158. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19159. /* iv, iv length */
  19160. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19161. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19162. /* aad, aad length */
  19163. {0x00}, 0,
  19164. /* msg, msg length */
  19165. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19166. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19167. /* ct, ct length */
  19168. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19169. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19170. /* tag, tag length */
  19171. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19172. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19173. /* valid */
  19174. 0,
  19175. },
  19176. {
  19177. /* key, key length */
  19178. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19179. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19180. /* iv, iv length */
  19181. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19182. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19183. /* aad, aad length */
  19184. {0x00}, 0,
  19185. /* msg, msg length */
  19186. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19187. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19188. /* ct, ct length */
  19189. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19190. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19191. /* tag, tag length */
  19192. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19193. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19194. /* valid */
  19195. 0,
  19196. },
  19197. {
  19198. /* key, key length */
  19199. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19200. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19201. /* iv, iv length */
  19202. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19203. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19204. /* aad, aad length */
  19205. {0x00}, 0,
  19206. /* msg, msg length */
  19207. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19208. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19209. /* ct, ct length */
  19210. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19211. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19212. /* tag, tag length */
  19213. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19215. /* valid */
  19216. 0,
  19217. },
  19218. {
  19219. /* key, key length */
  19220. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19221. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19222. /* iv, iv length */
  19223. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19224. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19225. /* aad, aad length */
  19226. {0x00}, 0,
  19227. /* msg, msg length */
  19228. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19229. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19230. /* ct, ct length */
  19231. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19232. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19233. /* tag, tag length */
  19234. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19235. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19236. /* valid */
  19237. 0,
  19238. },
  19239. {
  19240. /* key, key length */
  19241. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19242. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19243. /* iv, iv length */
  19244. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19245. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19246. /* aad, aad length */
  19247. {0x00}, 0,
  19248. /* msg, msg length */
  19249. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19250. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19251. /* ct, ct length */
  19252. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19253. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19254. /* tag, tag length */
  19255. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19256. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19257. /* valid */
  19258. 0,
  19259. },
  19260. {
  19261. /* key, key length */
  19262. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19263. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19264. /* iv, iv length */
  19265. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19266. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19267. /* aad, aad length */
  19268. {0x00}, 0,
  19269. /* msg, msg length */
  19270. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19271. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19272. /* ct, ct length */
  19273. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19274. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19275. /* tag, tag length */
  19276. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19277. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19278. /* valid */
  19279. 0,
  19280. },
  19281. };
  19282. byte ciphertext[sizeof(vectors[0].ct)];
  19283. byte authtag[sizeof(vectors[0].tag)];
  19284. int i;
  19285. int len;
  19286. int ret;
  19287. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19288. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19289. len = sizeof(authtag);
  19290. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19291. ciphertext,
  19292. vectors[i].msg, vectors[i].msg_length,
  19293. vectors[i].iv, vectors[i].iv_length,
  19294. authtag, len,
  19295. vectors[i].aad, vectors[i].aad_length),
  19296. 0);
  19297. /* check ciphertext matches vector */
  19298. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19299. 0);
  19300. /* check that computed tag matches vector only for vectors marked as valid */
  19301. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19302. if (vectors[i].valid) {
  19303. ExpectIntEQ(ret, 0);
  19304. }
  19305. else {
  19306. ExpectIntNE(ret, 0);
  19307. }
  19308. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19309. /* Decrypt, checking that the computed auth tags match */
  19310. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19311. ciphertext,
  19312. vectors[i].ct, vectors[i].ct_length,
  19313. vectors[i].iv, vectors[i].iv_length,
  19314. authtag, len,
  19315. vectors[i].aad, vectors[i].aad_length),
  19316. 0);
  19317. /* check decrypted ciphertext matches vector plaintext */
  19318. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19319. 0);
  19320. }
  19321. return EXPECT_RESULT();
  19322. } /* END test_wc_AesEaxVectors */
  19323. /*
  19324. * Testing test_wc_AesEaxEncryptAuth()
  19325. */
  19326. static int test_wc_AesEaxEncryptAuth(void)
  19327. {
  19328. EXPECT_DECLS;
  19329. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19330. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19331. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19332. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19333. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19334. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19335. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19336. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19337. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19338. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19339. byte ciphertext[sizeof(msg)];
  19340. byte authtag[AES_BLOCK_SIZE];
  19341. int i;
  19342. int len;
  19343. len = sizeof(authtag);
  19344. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19345. ciphertext,
  19346. msg, sizeof(msg),
  19347. iv, sizeof(iv),
  19348. authtag, (word32)len,
  19349. aad, sizeof(aad)),
  19350. 0);
  19351. /* Test null checking */
  19352. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19353. ciphertext,
  19354. msg, sizeof(msg),
  19355. iv, sizeof(iv),
  19356. authtag, (word32)len,
  19357. aad, sizeof(aad)),
  19358. BAD_FUNC_ARG);
  19359. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19360. NULL,
  19361. msg, sizeof(msg),
  19362. iv, sizeof(iv),
  19363. authtag, (word32)len,
  19364. aad, sizeof(aad)),
  19365. BAD_FUNC_ARG);
  19366. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19367. ciphertext,
  19368. NULL, sizeof(msg),
  19369. iv, sizeof(iv),
  19370. authtag, (word32)len,
  19371. aad, sizeof(aad)),
  19372. BAD_FUNC_ARG);
  19373. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19374. ciphertext,
  19375. msg, sizeof(msg),
  19376. NULL, sizeof(iv),
  19377. authtag, (word32)len,
  19378. aad, sizeof(aad)),
  19379. BAD_FUNC_ARG);
  19380. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19381. ciphertext,
  19382. msg, sizeof(msg),
  19383. iv, sizeof(iv),
  19384. NULL, (word32)len,
  19385. aad, sizeof(aad)),
  19386. BAD_FUNC_ARG);
  19387. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19388. ciphertext,
  19389. msg, sizeof(msg),
  19390. iv, sizeof(iv),
  19391. authtag, (word32)len,
  19392. NULL, sizeof(aad)),
  19393. BAD_FUNC_ARG);
  19394. /* Test bad key lengths */
  19395. for (i = 0; i <= 32; i++) {
  19396. int exp_ret;
  19397. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19398. || i == AES_256_KEY_SIZE) {
  19399. exp_ret = 0;
  19400. }
  19401. else {
  19402. exp_ret = BAD_FUNC_ARG;
  19403. }
  19404. ExpectIntEQ(wc_AesEaxEncryptAuth(key, (word32)i,
  19405. ciphertext,
  19406. msg, sizeof(msg),
  19407. iv, sizeof(iv),
  19408. authtag, (word32)len,
  19409. aad, sizeof(aad)),
  19410. exp_ret);
  19411. }
  19412. /* Test auth tag size out of range */
  19413. len = AES_BLOCK_SIZE + 1;
  19414. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19415. ciphertext,
  19416. msg, sizeof(msg),
  19417. iv, sizeof(iv),
  19418. authtag, (word32)len,
  19419. aad, sizeof(aad)),
  19420. BAD_FUNC_ARG);
  19421. return EXPECT_RESULT();
  19422. } /* END test_wc_AesEaxEncryptAuth() */
  19423. /*
  19424. * Testing test_wc_AesEaxDecryptAuth()
  19425. */
  19426. static int test_wc_AesEaxDecryptAuth(void)
  19427. {
  19428. EXPECT_DECLS;
  19429. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19430. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19431. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19432. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19433. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19434. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19435. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19436. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19437. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19438. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19439. /* Garbage tag that should always fail for above aad */
  19440. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19441. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19442. byte plaintext[sizeof(ct)];
  19443. int i;
  19444. int len;
  19445. len = sizeof(tag);
  19446. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19447. plaintext,
  19448. ct, sizeof(ct),
  19449. iv, sizeof(iv),
  19450. tag, (word32)len,
  19451. aad, sizeof(aad)),
  19452. AES_EAX_AUTH_E);
  19453. /* Test null checking */
  19454. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19455. plaintext,
  19456. ct, sizeof(ct),
  19457. iv, sizeof(iv),
  19458. tag, (word32)len,
  19459. aad, sizeof(aad)),
  19460. BAD_FUNC_ARG);
  19461. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19462. NULL,
  19463. ct, sizeof(ct),
  19464. iv, sizeof(iv),
  19465. tag, (word32)len,
  19466. aad, sizeof(aad)),
  19467. BAD_FUNC_ARG);
  19468. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19469. plaintext,
  19470. NULL, sizeof(ct),
  19471. iv, sizeof(iv),
  19472. tag, (word32)len,
  19473. aad, sizeof(aad)),
  19474. BAD_FUNC_ARG);
  19475. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19476. plaintext,
  19477. ct, sizeof(ct),
  19478. NULL, sizeof(iv),
  19479. tag, (word32)len,
  19480. aad, sizeof(aad)),
  19481. BAD_FUNC_ARG);
  19482. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19483. plaintext,
  19484. ct, sizeof(ct),
  19485. iv, sizeof(iv),
  19486. NULL, (word32)len,
  19487. aad, sizeof(aad)),
  19488. BAD_FUNC_ARG);
  19489. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19490. plaintext,
  19491. ct, sizeof(ct),
  19492. iv, sizeof(iv),
  19493. tag, (word32)len,
  19494. NULL, sizeof(aad)),
  19495. BAD_FUNC_ARG);
  19496. /* Test bad key lengths */
  19497. for (i = 0; i <= 32; i++) {
  19498. int exp_ret;
  19499. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19500. || i == AES_256_KEY_SIZE) {
  19501. exp_ret = AES_EAX_AUTH_E;
  19502. }
  19503. else {
  19504. exp_ret = BAD_FUNC_ARG;
  19505. }
  19506. ExpectIntEQ(wc_AesEaxDecryptAuth(key, (word32)i,
  19507. plaintext,
  19508. ct, sizeof(ct),
  19509. iv, sizeof(iv),
  19510. tag, (word32)len,
  19511. aad, sizeof(aad)),
  19512. exp_ret);
  19513. }
  19514. /* Test auth tag size out of range */
  19515. len = AES_BLOCK_SIZE + 1;
  19516. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19517. plaintext,
  19518. ct, sizeof(ct),
  19519. iv, sizeof(iv),
  19520. tag, (word32)len,
  19521. aad, sizeof(aad)),
  19522. BAD_FUNC_ARG);
  19523. return EXPECT_RESULT();
  19524. } /* END test_wc_AesEaxDecryptAuth() */
  19525. #endif /* WOLFSSL_AES_EAX &&
  19526. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19527. */
  19528. /*
  19529. * Testing wc_InitDsaKey()
  19530. */
  19531. static int test_wc_InitDsaKey(void)
  19532. {
  19533. EXPECT_DECLS;
  19534. #ifndef NO_DSA
  19535. DsaKey key;
  19536. XMEMSET(&key, 0, sizeof(DsaKey));
  19537. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19538. /* Pass in bad args. */
  19539. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19540. wc_FreeDsaKey(&key);
  19541. #endif
  19542. return EXPECT_RESULT();
  19543. } /* END test_wc_InitDsaKey */
  19544. /*
  19545. * Testing wc_DsaSign() and wc_DsaVerify()
  19546. */
  19547. static int test_wc_DsaSignVerify(void)
  19548. {
  19549. EXPECT_DECLS;
  19550. #if !defined(NO_DSA)
  19551. DsaKey key;
  19552. WC_RNG rng;
  19553. wc_Sha sha;
  19554. byte signature[DSA_SIG_SIZE];
  19555. byte hash[WC_SHA_DIGEST_SIZE];
  19556. word32 idx = 0;
  19557. word32 bytes;
  19558. int answer;
  19559. #ifdef USE_CERT_BUFFERS_1024
  19560. byte tmp[ONEK_BUF];
  19561. XMEMSET(tmp, 0, sizeof(tmp));
  19562. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19563. bytes = sizeof_dsa_key_der_1024;
  19564. #elif defined(USE_CERT_BUFFERS_2048)
  19565. byte tmp[TWOK_BUF];
  19566. XMEMSET(tmp, 0, sizeof(tmp));
  19567. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19568. bytes = sizeof_dsa_key_der_2048;
  19569. #else
  19570. byte tmp[TWOK_BUF];
  19571. XFILE fp = XBADFILE;
  19572. XMEMSET(tmp, 0, sizeof(tmp));
  19573. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19574. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19575. if (fp != XBADFILE)
  19576. XFCLOSE(fp);
  19577. #endif /* END USE_CERT_BUFFERS_1024 */
  19578. ExpectIntEQ(wc_InitSha(&sha), 0);
  19579. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19580. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19581. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19582. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19583. ExpectIntEQ(wc_InitRng(&rng), 0);
  19584. /* Sign. */
  19585. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19586. /* Test bad args. */
  19587. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19588. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19589. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19590. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19591. /* Verify. */
  19592. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19593. ExpectIntEQ(answer, 1);
  19594. /* Pass in bad args. */
  19595. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19596. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19597. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19598. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19599. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19600. /* hard set q to 0 and test fail case */
  19601. mp_free(&key.q);
  19602. mp_init(&key.q);
  19603. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19604. mp_set(&key.q, 1);
  19605. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19606. #endif
  19607. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19608. wc_FreeDsaKey(&key);
  19609. wc_ShaFree(&sha);
  19610. #endif
  19611. return EXPECT_RESULT();
  19612. } /* END test_wc_DsaSign */
  19613. /*
  19614. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19615. */
  19616. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19617. {
  19618. EXPECT_DECLS;
  19619. #if !defined(NO_DSA)
  19620. DsaKey key;
  19621. word32 bytes = 0;
  19622. word32 idx = 0;
  19623. int ret = 0;
  19624. #ifdef USE_CERT_BUFFERS_1024
  19625. byte tmp[ONEK_BUF];
  19626. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19627. bytes = sizeof_dsa_key_der_1024;
  19628. #elif defined(USE_CERT_BUFFERS_2048)
  19629. byte tmp[TWOK_BUF];
  19630. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19631. bytes = sizeof_dsa_key_der_2048;
  19632. #else
  19633. byte tmp[TWOK_BUF];
  19634. XFILE fp = XBADFILE;
  19635. XMEMSET(tmp, 0, sizeof(tmp));
  19636. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19637. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19638. if (fp != XBADFILE)
  19639. XFCLOSE(fp);
  19640. #endif /* END USE_CERT_BUFFERS_1024 */
  19641. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19642. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19643. /* Test bad args. */
  19644. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19645. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19646. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19647. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19648. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19649. wc_FreeDsaKey(&key);
  19650. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19651. idx = 0; /* Reset */
  19652. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19653. /* Test bad args. */
  19654. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19655. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19656. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19657. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19658. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19659. wc_FreeDsaKey(&key);
  19660. #endif /* !NO_DSA */
  19661. return EXPECT_RESULT();
  19662. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19663. /*
  19664. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19665. */
  19666. static int test_wc_MakeDsaKey(void)
  19667. {
  19668. EXPECT_DECLS;
  19669. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19670. DsaKey genKey;
  19671. WC_RNG rng;
  19672. XMEMSET(&genKey, 0, sizeof(genKey));
  19673. XMEMSET(&rng, 0, sizeof(rng));
  19674. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19675. ExpectIntEQ(wc_InitRng(&rng), 0);
  19676. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19677. /* Test bad args. */
  19678. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19679. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19680. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19681. BAD_FUNC_ARG);
  19682. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19683. /* Test bad args. */
  19684. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19685. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19686. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19687. wc_FreeDsaKey(&genKey);
  19688. #endif
  19689. return EXPECT_RESULT();
  19690. } /* END test_wc_MakeDsaKey */
  19691. /*
  19692. * Testing wc_DsaKeyToDer()
  19693. */
  19694. static int test_wc_DsaKeyToDer(void)
  19695. {
  19696. EXPECT_DECLS;
  19697. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19698. DsaKey key;
  19699. word32 bytes;
  19700. word32 idx = 0;
  19701. #ifdef USE_CERT_BUFFERS_1024
  19702. byte tmp[ONEK_BUF];
  19703. byte der[ONEK_BUF];
  19704. XMEMSET(tmp, 0, sizeof(tmp));
  19705. XMEMSET(der, 0, sizeof(der));
  19706. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19707. bytes = sizeof_dsa_key_der_1024;
  19708. #elif defined(USE_CERT_BUFFERS_2048)
  19709. byte tmp[TWOK_BUF];
  19710. byte der[TWOK_BUF];
  19711. XMEMSET(tmp, 0, sizeof(tmp));
  19712. XMEMSET(der, 0, sizeof(der));
  19713. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19714. bytes = sizeof_dsa_key_der_2048;
  19715. #else
  19716. byte tmp[TWOK_BUF];
  19717. byte der[TWOK_BUF];
  19718. XFILE fp = XBADFILE;
  19719. XMEMSET(tmp, 0, sizeof(tmp));
  19720. XMEMSET(der, 0, sizeof(der));
  19721. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19722. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19723. if (fp != XBADFILE)
  19724. XFCLOSE(fp);
  19725. #endif /* END USE_CERT_BUFFERS_1024 */
  19726. XMEMSET(&key, 0, sizeof(DsaKey));
  19727. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19728. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19729. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19730. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19731. /* Test bad args. */
  19732. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19733. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19734. wc_FreeDsaKey(&key);
  19735. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19736. return EXPECT_RESULT();
  19737. } /* END test_wc_DsaKeyToDer */
  19738. /*
  19739. * Testing wc_DsaKeyToPublicDer()
  19740. * (indirectly testing setDsaPublicKey())
  19741. */
  19742. static int test_wc_DsaKeyToPublicDer(void)
  19743. {
  19744. EXPECT_DECLS;
  19745. #ifndef HAVE_SELFTEST
  19746. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19747. DsaKey key;
  19748. WC_RNG rng;
  19749. byte* der = NULL;
  19750. word32 sz = 0;
  19751. word32 idx = 0;
  19752. XMEMSET(&key, 0, sizeof(DsaKey));
  19753. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19754. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19755. DYNAMIC_TYPE_TMP_BUFFER));
  19756. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19757. ExpectIntEQ(wc_InitRng(&rng), 0);
  19758. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19759. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19760. ExpectIntGE(sz = (word32)wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19761. wc_FreeDsaKey(&key);
  19762. idx = 0;
  19763. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19764. /* Test without the SubjectPublicKeyInfo header */
  19765. ExpectIntGE(sz = (word32)wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19766. wc_FreeDsaKey(&key);
  19767. idx = 0;
  19768. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19769. /* Test bad args. */
  19770. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19771. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19772. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19773. wc_FreeDsaKey(&key);
  19774. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19775. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19776. #endif /* !HAVE_SELFTEST */
  19777. return EXPECT_RESULT();
  19778. } /* END test_wc_DsaKeyToPublicDer */
  19779. /*
  19780. * Testing wc_DsaImportParamsRaw()
  19781. */
  19782. static int test_wc_DsaImportParamsRaw(void)
  19783. {
  19784. EXPECT_DECLS;
  19785. #if !defined(NO_DSA)
  19786. DsaKey key;
  19787. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19788. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19789. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19790. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19791. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19792. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19793. "80a0093113a8bd73";
  19794. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19795. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19796. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19797. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19798. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19799. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19800. "76341a7e7d9";
  19801. /* invalid p and q parameters */
  19802. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19803. const char* invalidQ = "96c5390a";
  19804. XMEMSET(&key, 0, sizeof(DsaKey));
  19805. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19806. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19807. /* test bad args */
  19808. /* null key struct */
  19809. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19810. /* null param pointers */
  19811. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19812. /* illegal p length */
  19813. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19814. /* illegal q length */
  19815. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19816. wc_FreeDsaKey(&key);
  19817. #endif
  19818. return EXPECT_RESULT();
  19819. } /* END test_wc_DsaImportParamsRaw */
  19820. /*
  19821. * Testing wc_DsaImportParamsRawCheck()
  19822. */
  19823. static int test_wc_DsaImportParamsRawCheck(void)
  19824. {
  19825. EXPECT_DECLS;
  19826. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19827. DsaKey key;
  19828. int trusted = 0;
  19829. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19830. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19831. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19832. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19833. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19834. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19835. "80a0093113a8bd73";
  19836. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19837. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19838. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19839. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19840. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19841. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19842. "76341a7e7d9";
  19843. /* invalid p and q parameters */
  19844. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19845. const char* invalidQ = "96c5390a";
  19846. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19847. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19848. /* test bad args */
  19849. /* null key struct */
  19850. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19851. BAD_FUNC_ARG);
  19852. /* null param pointers */
  19853. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19854. NULL), BAD_FUNC_ARG);
  19855. /* illegal p length */
  19856. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19857. BAD_FUNC_ARG);
  19858. /* illegal q length */
  19859. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19860. BAD_FUNC_ARG);
  19861. wc_FreeDsaKey(&key);
  19862. #endif
  19863. return EXPECT_RESULT();
  19864. } /* END test_wc_DsaImportParamsRawCheck */
  19865. /*
  19866. * Testing wc_DsaExportParamsRaw()
  19867. */
  19868. static int test_wc_DsaExportParamsRaw(void)
  19869. {
  19870. EXPECT_DECLS;
  19871. #if !defined(NO_DSA)
  19872. DsaKey key;
  19873. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19874. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19875. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19876. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19877. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19878. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19879. "80a0093113a8bd73";
  19880. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19881. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19882. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19883. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19884. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19885. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19886. "76341a7e7d9";
  19887. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19888. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19889. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19890. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19891. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19892. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19893. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19894. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19895. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19896. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19897. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19898. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19899. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19900. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19901. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19902. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19903. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19904. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19905. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19906. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19907. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19908. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19909. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19910. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19911. byte pOut[MAX_DSA_PARAM_SIZE];
  19912. byte qOut[MAX_DSA_PARAM_SIZE];
  19913. byte gOut[MAX_DSA_PARAM_SIZE];
  19914. word32 pOutSz;
  19915. word32 qOutSz;
  19916. word32 gOutSz;
  19917. XMEMSET(&key, 0, sizeof(DsaKey));
  19918. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19919. /* first test using imported raw parameters, for expected */
  19920. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19921. pOutSz = sizeof(pOut);
  19922. qOutSz = sizeof(qOut);
  19923. gOutSz = sizeof(gOut);
  19924. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19925. &gOutSz), 0);
  19926. /* validate exported parameters are correct */
  19927. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19928. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19929. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19930. /* test bad args */
  19931. /* null key struct */
  19932. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19933. &gOutSz), BAD_FUNC_ARG);
  19934. /* null output pointers */
  19935. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19936. &gOutSz), LENGTH_ONLY_E);
  19937. /* null output size pointers */
  19938. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19939. NULL), BAD_FUNC_ARG);
  19940. /* p output buffer size too small */
  19941. pOutSz = 1;
  19942. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19943. &gOutSz), BUFFER_E);
  19944. pOutSz = sizeof(pOut);
  19945. /* q output buffer size too small */
  19946. qOutSz = 1;
  19947. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19948. &gOutSz), BUFFER_E);
  19949. qOutSz = sizeof(qOut);
  19950. /* g output buffer size too small */
  19951. gOutSz = 1;
  19952. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19953. &gOutSz), BUFFER_E);
  19954. wc_FreeDsaKey(&key);
  19955. #endif
  19956. return EXPECT_RESULT();
  19957. } /* END test_wc_DsaExportParamsRaw */
  19958. /*
  19959. * Testing wc_DsaExportKeyRaw()
  19960. */
  19961. static int test_wc_DsaExportKeyRaw(void)
  19962. {
  19963. EXPECT_DECLS;
  19964. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19965. DsaKey key;
  19966. WC_RNG rng;
  19967. byte xOut[MAX_DSA_PARAM_SIZE];
  19968. byte yOut[MAX_DSA_PARAM_SIZE];
  19969. word32 xOutSz, yOutSz;
  19970. XMEMSET(&key, 0, sizeof(key));
  19971. XMEMSET(&rng, 0, sizeof(rng));
  19972. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19973. ExpectIntEQ(wc_InitRng(&rng), 0);
  19974. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19975. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19976. /* try successful export */
  19977. xOutSz = sizeof(xOut);
  19978. yOutSz = sizeof(yOut);
  19979. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19980. /* test bad args */
  19981. /* null key struct */
  19982. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19983. BAD_FUNC_ARG);
  19984. /* null output pointers */
  19985. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19986. LENGTH_ONLY_E);
  19987. /* null output size pointers */
  19988. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19989. BAD_FUNC_ARG);
  19990. /* x output buffer size too small */
  19991. xOutSz = 1;
  19992. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19993. BUFFER_E);
  19994. xOutSz = sizeof(xOut);
  19995. /* y output buffer size too small */
  19996. yOutSz = 1;
  19997. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19998. BUFFER_E);
  19999. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20000. wc_FreeDsaKey(&key);
  20001. #endif
  20002. return EXPECT_RESULT();
  20003. } /* END test_wc_DsaExportParamsRaw */
  20004. /*
  20005. * Testing wc_ed25519_make_key().
  20006. */
  20007. static int test_wc_ed25519_make_key(void)
  20008. {
  20009. EXPECT_DECLS;
  20010. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  20011. ed25519_key key;
  20012. WC_RNG rng;
  20013. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  20014. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  20015. XMEMSET(&key, 0, sizeof(ed25519_key));
  20016. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20017. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20018. ExpectIntEQ(wc_InitRng(&rng), 0);
  20019. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, (word32)pubkey_sz),
  20020. ECC_PRIV_KEY_E);
  20021. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, (word32)pubkey_sz),
  20022. ECC_PRIV_KEY_E);
  20023. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20024. /* Test bad args. */
  20025. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  20026. BAD_FUNC_ARG);
  20027. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  20028. BAD_FUNC_ARG);
  20029. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  20030. BAD_FUNC_ARG);
  20031. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  20032. BAD_FUNC_ARG);
  20033. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20034. wc_ed25519_free(&key);
  20035. #endif
  20036. return EXPECT_RESULT();
  20037. } /* END test_wc_ed25519_make_key */
  20038. /*
  20039. * Testing wc_ed25519_init()
  20040. */
  20041. static int test_wc_ed25519_init(void)
  20042. {
  20043. EXPECT_DECLS;
  20044. #if defined(HAVE_ED25519)
  20045. ed25519_key key;
  20046. XMEMSET(&key, 0, sizeof(ed25519_key));
  20047. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20048. /* Test bad args. */
  20049. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  20050. wc_ed25519_free(&key);
  20051. #endif
  20052. return EXPECT_RESULT();
  20053. } /* END test_wc_ed25519_init */
  20054. /*
  20055. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  20056. */
  20057. static int test_wc_ed25519_sign_msg(void)
  20058. {
  20059. EXPECT_DECLS;
  20060. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  20061. WC_RNG rng;
  20062. ed25519_key key;
  20063. byte msg[] = "Everybody gets Friday off.\n";
  20064. byte sig[ED25519_SIG_SIZE+1];
  20065. word32 msglen = sizeof(msg);
  20066. word32 siglen = ED25519_SIG_SIZE;
  20067. word32 badSigLen = ED25519_SIG_SIZE - 1;
  20068. #ifdef HAVE_ED25519_VERIFY
  20069. int verify_ok = 0; /*1 = Verify success.*/
  20070. #endif
  20071. /* Initialize stack variables. */
  20072. XMEMSET(&key, 0, sizeof(ed25519_key));
  20073. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20074. XMEMSET(sig, 0, sizeof(sig));
  20075. /* Initialize key. */
  20076. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20077. ExpectIntEQ(wc_InitRng(&rng), 0);
  20078. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20079. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  20080. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20081. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  20082. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20083. /* Test bad args. */
  20084. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  20085. BAD_FUNC_ARG);
  20086. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  20087. BAD_FUNC_ARG);
  20088. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  20089. BAD_FUNC_ARG);
  20090. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  20091. BAD_FUNC_ARG);
  20092. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  20093. BUFFER_E);
  20094. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  20095. badSigLen -= 1;
  20096. #ifdef HAVE_ED25519_VERIFY
  20097. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20098. &key), 0);
  20099. ExpectIntEQ(verify_ok, 1);
  20100. /* Test bad args. */
  20101. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20102. &verify_ok, &key), BAD_FUNC_ARG);
  20103. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20104. &verify_ok, &key), BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20106. &key), BAD_FUNC_ARG);
  20107. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20108. &key), BAD_FUNC_ARG);
  20109. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20110. BAD_FUNC_ARG);
  20111. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20112. NULL), BAD_FUNC_ARG);
  20113. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20114. &key), BAD_FUNC_ARG);
  20115. #endif /* Verify. */
  20116. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20117. wc_ed25519_free(&key);
  20118. #endif
  20119. return EXPECT_RESULT();
  20120. } /* END test_wc_ed25519_sign_msg */
  20121. /*
  20122. * Testing wc_ed25519_import_public()
  20123. */
  20124. static int test_wc_ed25519_import_public(void)
  20125. {
  20126. EXPECT_DECLS;
  20127. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20128. ed25519_key pubKey;
  20129. WC_RNG rng;
  20130. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20131. word32 inlen = sizeof(in);
  20132. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20133. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20134. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20135. ExpectIntEQ(wc_InitRng(&rng), 0);
  20136. #ifdef HAVE_ED25519_MAKE_KEY
  20137. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20138. #endif
  20139. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20140. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20141. /* Test bad args. */
  20142. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20143. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20144. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20145. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20146. wc_ed25519_free(&pubKey);
  20147. #endif
  20148. return EXPECT_RESULT();
  20149. } /* END wc_ed25519_import_public */
  20150. /*
  20151. * Testing wc_ed25519_import_private_key()
  20152. */
  20153. static int test_wc_ed25519_import_private_key(void)
  20154. {
  20155. EXPECT_DECLS;
  20156. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20157. ed25519_key key;
  20158. WC_RNG rng;
  20159. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20160. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20161. word32 privKeySz = sizeof(privKey);
  20162. word32 pubKeySz = sizeof(pubKey);
  20163. #ifdef HAVE_ED25519_KEY_EXPORT
  20164. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20165. word32 bothKeysSz = sizeof(bothKeys);
  20166. #endif
  20167. XMEMSET(&key, 0, sizeof(ed25519_key));
  20168. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20169. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20170. ExpectIntEQ(wc_InitRng(&rng), 0);
  20171. #ifdef HAVE_ED25519_MAKE_KEY
  20172. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20173. #endif
  20174. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20175. pubKeySz, &key, 1), 0);
  20176. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20177. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20178. #ifdef HAVE_ED25519_KEY_EXPORT
  20179. PRIVATE_KEY_UNLOCK();
  20180. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20181. PRIVATE_KEY_LOCK();
  20182. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20183. &key, 1), 0);
  20184. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20185. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20186. #endif
  20187. /* Test bad args. */
  20188. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20189. &key), BAD_FUNC_ARG);
  20190. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20191. pubKeySz, &key), BAD_FUNC_ARG);
  20192. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20193. pubKeySz, NULL), BAD_FUNC_ARG);
  20194. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20195. pubKeySz, &key), BAD_FUNC_ARG);
  20196. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20197. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20198. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20199. &key), BAD_FUNC_ARG);
  20200. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20201. wc_ed25519_free(&key);
  20202. #endif
  20203. return EXPECT_RESULT();
  20204. } /* END test_wc_ed25519_import_private_key */
  20205. /*
  20206. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20207. */
  20208. static int test_wc_ed25519_export(void)
  20209. {
  20210. EXPECT_DECLS;
  20211. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20212. ed25519_key key;
  20213. WC_RNG rng;
  20214. byte priv[ED25519_PRV_KEY_SIZE];
  20215. byte pub[ED25519_PUB_KEY_SIZE];
  20216. word32 privSz = sizeof(priv);
  20217. word32 pubSz = sizeof(pub);
  20218. #ifndef HAVE_ED25519_MAKE_KEY
  20219. const byte privKey[] = {
  20220. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20221. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20222. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20223. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20224. };
  20225. const byte pubKey[] = {
  20226. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20227. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20228. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20229. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20230. };
  20231. #endif
  20232. XMEMSET(&key, 0, sizeof(ed25519_key));
  20233. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20234. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20235. ExpectIntEQ(wc_InitRng(&rng), 0);
  20236. #ifdef HAVE_ED25519_MAKE_KEY
  20237. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20238. #else
  20239. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20240. pubKey, sizeof(pubKey), &key, 1), 0);
  20241. #endif
  20242. PRIVATE_KEY_UNLOCK();
  20243. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20244. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20245. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20246. /* Test bad args. */
  20247. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20248. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20249. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20250. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20251. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20252. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20253. /* Test bad args. */
  20254. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20255. BAD_FUNC_ARG);
  20256. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20257. BAD_FUNC_ARG);
  20258. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20259. BAD_FUNC_ARG);
  20260. PRIVATE_KEY_LOCK();
  20261. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20262. wc_ed25519_free(&key);
  20263. #endif
  20264. return EXPECT_RESULT();
  20265. } /* END test_wc_ed25519_export */
  20266. /*
  20267. * Testing wc_ed25519_size()
  20268. */
  20269. static int test_wc_ed25519_size(void)
  20270. {
  20271. EXPECT_DECLS;
  20272. #if defined(HAVE_ED25519)
  20273. ed25519_key key;
  20274. WC_RNG rng;
  20275. #ifndef HAVE_ED25519_MAKE_KEY
  20276. const byte privKey[] = {
  20277. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20278. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20279. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20280. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20281. };
  20282. const byte pubKey[] = {
  20283. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20284. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20285. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20286. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20287. };
  20288. #endif
  20289. XMEMSET(&key, 0, sizeof(ed25519_key));
  20290. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20291. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20292. ExpectIntEQ(wc_InitRng(&rng), 0);
  20293. #ifdef HAVE_ED25519_MAKE_KEY
  20294. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20295. #else
  20296. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20297. pubKey, sizeof(pubKey), &key, 1), 0);
  20298. #endif
  20299. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20300. /* Test bad args. */
  20301. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20302. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20303. /* Test bad args. */
  20304. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20305. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20306. /* Test bad args. */
  20307. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20308. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20309. /* Test bad args. */
  20310. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20311. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20312. wc_ed25519_free(&key);
  20313. #endif
  20314. return EXPECT_RESULT();
  20315. } /* END test_wc_ed25519_size */
  20316. /*
  20317. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20318. */
  20319. static int test_wc_ed25519_exportKey(void)
  20320. {
  20321. EXPECT_DECLS;
  20322. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20323. WC_RNG rng;
  20324. ed25519_key key;
  20325. byte priv[ED25519_PRV_KEY_SIZE];
  20326. byte pub[ED25519_PUB_KEY_SIZE];
  20327. byte privOnly[ED25519_PRV_KEY_SIZE];
  20328. word32 privSz = sizeof(priv);
  20329. word32 pubSz = sizeof(pub);
  20330. word32 privOnlySz = sizeof(privOnly);
  20331. #ifndef HAVE_ED25519_MAKE_KEY
  20332. const byte privKey[] = {
  20333. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20334. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20335. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20336. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20337. };
  20338. const byte pubKey[] = {
  20339. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20340. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20341. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20342. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20343. };
  20344. #endif
  20345. XMEMSET(&key, 0, sizeof(ed25519_key));
  20346. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20347. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20348. ExpectIntEQ(wc_InitRng(&rng), 0);
  20349. #ifdef HAVE_ED25519_MAKE_KEY
  20350. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20351. #else
  20352. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20353. pubKey, sizeof(pubKey), &key, 1), 0);
  20354. #endif
  20355. PRIVATE_KEY_UNLOCK();
  20356. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20357. /* Test bad args. */
  20358. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20359. BAD_FUNC_ARG);
  20360. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20361. BAD_FUNC_ARG);
  20362. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20363. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20364. /* Test bad args. */
  20365. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20366. BAD_FUNC_ARG);
  20367. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20368. BAD_FUNC_ARG);
  20369. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20370. BAD_FUNC_ARG);
  20371. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20372. BAD_FUNC_ARG);
  20373. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20374. BAD_FUNC_ARG);
  20375. PRIVATE_KEY_LOCK();
  20376. /* Cross check output. */
  20377. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20378. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20379. wc_ed25519_free(&key);
  20380. #endif
  20381. return EXPECT_RESULT();
  20382. } /* END test_wc_ed25519_exportKey */
  20383. /*
  20384. * Testing wc_Ed25519PublicKeyToDer
  20385. */
  20386. static int test_wc_Ed25519PublicKeyToDer(void)
  20387. {
  20388. EXPECT_DECLS;
  20389. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20390. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20391. ed25519_key key;
  20392. byte derBuf[1024];
  20393. XMEMSET(&key, 0, sizeof(ed25519_key));
  20394. /* Test bad args */
  20395. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20396. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20397. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20398. wc_ed25519_free(&key);
  20399. /* Test good args */
  20400. if (EXPECT_SUCCESS()) {
  20401. WC_RNG rng;
  20402. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20403. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20404. ExpectIntEQ(wc_InitRng(&rng), 0);
  20405. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20406. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20407. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20408. wc_ed25519_free(&key);
  20409. }
  20410. #endif
  20411. return EXPECT_RESULT();
  20412. } /* END testing wc_Ed25519PublicKeyToDer */
  20413. /*
  20414. * Testing wc_curve25519_init and wc_curve25519_free.
  20415. */
  20416. static int test_wc_curve25519_init(void)
  20417. {
  20418. EXPECT_DECLS;
  20419. #if defined(HAVE_CURVE25519)
  20420. curve25519_key key;
  20421. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20422. /* Test bad args for wc_curve25519_init */
  20423. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20424. /* Test good args for wc_curve_25519_free */
  20425. wc_curve25519_free(&key);
  20426. /* Test bad args for wc_curve25519 free. */
  20427. wc_curve25519_free(NULL);
  20428. #endif
  20429. return EXPECT_RESULT();
  20430. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20431. /*
  20432. * Testing test_wc_curve25519_size.
  20433. */
  20434. static int test_wc_curve25519_size(void)
  20435. {
  20436. EXPECT_DECLS;
  20437. #if defined(HAVE_CURVE25519)
  20438. curve25519_key key;
  20439. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20440. /* Test good args for wc_curve25519_size */
  20441. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20442. /* Test bad args for wc_curve25519_size */
  20443. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20444. wc_curve25519_free(&key);
  20445. #endif
  20446. return EXPECT_RESULT();
  20447. } /* END test_wc_curve25519_size*/
  20448. /*
  20449. * Testing test_wc_curve25519_export_key_raw().
  20450. */
  20451. static int test_wc_curve25519_export_key_raw(void)
  20452. {
  20453. EXPECT_DECLS;
  20454. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20455. curve25519_key key;
  20456. WC_RNG rng;
  20457. byte privateKey[CURVE25519_KEYSIZE];
  20458. byte publicKey[CURVE25519_KEYSIZE];
  20459. word32 prvkSz;
  20460. word32 pubkSz;
  20461. byte prik[CURVE25519_KEYSIZE];
  20462. byte pubk[CURVE25519_KEYSIZE];
  20463. word32 prksz;
  20464. word32 pbksz;
  20465. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20466. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20467. ExpectIntEQ(wc_InitRng(&rng), 0);
  20468. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20469. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20470. prvkSz = CURVE25519_KEYSIZE;
  20471. pubkSz = CURVE25519_KEYSIZE;
  20472. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20473. publicKey, &pubkSz), BAD_FUNC_ARG);
  20474. prvkSz = CURVE25519_KEYSIZE;
  20475. pubkSz = CURVE25519_KEYSIZE;
  20476. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20477. &pubkSz), BAD_FUNC_ARG);
  20478. prvkSz = CURVE25519_KEYSIZE;
  20479. pubkSz = CURVE25519_KEYSIZE;
  20480. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20481. publicKey, &pubkSz), BAD_FUNC_ARG);
  20482. /* prvkSz = CURVE25519_KEYSIZE; */
  20483. pubkSz = CURVE25519_KEYSIZE;
  20484. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20485. NULL, &pubkSz), BAD_FUNC_ARG);
  20486. prvkSz = CURVE25519_KEYSIZE;
  20487. pubkSz = CURVE25519_KEYSIZE;
  20488. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20489. publicKey, NULL), BAD_FUNC_ARG);
  20490. /* cross-testing */
  20491. prksz = CURVE25519_KEYSIZE;
  20492. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20493. pbksz = CURVE25519_KEYSIZE;
  20494. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20495. prvkSz = CURVE25519_KEYSIZE;
  20496. /* pubkSz = CURVE25519_KEYSIZE; */
  20497. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20498. publicKey, &pubkSz), 0);
  20499. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20500. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20501. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20502. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20503. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20504. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20505. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20506. wc_curve25519_free(&key);
  20507. #endif
  20508. return EXPECT_RESULT();
  20509. } /* end of test_wc_curve25519_export_key_raw */
  20510. /*
  20511. * Testing test_wc_curve25519_export_key_raw_ex().
  20512. */
  20513. static int test_wc_curve25519_export_key_raw_ex(void)
  20514. {
  20515. EXPECT_DECLS;
  20516. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20517. curve25519_key key;
  20518. WC_RNG rng;
  20519. byte privateKey[CURVE25519_KEYSIZE];
  20520. byte publicKey[CURVE25519_KEYSIZE];
  20521. word32 prvkSz;
  20522. word32 pubkSz;
  20523. byte prik[CURVE25519_KEYSIZE];
  20524. byte pubk[CURVE25519_KEYSIZE];
  20525. word32 prksz;
  20526. word32 pbksz;
  20527. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20528. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20529. ExpectIntEQ(wc_InitRng(&rng), 0);
  20530. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20531. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20532. prvkSz = CURVE25519_KEYSIZE;
  20533. pubkSz = CURVE25519_KEYSIZE;
  20534. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20535. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20536. prvkSz = CURVE25519_KEYSIZE;
  20537. pubkSz = CURVE25519_KEYSIZE;
  20538. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20539. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20540. prvkSz = CURVE25519_KEYSIZE;
  20541. pubkSz = CURVE25519_KEYSIZE;
  20542. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20543. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20544. /* prvkSz = CURVE25519_KEYSIZE; */
  20545. pubkSz = CURVE25519_KEYSIZE;
  20546. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20547. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20548. prvkSz = CURVE25519_KEYSIZE;
  20549. pubkSz = CURVE25519_KEYSIZE;
  20550. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20551. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20552. prvkSz = CURVE25519_KEYSIZE;
  20553. /* pubkSz = CURVE25519_KEYSIZE; */
  20554. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20555. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20556. prvkSz = CURVE25519_KEYSIZE;
  20557. pubkSz = CURVE25519_KEYSIZE;
  20558. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20559. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20560. prvkSz = CURVE25519_KEYSIZE;
  20561. pubkSz = CURVE25519_KEYSIZE;
  20562. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20563. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20564. /* prvkSz = CURVE25519_KEYSIZE; */
  20565. pubkSz = CURVE25519_KEYSIZE;
  20566. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20567. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20568. prvkSz = CURVE25519_KEYSIZE;
  20569. pubkSz = CURVE25519_KEYSIZE;
  20570. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20571. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20572. /* illegal value for endian */
  20573. prvkSz = CURVE25519_KEYSIZE;
  20574. /* pubkSz = CURVE25519_KEYSIZE; */
  20575. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20576. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20577. /* cross-testing */
  20578. prksz = CURVE25519_KEYSIZE;
  20579. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20580. pbksz = CURVE25519_KEYSIZE;
  20581. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20582. prvkSz = CURVE25519_KEYSIZE;
  20583. /* pubkSz = CURVE25519_KEYSIZE; */
  20584. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20585. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20586. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20587. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20588. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20589. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20590. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20591. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20592. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20593. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20594. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20595. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20596. /* try once with another endian */
  20597. prvkSz = CURVE25519_KEYSIZE;
  20598. pubkSz = CURVE25519_KEYSIZE;
  20599. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20600. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20601. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20602. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20603. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20604. wc_curve25519_free(&key);
  20605. #endif
  20606. return EXPECT_RESULT();
  20607. } /* end of test_wc_curve25519_export_key_raw_ex */
  20608. /*
  20609. * Testing wc_curve25519_make_key
  20610. */
  20611. static int test_wc_curve25519_make_key(void)
  20612. {
  20613. EXPECT_DECLS;
  20614. #if defined(HAVE_CURVE25519)
  20615. curve25519_key key;
  20616. WC_RNG rng;
  20617. int keysize;
  20618. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20619. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20620. ExpectIntEQ(wc_InitRng(&rng), 0);
  20621. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20622. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20623. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20624. /* test bad cases*/
  20625. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20626. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20627. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20628. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20629. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20630. wc_curve25519_free(&key);
  20631. #endif
  20632. return EXPECT_RESULT();
  20633. } /* END test_wc_curve25519_make_key*/
  20634. /*
  20635. * Testing wc_curve25519_shared_secret_ex
  20636. */
  20637. static int test_wc_curve25519_shared_secret_ex(void)
  20638. {
  20639. EXPECT_DECLS;
  20640. #if defined(HAVE_CURVE25519)
  20641. curve25519_key private_key;
  20642. curve25519_key public_key;
  20643. WC_RNG rng;
  20644. byte out[CURVE25519_KEYSIZE];
  20645. word32 outLen = sizeof(out);
  20646. int endian = EC25519_BIG_ENDIAN;
  20647. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20648. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20649. ExpectIntEQ(wc_InitRng(&rng), 0);
  20650. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20651. 0);
  20652. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20653. 0);
  20654. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20655. &outLen, endian), 0);
  20656. /* test bad cases*/
  20657. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20658. BAD_FUNC_ARG);
  20659. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20660. endian), BAD_FUNC_ARG);
  20661. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20662. endian), BAD_FUNC_ARG);
  20663. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20664. &outLen, endian), BAD_FUNC_ARG);
  20665. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20666. NULL, endian), BAD_FUNC_ARG);
  20667. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20668. * increasing to 0x8f checks for error being returned*/
  20669. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20670. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20671. &outLen, endian), ECC_BAD_ARG_E);
  20672. outLen = outLen - 2;
  20673. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20674. &outLen, endian), BAD_FUNC_ARG);
  20675. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20676. wc_curve25519_free(&private_key);
  20677. wc_curve25519_free(&public_key);
  20678. #endif
  20679. return EXPECT_RESULT();
  20680. } /* END test_wc_curve25519_shared_secret_ex*/
  20681. /*
  20682. * Testing wc_curve25519_make_pub
  20683. */
  20684. static int test_wc_curve25519_make_pub(void)
  20685. {
  20686. EXPECT_DECLS;
  20687. #ifdef HAVE_CURVE25519
  20688. curve25519_key key;
  20689. WC_RNG rng;
  20690. byte out[CURVE25519_KEYSIZE];
  20691. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20692. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20693. ExpectIntEQ(wc_InitRng(&rng), 0);
  20694. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20695. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20696. (int)sizeof(key.k), key.k), 0);
  20697. /* test bad cases*/
  20698. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20699. (int)sizeof out, out), ECC_BAD_ARG_E);
  20700. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20701. NULL), ECC_BAD_ARG_E);
  20702. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20703. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20704. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20705. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20706. /* verify clamping test */
  20707. key.k[0] |= ~248;
  20708. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20709. key.k), ECC_BAD_ARG_E);
  20710. key.k[0] &= 248;
  20711. /* repeat the expected-to-succeed test. */
  20712. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20713. key.k), 0);
  20714. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20715. wc_curve25519_free(&key);
  20716. #endif
  20717. return EXPECT_RESULT();
  20718. } /* END test_wc_curve25519_make_pub */
  20719. /*
  20720. * Testing test_wc_curve25519_export_public_ex
  20721. */
  20722. static int test_wc_curve25519_export_public_ex(void)
  20723. {
  20724. EXPECT_DECLS;
  20725. #if defined(HAVE_CURVE25519)
  20726. curve25519_key key;
  20727. WC_RNG rng;
  20728. byte out[CURVE25519_KEYSIZE];
  20729. word32 outLen = sizeof(out);
  20730. int endian = EC25519_BIG_ENDIAN;
  20731. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20732. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20733. ExpectIntEQ(wc_InitRng(&rng), 0);
  20734. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20735. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20736. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20737. /* test bad cases*/
  20738. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20739. BAD_FUNC_ARG);
  20740. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20741. BAD_FUNC_ARG);
  20742. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20743. BAD_FUNC_ARG);
  20744. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20745. BAD_FUNC_ARG);
  20746. outLen = outLen - 2;
  20747. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20748. ECC_BAD_ARG_E);
  20749. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20750. wc_curve25519_free(&key);
  20751. #endif
  20752. return EXPECT_RESULT();
  20753. } /* END test_wc_curve25519_export_public_ex*/
  20754. /*
  20755. * Testing test_wc_curve25519_import_private_raw_ex
  20756. */
  20757. static int test_wc_curve25519_import_private_raw_ex(void)
  20758. {
  20759. EXPECT_DECLS;
  20760. #if defined(HAVE_CURVE25519)
  20761. curve25519_key key;
  20762. WC_RNG rng;
  20763. byte priv[CURVE25519_KEYSIZE];
  20764. byte pub[CURVE25519_KEYSIZE];
  20765. word32 privSz = sizeof(priv);
  20766. word32 pubSz = sizeof(pub);
  20767. int endian = EC25519_BIG_ENDIAN;
  20768. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20769. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20770. ExpectIntEQ(wc_InitRng(&rng), 0);
  20771. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20772. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20773. endian), 0);
  20774. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20775. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20776. &key, endian), 0);
  20777. /* test bad cases*/
  20778. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20779. endian), BAD_FUNC_ARG);
  20780. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20781. &key, endian), BAD_FUNC_ARG);
  20782. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20783. &key, endian), BAD_FUNC_ARG);
  20784. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20785. NULL, endian), BAD_FUNC_ARG);
  20786. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20787. &key, endian), ECC_BAD_ARG_E);
  20788. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20789. &key, endian), ECC_BAD_ARG_E);
  20790. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20791. &key, EC25519_LITTLE_ENDIAN), 0);
  20792. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20793. wc_curve25519_free(&key);
  20794. #endif
  20795. return EXPECT_RESULT();
  20796. } /* END test_wc_curve25519_import_private_raw_ex*/
  20797. /*
  20798. * Testing test_wc_curve25519_import_private
  20799. */
  20800. static int test_wc_curve25519_import_private(void)
  20801. {
  20802. EXPECT_DECLS;
  20803. #if defined(HAVE_CURVE25519)
  20804. curve25519_key key;
  20805. WC_RNG rng;
  20806. byte priv[CURVE25519_KEYSIZE];
  20807. word32 privSz = sizeof(priv);
  20808. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20809. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20810. ExpectIntEQ(wc_InitRng(&rng), 0);
  20811. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20812. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20813. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20814. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20815. wc_curve25519_free(&key);
  20816. #endif
  20817. return EXPECT_RESULT();
  20818. } /* END test_wc_curve25519_import*/
  20819. /*
  20820. * Testing test_wc_curve25519_export_private_raw_ex
  20821. */
  20822. static int test_wc_curve25519_export_private_raw_ex(void)
  20823. {
  20824. EXPECT_DECLS;
  20825. #if defined(HAVE_CURVE25519)
  20826. curve25519_key key;
  20827. byte out[CURVE25519_KEYSIZE];
  20828. word32 outLen = sizeof(out);
  20829. int endian = EC25519_BIG_ENDIAN;
  20830. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20831. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20832. 0);
  20833. /* test bad cases*/
  20834. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20835. BAD_FUNC_ARG);
  20836. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20837. BAD_FUNC_ARG);
  20838. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20839. endian), BAD_FUNC_ARG);
  20840. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20841. BAD_FUNC_ARG);
  20842. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20843. EC25519_LITTLE_ENDIAN), 0);
  20844. outLen = outLen - 2;
  20845. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20846. ECC_BAD_ARG_E);
  20847. wc_curve25519_free(&key);
  20848. #endif
  20849. return EXPECT_RESULT();
  20850. } /* END test_wc_curve25519_export_private_raw_ex*/
  20851. /*
  20852. * Testing wc_ed448_make_key().
  20853. */
  20854. static int test_wc_ed448_make_key(void)
  20855. {
  20856. EXPECT_DECLS;
  20857. #if defined(HAVE_ED448)
  20858. ed448_key key;
  20859. WC_RNG rng;
  20860. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20861. XMEMSET(&key, 0, sizeof(ed448_key));
  20862. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20863. ExpectIntEQ(wc_ed448_init(&key), 0);
  20864. ExpectIntEQ(wc_InitRng(&rng), 0);
  20865. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20866. ECC_PRIV_KEY_E);
  20867. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20868. /* Test bad args. */
  20869. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20870. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20871. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20872. BAD_FUNC_ARG);
  20873. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20874. BAD_FUNC_ARG);
  20875. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20876. wc_ed448_free(&key);
  20877. #endif
  20878. return EXPECT_RESULT();
  20879. } /* END test_wc_ed448_make_key */
  20880. /*
  20881. * Testing wc_ed448_init()
  20882. */
  20883. static int test_wc_ed448_init(void)
  20884. {
  20885. EXPECT_DECLS;
  20886. #if defined(HAVE_ED448)
  20887. ed448_key key;
  20888. XMEMSET(&key, 0, sizeof(ed448_key));
  20889. ExpectIntEQ(wc_ed448_init(&key), 0);
  20890. /* Test bad args. */
  20891. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20892. wc_ed448_free(&key);
  20893. #endif
  20894. return EXPECT_RESULT();
  20895. } /* END test_wc_ed448_init */
  20896. /*
  20897. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20898. */
  20899. static int test_wc_ed448_sign_msg(void)
  20900. {
  20901. EXPECT_DECLS;
  20902. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20903. ed448_key key;
  20904. WC_RNG rng;
  20905. byte msg[] = "Everybody gets Friday off.\n";
  20906. byte sig[ED448_SIG_SIZE];
  20907. word32 msglen = sizeof(msg);
  20908. word32 siglen = sizeof(sig);
  20909. word32 badSigLen = sizeof(sig) - 1;
  20910. #ifdef HAVE_ED448_VERIFY
  20911. int verify_ok = 0; /*1 = Verify success.*/
  20912. #endif
  20913. /* Initialize stack variables. */
  20914. XMEMSET(&key, 0, sizeof(ed448_key));
  20915. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20916. XMEMSET(sig, 0, siglen);
  20917. /* Initialize key. */
  20918. ExpectIntEQ(wc_ed448_init(&key), 0);
  20919. ExpectIntEQ(wc_InitRng(&rng), 0);
  20920. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20921. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20922. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20923. /* Test bad args. */
  20924. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20925. BAD_FUNC_ARG);
  20926. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20927. BAD_FUNC_ARG);
  20928. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20929. BAD_FUNC_ARG);
  20930. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20931. BAD_FUNC_ARG);
  20932. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20933. BUFFER_E);
  20934. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20935. badSigLen -= 1;
  20936. #ifdef HAVE_ED448_VERIFY
  20937. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20938. NULL, 0), 0);
  20939. ExpectIntEQ(verify_ok, 1);
  20940. /* Test bad args. */
  20941. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20942. &key, NULL, 0), BAD_FUNC_ARG);
  20943. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20944. &key, NULL, 0), BAD_FUNC_ARG);
  20945. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20946. &key, NULL, 0), BAD_FUNC_ARG);
  20947. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20948. &key, NULL, 0), BAD_FUNC_ARG);
  20949. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20950. &key, NULL, 0), BAD_FUNC_ARG);
  20951. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20952. NULL, NULL, 0), BAD_FUNC_ARG);
  20953. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20954. &key, NULL, 0), BAD_FUNC_ARG);
  20955. #endif /* Verify. */
  20956. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20957. wc_ed448_free(&key);
  20958. #endif
  20959. return EXPECT_RESULT();
  20960. } /* END test_wc_ed448_sign_msg */
  20961. /*
  20962. * Testing wc_ed448_import_public()
  20963. */
  20964. static int test_wc_ed448_import_public(void)
  20965. {
  20966. EXPECT_DECLS;
  20967. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20968. ed448_key pubKey;
  20969. WC_RNG rng;
  20970. const byte in[] =
  20971. "Ed448PublicKeyUnitTest.................................\n";
  20972. word32 inlen = sizeof(in);
  20973. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20974. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20975. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20976. ExpectIntEQ(wc_InitRng(&rng), 0);
  20977. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20978. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20979. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20980. /* Test bad args. */
  20981. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20982. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20983. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20984. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20985. wc_ed448_free(&pubKey);
  20986. #endif
  20987. return EXPECT_RESULT();
  20988. } /* END wc_ed448_import_public */
  20989. /*
  20990. * Testing wc_ed448_import_private_key()
  20991. */
  20992. static int test_wc_ed448_import_private_key(void)
  20993. {
  20994. EXPECT_DECLS;
  20995. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20996. ed448_key key;
  20997. WC_RNG rng;
  20998. const byte privKey[] =
  20999. "Ed448PrivateKeyUnitTest................................\n";
  21000. const byte pubKey[] =
  21001. "Ed448PublicKeyUnitTest.................................\n";
  21002. word32 privKeySz = sizeof(privKey);
  21003. word32 pubKeySz = sizeof(pubKey);
  21004. #ifdef HAVE_ED448_KEY_EXPORT
  21005. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21006. word32 bothKeysSz = sizeof(bothKeys);
  21007. #endif
  21008. XMEMSET(&key, 0, sizeof(ed448_key));
  21009. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21010. ExpectIntEQ(wc_ed448_init(&key), 0);
  21011. ExpectIntEQ(wc_InitRng(&rng), 0);
  21012. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21013. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21014. pubKeySz, &key, 1), 0);
  21015. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21016. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21017. #ifdef HAVE_ED448_KEY_EXPORT
  21018. PRIVATE_KEY_UNLOCK();
  21019. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  21020. PRIVATE_KEY_LOCK();
  21021. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21022. &key, 1), 0);
  21023. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21024. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21025. #endif
  21026. /* Test bad args. */
  21027. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21028. &key), BAD_FUNC_ARG);
  21029. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  21030. &key), BAD_FUNC_ARG);
  21031. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21032. pubKeySz, NULL), BAD_FUNC_ARG);
  21033. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21034. pubKeySz, &key), BAD_FUNC_ARG);
  21035. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21036. pubKeySz - 1, &key), BAD_FUNC_ARG);
  21037. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  21038. BAD_FUNC_ARG);
  21039. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21040. wc_ed448_free(&key);
  21041. #endif
  21042. return EXPECT_RESULT();
  21043. } /* END test_wc_ed448_import_private_key */
  21044. /*
  21045. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21046. */
  21047. static int test_wc_ed448_export(void)
  21048. {
  21049. EXPECT_DECLS;
  21050. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21051. ed448_key key;
  21052. WC_RNG rng;
  21053. byte priv[ED448_PRV_KEY_SIZE];
  21054. byte pub[ED448_PUB_KEY_SIZE];
  21055. word32 privSz = sizeof(priv);
  21056. word32 pubSz = sizeof(pub);
  21057. XMEMSET(&key, 0, sizeof(ed448_key));
  21058. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21059. ExpectIntEQ(wc_ed448_init(&key), 0);
  21060. ExpectIntEQ(wc_InitRng(&rng), 0);
  21061. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21062. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  21063. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  21064. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  21065. /* Test bad args. */
  21066. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  21067. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  21068. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  21069. PRIVATE_KEY_UNLOCK();
  21070. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  21071. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  21072. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  21073. /* Test bad args. */
  21074. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  21075. BAD_FUNC_ARG);
  21076. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  21077. BAD_FUNC_ARG);
  21078. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  21079. PRIVATE_KEY_LOCK();
  21080. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21081. wc_ed448_free(&key);
  21082. #endif
  21083. return EXPECT_RESULT();
  21084. } /* END test_wc_ed448_export */
  21085. /*
  21086. * Testing wc_ed448_size()
  21087. */
  21088. static int test_wc_ed448_size(void)
  21089. {
  21090. EXPECT_DECLS;
  21091. #if defined(HAVE_ED448)
  21092. ed448_key key;
  21093. WC_RNG rng;
  21094. XMEMSET(&key, 0, sizeof(ed448_key));
  21095. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21096. ExpectIntEQ(wc_ed448_init(&key), 0);
  21097. ExpectIntEQ(wc_InitRng(&rng), 0);
  21098. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21099. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  21100. /* Test bad args. */
  21101. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21102. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21103. /* Test bad args. */
  21104. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21105. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21106. /* Test bad args. */
  21107. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21108. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21109. /* Test bad args. */
  21110. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21111. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21112. wc_ed448_free(&key);
  21113. #endif
  21114. return EXPECT_RESULT();
  21115. } /* END test_wc_ed448_size */
  21116. /*
  21117. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21118. */
  21119. static int test_wc_ed448_exportKey(void)
  21120. {
  21121. EXPECT_DECLS;
  21122. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21123. ed448_key key;
  21124. WC_RNG rng;
  21125. byte priv[ED448_PRV_KEY_SIZE];
  21126. byte pub[ED448_PUB_KEY_SIZE];
  21127. byte privOnly[ED448_PRV_KEY_SIZE];
  21128. word32 privSz = sizeof(priv);
  21129. word32 pubSz = sizeof(pub);
  21130. word32 privOnlySz = sizeof(privOnly);
  21131. XMEMSET(&key, 0, sizeof(ed448_key));
  21132. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21133. ExpectIntEQ(wc_ed448_init(&key), 0);
  21134. ExpectIntEQ(wc_InitRng(&rng), 0);
  21135. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21136. PRIVATE_KEY_UNLOCK();
  21137. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21138. /* Test bad args. */
  21139. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21140. BAD_FUNC_ARG);
  21141. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21142. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21143. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21144. /* Test bad args. */
  21145. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21146. BAD_FUNC_ARG);
  21147. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21148. BAD_FUNC_ARG);
  21149. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21150. BAD_FUNC_ARG);
  21151. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21152. BAD_FUNC_ARG);
  21153. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21154. BAD_FUNC_ARG);
  21155. PRIVATE_KEY_LOCK();
  21156. /* Cross check output. */
  21157. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21158. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21159. wc_ed448_free(&key);
  21160. #endif
  21161. return EXPECT_RESULT();
  21162. } /* END test_wc_ed448_exportKey */
  21163. /*
  21164. * Testing wc_Ed448PublicKeyToDer
  21165. */
  21166. static int test_wc_Ed448PublicKeyToDer(void)
  21167. {
  21168. EXPECT_DECLS;
  21169. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21170. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21171. ed448_key key;
  21172. byte derBuf[1024];
  21173. XMEMSET(&key, 0, sizeof(ed448_key));
  21174. /* Test bad args */
  21175. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21176. ExpectIntEQ(wc_ed448_init(&key), 0);
  21177. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21178. wc_ed448_free(&key);
  21179. /* Test good args */
  21180. if (EXPECT_SUCCESS()) {
  21181. WC_RNG rng;
  21182. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21183. ExpectIntEQ(wc_ed448_init(&key), 0);
  21184. ExpectIntEQ(wc_InitRng(&rng), 0);
  21185. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21186. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21187. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21188. wc_ed448_free(&key);
  21189. }
  21190. #endif
  21191. return EXPECT_RESULT();
  21192. } /* END testing wc_Ed448PublicKeyToDer */
  21193. /*
  21194. * Testing wc_curve448_init and wc_curve448_free.
  21195. */
  21196. static int test_wc_curve448_init(void)
  21197. {
  21198. EXPECT_DECLS;
  21199. #if defined(HAVE_CURVE448)
  21200. curve448_key key;
  21201. /* Test bad args for wc_curve448_init */
  21202. ExpectIntEQ(wc_curve448_init(&key), 0);
  21203. /* Test bad args for wc_curve448_init */
  21204. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21205. /* Test good args for wc_curve_448_free */
  21206. wc_curve448_free(&key);
  21207. /* Test bad args for wc_curve448_free */
  21208. wc_curve448_free(NULL);
  21209. #endif
  21210. return EXPECT_RESULT();
  21211. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21212. /*
  21213. * Testing wc_curve448_make_key
  21214. */
  21215. static int test_wc_curve448_make_key(void)
  21216. {
  21217. EXPECT_DECLS;
  21218. #if defined(HAVE_CURVE448)
  21219. curve448_key key;
  21220. WC_RNG rng;
  21221. int keysize;
  21222. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21223. ExpectIntEQ(wc_curve448_init(&key), 0);
  21224. ExpectIntEQ(wc_InitRng(&rng), 0);
  21225. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21226. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21227. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21228. /* test bad cases */
  21229. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21230. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21231. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21232. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21233. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21234. wc_curve448_free(&key);
  21235. #endif
  21236. return EXPECT_RESULT();
  21237. } /* END test_wc_curve448_make_key*/
  21238. /*
  21239. * Testing test_wc_curve448_shared_secret_ex
  21240. */
  21241. static int test_wc_curve448_shared_secret_ex(void)
  21242. {
  21243. EXPECT_DECLS;
  21244. #if defined(HAVE_CURVE448)
  21245. curve448_key private_key;
  21246. curve448_key public_key;
  21247. WC_RNG rng;
  21248. byte out[CURVE448_KEY_SIZE];
  21249. word32 outLen = sizeof(out);
  21250. int endian = EC448_BIG_ENDIAN;
  21251. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21252. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21253. ExpectIntEQ(wc_InitRng(&rng), 0);
  21254. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21255. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21256. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21257. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21258. &outLen, endian), 0);
  21259. /* test bad cases */
  21260. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21261. BAD_FUNC_ARG);
  21262. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21263. endian), BAD_FUNC_ARG);
  21264. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21265. endian), BAD_FUNC_ARG);
  21266. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21267. &outLen, endian), BAD_FUNC_ARG);
  21268. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21269. NULL, endian), BAD_FUNC_ARG);
  21270. outLen = outLen - 2;
  21271. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21272. &outLen, endian), BAD_FUNC_ARG);
  21273. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21274. wc_curve448_free(&private_key);
  21275. wc_curve448_free(&public_key);
  21276. #endif
  21277. return EXPECT_RESULT();
  21278. } /* END test_wc_curve448_shared_secret_ex*/
  21279. /*
  21280. * Testing test_wc_curve448_export_public_ex
  21281. */
  21282. static int test_wc_curve448_export_public_ex(void)
  21283. {
  21284. EXPECT_DECLS;
  21285. #if defined(HAVE_CURVE448)
  21286. WC_RNG rng;
  21287. curve448_key key;
  21288. byte out[CURVE448_KEY_SIZE];
  21289. word32 outLen = sizeof(out);
  21290. int endian = EC448_BIG_ENDIAN;
  21291. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21292. ExpectIntEQ(wc_curve448_init(&key), 0);
  21293. ExpectIntEQ(wc_InitRng(&rng), 0);
  21294. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21295. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21296. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21297. /* test bad cases*/
  21298. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21299. BAD_FUNC_ARG);
  21300. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21301. BAD_FUNC_ARG);
  21302. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21303. BAD_FUNC_ARG);
  21304. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21305. BAD_FUNC_ARG);
  21306. outLen = outLen - 2;
  21307. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21308. ECC_BAD_ARG_E);
  21309. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21310. wc_curve448_free(&key);
  21311. #endif
  21312. return EXPECT_RESULT();
  21313. } /* END test_wc_curve448_export_public_ex*/
  21314. /*
  21315. * Testing test_wc_curve448_export_private_raw_ex
  21316. */
  21317. static int test_wc_curve448_export_private_raw_ex(void)
  21318. {
  21319. EXPECT_DECLS;
  21320. #if defined(HAVE_CURVE448)
  21321. curve448_key key;
  21322. byte out[CURVE448_KEY_SIZE];
  21323. word32 outLen = sizeof(out);
  21324. int endian = EC448_BIG_ENDIAN;
  21325. ExpectIntEQ(wc_curve448_init(&key), 0);
  21326. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21327. 0);
  21328. /* test bad cases*/
  21329. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21330. BAD_FUNC_ARG);
  21331. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21332. BAD_FUNC_ARG);
  21333. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21334. BAD_FUNC_ARG);
  21335. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21336. BAD_FUNC_ARG);
  21337. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21338. EC448_LITTLE_ENDIAN), 0);
  21339. outLen = outLen - 2;
  21340. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21341. ECC_BAD_ARG_E);
  21342. wc_curve448_free(&key);
  21343. #endif
  21344. return EXPECT_RESULT();
  21345. } /* END test_wc_curve448_export_private_raw_ex*/
  21346. /*
  21347. * Testing test_wc_curve448_import_private_raw_ex
  21348. */
  21349. static int test_wc_curve448_import_private_raw_ex(void)
  21350. {
  21351. EXPECT_DECLS;
  21352. #if defined(HAVE_CURVE448)
  21353. curve448_key key;
  21354. WC_RNG rng;
  21355. byte priv[CURVE448_KEY_SIZE];
  21356. byte pub[CURVE448_KEY_SIZE];
  21357. word32 privSz = sizeof(priv);
  21358. word32 pubSz = sizeof(pub);
  21359. int endian = EC448_BIG_ENDIAN;
  21360. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21361. ExpectIntEQ(wc_curve448_init(&key), 0);
  21362. ExpectIntEQ(wc_InitRng(&rng), 0);
  21363. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21364. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21365. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21366. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21367. &key, endian), 0);
  21368. /* test bad cases */
  21369. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21370. BAD_FUNC_ARG);
  21371. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21372. &key, endian), BAD_FUNC_ARG);
  21373. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21374. &key, endian), BAD_FUNC_ARG);
  21375. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21376. NULL, endian), BAD_FUNC_ARG);
  21377. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21378. &key, endian), ECC_BAD_ARG_E);
  21379. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21380. &key, endian), ECC_BAD_ARG_E);
  21381. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21382. &key, EC448_LITTLE_ENDIAN), 0);
  21383. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21384. wc_curve448_free(&key);
  21385. #endif
  21386. return EXPECT_RESULT();
  21387. } /* END test_wc_curve448_import_private_raw_ex*/
  21388. /*
  21389. * Testing test_curve448_export_key_raw
  21390. */
  21391. static int test_wc_curve448_export_key_raw(void)
  21392. {
  21393. EXPECT_DECLS;
  21394. #if defined(HAVE_CURVE448)
  21395. curve448_key key;
  21396. WC_RNG rng;
  21397. byte priv[CURVE448_KEY_SIZE];
  21398. byte pub[CURVE448_KEY_SIZE];
  21399. word32 privSz = sizeof(priv);
  21400. word32 pubSz = sizeof(pub);
  21401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21402. ExpectIntEQ(wc_curve448_init(&key), 0);
  21403. ExpectIntEQ(wc_InitRng(&rng), 0);
  21404. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21405. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21406. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21407. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21408. 0);
  21409. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21410. wc_curve448_free(&key);
  21411. #endif
  21412. return EXPECT_RESULT();
  21413. } /* END test_wc_curve448_import_private_raw_ex*/
  21414. /*
  21415. * Testing test_wc_curve448_import_private
  21416. */
  21417. static int test_wc_curve448_import_private(void)
  21418. {
  21419. EXPECT_DECLS;
  21420. #if defined(HAVE_CURVE448)
  21421. curve448_key key;
  21422. WC_RNG rng;
  21423. byte priv[CURVE448_KEY_SIZE];
  21424. word32 privSz = sizeof(priv);
  21425. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21426. ExpectIntEQ(wc_curve448_init(&key), 0);
  21427. ExpectIntEQ(wc_InitRng(&rng), 0);
  21428. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21429. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21430. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21431. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21432. wc_curve448_free(&key);
  21433. #endif
  21434. return EXPECT_RESULT();
  21435. } /* END test_wc_curve448_import*/
  21436. /*
  21437. * Testing test_wc_curve448_size.
  21438. */
  21439. static int test_wc_curve448_size(void)
  21440. {
  21441. EXPECT_DECLS;
  21442. #if defined(HAVE_CURVE448)
  21443. curve448_key key;
  21444. ExpectIntEQ(wc_curve448_init(&key), 0);
  21445. /* Test good args for wc_curve448_size */
  21446. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21447. /* Test bad args for wc_curve448_size */
  21448. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21449. wc_curve448_free(&key);
  21450. #endif
  21451. return EXPECT_RESULT();
  21452. } /* END test_wc_curve448_size*/
  21453. /*
  21454. * Testing wc_ecc_make_key.
  21455. */
  21456. static int test_wc_ecc_make_key(void)
  21457. {
  21458. EXPECT_DECLS;
  21459. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21460. ecc_key key;
  21461. WC_RNG rng;
  21462. int ret;
  21463. XMEMSET(&key, 0, sizeof(ecc_key));
  21464. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21465. ExpectIntEQ(wc_ecc_init(&key), 0);
  21466. ExpectIntEQ(wc_InitRng(&rng), 0);
  21467. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21468. #if defined(WOLFSSL_ASYNC_CRYPT)
  21469. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21470. #endif
  21471. ExpectIntEQ(ret, 0);
  21472. /* Pass in bad args. */
  21473. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21474. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  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_make_key */
  21483. /*
  21484. * Testing wc_ecc_init()
  21485. */
  21486. static int test_wc_ecc_init(void)
  21487. {
  21488. EXPECT_DECLS;
  21489. #ifdef HAVE_ECC
  21490. ecc_key key;
  21491. XMEMSET(&key, 0, sizeof(ecc_key));
  21492. ExpectIntEQ(wc_ecc_init(&key), 0);
  21493. /* Pass in bad args. */
  21494. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21495. wc_ecc_free(&key);
  21496. #endif
  21497. return EXPECT_RESULT();
  21498. } /* END test_wc_ecc_init */
  21499. /*
  21500. * Testing wc_ecc_check_key()
  21501. */
  21502. static int test_wc_ecc_check_key(void)
  21503. {
  21504. EXPECT_DECLS;
  21505. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21506. ecc_key key;
  21507. WC_RNG rng;
  21508. int ret;
  21509. XMEMSET(&rng, 0, sizeof(rng));
  21510. XMEMSET(&key, 0, sizeof(key));
  21511. ExpectIntEQ(wc_ecc_init(&key), 0);
  21512. ExpectIntEQ(wc_InitRng(&rng), 0);
  21513. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21514. #if defined(WOLFSSL_ASYNC_CRYPT)
  21515. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21516. #endif
  21517. ExpectIntEQ(ret, 0);
  21518. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21519. /* Pass in bad args. */
  21520. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21521. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21522. wc_ecc_free(&key);
  21523. #ifdef FP_ECC
  21524. wc_ecc_fp_free();
  21525. #endif
  21526. #endif
  21527. return EXPECT_RESULT();
  21528. } /* END test_wc_ecc_check_key */
  21529. /*
  21530. * Testing wc_ecc_get_generator()
  21531. */
  21532. static int test_wc_ecc_get_generator(void)
  21533. {
  21534. EXPECT_DECLS;
  21535. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21536. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21537. ecc_point* pt = NULL;
  21538. ExpectNotNull(pt = wc_ecc_new_point());
  21539. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21540. MP_OKAY);
  21541. /* Test bad args. */
  21542. /* Returns Zero for bad arg. */
  21543. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21544. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21545. MP_OKAY);
  21546. /* If we ever get to 1000 curves increase this number */
  21547. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21548. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21549. wc_ecc_del_point(pt);
  21550. #endif
  21551. return EXPECT_RESULT();
  21552. } /* END test_wc_ecc_get_generator */
  21553. /*
  21554. * Testing wc_ecc_size()
  21555. */
  21556. static int test_wc_ecc_size(void)
  21557. {
  21558. EXPECT_DECLS;
  21559. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21560. WC_RNG rng;
  21561. ecc_key key;
  21562. int ret;
  21563. XMEMSET(&key, 0, sizeof(ecc_key));
  21564. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21565. ExpectIntEQ(wc_ecc_init(&key), 0);
  21566. ExpectIntEQ(wc_InitRng(&rng), 0);
  21567. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21568. #if defined(WOLFSSL_ASYNC_CRYPT)
  21569. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21570. #endif
  21571. ExpectIntEQ(ret, 0);
  21572. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21573. /* Test bad args. */
  21574. /* Returns Zero for bad arg. */
  21575. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21576. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21577. wc_ecc_free(&key);
  21578. #endif
  21579. return EXPECT_RESULT();
  21580. } /* END test_wc_ecc_size */
  21581. static int test_wc_ecc_params(void)
  21582. {
  21583. EXPECT_DECLS;
  21584. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21585. It was added after certifications */
  21586. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21587. const ecc_set_type* ecc_set;
  21588. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21589. /* Test for SECP256R1 curve */
  21590. int curve_id = ECC_SECP256R1;
  21591. int curve_idx;
  21592. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21593. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21594. ExpectIntEQ(ecc_set->id, curve_id);
  21595. #endif
  21596. /* Test case when SECP256R1 is not enabled */
  21597. /* Test that we get curve params for index 0 */
  21598. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21599. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21600. return EXPECT_RESULT();
  21601. }
  21602. /*
  21603. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21604. */
  21605. static int test_wc_ecc_signVerify_hash(void)
  21606. {
  21607. EXPECT_DECLS;
  21608. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21609. ecc_key key;
  21610. WC_RNG rng;
  21611. int ret;
  21612. #ifdef HAVE_ECC_VERIFY
  21613. int verify = 0;
  21614. #endif
  21615. word32 siglen = ECC_BUFSIZE;
  21616. byte sig[ECC_BUFSIZE];
  21617. byte adjustedSig[ECC_BUFSIZE+1];
  21618. byte digest[] = TEST_STRING;
  21619. word32 digestlen = (word32)TEST_STRING_SZ;
  21620. /* Init stack var */
  21621. XMEMSET(&key, 0, sizeof(ecc_key));
  21622. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21623. XMEMSET(sig, 0, siglen);
  21624. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21625. /* Init structs. */
  21626. ExpectIntEQ(wc_ecc_init(&key), 0);
  21627. ExpectIntEQ(wc_InitRng(&rng), 0);
  21628. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21629. #if defined(WOLFSSL_ASYNC_CRYPT)
  21630. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21631. #endif
  21632. ExpectIntEQ(ret, 0);
  21633. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21634. 0);
  21635. /* Check bad args. */
  21636. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21637. ECC_BAD_ARG_E);
  21638. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21639. ECC_BAD_ARG_E);
  21640. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21641. ECC_BAD_ARG_E);
  21642. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21643. ECC_BAD_ARG_E);
  21644. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21645. ECC_BAD_ARG_E);
  21646. #ifdef HAVE_ECC_VERIFY
  21647. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21648. &key), 0);
  21649. ExpectIntEQ(verify, 1);
  21650. /* test check on length of signature passed in */
  21651. XMEMCPY(adjustedSig, sig, siglen);
  21652. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21653. #ifndef NO_STRICT_ECDSA_LEN
  21654. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21655. &verify, &key), 0);
  21656. #else
  21657. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21658. * is allowed */
  21659. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21660. &verify, &key), 0);
  21661. #endif
  21662. /* Test bad args. */
  21663. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21664. &key), ECC_BAD_ARG_E);
  21665. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21666. ECC_BAD_ARG_E);
  21667. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21668. ECC_BAD_ARG_E);
  21669. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21670. NULL), ECC_BAD_ARG_E);
  21671. #endif /* HAVE_ECC_VERIFY */
  21672. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21673. wc_ecc_free(&key);
  21674. #ifdef FP_ECC
  21675. wc_ecc_fp_free();
  21676. #endif
  21677. #endif
  21678. return EXPECT_RESULT();
  21679. } /* END test_wc_ecc_sign_hash */
  21680. /*
  21681. * Testing wc_ecc_shared_secret()
  21682. */
  21683. static int test_wc_ecc_shared_secret(void)
  21684. {
  21685. EXPECT_DECLS;
  21686. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21687. ecc_key key;
  21688. ecc_key pubKey;
  21689. WC_RNG rng;
  21690. #if defined(NO_ECC256)
  21691. int ret;
  21692. #endif
  21693. byte out[KEY32];
  21694. int keySz = sizeof(out);
  21695. word32 outlen = (word32)sizeof(out);
  21696. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21697. const char* qx =
  21698. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21699. const char* qy =
  21700. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21701. const char* d =
  21702. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21703. const char* curveName = "SECP256R1";
  21704. const byte expected_shared_secret[] =
  21705. {
  21706. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21707. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21708. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21709. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21710. };
  21711. #endif
  21712. PRIVATE_KEY_UNLOCK();
  21713. /* Initialize variables. */
  21714. XMEMSET(&key, 0, sizeof(ecc_key));
  21715. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21716. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21717. XMEMSET(out, 0, keySz);
  21718. ExpectIntEQ(wc_ecc_init(&key), 0);
  21719. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21720. ExpectIntEQ(wc_InitRng(&rng), 0);
  21721. #if !defined(NO_ECC256)
  21722. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21723. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21724. #else
  21725. ret = wc_ecc_make_key(&rng, keySz, &key);
  21726. #if defined(WOLFSSL_ASYNC_CRYPT)
  21727. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21728. #endif
  21729. ExpectIntEQ(ret, 0);
  21730. ret = wc_ecc_make_key(&rng, keySz, &key);
  21731. #if defined(WOLFSSL_ASYNC_CRYPT)
  21732. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21733. #endif
  21734. ExpectIntEQ(ret, 0);
  21735. #endif
  21736. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21737. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21738. !defined(HAVE_SELFTEST)
  21739. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21740. #endif
  21741. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21742. #if !defined(NO_ECC256)
  21743. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21744. #endif
  21745. /* Test bad args. */
  21746. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21747. BAD_FUNC_ARG);
  21748. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21749. BAD_FUNC_ARG);
  21750. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21751. BAD_FUNC_ARG);
  21752. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21753. BAD_FUNC_ARG);
  21754. /* Invalid length */
  21755. outlen = 1;
  21756. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21757. BUFFER_E);
  21758. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21759. wc_ecc_free(&pubKey);
  21760. wc_ecc_free(&key);
  21761. #ifdef FP_ECC
  21762. wc_ecc_fp_free();
  21763. #endif
  21764. PRIVATE_KEY_LOCK();
  21765. #endif
  21766. return EXPECT_RESULT();
  21767. } /* END tests_wc_ecc_shared_secret */
  21768. /*
  21769. * testint wc_ecc_export_x963()
  21770. */
  21771. static int test_wc_ecc_export_x963(void)
  21772. {
  21773. EXPECT_DECLS;
  21774. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21775. ecc_key key;
  21776. WC_RNG rng;
  21777. byte out[ECC_ASN963_MAX_BUF_SZ];
  21778. word32 outlen = sizeof(out);
  21779. int ret;
  21780. PRIVATE_KEY_UNLOCK();
  21781. /* Initialize variables. */
  21782. XMEMSET(&key, 0, sizeof(ecc_key));
  21783. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21784. XMEMSET(out, 0, outlen);
  21785. ExpectIntEQ(wc_ecc_init(&key), 0);
  21786. ExpectIntEQ(wc_InitRng(&rng), 0);
  21787. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21788. #if defined(WOLFSSL_ASYNC_CRYPT)
  21789. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21790. #endif
  21791. ExpectIntEQ(ret, 0);
  21792. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21793. /* Test bad args. */
  21794. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21795. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21796. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21797. key.idx = -4;
  21798. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21799. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21800. wc_ecc_free(&key);
  21801. #ifdef FP_ECC
  21802. wc_ecc_fp_free();
  21803. #endif
  21804. PRIVATE_KEY_LOCK();
  21805. #endif
  21806. return EXPECT_RESULT();
  21807. } /* END test_wc_ecc_export_x963 */
  21808. /*
  21809. * Testing wc_ecc_export_x963_ex()
  21810. * compile with --enable-compkey will use compression.
  21811. */
  21812. static int test_wc_ecc_export_x963_ex(void)
  21813. {
  21814. EXPECT_DECLS;
  21815. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21816. ecc_key key;
  21817. WC_RNG rng;
  21818. int ret;
  21819. byte out[ECC_ASN963_MAX_BUF_SZ];
  21820. word32 outlen = sizeof(out);
  21821. #ifdef HAVE_COMP_KEY
  21822. word32 badOutLen = 5;
  21823. #endif
  21824. /* Init stack variables. */
  21825. XMEMSET(&key, 0, sizeof(ecc_key));
  21826. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21827. XMEMSET(out, 0, outlen);
  21828. PRIVATE_KEY_UNLOCK();
  21829. ExpectIntEQ(wc_ecc_init(&key), 0);
  21830. ExpectIntEQ(wc_InitRng(&rng), 0);
  21831. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21832. #if defined(WOLFSSL_ASYNC_CRYPT)
  21833. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21834. #endif
  21835. ExpectIntEQ(ret, 0);
  21836. #ifdef HAVE_COMP_KEY
  21837. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21838. #else
  21839. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21840. #endif
  21841. /* Test bad args. */
  21842. #ifdef HAVE_COMP_KEY
  21843. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21844. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21845. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21846. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21847. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21848. #else
  21849. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21850. LENGTH_ONLY_E);
  21851. #endif
  21852. key.idx = -4;
  21853. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21854. #else
  21855. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21856. ECC_BAD_ARG_E);
  21857. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21858. LENGTH_ONLY_E);
  21859. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21860. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21861. ECC_BAD_ARG_E);
  21862. key.idx = -4;
  21863. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21864. ECC_BAD_ARG_E);
  21865. #endif
  21866. PRIVATE_KEY_LOCK();
  21867. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21868. wc_ecc_free(&key);
  21869. #ifdef FP_ECC
  21870. wc_ecc_fp_free();
  21871. #endif
  21872. #endif
  21873. return EXPECT_RESULT();
  21874. } /* END test_wc_ecc_export_x963_ex */
  21875. /*
  21876. * testing wc_ecc_import_x963()
  21877. */
  21878. static int test_wc_ecc_import_x963(void)
  21879. {
  21880. EXPECT_DECLS;
  21881. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21882. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21883. ecc_key pubKey;
  21884. ecc_key key;
  21885. WC_RNG rng;
  21886. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21887. word32 x963Len = (word32)sizeof(x963);
  21888. int ret;
  21889. /* Init stack variables. */
  21890. XMEMSET(&key, 0, sizeof(ecc_key));
  21891. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21892. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21893. XMEMSET(x963, 0, x963Len);
  21894. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21895. ExpectIntEQ(wc_ecc_init(&key), 0);
  21896. ExpectIntEQ(wc_InitRng(&rng), 0);
  21897. #if FIPS_VERSION3_GE(6,0,0)
  21898. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21899. #else
  21900. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21901. #endif
  21902. #if defined(WOLFSSL_ASYNC_CRYPT)
  21903. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21904. #endif
  21905. ExpectIntEQ(ret, 0);
  21906. PRIVATE_KEY_UNLOCK();
  21907. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21908. PRIVATE_KEY_LOCK();
  21909. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21910. /* Test bad args. */
  21911. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21912. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21913. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21914. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21915. wc_ecc_free(&key);
  21916. wc_ecc_free(&pubKey);
  21917. #ifdef FP_ECC
  21918. wc_ecc_fp_free();
  21919. #endif
  21920. #endif
  21921. return EXPECT_RESULT();
  21922. } /* END wc_ecc_import_x963 */
  21923. /*
  21924. * testing wc_ecc_import_private_key()
  21925. */
  21926. static int test_wc_ecc_import_private_key(void)
  21927. {
  21928. EXPECT_DECLS;
  21929. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21930. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21931. ecc_key key;
  21932. ecc_key keyImp;
  21933. WC_RNG rng;
  21934. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21935. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21936. word32 privKeySz = (word32)sizeof(privKey);
  21937. word32 x963KeySz = (word32)sizeof(x963Key);
  21938. int ret;
  21939. /* Init stack variables. */
  21940. XMEMSET(&key, 0, sizeof(ecc_key));
  21941. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21942. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21943. XMEMSET(privKey, 0, privKeySz);
  21944. XMEMSET(x963Key, 0, x963KeySz);
  21945. PRIVATE_KEY_UNLOCK();
  21946. ExpectIntEQ(wc_ecc_init(&key), 0);
  21947. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21948. ExpectIntEQ(wc_InitRng(&rng), 0);
  21949. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21950. #if defined(WOLFSSL_ASYNC_CRYPT)
  21951. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21952. #endif
  21953. ExpectIntEQ(ret, 0);
  21954. PRIVATE_KEY_UNLOCK();
  21955. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21956. PRIVATE_KEY_LOCK();
  21957. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21958. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21959. x963KeySz, &keyImp), 0);
  21960. /* Pass in bad args. */
  21961. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21962. x963KeySz, NULL), BAD_FUNC_ARG);
  21963. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21964. &keyImp), BAD_FUNC_ARG);
  21965. PRIVATE_KEY_LOCK();
  21966. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21967. wc_ecc_free(&keyImp);
  21968. wc_ecc_free(&key);
  21969. #ifdef FP_ECC
  21970. wc_ecc_fp_free();
  21971. #endif
  21972. #endif
  21973. return EXPECT_RESULT();
  21974. } /* END test_wc_ecc_import_private_key */
  21975. /*
  21976. * Testing wc_ecc_export_private_only()
  21977. */
  21978. static int test_wc_ecc_export_private_only(void)
  21979. {
  21980. EXPECT_DECLS;
  21981. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21982. ecc_key key;
  21983. WC_RNG rng;
  21984. byte out[ECC_PRIV_KEY_BUF];
  21985. word32 outlen = sizeof(out);
  21986. int ret;
  21987. /* Init stack variables. */
  21988. XMEMSET(&key, 0, sizeof(ecc_key));
  21989. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21990. XMEMSET(out, 0, outlen);
  21991. PRIVATE_KEY_UNLOCK();
  21992. ExpectIntEQ(wc_ecc_init(&key), 0);
  21993. ExpectIntEQ(wc_InitRng(&rng), 0);
  21994. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21995. #if defined(WOLFSSL_ASYNC_CRYPT)
  21996. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21997. #endif
  21998. ExpectIntEQ(ret, 0);
  21999. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  22000. /* Pass in bad args. */
  22001. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  22002. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  22003. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  22004. PRIVATE_KEY_LOCK();
  22005. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22006. wc_ecc_free(&key);
  22007. #ifdef FP_ECC
  22008. wc_ecc_fp_free();
  22009. #endif
  22010. #endif
  22011. return EXPECT_RESULT();
  22012. } /* END test_wc_ecc_export_private_only */
  22013. /*
  22014. * Testing wc_ecc_rs_to_sig()
  22015. */
  22016. static int test_wc_ecc_rs_to_sig(void)
  22017. {
  22018. EXPECT_DECLS;
  22019. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22020. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22021. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22022. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22023. const char* zeroStr = "0";
  22024. byte sig[ECC_MAX_SIG_SIZE];
  22025. word32 siglen = (word32)sizeof(sig);
  22026. /* R and S max size is the order of curve. 2^192.*/
  22027. int keySz = KEY24;
  22028. byte r[KEY24];
  22029. byte s[KEY24];
  22030. word32 rlen = (word32)sizeof(r);
  22031. word32 slen = (word32)sizeof(s);
  22032. /* Init stack variables. */
  22033. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22034. XMEMSET(r, 0, keySz);
  22035. XMEMSET(s, 0, keySz);
  22036. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  22037. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  22038. /* Test bad args. */
  22039. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  22040. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  22041. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  22042. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  22043. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  22044. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  22045. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  22046. ECC_BAD_ARG_E);
  22047. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  22048. ECC_BAD_ARG_E);
  22049. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  22050. ECC_BAD_ARG_E);
  22051. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  22052. ECC_BAD_ARG_E);
  22053. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  22054. ECC_BAD_ARG_E);
  22055. #endif
  22056. return EXPECT_RESULT();
  22057. } /* END test_wc_ecc_rs_to_sig */
  22058. static int test_wc_ecc_import_raw(void)
  22059. {
  22060. EXPECT_DECLS;
  22061. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22062. ecc_key key;
  22063. const char* qx =
  22064. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22065. const char* qy =
  22066. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22067. const char* d =
  22068. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22069. const char* curveName = "SECP256R1";
  22070. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22071. const char* kNullStr = "";
  22072. int ret;
  22073. #endif
  22074. XMEMSET(&key, 0, sizeof(ecc_key));
  22075. ExpectIntEQ(wc_ecc_init(&key), 0);
  22076. /* Test good import */
  22077. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  22078. /* Test bad args. */
  22079. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  22080. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  22081. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  22082. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  22083. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22084. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22085. wc_ecc_free(&key);
  22086. #endif
  22087. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  22088. curveName), 0);
  22089. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22090. #endif
  22091. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22092. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22093. wc_ecc_free(&key);
  22094. #endif
  22095. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22096. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22097. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22098. #else
  22099. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22100. #endif
  22101. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22102. wc_ecc_free(&key);
  22103. #endif
  22104. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22105. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22106. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22107. #else
  22108. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22109. #endif
  22110. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22111. wc_ecc_free(&key);
  22112. #endif
  22113. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22114. #endif
  22115. wc_ecc_free(&key);
  22116. #endif
  22117. return EXPECT_RESULT();
  22118. } /* END test_wc_ecc_import_raw */
  22119. static int test_wc_ecc_import_unsigned(void)
  22120. {
  22121. EXPECT_DECLS;
  22122. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22123. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22124. HAVE_FIPS_VERSION >= 2))
  22125. ecc_key key;
  22126. const byte qx[] = {
  22127. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22128. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22129. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22130. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22131. };
  22132. const byte qy[] = {
  22133. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22134. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22135. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22136. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22137. };
  22138. const byte d[] = {
  22139. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22140. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22141. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22142. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22143. };
  22144. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22145. const byte nullBytes[32] = {0};
  22146. int ret;
  22147. #endif
  22148. int curveId = ECC_SECP256R1;
  22149. XMEMSET(&key, 0, sizeof(ecc_key));
  22150. ExpectIntEQ(wc_ecc_init(&key), 0);
  22151. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22152. curveId), 0);
  22153. /* Test bad args. */
  22154. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22155. curveId), BAD_FUNC_ARG);
  22156. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22157. curveId), BAD_FUNC_ARG);
  22158. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22159. curveId), BAD_FUNC_ARG);
  22160. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22161. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22162. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22163. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22164. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22165. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22166. #endif
  22167. wc_ecc_free(&key);
  22168. #endif
  22169. return EXPECT_RESULT();
  22170. } /* END test_wc_ecc_import_unsigned */
  22171. /*
  22172. * Testing wc_ecc_sig_size()
  22173. */
  22174. static int test_wc_ecc_sig_size(void)
  22175. {
  22176. EXPECT_DECLS;
  22177. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22178. ecc_key key;
  22179. WC_RNG rng;
  22180. int keySz = KEY16;
  22181. int ret;
  22182. XMEMSET(&rng, 0, sizeof(rng));
  22183. XMEMSET(&key, 0, sizeof(key));
  22184. ExpectIntEQ(wc_ecc_init(&key), 0);
  22185. ExpectIntEQ(wc_InitRng(&rng), 0);
  22186. ret = wc_ecc_make_key(&rng, keySz, &key);
  22187. #if defined(WOLFSSL_ASYNC_CRYPT)
  22188. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22189. #endif
  22190. ExpectIntEQ(ret, 0);
  22191. ExpectIntLE(wc_ecc_sig_size(&key),
  22192. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22193. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22194. wc_ecc_free(&key);
  22195. #endif
  22196. return EXPECT_RESULT();
  22197. } /* END test_wc_ecc_sig_size */
  22198. /*
  22199. * Testing wc_ecc_ctx_new()
  22200. */
  22201. static int test_wc_ecc_ctx_new(void)
  22202. {
  22203. EXPECT_DECLS;
  22204. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22205. WC_RNG rng;
  22206. ecEncCtx* cli = NULL;
  22207. ecEncCtx* srv = NULL;
  22208. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22209. ExpectIntEQ(wc_InitRng(&rng), 0);
  22210. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22211. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22212. wc_ecc_ctx_free(cli);
  22213. cli = NULL;
  22214. wc_ecc_ctx_free(srv);
  22215. /* Test bad args. */
  22216. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22217. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22218. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22219. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22220. wc_ecc_ctx_free(cli);
  22221. #endif
  22222. return EXPECT_RESULT();
  22223. } /* END test_wc_ecc_ctx_new */
  22224. /*
  22225. * Tesing wc_ecc_reset()
  22226. */
  22227. static int test_wc_ecc_ctx_reset(void)
  22228. {
  22229. EXPECT_DECLS;
  22230. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22231. ecEncCtx* ctx = NULL;
  22232. WC_RNG rng;
  22233. XMEMSET(&rng, 0, sizeof(rng));
  22234. ExpectIntEQ(wc_InitRng(&rng), 0);
  22235. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22236. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22237. /* Pass in bad args. */
  22238. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22239. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22240. wc_ecc_ctx_free(ctx);
  22241. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22242. #endif
  22243. return EXPECT_RESULT();
  22244. } /* END test_wc_ecc_ctx_reset */
  22245. /*
  22246. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22247. */
  22248. static int test_wc_ecc_ctx_set_peer_salt(void)
  22249. {
  22250. EXPECT_DECLS;
  22251. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22252. WC_RNG rng;
  22253. ecEncCtx* cliCtx = NULL;
  22254. ecEncCtx* servCtx = NULL;
  22255. const byte* cliSalt = NULL;
  22256. const byte* servSalt = NULL;
  22257. XMEMSET(&rng, 0, sizeof(rng));
  22258. ExpectIntEQ(wc_InitRng(&rng), 0);
  22259. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22260. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22261. /* Test bad args. */
  22262. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22263. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22264. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22265. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22266. /* Test bad args. */
  22267. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22268. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22269. wc_ecc_ctx_free(cliCtx);
  22270. wc_ecc_ctx_free(servCtx);
  22271. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22272. #endif
  22273. return EXPECT_RESULT();
  22274. } /* END test_wc_ecc_ctx_set_peer_salt */
  22275. /*
  22276. * Testing wc_ecc_ctx_set_info()
  22277. */
  22278. static int test_wc_ecc_ctx_set_info(void)
  22279. {
  22280. EXPECT_DECLS;
  22281. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22282. ecEncCtx* ctx = NULL;
  22283. WC_RNG rng;
  22284. const char* optInfo = "Optional Test Info.";
  22285. int optInfoSz = (int)XSTRLEN(optInfo);
  22286. const char* badOptInfo = NULL;
  22287. XMEMSET(&rng, 0, sizeof(rng));
  22288. ExpectIntEQ(wc_InitRng(&rng), 0);
  22289. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22290. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22291. /* Test bad args. */
  22292. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22293. BAD_FUNC_ARG);
  22294. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22295. BAD_FUNC_ARG);
  22296. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22297. wc_ecc_ctx_free(ctx);
  22298. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22299. #endif
  22300. return EXPECT_RESULT();
  22301. } /* END test_wc_ecc_ctx_set_info */
  22302. /*
  22303. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22304. */
  22305. static int test_wc_ecc_encryptDecrypt(void)
  22306. {
  22307. EXPECT_DECLS;
  22308. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22309. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22310. ecc_key srvKey;
  22311. ecc_key cliKey;
  22312. ecc_key tmpKey;
  22313. WC_RNG rng;
  22314. int ret;
  22315. const char* msg = "EccBlock Size 16";
  22316. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22317. #ifdef WOLFSSL_ECIES_OLD
  22318. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22319. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22320. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22321. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22322. #else
  22323. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22324. WC_SHA256_DIGEST_SIZE];
  22325. #endif
  22326. word32 outSz = (word32)sizeof(out);
  22327. byte plain[sizeof("EccBlock Size 16")];
  22328. word32 plainSz = (word32)sizeof(plain);
  22329. int keySz = KEY20;
  22330. /* Init stack variables. */
  22331. XMEMSET(out, 0, outSz);
  22332. XMEMSET(plain, 0, plainSz);
  22333. XMEMSET(&rng, 0, sizeof(rng));
  22334. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22335. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22336. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22337. ExpectIntEQ(wc_InitRng(&rng), 0);
  22338. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22339. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22340. #if defined(WOLFSSL_ASYNC_CRYPT)
  22341. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22342. #endif
  22343. ExpectIntEQ(ret, 0);
  22344. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22345. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22346. #if defined(WOLFSSL_ASYNC_CRYPT)
  22347. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22348. #endif
  22349. ExpectIntEQ(ret, 0);
  22350. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22351. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22352. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22353. !defined(HAVE_SELFTEST)
  22354. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22355. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22356. #endif
  22357. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22358. &outSz, NULL), 0);
  22359. /* Test bad args. */
  22360. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22361. NULL), BAD_FUNC_ARG);
  22362. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22363. NULL), BAD_FUNC_ARG);
  22364. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22365. NULL), BAD_FUNC_ARG);
  22366. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22367. &outSz, NULL), BAD_FUNC_ARG);
  22368. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22369. NULL), BAD_FUNC_ARG);
  22370. #ifdef WOLFSSL_ECIES_OLD
  22371. tmpKey.dp = cliKey.dp;
  22372. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22373. #endif
  22374. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22375. NULL), 0);
  22376. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22377. NULL), BAD_FUNC_ARG);
  22378. #ifdef WOLFSSL_ECIES_OLD
  22379. /* NULL parameter allowed in new implementations - public key comes from
  22380. * the message. */
  22381. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22382. NULL), BAD_FUNC_ARG);
  22383. #endif
  22384. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22385. NULL), BAD_FUNC_ARG);
  22386. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22387. NULL), BAD_FUNC_ARG);
  22388. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22389. BAD_FUNC_ARG);
  22390. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22391. wc_ecc_free(&tmpKey);
  22392. wc_ecc_free(&srvKey);
  22393. wc_ecc_free(&cliKey);
  22394. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22395. #endif
  22396. return EXPECT_RESULT();
  22397. } /* END test_wc_ecc_encryptDecrypt */
  22398. /*
  22399. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22400. */
  22401. static int test_wc_ecc_del_point(void)
  22402. {
  22403. EXPECT_DECLS;
  22404. #if defined(HAVE_ECC)
  22405. ecc_point* pt = NULL;
  22406. ExpectNotNull(pt = wc_ecc_new_point());
  22407. wc_ecc_del_point(pt);
  22408. #endif
  22409. return EXPECT_RESULT();
  22410. } /* END test_wc_ecc_del_point */
  22411. /*
  22412. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22413. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22414. * and wc_ecc_cmp_point()
  22415. */
  22416. static int test_wc_ecc_pointFns(void)
  22417. {
  22418. EXPECT_DECLS;
  22419. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22420. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22421. !defined(WOLFSSL_ATECC608A)
  22422. ecc_key key;
  22423. WC_RNG rng;
  22424. int ret;
  22425. ecc_point* point = NULL;
  22426. ecc_point* cpypt = NULL;
  22427. int idx = 0;
  22428. int keySz = KEY32;
  22429. byte der[DER_SZ(KEY32)];
  22430. word32 derlenChk = 0;
  22431. word32 derSz = DER_SZ(KEY32);
  22432. /* Init stack variables. */
  22433. XMEMSET(der, 0, derSz);
  22434. XMEMSET(&key, 0, sizeof(ecc_key));
  22435. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22436. ExpectIntEQ(wc_InitRng(&rng), 0);
  22437. ExpectIntEQ(wc_ecc_init(&key), 0);
  22438. ret = wc_ecc_make_key(&rng, keySz, &key);
  22439. #if defined(WOLFSSL_ASYNC_CRYPT)
  22440. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22441. #endif
  22442. ExpectIntEQ(ret, 0);
  22443. ExpectNotNull(point = wc_ecc_new_point());
  22444. ExpectNotNull(cpypt = wc_ecc_new_point());
  22445. /* Export */
  22446. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22447. &derlenChk), LENGTH_ONLY_E);
  22448. /* Check length value. */
  22449. ExpectIntEQ(derSz, derlenChk);
  22450. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22451. &derSz), 0);
  22452. /* Test bad args. */
  22453. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22454. ECC_BAD_ARG_E);
  22455. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22456. ECC_BAD_ARG_E);
  22457. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22458. NULL), ECC_BAD_ARG_E);
  22459. /* Import */
  22460. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22461. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22462. /* Test bad args. */
  22463. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22464. ECC_BAD_ARG_E);
  22465. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22466. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22467. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22468. ECC_BAD_ARG_E);
  22469. /* Copy */
  22470. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22471. /* Test bad args. */
  22472. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22473. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22474. /* Compare point */
  22475. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22476. /* Test bad args. */
  22477. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22478. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22479. /* At infinity if return == 1, otherwise return == 0. */
  22480. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22481. /* Test bad args. */
  22482. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22483. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22484. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22485. #ifdef USE_ECC_B_PARAM
  22486. /* On curve if ret == 0 */
  22487. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22488. /* Test bad args. */
  22489. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22490. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22491. #endif /* USE_ECC_B_PARAM */
  22492. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22493. /* Free */
  22494. wc_ecc_del_point(point);
  22495. wc_ecc_del_point(cpypt);
  22496. wc_ecc_free(&key);
  22497. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22498. #endif
  22499. return EXPECT_RESULT();
  22500. } /* END test_wc_ecc_pointFns */
  22501. /*
  22502. * Testing wc_ecc_shared_secret_ssh()
  22503. */
  22504. static int test_wc_ecc_shared_secret_ssh(void)
  22505. {
  22506. EXPECT_DECLS;
  22507. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22508. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22509. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22510. !defined(WOLFSSL_CRYPTOCELL)
  22511. ecc_key key;
  22512. ecc_key key2;
  22513. WC_RNG rng;
  22514. int ret;
  22515. int keySz = KEY32;
  22516. #if FIPS_VERSION3_GE(6,0,0)
  22517. int key2Sz = KEY28;
  22518. #else
  22519. int key2Sz = KEY24;
  22520. #endif
  22521. byte secret[KEY32];
  22522. word32 secretLen = (word32)keySz;
  22523. /* Init stack variables. */
  22524. XMEMSET(&key, 0, sizeof(ecc_key));
  22525. XMEMSET(&key2, 0, sizeof(ecc_key));
  22526. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22527. XMEMSET(secret, 0, secretLen);
  22528. PRIVATE_KEY_UNLOCK();
  22529. /* Make keys */
  22530. ExpectIntEQ(wc_ecc_init(&key), 0);
  22531. ExpectIntEQ(wc_InitRng(&rng), 0);
  22532. ret = wc_ecc_make_key(&rng, keySz, &key);
  22533. #if defined(WOLFSSL_ASYNC_CRYPT)
  22534. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22535. #endif
  22536. ExpectIntEQ(ret, 0);
  22537. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22538. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22539. ExpectIntEQ(wc_InitRng(&rng), 0);
  22540. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22541. #if defined(WOLFSSL_ASYNC_CRYPT)
  22542. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22543. #endif
  22544. ExpectIntEQ(ret, 0);
  22545. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22546. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22547. !defined(HAVE_SELFTEST)
  22548. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22549. #endif
  22550. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22551. &secretLen), 0);
  22552. /* Pass in bad args. */
  22553. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22554. &secretLen), BAD_FUNC_ARG);
  22555. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22556. BAD_FUNC_ARG);
  22557. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22558. BAD_FUNC_ARG);
  22559. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22560. BAD_FUNC_ARG);
  22561. key.type = ECC_PUBLICKEY;
  22562. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22563. &secretLen), ECC_BAD_ARG_E);
  22564. PRIVATE_KEY_LOCK();
  22565. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22566. wc_ecc_free(&key);
  22567. wc_ecc_free(&key2);
  22568. #ifdef FP_ECC
  22569. wc_ecc_fp_free();
  22570. #endif
  22571. #endif
  22572. return EXPECT_RESULT();
  22573. } /* END test_wc_ecc_shared_secret_ssh */
  22574. /*
  22575. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22576. */
  22577. static int test_wc_ecc_verify_hash_ex(void)
  22578. {
  22579. EXPECT_DECLS;
  22580. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22581. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22582. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22583. ecc_key key;
  22584. WC_RNG rng;
  22585. int ret;
  22586. mp_int r;
  22587. mp_int s;
  22588. mp_int z;
  22589. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22590. unsigned char iHash[] = "Everyone gets Friday off.......";
  22591. unsigned char shortHash[] = TEST_STRING;
  22592. word32 hashlen = sizeof(hash);
  22593. word32 iHashLen = sizeof(iHash);
  22594. word32 shortHashLen = sizeof(shortHash);
  22595. int keySz = KEY32;
  22596. int verify_ok = 0;
  22597. XMEMSET(&key, 0, sizeof(ecc_key));
  22598. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22599. XMEMSET(&r, 0, sizeof(mp_int));
  22600. XMEMSET(&s, 0, sizeof(mp_int));
  22601. XMEMSET(&z, 0, sizeof(mp_int));
  22602. /* Initialize r, s and z. */
  22603. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22604. ExpectIntEQ(wc_ecc_init(&key), 0);
  22605. ExpectIntEQ(wc_InitRng(&rng), 0);
  22606. ret = wc_ecc_make_key(&rng, keySz, &key);
  22607. #if defined(WOLFSSL_ASYNC_CRYPT)
  22608. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22609. #endif
  22610. ExpectIntEQ(ret, 0);
  22611. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22612. /* verify_ok should be 1. */
  22613. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22614. 0);
  22615. ExpectIntEQ(verify_ok, 1);
  22616. /* verify_ok should be 0 */
  22617. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22618. &key), 0);
  22619. ExpectIntEQ(verify_ok, 0);
  22620. /* verify_ok should be 0. */
  22621. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22622. &verify_ok, &key), 0);
  22623. ExpectIntEQ(verify_ok, 0);
  22624. /* Test bad args. */
  22625. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22626. ECC_BAD_ARG_E);
  22627. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22628. ECC_BAD_ARG_E);
  22629. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22630. ECC_BAD_ARG_E);
  22631. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22632. ECC_BAD_ARG_E);
  22633. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22634. ECC_BAD_ARG_E);
  22635. /* Test bad args. */
  22636. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22637. &verify_ok, &key), ECC_BAD_ARG_E);
  22638. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22639. &verify_ok, &key), ECC_BAD_ARG_E);
  22640. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22641. &verify_ok, &key), MP_ZERO_E);
  22642. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22643. &verify_ok, &key), MP_ZERO_E);
  22644. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22645. &verify_ok, &key), MP_ZERO_E);
  22646. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22647. &key), ECC_BAD_ARG_E);
  22648. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22649. &key), ECC_BAD_ARG_E);
  22650. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22651. &verify_ok, NULL), ECC_BAD_ARG_E);
  22652. wc_ecc_free(&key);
  22653. mp_free(&r);
  22654. mp_free(&s);
  22655. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22656. #endif
  22657. return EXPECT_RESULT();
  22658. } /* END test_wc_ecc_verify_hash_ex */
  22659. /*
  22660. * Testing wc_ecc_mulmod()
  22661. */
  22662. static int test_wc_ecc_mulmod(void)
  22663. {
  22664. EXPECT_DECLS;
  22665. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22666. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22667. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22668. ecc_key key1;
  22669. ecc_key key2;
  22670. ecc_key key3;
  22671. WC_RNG rng;
  22672. int ret;
  22673. XMEMSET(&key1, 0, sizeof(ecc_key));
  22674. XMEMSET(&key2, 0, sizeof(ecc_key));
  22675. XMEMSET(&key3, 0, sizeof(ecc_key));
  22676. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22677. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22678. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22679. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22680. ExpectIntEQ(wc_InitRng(&rng), 0);
  22681. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22682. #if defined(WOLFSSL_ASYNC_CRYPT)
  22683. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22684. #endif
  22685. ExpectIntEQ(ret, 0);
  22686. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22687. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22688. key1.dp->Af, ECC_SECP256R1), 0);
  22689. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22690. key1.dp->prime, ECC_SECP256R1), 0);
  22691. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22692. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22693. 1), 0);
  22694. /* Test bad args. */
  22695. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22696. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22697. ECC_BAD_ARG_E);
  22698. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22699. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22700. ECC_BAD_ARG_E);
  22701. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22702. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22703. ECC_BAD_ARG_E);
  22704. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22705. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22706. wc_ecc_free(&key1);
  22707. wc_ecc_free(&key2);
  22708. wc_ecc_free(&key3);
  22709. #ifdef FP_ECC
  22710. wc_ecc_fp_free();
  22711. #endif
  22712. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22713. return EXPECT_RESULT();
  22714. } /* END test_wc_ecc_mulmod */
  22715. /*
  22716. * Testing wc_ecc_is_valid_idx()
  22717. */
  22718. static int test_wc_ecc_is_valid_idx(void)
  22719. {
  22720. EXPECT_DECLS;
  22721. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22722. ecc_key key;
  22723. WC_RNG rng;
  22724. int ret;
  22725. int iVal = -2;
  22726. int iVal2 = 3000;
  22727. XMEMSET(&key, 0, sizeof(ecc_key));
  22728. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22729. ExpectIntEQ(wc_ecc_init(&key), 0);
  22730. ExpectIntEQ(wc_InitRng(&rng), 0);
  22731. ret = wc_ecc_make_key(&rng, 32, &key);
  22732. #if defined(WOLFSSL_ASYNC_CRYPT)
  22733. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22734. #endif
  22735. ExpectIntEQ(ret, 0);
  22736. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22737. /* Test bad args. */
  22738. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22739. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22740. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22741. wc_ecc_free(&key);
  22742. #ifdef FP_ECC
  22743. wc_ecc_fp_free();
  22744. #endif
  22745. #endif
  22746. return EXPECT_RESULT();
  22747. } /* END test_wc_ecc_is_valid_idx */
  22748. /*
  22749. * Testing wc_ecc_get_curve_id_from_oid()
  22750. */
  22751. static int test_wc_ecc_get_curve_id_from_oid(void)
  22752. {
  22753. EXPECT_DECLS;
  22754. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22755. !defined(HAVE_FIPS)
  22756. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22757. word32 len = sizeof(oid);
  22758. /* Bad Cases */
  22759. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22760. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22761. /* Good Case */
  22762. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22763. #endif
  22764. return EXPECT_RESULT();
  22765. } /* END test_wc_ecc_get_curve_id_from_oid */
  22766. /*
  22767. * Testing wc_ecc_sig_size_calc()
  22768. */
  22769. static int test_wc_ecc_sig_size_calc(void)
  22770. {
  22771. EXPECT_DECLS;
  22772. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22773. ecc_key key;
  22774. WC_RNG rng;
  22775. int sz = 0;
  22776. int ret;
  22777. XMEMSET(&key, 0, sizeof(ecc_key));
  22778. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22779. ExpectIntEQ(wc_ecc_init(&key), 0);
  22780. ExpectIntEQ(wc_InitRng(&rng), 0);
  22781. ret = wc_ecc_make_key(&rng, 16, &key);
  22782. #if defined(WOLFSSL_ASYNC_CRYPT)
  22783. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22784. #endif
  22785. #if FIPS_VERSION3_GE(6,0,0)
  22786. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22787. #else
  22788. ExpectIntEQ(ret, 0);
  22789. #endif
  22790. #if FIPS_VERSION3_LT(6,0,0)
  22791. sz = key.dp->size;
  22792. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22793. #else
  22794. (void) sz;
  22795. #endif
  22796. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22797. wc_ecc_free(&key);
  22798. #endif
  22799. return EXPECT_RESULT();
  22800. } /* END test_wc_ecc_sig_size_calc */
  22801. /*
  22802. * Testing wc_ecc_sm2_make_key()
  22803. */
  22804. static int test_wc_ecc_sm2_make_key(void)
  22805. {
  22806. int res = TEST_SKIPPED;
  22807. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22808. EXPECT_DECLS;
  22809. WC_RNG rng[1];
  22810. ecc_key key[1];
  22811. XMEMSET(rng, 0, sizeof(*rng));
  22812. XMEMSET(key, 0, sizeof(*key));
  22813. ExpectIntEQ(wc_InitRng(rng), 0);
  22814. ExpectIntEQ(wc_ecc_init(key), 0);
  22815. /* Test invalid parameters. */
  22816. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22817. BAD_FUNC_ARG);
  22818. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22819. BAD_FUNC_ARG);
  22820. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22821. BAD_FUNC_ARG);
  22822. /* Test valid parameters. */
  22823. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22824. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22825. wc_ecc_free(key);
  22826. wc_FreeRng(rng);
  22827. #ifdef FP_ECC
  22828. wc_ecc_fp_free();
  22829. #endif
  22830. res = EXPECT_RESULT();
  22831. #endif
  22832. return res;
  22833. }
  22834. /*
  22835. * Testing wc_ecc_sm2_shared_secret()
  22836. */
  22837. static int test_wc_ecc_sm2_shared_secret(void)
  22838. {
  22839. int res = TEST_SKIPPED;
  22840. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22841. EXPECT_DECLS;
  22842. WC_RNG rng[1];
  22843. ecc_key keyA[1];
  22844. ecc_key keyB[1];
  22845. byte outA[32];
  22846. byte outB[32];
  22847. word32 outALen = 32;
  22848. word32 outBLen = 32;
  22849. XMEMSET(rng, 0, sizeof(*rng));
  22850. XMEMSET(keyA, 0, sizeof(*keyA));
  22851. XMEMSET(keyB, 0, sizeof(*keyB));
  22852. ExpectIntEQ(wc_InitRng(rng), 0);
  22853. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22854. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22855. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22856. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22857. #ifdef ECC_TIMING_RESISTANT
  22858. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22859. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22860. #endif
  22861. /* Test invalid parameters. */
  22862. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22863. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22864. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22865. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22866. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22867. BAD_FUNC_ARG);
  22868. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22869. BAD_FUNC_ARG);
  22870. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22871. BAD_FUNC_ARG);
  22872. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22873. BAD_FUNC_ARG);
  22874. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22875. /* Test valid parameters. */
  22876. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22877. ExpectIntLE(outALen, 32);
  22878. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22879. ExpectIntLE(outBLen, 32);
  22880. ExpectIntEQ(outALen, outBLen);
  22881. ExpectBufEQ(outA, outB, outALen);
  22882. wc_ecc_free(keyB);
  22883. wc_ecc_free(keyA);
  22884. wc_FreeRng(rng);
  22885. #ifdef FP_ECC
  22886. wc_ecc_fp_free();
  22887. #endif
  22888. res = EXPECT_RESULT();
  22889. #endif
  22890. return res;
  22891. }
  22892. /*
  22893. * Testing wc_ecc_sm2_create_digest()
  22894. */
  22895. static int test_wc_ecc_sm2_create_digest(void)
  22896. {
  22897. int res = TEST_SKIPPED;
  22898. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22899. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22900. EXPECT_DECLS;
  22901. ecc_key key[1];
  22902. enum wc_HashType hashType;
  22903. unsigned char pub[] = {
  22904. 0x04,
  22905. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22906. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22907. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22908. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22909. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22910. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22911. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22912. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22913. };
  22914. unsigned char id[] = {
  22915. 0x01, 0x02, 0x03,
  22916. };
  22917. unsigned char msg[] = {
  22918. 0x01, 0x02, 0x03,
  22919. };
  22920. unsigned char hash[32];
  22921. #ifdef WOLFSSL_SM3
  22922. unsigned char expHash[32] = {
  22923. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22924. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22925. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22926. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22927. };
  22928. #else
  22929. unsigned char expHash[32] = {
  22930. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22931. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22932. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22933. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22934. };
  22935. #endif
  22936. #ifdef WOLFSSL_SM3
  22937. hashType = WC_HASH_TYPE_SM3;
  22938. #else
  22939. hashType = WC_HASH_TYPE_SHA256;
  22940. #endif
  22941. XMEMSET(key, 0, sizeof(*key));
  22942. ExpectIntEQ(wc_ecc_init(key), 0);
  22943. /* Test with no curve set. */
  22944. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22945. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22946. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22947. /* Test invalid parameters. */
  22948. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22949. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22950. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22951. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22952. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22953. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22954. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22955. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22956. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22957. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22958. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22959. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22960. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22961. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22962. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22963. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22964. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22965. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22966. /* Bad hash type. */
  22967. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22968. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22969. -1, hash, 0, key), BAD_FUNC_ARG);
  22970. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22971. /* Bad hash size. */
  22972. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22973. hashType, hash, 0, key), BUFFER_E);
  22974. /* Test valid parameters. */
  22975. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22976. hashType, hash, sizeof(hash), key), 0);
  22977. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22978. wc_ecc_free(key);
  22979. res = EXPECT_RESULT();
  22980. #endif
  22981. return res;
  22982. }
  22983. /*
  22984. * Testing wc_ecc_sm2_verify_hash_ex()
  22985. */
  22986. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22987. {
  22988. int res = TEST_SKIPPED;
  22989. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22990. defined(WOLFSSL_PUBLIC_MP)
  22991. EXPECT_DECLS;
  22992. ecc_key key[1];
  22993. mp_int r[1];
  22994. mp_int s[1];
  22995. int verified;
  22996. unsigned char pub[] = {
  22997. 0x04,
  22998. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22999. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23000. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23001. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23002. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23003. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23004. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23005. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23006. };
  23007. unsigned char hash[] = {
  23008. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23009. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23010. 0x50, 0x69, 0x5B, 0x20
  23011. };
  23012. unsigned char rData[] = {
  23013. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23014. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23015. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23016. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  23017. };
  23018. unsigned char sData[] = {
  23019. 0x1D,
  23020. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23021. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23022. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23023. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23024. };
  23025. unsigned char rBadData[] = {
  23026. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23027. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23028. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23029. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  23030. };
  23031. XMEMSET(key, 0, sizeof(*key));
  23032. XMEMSET(r, 0, sizeof(*r));
  23033. XMEMSET(s, 0, sizeof(*s));
  23034. ExpectIntEQ(mp_init(r), 0);
  23035. ExpectIntEQ(mp_init(s), 0);
  23036. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  23037. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  23038. ExpectIntEQ(wc_ecc_init(key), 0);
  23039. /* Test with no curve set. */
  23040. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23041. &verified, key), BAD_FUNC_ARG);
  23042. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23043. /* Test invalid parameters. */
  23044. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23045. NULL, NULL), BAD_FUNC_ARG);
  23046. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  23047. NULL, NULL), BAD_FUNC_ARG);
  23048. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  23049. NULL, NULL), BAD_FUNC_ARG);
  23050. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  23051. NULL, NULL), BAD_FUNC_ARG);
  23052. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23053. &verified, NULL), BAD_FUNC_ARG);
  23054. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23055. NULL, key), BAD_FUNC_ARG);
  23056. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  23057. &verified, key), BAD_FUNC_ARG);
  23058. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  23059. &verified, key), BAD_FUNC_ARG);
  23060. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  23061. &verified, key), BAD_FUNC_ARG);
  23062. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23063. NULL, key), BAD_FUNC_ARG);
  23064. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23065. &verified, NULL), BAD_FUNC_ARG);
  23066. /* Make key not on the SM2 curve. */
  23067. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23068. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23069. &verified, key), BAD_FUNC_ARG);
  23070. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23071. /* Test valid parameters. */
  23072. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23073. &verified, key), 0);
  23074. ExpectIntEQ(verified, 1);
  23075. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  23076. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23077. &verified, key), 0);
  23078. ExpectIntEQ(verified, 0);
  23079. mp_free(s);
  23080. mp_free(r);
  23081. wc_ecc_free(key);
  23082. #ifdef FP_ECC
  23083. wc_ecc_fp_free();
  23084. #endif
  23085. res = EXPECT_RESULT();
  23086. #endif
  23087. return res;
  23088. }
  23089. /*
  23090. * Testing wc_ecc_sm2_verify_hash()
  23091. */
  23092. static int test_wc_ecc_sm2_verify_hash(void)
  23093. {
  23094. int res = TEST_SKIPPED;
  23095. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  23096. EXPECT_DECLS;
  23097. ecc_key key[1];
  23098. int verified;
  23099. unsigned char pub[] = {
  23100. 0x04,
  23101. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23102. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23103. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23104. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23105. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23106. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23107. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23108. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23109. };
  23110. unsigned char hash[] = {
  23111. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23112. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23113. 0x50, 0x69, 0x5B, 0x20
  23114. };
  23115. unsigned char sig[] = {
  23116. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23117. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23118. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23119. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23120. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23121. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23122. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23123. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23124. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23125. };
  23126. unsigned char sigBad[] = {
  23127. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23128. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23129. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23130. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23131. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23132. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23133. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23134. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23135. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23136. };
  23137. XMEMSET(key, 0, sizeof(*key));
  23138. ExpectIntEQ(wc_ecc_init(key), 0);
  23139. /* Test with no curve set. */
  23140. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23141. &verified, key), BAD_FUNC_ARG);
  23142. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23143. /* Test invalid parameters. */
  23144. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23145. NULL, NULL), BAD_FUNC_ARG);
  23146. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23147. NULL, NULL), BAD_FUNC_ARG);
  23148. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23149. NULL, NULL), BAD_FUNC_ARG);
  23150. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23151. &verified, NULL), BAD_FUNC_ARG);
  23152. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23153. NULL, key), BAD_FUNC_ARG);
  23154. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23155. &verified, key), BAD_FUNC_ARG);
  23156. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23157. &verified, key), BAD_FUNC_ARG);
  23158. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23159. NULL, key), BAD_FUNC_ARG);
  23160. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23161. &verified, NULL), BAD_FUNC_ARG);
  23162. /* Make key not on the SM2 curve. */
  23163. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23164. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23165. &verified, key), BAD_FUNC_ARG);
  23166. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23167. /* Test valid parameters. */
  23168. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23169. &verified, key), 0);
  23170. ExpectIntEQ(verified, 1);
  23171. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23172. sizeof(hash), &verified, key), 0);
  23173. ExpectIntEQ(verified, 0);
  23174. wc_ecc_free(key);
  23175. #ifdef FP_ECC
  23176. wc_ecc_fp_free();
  23177. #endif
  23178. res = EXPECT_RESULT();
  23179. #endif
  23180. return res;
  23181. }
  23182. /*
  23183. * Testing wc_ecc_sm2_verify_hash_ex()
  23184. */
  23185. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23186. {
  23187. int res = TEST_SKIPPED;
  23188. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23189. defined(WOLFSSL_PUBLIC_MP)
  23190. EXPECT_DECLS;
  23191. WC_RNG rng[1];
  23192. ecc_key key[1];
  23193. mp_int r[1];
  23194. mp_int s[1];
  23195. unsigned char hash[32];
  23196. #ifdef HAVE_ECC_VERIFY
  23197. int verified;
  23198. #endif
  23199. XMEMSET(rng, 0, sizeof(*rng));
  23200. XMEMSET(key, 0, sizeof(*key));
  23201. XMEMSET(r, 0, sizeof(*r));
  23202. XMEMSET(s, 0, sizeof(*s));
  23203. ExpectIntEQ(wc_InitRng(rng), 0);
  23204. ExpectIntEQ(mp_init(r), 0);
  23205. ExpectIntEQ(mp_init(s), 0);
  23206. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23207. ExpectIntEQ(wc_ecc_init(key), 0);
  23208. /* Test with no curve set. */
  23209. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23210. BAD_FUNC_ARG);
  23211. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23212. /* Test invalid parameters. */
  23213. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23214. NULL), BAD_FUNC_ARG);
  23215. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23216. NULL), BAD_FUNC_ARG);
  23217. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23218. NULL), BAD_FUNC_ARG);
  23219. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23220. NULL), BAD_FUNC_ARG);
  23221. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23222. NULL), BAD_FUNC_ARG);
  23223. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23224. s), BAD_FUNC_ARG);
  23225. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23226. BAD_FUNC_ARG);
  23227. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23228. BAD_FUNC_ARG);
  23229. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23230. BAD_FUNC_ARG);
  23231. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23232. BAD_FUNC_ARG);
  23233. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23234. BAD_FUNC_ARG);
  23235. /* Make key not on the SM2 curve. */
  23236. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23237. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23238. BAD_FUNC_ARG);
  23239. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23240. #ifdef WOLFSSL_SP_MATH_ALL
  23241. {
  23242. mp_int smallR[1];
  23243. sp_init_size(smallR, 1);
  23244. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23245. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23246. smallR, s), 0);
  23247. }
  23248. #endif
  23249. /* Test valid parameters. */
  23250. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23251. 0);
  23252. #ifdef HAVE_ECC_VERIFY
  23253. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23254. key), 0);
  23255. ExpectIntEQ(verified, 1);
  23256. #endif
  23257. mp_free(s);
  23258. mp_free(r);
  23259. wc_ecc_free(key);
  23260. wc_FreeRng(rng);
  23261. #ifdef FP_ECC
  23262. wc_ecc_fp_free();
  23263. #endif
  23264. res = EXPECT_RESULT();
  23265. #endif
  23266. return res;
  23267. }
  23268. /*
  23269. * Testing wc_ecc_sm2_verify_hash()
  23270. */
  23271. static int test_wc_ecc_sm2_sign_hash(void)
  23272. {
  23273. int res = TEST_SKIPPED;
  23274. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23275. EXPECT_DECLS;
  23276. WC_RNG rng[1];
  23277. ecc_key key[1];
  23278. unsigned char hash[32];
  23279. unsigned char sig[72];
  23280. word32 sigSz = sizeof(sig);
  23281. #ifdef HAVE_ECC_VERIFY
  23282. int verified;
  23283. #endif
  23284. XMEMSET(rng, 0, sizeof(*rng));
  23285. XMEMSET(key, 0, sizeof(*key));
  23286. ExpectIntEQ(wc_InitRng(rng), 0);
  23287. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23288. ExpectIntEQ(wc_ecc_init(key), 0);
  23289. /* Test with no curve set. */
  23290. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23291. BAD_FUNC_ARG);
  23292. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23293. /* Test invalid parameters. */
  23294. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23295. NULL), BAD_FUNC_ARG);
  23296. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23297. NULL), BAD_FUNC_ARG);
  23298. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23299. NULL), BAD_FUNC_ARG);
  23300. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23301. NULL), BAD_FUNC_ARG);
  23302. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23303. NULL), BAD_FUNC_ARG);
  23304. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23305. key), BAD_FUNC_ARG);
  23306. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23307. key), BAD_FUNC_ARG);
  23308. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23309. key), BAD_FUNC_ARG);
  23310. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23311. key), BAD_FUNC_ARG);
  23312. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23313. key), BAD_FUNC_ARG);
  23314. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23315. NULL), BAD_FUNC_ARG);
  23316. /* Make key not on the SM2 curve. */
  23317. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23318. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23319. BAD_FUNC_ARG);
  23320. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23321. /* Test valid parameters. */
  23322. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23323. 0);
  23324. #ifdef HAVE_ECC_VERIFY
  23325. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23326. &verified, key), 0);
  23327. ExpectIntEQ(verified, 1);
  23328. #endif
  23329. wc_ecc_free(key);
  23330. wc_FreeRng(rng);
  23331. #ifdef FP_ECC
  23332. wc_ecc_fp_free();
  23333. #endif
  23334. res = EXPECT_RESULT();
  23335. #endif
  23336. return res;
  23337. }
  23338. /*
  23339. * Testing ToTraditional
  23340. */
  23341. static int test_ToTraditional(void)
  23342. {
  23343. EXPECT_DECLS;
  23344. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23345. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23346. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23347. XFILE f = XBADFILE;
  23348. byte input[TWOK_BUF];
  23349. word32 sz = 0;
  23350. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23351. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23352. if (f != XBADFILE)
  23353. XFCLOSE(f);
  23354. /* Good case */
  23355. ExpectIntGT(ToTraditional(input, sz), 0);
  23356. /* Bad cases */
  23357. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23358. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23359. #ifdef WOLFSSL_ASN_TEMPLATE
  23360. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23361. #else
  23362. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23363. #endif
  23364. #endif
  23365. return EXPECT_RESULT();
  23366. } /* End test_ToTraditional*/
  23367. /*
  23368. * Testing wc_EccPrivateKeyToDer
  23369. */
  23370. static int test_wc_EccPrivateKeyToDer(void)
  23371. {
  23372. EXPECT_DECLS;
  23373. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23374. byte output[ONEK_BUF];
  23375. ecc_key eccKey;
  23376. WC_RNG rng;
  23377. word32 inLen;
  23378. word32 outLen = 0;
  23379. int ret;
  23380. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23381. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23382. PRIVATE_KEY_UNLOCK();
  23383. ExpectIntEQ(wc_InitRng(&rng), 0);
  23384. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23385. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23386. #if defined(WOLFSSL_ASYNC_CRYPT)
  23387. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23388. #endif
  23389. ExpectIntEQ(ret, 0);
  23390. inLen = (word32)sizeof(output);
  23391. /* Bad Cases */
  23392. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23393. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23394. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23395. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23396. /* Good Case */
  23397. ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23398. wc_ecc_free(&eccKey);
  23399. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23400. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23401. {
  23402. /* test importing private only into a PKEY struct */
  23403. EC_KEY* ec = NULL;
  23404. EVP_PKEY* pkey = NULL;
  23405. const unsigned char* der;
  23406. der = output;
  23407. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23408. der = output;
  23409. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23410. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23411. if (EXPECT_FAIL()) {
  23412. EC_KEY_free(ec);
  23413. }
  23414. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23415. }
  23416. #endif
  23417. PRIVATE_KEY_LOCK();
  23418. #endif
  23419. return EXPECT_RESULT();
  23420. } /* End test_wc_EccPrivateKeyToDer*/
  23421. /*
  23422. * Testing wc_DhPublicKeyDecode
  23423. */
  23424. static int test_wc_DhPublicKeyDecode(void)
  23425. {
  23426. EXPECT_DECLS;
  23427. #ifndef NO_DH
  23428. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23429. DhKey key;
  23430. word32 inOutIdx;
  23431. XMEMSET(&key, 0, sizeof(DhKey));
  23432. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23433. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23434. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23435. BAD_FUNC_ARG);
  23436. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23437. BAD_FUNC_ARG);
  23438. inOutIdx = 0;
  23439. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23440. BAD_FUNC_ARG);
  23441. inOutIdx = 0;
  23442. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23443. BAD_FUNC_ARG);
  23444. inOutIdx = 0;
  23445. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23446. sizeof_dh_pub_key_der_2048), 0);
  23447. ExpectIntNE(key.p.used, 0);
  23448. ExpectIntNE(key.g.used, 0);
  23449. ExpectIntEQ(key.q.used, 0);
  23450. ExpectIntNE(key.pub.used, 0);
  23451. ExpectIntEQ(key.priv.used, 0);
  23452. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23453. #endif
  23454. #endif /* !NO_DH */
  23455. return EXPECT_RESULT();
  23456. }
  23457. /*
  23458. * Testing wc_Ed25519KeyToDer
  23459. */
  23460. static int test_wc_Ed25519KeyToDer(void)
  23461. {
  23462. EXPECT_DECLS;
  23463. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23464. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23465. byte output[ONEK_BUF];
  23466. ed25519_key ed25519Key;
  23467. WC_RNG rng;
  23468. word32 inLen;
  23469. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23470. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23471. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23472. ExpectIntEQ(wc_InitRng(&rng), 0);
  23473. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23474. inLen = (word32)sizeof(output);
  23475. /* Bad Cases */
  23476. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23477. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23478. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23479. /* Good Cases */
  23480. /* length only */
  23481. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23482. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23483. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23484. wc_ed25519_free(&ed25519Key);
  23485. #endif
  23486. return EXPECT_RESULT();
  23487. } /* End test_wc_Ed25519KeyToDer*/
  23488. /*
  23489. * Testing wc_Ed25519PrivateKeyToDer
  23490. */
  23491. static int test_wc_Ed25519PrivateKeyToDer(void)
  23492. {
  23493. EXPECT_DECLS;
  23494. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23495. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23496. byte output[ONEK_BUF];
  23497. ed25519_key ed25519PrivKey;
  23498. WC_RNG rng;
  23499. word32 inLen;
  23500. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23501. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23502. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23503. ExpectIntEQ(wc_InitRng(&rng), 0);
  23504. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23505. 0);
  23506. inLen = (word32)sizeof(output);
  23507. /* Bad Cases */
  23508. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23509. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23510. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23511. BAD_FUNC_ARG);
  23512. /* Good Cases */
  23513. /* length only */
  23514. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23515. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23516. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23517. wc_ed25519_free(&ed25519PrivKey);
  23518. #endif
  23519. return EXPECT_RESULT();
  23520. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23521. /*
  23522. * Testing wc_Ed448KeyToDer
  23523. */
  23524. static int test_wc_Ed448KeyToDer(void)
  23525. {
  23526. EXPECT_DECLS;
  23527. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23528. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23529. byte output[ONEK_BUF];
  23530. ed448_key ed448Key;
  23531. WC_RNG rng;
  23532. word32 inLen;
  23533. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23534. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23535. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23536. ExpectIntEQ(wc_InitRng(&rng), 0);
  23537. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23538. inLen = (word32)sizeof(output);
  23539. /* Bad Cases */
  23540. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23541. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23542. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23543. /* Good Cases */
  23544. /* length only */
  23545. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23546. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23547. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23548. wc_ed448_free(&ed448Key);
  23549. #endif
  23550. return EXPECT_RESULT();
  23551. } /* End test_wc_Ed448KeyToDer*/
  23552. /*
  23553. * Testing wc_Ed448PrivateKeyToDer
  23554. */
  23555. static int test_wc_Ed448PrivateKeyToDer(void)
  23556. {
  23557. EXPECT_DECLS;
  23558. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23559. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23560. byte output[ONEK_BUF];
  23561. ed448_key ed448PrivKey;
  23562. WC_RNG rng;
  23563. word32 inLen;
  23564. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23565. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23566. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23567. ExpectIntEQ(wc_InitRng(&rng), 0);
  23568. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23569. 0);
  23570. inLen = (word32)sizeof(output);
  23571. /* Bad Cases */
  23572. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23573. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23574. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23575. BAD_FUNC_ARG);
  23576. /* Good cases */
  23577. /* length only */
  23578. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23579. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23580. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23581. wc_ed448_free(&ed448PrivKey);
  23582. #endif
  23583. return EXPECT_RESULT();
  23584. } /* End test_wc_Ed448PrivateKeyToDer*/
  23585. /*
  23586. * Testing wc_Curve448PrivateKeyToDer
  23587. */
  23588. static int test_wc_Curve448PrivateKeyToDer(void)
  23589. {
  23590. EXPECT_DECLS;
  23591. #if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
  23592. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23593. byte output[ONEK_BUF];
  23594. curve448_key curve448PrivKey;
  23595. WC_RNG rng;
  23596. word32 inLen;
  23597. XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
  23598. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23599. ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
  23600. ExpectIntEQ(wc_InitRng(&rng), 0);
  23601. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
  23602. 0);
  23603. inLen = (word32)sizeof(output);
  23604. /* Bad Cases */
  23605. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23606. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23607. ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
  23608. BAD_FUNC_ARG);
  23609. /* Good cases */
  23610. /* length only */
  23611. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
  23612. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
  23613. /* Bad Cases */
  23614. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  23615. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
  23616. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
  23617. BUFFER_E);
  23618. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
  23619. BUFFER_E);
  23620. /* Good cases */
  23621. /* length only */
  23622. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
  23623. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
  23624. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
  23625. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
  23626. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23627. wc_curve448_free(&curve448PrivKey);
  23628. #endif
  23629. return EXPECT_RESULT();
  23630. } /* End wc_Curve448PrivateKeyToDer*/
  23631. /*
  23632. * Testing wc_SetSubjectBuffer
  23633. */
  23634. static int test_wc_SetSubjectBuffer(void)
  23635. {
  23636. EXPECT_DECLS;
  23637. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23638. Cert cert;
  23639. XFILE file = XBADFILE;
  23640. byte* der = NULL;
  23641. word32 derSz;
  23642. derSz = FOURK_BUF;
  23643. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23644. DYNAMIC_TYPE_TMP_BUFFER));
  23645. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23646. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23647. if (file != XBADFILE)
  23648. XFCLOSE(file);
  23649. ExpectIntEQ(wc_InitCert(&cert), 0);
  23650. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, (int)derSz), 0);
  23651. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, (int)derSz), BAD_FUNC_ARG);
  23652. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23653. #endif
  23654. return EXPECT_RESULT();
  23655. } /* End test_wc_SetSubjectBuffer*/
  23656. /*
  23657. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23658. */
  23659. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23660. {
  23661. EXPECT_DECLS;
  23662. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23663. WC_RNG rng;
  23664. Cert cert;
  23665. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23666. RsaKey rsaKey;
  23667. int bits = 2048;
  23668. #endif
  23669. #if defined(HAVE_ECC)
  23670. ecc_key eccKey;
  23671. int ret;
  23672. #endif
  23673. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23674. ed25519_key ed25519Key;
  23675. #endif
  23676. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23677. ed448_key ed448Key;
  23678. #endif
  23679. #ifndef HAVE_FIPS
  23680. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23681. #else
  23682. ExpectIntEQ(wc_InitRng(&rng), 0);
  23683. #endif
  23684. ExpectIntEQ(wc_InitCert(&cert), 0);
  23685. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23686. /* RSA */
  23687. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23688. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23689. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23690. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23691. 0);
  23692. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23693. #endif
  23694. #if defined(HAVE_ECC)
  23695. /* ECC */
  23696. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23697. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23698. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23699. #if defined(WOLFSSL_ASYNC_CRYPT)
  23700. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23701. #endif
  23702. ExpectIntEQ(ret, 0);
  23703. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23704. 0);
  23705. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23706. #endif
  23707. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23708. /* ED25519 */
  23709. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23710. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23711. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23712. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23713. &ed25519Key), 0);
  23714. wc_ed25519_free(&ed25519Key);
  23715. #endif
  23716. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23717. /* ED448 */
  23718. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23719. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23720. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23721. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23722. &ed448Key), 0);
  23723. wc_ed448_free(&ed448Key);
  23724. #endif
  23725. wc_FreeRng(&rng);
  23726. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23727. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23728. return EXPECT_RESULT();
  23729. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23730. /*
  23731. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23732. */
  23733. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23734. {
  23735. EXPECT_DECLS;
  23736. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23737. WC_RNG rng;
  23738. Cert cert;
  23739. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23740. RsaKey rsaKey;
  23741. int bits = 2048;
  23742. #endif
  23743. #if defined(HAVE_ECC)
  23744. ecc_key eccKey;
  23745. int ret;
  23746. #endif
  23747. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23748. ed25519_key ed25519Key;
  23749. #endif
  23750. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23751. ed448_key ed448Key;
  23752. #endif
  23753. #ifndef HAVE_FIPS
  23754. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23755. #else
  23756. ExpectIntEQ(wc_InitRng(&rng), 0);
  23757. #endif
  23758. ExpectIntEQ(wc_InitCert(&cert), 0);
  23759. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23760. /* RSA */
  23761. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23762. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23763. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23764. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23765. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23766. #endif
  23767. #if defined(HAVE_ECC)
  23768. /* ECC */
  23769. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23770. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23771. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23772. #if defined(WOLFSSL_ASYNC_CRYPT)
  23773. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23774. #endif
  23775. ExpectIntEQ(ret, 0);
  23776. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23777. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23778. #endif
  23779. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23780. /* ED25519 */
  23781. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23782. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23783. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23784. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23785. &ed25519Key), 0);
  23786. wc_ed25519_free(&ed25519Key);
  23787. #endif
  23788. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23789. /* ED448 */
  23790. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23791. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23792. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23793. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23794. 0);
  23795. wc_ed448_free(&ed448Key);
  23796. #endif
  23797. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23798. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23799. return EXPECT_RESULT();
  23800. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23801. /*
  23802. * Testing wc_PKCS7_New()
  23803. */
  23804. static int test_wc_PKCS7_New(void)
  23805. {
  23806. EXPECT_DECLS;
  23807. #if defined(HAVE_PKCS7)
  23808. PKCS7* pkcs7 = NULL;
  23809. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23810. wc_PKCS7_Free(pkcs7);
  23811. #endif
  23812. return EXPECT_RESULT();
  23813. } /* END test-wc_PKCS7_New */
  23814. /*
  23815. * Testing wc_PKCS7_Init()
  23816. */
  23817. static int test_wc_PKCS7_Init(void)
  23818. {
  23819. EXPECT_DECLS;
  23820. #if defined(HAVE_PKCS7)
  23821. PKCS7* pkcs7 = NULL;
  23822. void* heap = NULL;
  23823. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23824. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23825. /* Pass in bad args. */
  23826. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23827. wc_PKCS7_Free(pkcs7);
  23828. #endif
  23829. return EXPECT_RESULT();
  23830. } /* END test-wc_PKCS7_Init */
  23831. /*
  23832. * Testing wc_PKCS7_InitWithCert()
  23833. */
  23834. static int test_wc_PKCS7_InitWithCert(void)
  23835. {
  23836. EXPECT_DECLS;
  23837. #if defined(HAVE_PKCS7)
  23838. PKCS7* pkcs7 = NULL;
  23839. #ifndef NO_RSA
  23840. #if defined(USE_CERT_BUFFERS_2048)
  23841. unsigned char cert[sizeof(client_cert_der_2048)];
  23842. int certSz = (int)sizeof(cert);
  23843. XMEMSET(cert, 0, certSz);
  23844. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23845. #elif defined(USE_CERT_BUFFERS_1024)
  23846. unsigned char cert[sizeof(client_cert_der_1024)];
  23847. int certSz = (int)sizeof(cert);
  23848. XMEMSET(cert, 0, certSz);
  23849. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23850. #else
  23851. unsigned char cert[ONEK_BUF];
  23852. XFILE fp = XBADFILE;
  23853. int certSz;
  23854. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23855. XBADFILE);
  23856. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23857. fp), 0);
  23858. if (fp != XBADFILE)
  23859. XFCLOSE(fp);
  23860. #endif
  23861. #elif defined(HAVE_ECC)
  23862. #if defined(USE_CERT_BUFFERS_256)
  23863. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23864. int certSz = (int)sizeof(cert);
  23865. XMEMSET(cert, 0, certSz);
  23866. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23867. #else
  23868. unsigned char cert[ONEK_BUF];
  23869. XFILE fp = XBADFILE;
  23870. int certSz;
  23871. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23872. XBADFILE);
  23873. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23874. fp), 0);
  23875. if (fp != XBADFILE)
  23876. XFCLOSE(fp);
  23877. #endif
  23878. #else
  23879. #error PKCS7 requires ECC or RSA
  23880. #endif
  23881. #ifdef HAVE_ECC
  23882. {
  23883. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23884. static unsigned char certWithInvalidEccKey[] = {
  23885. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23886. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23887. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23888. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23889. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23890. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23891. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23892. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23893. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23894. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23895. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23896. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23897. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23898. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23899. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23900. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23901. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23902. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23903. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23904. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23905. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23906. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23907. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23908. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23909. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23910. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23911. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23912. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23913. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23914. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23915. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23916. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23917. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23918. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23919. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23920. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23921. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23922. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23923. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23924. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23925. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23926. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23927. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23928. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23929. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23930. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23931. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23932. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23933. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23934. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23935. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23936. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23937. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23938. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23939. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23940. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23941. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23942. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23943. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23944. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23945. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23946. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23947. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23948. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23949. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23950. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23951. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23952. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23953. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23954. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23955. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23956. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23957. 0x08, 0xA8, 0xB4
  23958. };
  23959. #endif
  23960. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23961. /* If initialization is not successful, it's free'd in init func. */
  23962. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23963. 0);
  23964. wc_PKCS7_Free(pkcs7);
  23965. pkcs7 = NULL;
  23966. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23967. /* Valid initialization usage. */
  23968. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23969. /* Pass in bad args. No need free for null checks, free at end.*/
  23970. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23971. BAD_FUNC_ARG);
  23972. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23973. BAD_FUNC_ARG);
  23974. #ifdef HAVE_ECC
  23975. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23976. sizeof(certWithInvalidEccKey)), 0);
  23977. }
  23978. #endif
  23979. wc_PKCS7_Free(pkcs7);
  23980. #endif
  23981. return EXPECT_RESULT();
  23982. } /* END test_wc_PKCS7_InitWithCert */
  23983. /*
  23984. * Testing wc_PKCS7_EncodeData()
  23985. */
  23986. static int test_wc_PKCS7_EncodeData(void)
  23987. {
  23988. EXPECT_DECLS;
  23989. #if defined(HAVE_PKCS7)
  23990. PKCS7* pkcs7 = NULL;
  23991. byte output[FOURK_BUF];
  23992. byte data[] = "My encoded DER cert.";
  23993. #ifndef NO_RSA
  23994. #if defined(USE_CERT_BUFFERS_2048)
  23995. unsigned char cert[sizeof(client_cert_der_2048)];
  23996. unsigned char key[sizeof(client_key_der_2048)];
  23997. int certSz = (int)sizeof(cert);
  23998. int keySz = (int)sizeof(key);
  23999. XMEMSET(cert, 0, certSz);
  24000. XMEMSET(key, 0, keySz);
  24001. XMEMCPY(cert, client_cert_der_2048, certSz);
  24002. XMEMCPY(key, client_key_der_2048, keySz);
  24003. #elif defined(USE_CERT_BUFFERS_1024)
  24004. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24005. unsigned char key[sizeof_client_key_der_1024];
  24006. int certSz = (int)sizeof(cert);
  24007. int keySz = (int)sizeof(key);
  24008. XMEMSET(cert, 0, certSz);
  24009. XMEMSET(key, 0, keySz);
  24010. XMEMCPY(cert, client_cert_der_1024, certSz);
  24011. XMEMCPY(key, client_key_der_1024, keySz);
  24012. #else
  24013. unsigned char cert[ONEK_BUF];
  24014. unsigned char key[ONEK_BUF];
  24015. XFILE fp = XBADFILE;
  24016. int certSz;
  24017. int keySz;
  24018. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24019. XBADFILE);
  24020. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24021. fp), 0);
  24022. if (fp != XBADFILE) {
  24023. XFCLOSE(fp);
  24024. fp = XBADFILE;
  24025. }
  24026. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24027. XBADFILE);
  24028. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24029. 0);
  24030. if (fp != XBADFILE)
  24031. XFCLOSE(fp);
  24032. #endif
  24033. #elif defined(HAVE_ECC)
  24034. #if defined(USE_CERT_BUFFERS_256)
  24035. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24036. unsigned char key[sizeof(ecc_clikey_der_256)];
  24037. int certSz = (int)sizeof(cert);
  24038. int keySz = (int)sizeof(key);
  24039. XMEMSET(cert, 0, certSz);
  24040. XMEMSET(key, 0, keySz);
  24041. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24042. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24043. #else
  24044. unsigned char cert[ONEK_BUF];
  24045. unsigned char key[ONEK_BUF];
  24046. XFILE fp = XBADFILE;
  24047. int certSz, keySz;
  24048. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24049. XBADFILE);
  24050. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24051. fp), 0);
  24052. if (fp != XBADFILE) {
  24053. XFCLOSE(fp);
  24054. fp = XBADFILE;
  24055. }
  24056. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24057. XBADFILE);
  24058. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24059. 0);
  24060. if (fp != XBADFILE)
  24061. XFCLOSE(fp);
  24062. #endif
  24063. #endif
  24064. XMEMSET(output, 0, sizeof(output));
  24065. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24066. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24067. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24068. if (pkcs7 != NULL) {
  24069. pkcs7->content = data;
  24070. pkcs7->contentSz = sizeof(data);
  24071. pkcs7->privateKey = key;
  24072. pkcs7->privateKeySz = (word32)keySz;
  24073. }
  24074. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24075. /* Test bad args. */
  24076. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24077. BAD_FUNC_ARG);
  24078. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24079. BAD_FUNC_ARG);
  24080. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24081. wc_PKCS7_Free(pkcs7);
  24082. #endif
  24083. return EXPECT_RESULT();
  24084. } /* END test_wc_PKCS7_EncodeData */
  24085. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24086. !defined(NO_RSA) && !defined(NO_SHA256)
  24087. /* RSA sign raw digest callback */
  24088. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24089. byte* out, word32 outSz, byte* privateKey,
  24090. word32 privateKeySz, int devid, int hashOID)
  24091. {
  24092. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24093. byte digInfoEncoding[] = {
  24094. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24095. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24096. 0x00, 0x04, 0x20
  24097. };
  24098. int ret;
  24099. byte digestInfo[ONEK_BUF];
  24100. byte sig[FOURK_BUF];
  24101. word32 digestInfoSz = 0;
  24102. word32 idx = 0;
  24103. RsaKey rsa;
  24104. /* SHA-256 required only for this example callback due to above
  24105. * digInfoEncoding[] */
  24106. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24107. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24108. (hashOID != SHA256h)) {
  24109. return -1;
  24110. }
  24111. /* build DigestInfo */
  24112. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24113. digestInfoSz += sizeof(digInfoEncoding);
  24114. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24115. digestInfoSz += digestSz;
  24116. /* set up RSA key */
  24117. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24118. if (ret != 0) {
  24119. return ret;
  24120. }
  24121. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24122. /* sign DigestInfo */
  24123. if (ret == 0) {
  24124. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24125. &rsa, pkcs7->rng);
  24126. if (ret > 0) {
  24127. if (ret > (int)outSz) {
  24128. /* output buffer too small */
  24129. ret = -1;
  24130. }
  24131. else {
  24132. /* success, ret holds sig size */
  24133. XMEMCPY(out, sig, ret);
  24134. }
  24135. }
  24136. }
  24137. wc_FreeRsaKey(&rsa);
  24138. return ret;
  24139. }
  24140. #endif
  24141. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  24142. typedef struct encodeSignedDataStream {
  24143. byte out[FOURK_BUF*3];
  24144. int idx;
  24145. word32 outIdx;
  24146. } encodeSignedDataStream;
  24147. /* content is 8k of partially created bundle */
  24148. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  24149. {
  24150. int ret = 0;
  24151. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24152. if (strm->outIdx < pkcs7->contentSz) {
  24153. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  24154. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  24155. *content = strm->out + strm->outIdx;
  24156. strm->outIdx += ret;
  24157. }
  24158. (void)pkcs7;
  24159. return ret;
  24160. }
  24161. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  24162. void* ctx)
  24163. {
  24164. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24165. XMEMCPY(strm->out + strm->idx, output, outputSz);
  24166. strm->idx += outputSz;
  24167. (void)pkcs7;
  24168. return 0;
  24169. }
  24170. #endif
  24171. /*
  24172. * Testing wc_PKCS7_EncodeSignedData()
  24173. */
  24174. static int test_wc_PKCS7_EncodeSignedData(void)
  24175. {
  24176. EXPECT_DECLS;
  24177. #if defined(HAVE_PKCS7)
  24178. PKCS7* pkcs7 = NULL;
  24179. WC_RNG rng;
  24180. byte output[FOURK_BUF];
  24181. byte badOut[1];
  24182. word32 outputSz = (word32)sizeof(output);
  24183. word32 badOutSz = 0;
  24184. byte data[] = "Test data to encode.";
  24185. #ifndef NO_RSA
  24186. #if defined(USE_CERT_BUFFERS_2048)
  24187. byte key[sizeof(client_key_der_2048)];
  24188. byte cert[sizeof(client_cert_der_2048)];
  24189. word32 keySz = (word32)sizeof(key);
  24190. word32 certSz = (word32)sizeof(cert);
  24191. XMEMSET(key, 0, keySz);
  24192. XMEMSET(cert, 0, certSz);
  24193. XMEMCPY(key, client_key_der_2048, keySz);
  24194. XMEMCPY(cert, client_cert_der_2048, certSz);
  24195. #elif defined(USE_CERT_BUFFERS_1024)
  24196. byte key[sizeof_client_key_der_1024];
  24197. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24198. word32 keySz = (word32)sizeof(key);
  24199. word32 certSz = (word32)sizeof(cert);
  24200. XMEMSET(key, 0, keySz);
  24201. XMEMSET(cert, 0, certSz);
  24202. XMEMCPY(key, client_key_der_1024, keySz);
  24203. XMEMCPY(cert, client_cert_der_1024, certSz);
  24204. #else
  24205. unsigned char cert[ONEK_BUF];
  24206. unsigned char key[ONEK_BUF];
  24207. XFILE fp = XBADFILE;
  24208. int certSz;
  24209. int keySz;
  24210. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24211. XBADFILE);
  24212. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24213. fp), 0);
  24214. if (fp != XBADFILE) {
  24215. XFCLOSE(fp);
  24216. fp = XBADFILE;
  24217. }
  24218. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24219. XBADFILE);
  24220. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24221. 0);
  24222. if (fp != XBADFILE)
  24223. XFCLOSE(fp);
  24224. #endif
  24225. #elif defined(HAVE_ECC)
  24226. #if defined(USE_CERT_BUFFERS_256)
  24227. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24228. unsigned char key[sizeof(ecc_clikey_der_256)];
  24229. int certSz = (int)sizeof(cert);
  24230. int keySz = (int)sizeof(key);
  24231. XMEMSET(cert, 0, certSz);
  24232. XMEMSET(key, 0, keySz);
  24233. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24234. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24235. #else
  24236. unsigned char cert[ONEK_BUF];
  24237. unsigned char key[ONEK_BUF];
  24238. XFILE fp = XBADFILE;
  24239. int certSz;
  24240. int keySz;
  24241. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24242. XBADFILE);
  24243. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  24244. if (fp != XBADFILE) {
  24245. XFCLOSE(fp);
  24246. fp = XBADFILE;
  24247. }
  24248. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24249. XBADFILE);
  24250. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  24251. if (fp != XBADFILE)
  24252. XFCLOSE(fp);
  24253. #endif
  24254. #endif
  24255. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24256. XMEMSET(output, 0, outputSz);
  24257. ExpectIntEQ(wc_InitRng(&rng), 0);
  24258. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24259. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24260. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24261. if (pkcs7 != NULL) {
  24262. pkcs7->content = data;
  24263. pkcs7->contentSz = (word32)sizeof(data);
  24264. pkcs7->privateKey = key;
  24265. pkcs7->privateKeySz = (word32)sizeof(key);
  24266. pkcs7->encryptOID = RSAk;
  24267. #ifdef NO_SHA
  24268. pkcs7->hashOID = SHA256h;
  24269. #else
  24270. pkcs7->hashOID = SHAh;
  24271. #endif
  24272. pkcs7->rng = &rng;
  24273. }
  24274. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 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. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24280. #ifdef ASN_BER_TO_DER
  24281. wc_PKCS7_Free(pkcs7);
  24282. /* reinitialize and test setting stream mode */
  24283. {
  24284. int signedSz = 0;
  24285. encodeSignedDataStream strm;
  24286. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24287. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24288. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24289. if (pkcs7 != NULL) {
  24290. pkcs7->content = data;
  24291. pkcs7->contentSz = (word32)sizeof(data);
  24292. pkcs7->privateKey = key;
  24293. pkcs7->privateKeySz = (word32)sizeof(key);
  24294. pkcs7->encryptOID = RSAk;
  24295. #ifdef NO_SHA
  24296. pkcs7->hashOID = SHA256h;
  24297. #else
  24298. pkcs7->hashOID = SHAh;
  24299. #endif
  24300. pkcs7->rng = &rng;
  24301. }
  24302. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24303. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24304. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24305. BAD_FUNC_ARG);
  24306. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24307. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24308. outputSz), 0);
  24309. wc_PKCS7_Free(pkcs7);
  24310. pkcs7 = NULL;
  24311. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24312. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24313. /* use exact signed buffer size since BER encoded */
  24314. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)signedSz), 0);
  24315. wc_PKCS7_Free(pkcs7);
  24316. /* now try with using callbacks for IO */
  24317. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24318. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24319. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24320. if (pkcs7 != NULL) {
  24321. pkcs7->contentSz = FOURK_BUF*2;
  24322. pkcs7->privateKey = key;
  24323. pkcs7->privateKeySz = (word32)sizeof(key);
  24324. pkcs7->encryptOID = RSAk;
  24325. #ifdef NO_SHA
  24326. pkcs7->hashOID = SHA256h;
  24327. #else
  24328. pkcs7->hashOID = SHAh;
  24329. #endif
  24330. pkcs7->rng = &rng;
  24331. }
  24332. XMEMSET(&strm, 0, sizeof(strm));
  24333. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24334. StreamOutputCB, (void*)&strm), 0);
  24335. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24336. wc_PKCS7_Free(pkcs7);
  24337. pkcs7 = NULL;
  24338. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24339. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24340. /* use exact signed buffer size since BER encoded */
  24341. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, (word32)signedSz), 0);
  24342. }
  24343. #endif
  24344. #ifndef NO_PKCS7_STREAM
  24345. wc_PKCS7_Free(pkcs7);
  24346. pkcs7 = NULL;
  24347. {
  24348. word32 z;
  24349. int ret;
  24350. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24351. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24352. /* test for streaming mode */
  24353. ret = -1;
  24354. for (z = 0; z < outputSz && ret != 0; z++) {
  24355. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24356. if (ret < 0){
  24357. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24358. }
  24359. }
  24360. ExpectIntEQ(ret, 0);
  24361. ExpectIntNE(pkcs7->contentSz, 0);
  24362. ExpectNotNull(pkcs7->contentDynamic);
  24363. }
  24364. #endif /* !NO_PKCS7_STREAM */
  24365. /* Pass in bad args. */
  24366. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24367. BAD_FUNC_ARG);
  24368. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24369. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24370. badOutSz), BAD_FUNC_ARG);
  24371. if (pkcs7 != NULL) {
  24372. pkcs7->hashOID = 0; /* bad hashOID */
  24373. }
  24374. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24375. BAD_FUNC_ARG);
  24376. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24377. !defined(NO_RSA) && !defined(NO_SHA256)
  24378. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24379. * Example callback assumes SHA-256, so only run test if compiled in. */
  24380. wc_PKCS7_Free(pkcs7);
  24381. pkcs7 = NULL;
  24382. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24383. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24384. if (pkcs7 != NULL) {
  24385. pkcs7->content = data;
  24386. pkcs7->contentSz = (word32)sizeof(data);
  24387. pkcs7->privateKey = key;
  24388. pkcs7->privateKeySz = (word32)sizeof(key);
  24389. pkcs7->encryptOID = RSAk;
  24390. pkcs7->hashOID = SHA256h;
  24391. pkcs7->rng = &rng;
  24392. }
  24393. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24394. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24395. #endif
  24396. wc_PKCS7_Free(pkcs7);
  24397. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24398. #endif
  24399. return EXPECT_RESULT();
  24400. } /* END test_wc_PKCS7_EncodeSignedData */
  24401. /*
  24402. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24403. */
  24404. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24405. {
  24406. EXPECT_DECLS;
  24407. #if defined(HAVE_PKCS7)
  24408. int i;
  24409. PKCS7* pkcs7 = NULL;
  24410. WC_RNG rng;
  24411. byte outputHead[FOURK_BUF/2];
  24412. byte outputFoot[FOURK_BUF/2];
  24413. word32 outputHeadSz = (word32)sizeof(outputHead);
  24414. word32 outputFootSz = (word32)sizeof(outputFoot);
  24415. byte data[FOURK_BUF];
  24416. wc_HashAlg hash;
  24417. #ifdef NO_SHA
  24418. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24419. #else
  24420. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24421. #endif
  24422. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24423. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  24424. #ifndef NO_RSA
  24425. #if defined(USE_CERT_BUFFERS_2048)
  24426. byte key[sizeof(client_key_der_2048)];
  24427. byte cert[sizeof(client_cert_der_2048)];
  24428. word32 keySz = (word32)sizeof(key);
  24429. word32 certSz = (word32)sizeof(cert);
  24430. XMEMSET(key, 0, keySz);
  24431. XMEMSET(cert, 0, certSz);
  24432. XMEMCPY(key, client_key_der_2048, keySz);
  24433. XMEMCPY(cert, client_cert_der_2048, certSz);
  24434. #elif defined(USE_CERT_BUFFERS_1024)
  24435. byte key[sizeof_client_key_der_1024];
  24436. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24437. word32 keySz = (word32)sizeof(key);
  24438. word32 certSz = (word32)sizeof(cert);
  24439. XMEMSET(key, 0, keySz);
  24440. XMEMSET(cert, 0, certSz);
  24441. XMEMCPY(key, client_key_der_1024, keySz);
  24442. XMEMCPY(cert, client_cert_der_1024, certSz);
  24443. #else
  24444. unsigned char cert[ONEK_BUF];
  24445. unsigned char key[ONEK_BUF];
  24446. XFILE fp = XBADFILE;
  24447. int certSz;
  24448. int keySz;
  24449. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24450. XBADFILE);
  24451. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24452. fp), 0);
  24453. if (fp != XBADFILE) {
  24454. XFCLOSE(fp);
  24455. fp = XBADFILE;
  24456. }
  24457. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24458. XBADFILE);
  24459. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24460. 0);
  24461. if (fp != XBADFILE)
  24462. XFCLOSE(fp);
  24463. #endif
  24464. #elif defined(HAVE_ECC)
  24465. #if defined(USE_CERT_BUFFERS_256)
  24466. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24467. unsigned char key[sizeof(ecc_clikey_der_256)];
  24468. int certSz = (int)sizeof(cert);
  24469. int keySz = (int)sizeof(key);
  24470. XMEMSET(cert, 0, certSz);
  24471. XMEMSET(key, 0, keySz);
  24472. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24473. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24474. #else
  24475. unsigned char cert[ONEK_BUF];
  24476. unsigned char key[ONEK_BUF];
  24477. XFILE fp = XBADFILE;
  24478. int certSz;
  24479. int keySz;
  24480. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24481. XBADFILE);
  24482. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24483. fp), 0);
  24484. if (fp != XBADFILE) {
  24485. XFCLOSE(fp);
  24486. fp = XBADFILE;
  24487. }
  24488. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24489. XBADFILE);
  24490. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24491. 0);
  24492. if (fp != XBADFILE)
  24493. XFCLOSE(fp);
  24494. #endif
  24495. #endif
  24496. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24497. /* initialize large data with sequence */
  24498. for (i=0; i<(int)sizeof(data); i++)
  24499. data[i] = i & 0xff;
  24500. XMEMSET(outputHead, 0, outputHeadSz);
  24501. XMEMSET(outputFoot, 0, outputFootSz);
  24502. ExpectIntEQ(wc_InitRng(&rng), 0);
  24503. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24504. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24505. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24506. if (pkcs7 != NULL) {
  24507. pkcs7->content = NULL; /* not used for ex */
  24508. pkcs7->contentSz = (word32)sizeof(data);
  24509. pkcs7->privateKey = key;
  24510. pkcs7->privateKeySz = (word32)sizeof(key);
  24511. pkcs7->encryptOID = RSAk;
  24512. #ifdef NO_SHA
  24513. pkcs7->hashOID = SHA256h;
  24514. #else
  24515. pkcs7->hashOID = SHAh;
  24516. #endif
  24517. pkcs7->rng = &rng;
  24518. }
  24519. /* calculate hash for content */
  24520. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24521. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24522. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24523. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24524. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24525. /* Perform PKCS7 sign using hash directly */
  24526. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24527. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24528. ExpectIntGT(outputHeadSz, 0);
  24529. ExpectIntGT(outputFootSz, 0);
  24530. wc_PKCS7_Free(pkcs7);
  24531. pkcs7 = NULL;
  24532. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24533. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24534. /* required parameter even on verify when using _ex, if using outputHead
  24535. * and outputFoot */
  24536. if (pkcs7 != NULL) {
  24537. pkcs7->contentSz = (word32)sizeof(data);
  24538. }
  24539. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24540. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24541. wc_PKCS7_Free(pkcs7);
  24542. pkcs7 = NULL;
  24543. /* assembly complete PKCS7 sign and use normal verify */
  24544. {
  24545. byte* output = NULL;
  24546. word32 outputSz = 0;
  24547. #ifndef NO_PKCS7_STREAM
  24548. word32 z;
  24549. int ret;
  24550. #endif /* !NO_PKCS7_STREAM */
  24551. ExpectNotNull(output = (byte*)XMALLOC(
  24552. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24553. DYNAMIC_TYPE_TMP_BUFFER));
  24554. if (output != NULL) {
  24555. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24556. outputSz += outputHeadSz;
  24557. XMEMCPY(&output[outputSz], data, sizeof(data));
  24558. outputSz += sizeof(data);
  24559. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24560. outputSz += outputFootSz;
  24561. }
  24562. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24563. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24564. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24565. #ifndef NO_PKCS7_STREAM
  24566. wc_PKCS7_Free(pkcs7);
  24567. pkcs7 = NULL;
  24568. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24569. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24570. /* test for streaming mode */
  24571. ret = -1;
  24572. for (z = 0; z < outputSz && ret != 0; z++) {
  24573. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24574. if (ret < 0){
  24575. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24576. }
  24577. }
  24578. ExpectIntEQ(ret, 0);
  24579. ExpectIntNE(pkcs7->contentSz, 0);
  24580. ExpectNotNull(pkcs7->contentDynamic);
  24581. wc_PKCS7_Free(pkcs7);
  24582. pkcs7 = NULL;
  24583. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24584. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24585. #endif /* !NO_PKCS7_STREAM */
  24586. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24587. }
  24588. /* Pass in bad args. */
  24589. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24590. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24591. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24592. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24593. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24594. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24595. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24596. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24597. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24598. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24599. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24600. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24601. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24602. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24603. if (pkcs7 != NULL) {
  24604. pkcs7->hashOID = 0; /* bad hashOID */
  24605. }
  24606. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24607. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24608. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24609. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24610. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24611. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24612. #ifndef NO_PKCS7_STREAM
  24613. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24614. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24615. #else
  24616. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24617. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24618. #endif
  24619. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24620. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24621. #ifndef NO_PKCS7_STREAM
  24622. /* can pass in 0 buffer length with streaming API */
  24623. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24624. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24625. #else
  24626. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24627. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24628. #endif
  24629. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24630. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24631. #ifndef NO_PKCS7_STREAM
  24632. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24633. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24634. #else
  24635. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24636. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24637. #endif
  24638. wc_PKCS7_Free(pkcs7);
  24639. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24640. #endif
  24641. return EXPECT_RESULT();
  24642. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24643. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24644. /**
  24645. * Loads certs/keys from files or buffers into the argument buffers,
  24646. * helper function called by CreatePKCS7SignedData().
  24647. *
  24648. * Returns 0 on success, negative on error.
  24649. */
  24650. static int LoadPKCS7SignedDataCerts(
  24651. int useIntermediateCertChain, int pkAlgoType,
  24652. byte* intCARoot, word32* intCARootSz,
  24653. byte* intCA1, word32* intCA1Sz,
  24654. byte* intCA2, word32* intCA2Sz,
  24655. byte* cert, word32* certSz,
  24656. byte* key, word32* keySz)
  24657. {
  24658. EXPECT_DECLS;
  24659. int ret = 0;
  24660. XFILE fp = XBADFILE;
  24661. #ifndef NO_RSA
  24662. const char* intCARootRSA = "./certs/ca-cert.der";
  24663. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24664. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24665. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24666. const char* intServKeyRSA = "./certs/server-key.der";
  24667. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24668. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24669. const char* cli1024Key = "./certs/1024/client-key.der";
  24670. #endif
  24671. #endif
  24672. #ifdef HAVE_ECC
  24673. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24674. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24675. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24676. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24677. const char* intServKeyECC = "./certs/ecc-key.der";
  24678. #ifndef USE_CERT_BUFFERS_256
  24679. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24680. const char* cliEccKey = "./certs/client-ecc-key.der";
  24681. #endif
  24682. #endif
  24683. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24684. ((useIntermediateCertChain == 1) &&
  24685. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24686. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24687. return BAD_FUNC_ARG;
  24688. }
  24689. /* Read/load certs and keys to use for signing based on PK type and chain */
  24690. switch (pkAlgoType) {
  24691. #ifndef NO_RSA
  24692. case RSA_TYPE:
  24693. if (useIntermediateCertChain == 1) {
  24694. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24695. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24696. if (fp != XBADFILE) {
  24697. XFCLOSE(fp);
  24698. fp = XBADFILE;
  24699. }
  24700. ExpectIntGT(*intCARootSz, 0);
  24701. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24702. if (fp != XBADFILE) {
  24703. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24704. XFCLOSE(fp);
  24705. fp = XBADFILE;
  24706. }
  24707. ExpectIntGT(*intCA1Sz, 0);
  24708. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24709. if (fp != XBADFILE) {
  24710. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24711. XFCLOSE(fp);
  24712. fp = XBADFILE;
  24713. }
  24714. ExpectIntGT(*intCA2Sz, 0);
  24715. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24716. if (fp != XBADFILE) {
  24717. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24718. XFCLOSE(fp);
  24719. fp = XBADFILE;
  24720. }
  24721. ExpectIntGT(*certSz, 0);
  24722. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24723. if (fp != XBADFILE) {
  24724. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24725. XFCLOSE(fp);
  24726. fp = XBADFILE;
  24727. }
  24728. ExpectIntGT(*keySz, 0);
  24729. }
  24730. else {
  24731. #if defined(USE_CERT_BUFFERS_2048)
  24732. *keySz = sizeof_client_key_der_2048;
  24733. *certSz = sizeof_client_cert_der_2048;
  24734. XMEMCPY(key, client_key_der_2048, *keySz);
  24735. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24736. #elif defined(USE_CERT_BUFFERS_1024)
  24737. *keySz = sizeof_client_key_der_1024;
  24738. *certSz = sizeof_client_cert_der_1024;
  24739. XMEMCPY(key, client_key_der_1024, *keySz);
  24740. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24741. #else
  24742. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24743. if (fp != XBADFILE) {
  24744. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24745. XFCLOSE(fp);
  24746. fp = XBADFILE;
  24747. }
  24748. ExpectIntGT(*keySz, 0);
  24749. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24750. if (fp != XBADFILE) {
  24751. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24752. XFCLOSE(fp);
  24753. fp = XBADFILE;
  24754. }
  24755. ExpectIntGT(*certSz, 0);
  24756. #endif /* USE_CERT_BUFFERS_2048 */
  24757. }
  24758. break;
  24759. #endif /* !NO_RSA */
  24760. #ifdef HAVE_ECC
  24761. case ECC_TYPE:
  24762. if (useIntermediateCertChain == 1) {
  24763. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24764. if (fp != XBADFILE) {
  24765. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24766. fp);
  24767. XFCLOSE(fp);
  24768. fp = XBADFILE;
  24769. }
  24770. ExpectIntGT(*intCARootSz, 0);
  24771. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24772. if (fp != XBADFILE) {
  24773. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24774. XFCLOSE(fp);
  24775. fp = XBADFILE;
  24776. }
  24777. ExpectIntGT(*intCA1Sz, 0);
  24778. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24779. if (fp != XBADFILE) {
  24780. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24781. XFCLOSE(fp);
  24782. fp = XBADFILE;
  24783. }
  24784. ExpectIntGT(*intCA2Sz, 0);
  24785. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24786. if (fp != XBADFILE) {
  24787. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24788. XFCLOSE(fp);
  24789. fp = XBADFILE;
  24790. }
  24791. ExpectIntGT(*certSz, 0);
  24792. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24793. if (fp != XBADFILE) {
  24794. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24795. XFCLOSE(fp);
  24796. fp = XBADFILE;
  24797. }
  24798. ExpectIntGT(*keySz, 0);
  24799. }
  24800. else {
  24801. #if defined(USE_CERT_BUFFERS_256)
  24802. *keySz = sizeof_ecc_clikey_der_256;
  24803. *certSz = sizeof_cliecc_cert_der_256;
  24804. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24805. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24806. #else
  24807. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24808. if (fp != XBADFILE) {
  24809. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24810. XFCLOSE(fp);
  24811. fp = XBADFILE;
  24812. }
  24813. ExpectIntGT(*keySz, 0);
  24814. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24815. if (fp != XBADFILE) {
  24816. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24817. XFCLOSE(fp);
  24818. fp = XBADFILE;
  24819. }
  24820. ExpectIntGT(*certSz, 0);
  24821. #endif /* USE_CERT_BUFFERS_256 */
  24822. }
  24823. break;
  24824. #endif /* HAVE_ECC */
  24825. default:
  24826. WOLFSSL_MSG("Unsupported SignedData PK type");
  24827. ret = BAD_FUNC_ARG;
  24828. break;
  24829. }
  24830. if (EXPECT_FAIL() && (ret == 0)) {
  24831. ret = BAD_FUNC_ARG;
  24832. }
  24833. return ret;
  24834. }
  24835. /**
  24836. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24837. *
  24838. * output output buffer to place SignedData
  24839. * outputSz size of output buffer
  24840. * data data buffer to be signed
  24841. * dataSz size of data buffer
  24842. * withAttribs [1/0] include attributes in SignedData message
  24843. * detachedSig [1/0] create detached signature, no content
  24844. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24845. * root CAs in bundle
  24846. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24847. *
  24848. * Return size of bundle created on success, negative on error */
  24849. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24850. byte* data, word32 dataSz,
  24851. int withAttribs, int detachedSig,
  24852. int useIntermediateCertChain,
  24853. int pkAlgoType)
  24854. {
  24855. EXPECT_DECLS;
  24856. int ret = 0;
  24857. WC_RNG rng;
  24858. PKCS7* pkcs7 = NULL;
  24859. static byte messageTypeOid[] =
  24860. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24861. 0x09, 0x02 };
  24862. static byte messageType[] = { 0x13, 2, '1', '9' };
  24863. PKCS7Attrib attribs[] =
  24864. {
  24865. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24866. sizeof(messageType) }
  24867. };
  24868. byte intCARoot[TWOK_BUF];
  24869. byte intCA1[TWOK_BUF];
  24870. byte intCA2[TWOK_BUF];
  24871. byte cert[TWOK_BUF];
  24872. byte key[TWOK_BUF];
  24873. word32 intCARootSz = sizeof(intCARoot);
  24874. word32 intCA1Sz = sizeof(intCA1);
  24875. word32 intCA2Sz = sizeof(intCA2);
  24876. word32 certSz = sizeof(cert);
  24877. word32 keySz = sizeof(key);
  24878. XMEMSET(intCARoot, 0, intCARootSz);
  24879. XMEMSET(intCA1, 0, intCA1Sz);
  24880. XMEMSET(intCA2, 0, intCA2Sz);
  24881. XMEMSET(cert, 0, certSz);
  24882. XMEMSET(key, 0, keySz);
  24883. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24884. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24885. cert, &certSz, key, &keySz);
  24886. ExpectIntEQ(ret, 0);
  24887. XMEMSET(output, 0, outputSz);
  24888. ExpectIntEQ(wc_InitRng(&rng), 0);
  24889. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24890. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24891. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24892. if (useIntermediateCertChain == 1) {
  24893. /* Add intermediate and root CA certs into SignedData Certs SET */
  24894. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24895. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24896. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24897. }
  24898. if (pkcs7 != NULL) {
  24899. pkcs7->content = data;
  24900. pkcs7->contentSz = dataSz;
  24901. pkcs7->privateKey = key;
  24902. pkcs7->privateKeySz = (word32)sizeof(key);
  24903. if (pkAlgoType == RSA_TYPE) {
  24904. pkcs7->encryptOID = RSAk;
  24905. }
  24906. else {
  24907. pkcs7->encryptOID = ECDSAk;
  24908. }
  24909. #ifdef NO_SHA
  24910. pkcs7->hashOID = SHA256h;
  24911. #else
  24912. pkcs7->hashOID = SHAh;
  24913. #endif
  24914. pkcs7->rng = &rng;
  24915. if (withAttribs) {
  24916. /* include a signed attribute */
  24917. pkcs7->signedAttribs = attribs;
  24918. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24919. }
  24920. }
  24921. if (detachedSig) {
  24922. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24923. }
  24924. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, (word32)outputSz);
  24925. ExpectIntGT(outputSz, 0);
  24926. wc_PKCS7_Free(pkcs7);
  24927. pkcs7 = NULL;
  24928. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24929. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24930. if (detachedSig && (pkcs7 != NULL)) {
  24931. pkcs7->content = data;
  24932. pkcs7->contentSz = dataSz;
  24933. }
  24934. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)outputSz), 0);
  24935. wc_PKCS7_Free(pkcs7);
  24936. wc_FreeRng(&rng);
  24937. if (EXPECT_FAIL()) {
  24938. outputSz = 0;
  24939. }
  24940. return outputSz;
  24941. }
  24942. #endif
  24943. /*
  24944. * Testing wc_PKCS_VerifySignedData()
  24945. */
  24946. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24947. {
  24948. EXPECT_DECLS;
  24949. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24950. PKCS7* pkcs7 = NULL;
  24951. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24952. word32 outputSz = sizeof(output);
  24953. byte data[] = "Test data to encode.";
  24954. byte badOut[1];
  24955. word32 badOutSz = 0;
  24956. byte badContent[] = "This is different content than was signed";
  24957. wc_HashAlg hash;
  24958. #ifdef NO_SHA
  24959. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24960. #else
  24961. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24962. #endif
  24963. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24964. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  24965. #ifndef NO_RSA
  24966. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24967. /* contentType OID (1.2.840.113549.1.9.3) */
  24968. static const byte contentTypeOid[] =
  24969. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24970. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24971. static const byte dataType[] =
  24972. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24973. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24974. static const byte messageDigestOid[] =
  24975. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24976. #ifndef NO_ASN_TIME
  24977. /* signingTime OID () */
  24978. static const byte signingTimeOid[] =
  24979. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24980. #endif
  24981. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24982. int dateLength = 0;
  24983. byte dateFormat;
  24984. const byte* datePart = NULL;
  24985. struct tm timearg;
  24986. time_t now;
  24987. struct tm* nowTm = NULL;
  24988. #ifdef NEED_TMP_TIME
  24989. struct tm tmpTimeStorage;
  24990. struct tm* tmpTime = &tmpTimeStorage;
  24991. #endif
  24992. #endif /* !NO_ASN && !NO_ASN_TIME */
  24993. #ifndef NO_PKCS7_STREAM
  24994. word32 z;
  24995. int ret;
  24996. #endif /* !NO_PKCS7_STREAM */
  24997. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24998. /* Success test with RSA certs/key */
  24999. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25000. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  25001. /* calculate hash for content, used later */
  25002. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25003. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25004. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25005. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25006. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25007. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25008. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25009. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25010. #ifndef NO_PKCS7_STREAM
  25011. wc_PKCS7_Free(pkcs7);
  25012. pkcs7 = NULL;
  25013. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25014. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25015. /* test for streaming */
  25016. ret = -1;
  25017. for (z = 0; z < outputSz && ret != 0; z++) {
  25018. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25019. if (ret < 0){
  25020. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25021. }
  25022. }
  25023. ExpectIntEQ(ret, 0);
  25024. ExpectIntNE(pkcs7->contentSz, 0);
  25025. ExpectNotNull(pkcs7->contentDynamic);
  25026. #endif /* !NO_PKCS7_STREAM */
  25027. /* Check that decoded signed attributes are correct */
  25028. /* messageDigest should be first */
  25029. if (pkcs7 != NULL) {
  25030. decodedAttrib = pkcs7->decodedAttrib;
  25031. }
  25032. ExpectNotNull(decodedAttrib);
  25033. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  25034. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  25035. decodedAttrib->oidSz), 0);
  25036. /* + 2 for OCTET STRING and length bytes */
  25037. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  25038. ExpectNotNull(decodedAttrib->value);
  25039. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  25040. #ifndef NO_ASN_TIME
  25041. /* signingTime should be second */
  25042. if (decodedAttrib != NULL) {
  25043. decodedAttrib = decodedAttrib->next;
  25044. }
  25045. ExpectNotNull(decodedAttrib);
  25046. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  25047. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  25048. decodedAttrib->oidSz), 0);
  25049. ExpectIntGT(decodedAttrib->valueSz, 0);
  25050. ExpectNotNull(decodedAttrib->value);
  25051. #endif
  25052. /* Verify signingTime if ASN and time are available */
  25053. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  25054. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  25055. &datePart, &dateFormat, &dateLength), 0);
  25056. ExpectNotNull(datePart);
  25057. ExpectIntGT(dateLength, 0);
  25058. XMEMSET(&timearg, 0, sizeof(timearg));
  25059. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  25060. &timearg), 0);
  25061. /* Get current time and compare year/month/day against attribute value */
  25062. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  25063. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  25064. ExpectNotNull(nowTm);
  25065. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  25066. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  25067. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  25068. #endif /* !NO_ASN && !NO_ASN_TIME */
  25069. /* contentType should be third */
  25070. if (decodedAttrib != NULL) {
  25071. decodedAttrib = decodedAttrib->next;
  25072. }
  25073. ExpectNotNull(decodedAttrib);
  25074. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  25075. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  25076. decodedAttrib->oidSz), 0);
  25077. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  25078. ExpectNotNull(decodedAttrib->value);
  25079. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  25080. 0);
  25081. #endif /* !NO_RSA */
  25082. /* Test bad args. */
  25083. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  25084. BAD_FUNC_ARG);
  25085. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  25086. BAD_FUNC_ARG);
  25087. #ifndef NO_PKCS7_STREAM
  25088. /* can pass in 0 buffer length with streaming API */
  25089. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25090. badOutSz), WC_PKCS7_WANT_READ_E);
  25091. #else
  25092. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  25093. badOutSz), BAD_FUNC_ARG);
  25094. #endif
  25095. wc_PKCS7_Free(pkcs7);
  25096. pkcs7 = NULL;
  25097. #ifndef NO_RSA
  25098. /* Try RSA certs/key/sig first */
  25099. outputSz = sizeof(output);
  25100. XMEMSET(output, 0, outputSz);
  25101. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25102. (word32)sizeof(data),
  25103. 1, 1, 0, RSA_TYPE)), 0);
  25104. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25105. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25106. if (pkcs7 != NULL) {
  25107. pkcs7->content = badContent;
  25108. pkcs7->contentSz = sizeof(badContent);
  25109. }
  25110. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25111. SIG_VERIFY_E);
  25112. wc_PKCS7_Free(pkcs7);
  25113. pkcs7 = NULL;
  25114. #ifndef NO_PKCS7_STREAM
  25115. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25116. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25117. if (pkcs7 != NULL) {
  25118. pkcs7->content = badContent;
  25119. pkcs7->contentSz = sizeof(badContent);
  25120. }
  25121. /* test for streaming */
  25122. ret = -1;
  25123. for (z = 0; z < outputSz && ret != 0; z++) {
  25124. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25125. if (ret == WC_PKCS7_WANT_READ_E){
  25126. continue;
  25127. }
  25128. else if (ret < 0) {
  25129. break;
  25130. }
  25131. }
  25132. ExpectIntEQ(ret, SIG_VERIFY_E);
  25133. ExpectIntNE(pkcs7->contentSz, 0);
  25134. ExpectNotNull(pkcs7->contentDynamic);
  25135. wc_PKCS7_Free(pkcs7);
  25136. pkcs7 = NULL;
  25137. #endif /* !NO_PKCS7_STREAM */
  25138. /* Test success case with detached signature and valid content */
  25139. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25140. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25141. if (pkcs7 != NULL) {
  25142. pkcs7->content = data;
  25143. pkcs7->contentSz = sizeof(data);
  25144. }
  25145. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25146. wc_PKCS7_Free(pkcs7);
  25147. pkcs7 = NULL;
  25148. #ifndef NO_PKCS7_STREAM
  25149. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25150. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25151. if (pkcs7 != NULL) {
  25152. pkcs7->content = data;
  25153. pkcs7->contentSz = sizeof(data);
  25154. }
  25155. /* test for streaming */
  25156. ret = -1;
  25157. for (z = 0; z < outputSz && ret != 0; z++) {
  25158. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25159. if (ret < 0){
  25160. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25161. }
  25162. }
  25163. ExpectIntEQ(ret, 0);
  25164. ExpectIntNE(pkcs7->contentSz, 0);
  25165. ExpectNotNull(pkcs7->contentDynamic);
  25166. wc_PKCS7_Free(pkcs7);
  25167. pkcs7 = NULL;
  25168. #endif /* !NO_PKCS7_STREAM */
  25169. /* verify using pre-computed content digest only (no content) */
  25170. {
  25171. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25172. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25173. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25174. output, outputSz, NULL, 0), 0);
  25175. wc_PKCS7_Free(pkcs7);
  25176. pkcs7 = NULL;
  25177. }
  25178. #endif /* !NO_RSA */
  25179. /* Test verify on signedData containing intermediate/root CA certs */
  25180. #ifndef NO_RSA
  25181. outputSz = sizeof(output);
  25182. XMEMSET(output, 0, outputSz);
  25183. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25184. (word32)sizeof(data),
  25185. 0, 0, 1, RSA_TYPE)), 0);
  25186. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25187. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25188. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25189. wc_PKCS7_Free(pkcs7);
  25190. pkcs7 = NULL;
  25191. #ifndef NO_PKCS7_STREAM
  25192. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25193. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25194. /* test for streaming */
  25195. ret = -1;
  25196. for (z = 0; z < outputSz && ret != 0; z++) {
  25197. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25198. if (ret < 0){
  25199. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25200. }
  25201. }
  25202. ExpectIntEQ(ret, 0);
  25203. ExpectIntNE(pkcs7->contentSz, 0);
  25204. ExpectNotNull(pkcs7->contentDynamic);
  25205. wc_PKCS7_Free(pkcs7);
  25206. pkcs7 = NULL;
  25207. #endif /* !NO_PKCS7_STREAM */
  25208. #endif /* !NO_RSA */
  25209. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  25210. !defined(NO_FILESYSTEM)
  25211. {
  25212. XFILE signedBundle = XBADFILE;
  25213. int signedBundleSz = 0;
  25214. int chunkSz = 1;
  25215. int i, rc = 0;
  25216. byte* buf = NULL;
  25217. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  25218. "rb")) != XBADFILE);
  25219. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  25220. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  25221. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  25222. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  25223. DYNAMIC_TYPE_FILE));
  25224. if (buf != NULL) {
  25225. ExpectIntEQ(XFREAD(buf, 1, (size_t)signedBundleSz, signedBundle),
  25226. signedBundleSz);
  25227. }
  25228. if (signedBundle != XBADFILE) {
  25229. XFCLOSE(signedBundle);
  25230. signedBundle = XBADFILE;
  25231. }
  25232. if (buf != NULL) {
  25233. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25234. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25235. for (i = 0; i < signedBundleSz;) {
  25236. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25237. chunkSz;
  25238. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  25239. if (rc < 0 ) {
  25240. if (rc == WC_PKCS7_WANT_READ_E) {
  25241. i += sz;
  25242. continue;
  25243. }
  25244. break;
  25245. }
  25246. else {
  25247. break;
  25248. }
  25249. }
  25250. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  25251. wc_PKCS7_Free(pkcs7);
  25252. pkcs7 = NULL;
  25253. }
  25254. /* now try with malformed bundle */
  25255. if (buf != NULL) {
  25256. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25257. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25258. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25259. for (i = 0; i < signedBundleSz;) {
  25260. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25261. chunkSz;
  25262. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  25263. if (rc < 0 ) {
  25264. if (rc == WC_PKCS7_WANT_READ_E) {
  25265. i += sz;
  25266. continue;
  25267. }
  25268. break;
  25269. }
  25270. else {
  25271. break;
  25272. }
  25273. }
  25274. ExpectIntEQ(rc, ASN_PARSE_E);
  25275. wc_PKCS7_Free(pkcs7);
  25276. pkcs7 = NULL;
  25277. }
  25278. if (buf != NULL)
  25279. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25280. }
  25281. #endif /* BER and stream */
  25282. #endif
  25283. return EXPECT_RESULT();
  25284. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25285. /*
  25286. * Testing wc_PKCS_VerifySignedData()
  25287. */
  25288. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25289. {
  25290. EXPECT_DECLS;
  25291. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25292. PKCS7* pkcs7 = NULL;
  25293. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25294. word32 outputSz = sizeof(output);
  25295. byte data[] = "Test data to encode.";
  25296. byte badContent[] = "This is different content than was signed";
  25297. wc_HashAlg hash;
  25298. #ifndef NO_PKCS7_STREAM
  25299. word32 z;
  25300. int ret;
  25301. #endif /* !NO_PKCS7_STREAM */
  25302. #ifdef NO_SHA
  25303. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25304. #else
  25305. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25306. #endif
  25307. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25308. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  25309. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25310. /* Success test with ECC certs/key */
  25311. outputSz = sizeof(output);
  25312. XMEMSET(output, 0, outputSz);
  25313. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25314. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25315. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25316. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25317. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25318. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25319. wc_PKCS7_Free(pkcs7);
  25320. pkcs7 = NULL;
  25321. #ifndef NO_PKCS7_STREAM
  25322. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25323. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25324. /* test for streaming */
  25325. ret = -1;
  25326. for (z = 0; z < outputSz && ret != 0; z++) {
  25327. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25328. if (ret < 0){
  25329. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25330. }
  25331. }
  25332. ExpectIntEQ(ret, 0);
  25333. ExpectIntNE(pkcs7->contentSz, 0);
  25334. ExpectNotNull(pkcs7->contentDynamic);
  25335. wc_PKCS7_Free(pkcs7);
  25336. pkcs7 = NULL;
  25337. #endif /* !NO_PKCS7_STREAM */
  25338. /* Invalid content should error, use detached signature so we can
  25339. * easily change content */
  25340. outputSz = sizeof(output);
  25341. XMEMSET(output, 0, outputSz);
  25342. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25343. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25344. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25345. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25346. if (pkcs7 != NULL) {
  25347. pkcs7->content = badContent;
  25348. pkcs7->contentSz = sizeof(badContent);
  25349. }
  25350. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25351. SIG_VERIFY_E);
  25352. wc_PKCS7_Free(pkcs7);
  25353. pkcs7 = NULL;
  25354. #ifndef NO_PKCS7_STREAM
  25355. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25356. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25357. if (pkcs7 != NULL) {
  25358. pkcs7->content = badContent;
  25359. pkcs7->contentSz = sizeof(badContent);
  25360. }
  25361. /* test for streaming */
  25362. ret = -1;
  25363. for (z = 0; z < outputSz && ret != 0; z++) {
  25364. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25365. if (ret == WC_PKCS7_WANT_READ_E){
  25366. continue;
  25367. }
  25368. else if (ret < 0) {
  25369. break;
  25370. }
  25371. }
  25372. ExpectIntEQ(ret, SIG_VERIFY_E);
  25373. ExpectIntNE(pkcs7->contentSz, 0);
  25374. ExpectNotNull(pkcs7->contentDynamic);
  25375. wc_PKCS7_Free(pkcs7);
  25376. pkcs7 = NULL;
  25377. #endif /* !NO_PKCS7_STREAM */
  25378. /* Test success case with detached signature and valid content */
  25379. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25380. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25381. if (pkcs7 != NULL) {
  25382. pkcs7->content = data;
  25383. pkcs7->contentSz = sizeof(data);
  25384. }
  25385. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25386. wc_PKCS7_Free(pkcs7);
  25387. pkcs7 = NULL;
  25388. #ifndef NO_PKCS7_STREAM
  25389. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25390. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25391. if (pkcs7 != NULL) {
  25392. pkcs7->content = data;
  25393. pkcs7->contentSz = sizeof(data);
  25394. }
  25395. /* test for streaming */
  25396. ret = -1;
  25397. for (z = 0; z < outputSz && ret != 0; z++) {
  25398. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25399. if (ret < 0){
  25400. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25401. }
  25402. }
  25403. ExpectIntEQ(ret, 0);
  25404. ExpectIntNE(pkcs7->contentSz, 0);
  25405. ExpectNotNull(pkcs7->contentDynamic);
  25406. wc_PKCS7_Free(pkcs7);
  25407. pkcs7 = NULL;
  25408. #endif /* !NO_PKCS7_STREAM */
  25409. /* verify using pre-computed content digest only (no content) */
  25410. {
  25411. /* calculate hash for content */
  25412. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25413. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25414. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25415. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25416. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25417. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25418. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25419. output, outputSz, NULL, 0), 0);
  25420. wc_PKCS7_Free(pkcs7);
  25421. pkcs7 = NULL;
  25422. }
  25423. /* Test verify on signedData containing intermediate/root CA certs */
  25424. outputSz = sizeof(output);
  25425. XMEMSET(output, 0, outputSz);
  25426. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25427. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25428. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25429. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25430. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25431. wc_PKCS7_Free(pkcs7);
  25432. pkcs7 = NULL;
  25433. #ifndef NO_PKCS7_STREAM
  25434. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25435. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25436. /* test for streaming */
  25437. ret = -1;
  25438. for (z = 0; z < outputSz && ret != 0; z++) {
  25439. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25440. if (ret < 0){
  25441. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25442. }
  25443. }
  25444. ExpectIntEQ(ret, 0);
  25445. ExpectIntNE(pkcs7->contentSz, 0);
  25446. ExpectNotNull(pkcs7->contentDynamic);
  25447. wc_PKCS7_Free(pkcs7);
  25448. pkcs7 = NULL;
  25449. #endif /* !NO_PKCS7_STREAM */
  25450. #endif
  25451. return EXPECT_RESULT();
  25452. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25453. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25454. !defined(NO_AES_256)
  25455. static const byte defKey[] = {
  25456. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25457. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25458. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25459. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25460. };
  25461. static byte aesHandle[32]; /* simulated hardware key handle */
  25462. /* return 0 on success */
  25463. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25464. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25465. byte* in, int inSz, byte* out, void* usrCtx)
  25466. {
  25467. int ret;
  25468. Aes aes;
  25469. if (usrCtx == NULL) {
  25470. /* no simulated handle passed in */
  25471. return -1;
  25472. }
  25473. switch (encryptOID) {
  25474. case AES256CBCb:
  25475. if (ivSz != AES_BLOCK_SIZE)
  25476. return BAD_FUNC_ARG;
  25477. break;
  25478. default:
  25479. WOLFSSL_MSG("Unsupported content cipher type for test");
  25480. return ALGO_ID_E;
  25481. };
  25482. /* simulate using handle to get key */
  25483. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25484. if (ret == 0) {
  25485. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25486. if (ret == 0)
  25487. ret = wc_AesCbcDecrypt(&aes, out, in, (word32)inSz);
  25488. wc_AesFree(&aes);
  25489. }
  25490. (void)aad;
  25491. (void)aadSz;
  25492. (void)authTag;
  25493. (void)authTagSz;
  25494. (void)pkcs7;
  25495. return ret;
  25496. }
  25497. /* returns key size on success */
  25498. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25499. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25500. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25501. {
  25502. int ret = -1;
  25503. (void)cekSz;
  25504. (void)cek;
  25505. (void)outSz;
  25506. (void)keyIdSz;
  25507. (void)direction;
  25508. (void)orginKey; /* used with KAKRI */
  25509. (void)orginKeySz;
  25510. if (out == NULL)
  25511. return BAD_FUNC_ARG;
  25512. if (keyId[0] != 0x00) {
  25513. return -1;
  25514. }
  25515. if (type != (int)PKCS7_KEKRI) {
  25516. return -1;
  25517. }
  25518. switch (keyWrapAlgo) {
  25519. case AES256_WRAP:
  25520. /* simulate setting a handle for later decryption but use key
  25521. * as handle in the test case here */
  25522. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25523. aesHandle, sizeof(aesHandle), NULL);
  25524. if (ret < 0)
  25525. return ret;
  25526. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25527. if (ret < 0)
  25528. return ret;
  25529. /* return key size on success */
  25530. return sizeof(defKey);
  25531. default:
  25532. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25533. return BAD_KEYWRAP_ALG_E;
  25534. };
  25535. }
  25536. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25537. /*
  25538. * Testing wc_PKCS7_EncodeEnvelopedData()
  25539. */
  25540. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25541. {
  25542. EXPECT_DECLS;
  25543. #if defined(HAVE_PKCS7)
  25544. PKCS7* pkcs7 = NULL;
  25545. #ifdef ASN_BER_TO_DER
  25546. int encodedSz = 0;
  25547. #endif
  25548. #ifdef ECC_TIMING_RESISTANT
  25549. WC_RNG rng;
  25550. #endif
  25551. word32 tempWrd32 = 0;
  25552. byte* tmpBytePtr = NULL;
  25553. const char input[] = "Test data to encode.";
  25554. int i;
  25555. int testSz = 0;
  25556. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25557. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25558. byte* rsaCert = NULL;
  25559. byte* rsaPrivKey = NULL;
  25560. word32 rsaCertSz;
  25561. word32 rsaPrivKeySz;
  25562. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25563. !defined(USE_CERT_BUFFERS_2048) )
  25564. static const char* rsaClientCert = "./certs/client-cert.der";
  25565. static const char* rsaClientKey = "./certs/client-key.der";
  25566. rsaCertSz = (word32)sizeof(rsaClientCert);
  25567. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25568. #endif
  25569. #endif
  25570. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25571. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25572. byte* eccCert = NULL;
  25573. byte* eccPrivKey = NULL;
  25574. word32 eccCertSz;
  25575. word32 eccPrivKeySz;
  25576. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25577. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25578. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25579. #endif
  25580. #endif
  25581. /* Generic buffer size. */
  25582. byte output[ONEK_BUF];
  25583. byte decoded[sizeof(input)/sizeof(char)];
  25584. int decodedSz = 0;
  25585. #ifndef NO_FILESYSTEM
  25586. XFILE certFile = XBADFILE;
  25587. XFILE keyFile = XBADFILE;
  25588. #endif
  25589. #ifdef ECC_TIMING_RESISTANT
  25590. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25591. #endif
  25592. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25593. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25594. /* RSA certs and keys. */
  25595. #if defined(USE_CERT_BUFFERS_1024)
  25596. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25597. /* Allocate buffer space. */
  25598. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25599. DYNAMIC_TYPE_TMP_BUFFER));
  25600. /* Init buffer. */
  25601. if (rsaCert != NULL) {
  25602. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25603. }
  25604. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25605. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25606. DYNAMIC_TYPE_TMP_BUFFER));
  25607. if (rsaPrivKey != NULL) {
  25608. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25609. }
  25610. #elif defined(USE_CERT_BUFFERS_2048)
  25611. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25612. /* Allocate buffer */
  25613. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25614. DYNAMIC_TYPE_TMP_BUFFER));
  25615. /* Init buffer. */
  25616. if (rsaCert != NULL) {
  25617. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25618. }
  25619. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25620. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25621. DYNAMIC_TYPE_TMP_BUFFER));
  25622. if (rsaPrivKey != NULL) {
  25623. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25624. }
  25625. #else
  25626. /* File system. */
  25627. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25628. rsaCertSz = (word32)FOURK_BUF;
  25629. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25630. DYNAMIC_TYPE_TMP_BUFFER));
  25631. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25632. certFile)) > 0);
  25633. if (certFile != XBADFILE)
  25634. XFCLOSE(certFile);
  25635. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25636. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25637. DYNAMIC_TYPE_TMP_BUFFER));
  25638. rsaPrivKeySz = (word32)FOURK_BUF;
  25639. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25640. keyFile)) > 0);
  25641. if (keyFile != XBADFILE)
  25642. XFCLOSE(keyFile);
  25643. #endif /* USE_CERT_BUFFERS */
  25644. #endif /* NO_RSA */
  25645. /* ECC */
  25646. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25647. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25648. #ifdef USE_CERT_BUFFERS_256
  25649. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25650. DYNAMIC_TYPE_TMP_BUFFER));
  25651. /* Init buffer. */
  25652. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25653. if (eccCert != NULL) {
  25654. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25655. }
  25656. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25657. DYNAMIC_TYPE_TMP_BUFFER));
  25658. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25659. if (eccPrivKey != NULL) {
  25660. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25661. }
  25662. #else /* File system. */
  25663. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25664. eccCertSz = (word32)FOURK_BUF;
  25665. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25666. DYNAMIC_TYPE_TMP_BUFFER));
  25667. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25668. certFile)) > 0);
  25669. if (certFile != XBADFILE) {
  25670. XFCLOSE(certFile);
  25671. }
  25672. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25673. eccPrivKeySz = (word32)FOURK_BUF;
  25674. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25675. DYNAMIC_TYPE_TMP_BUFFER));
  25676. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25677. keyFile)) > 0);
  25678. if (keyFile != XBADFILE) {
  25679. XFCLOSE(keyFile);
  25680. }
  25681. #endif /* USE_CERT_BUFFERS_256 */
  25682. #endif /* END HAVE_ECC */
  25683. #ifndef NO_FILESYSTEM
  25684. /* Silence. */
  25685. (void)keyFile;
  25686. (void)certFile;
  25687. #endif
  25688. {
  25689. const pkcs7EnvelopedVector testVectors[] = {
  25690. /* DATA is a global variable defined in the makefile. */
  25691. #if !defined(NO_RSA)
  25692. #ifndef NO_DES3
  25693. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25694. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25695. #endif /* NO_DES3 */
  25696. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25697. #ifndef NO_AES_128
  25698. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25699. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25700. #endif
  25701. #ifndef NO_AES_192
  25702. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25703. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25704. #endif
  25705. #ifndef NO_AES_256
  25706. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25707. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25708. #endif
  25709. #endif /* NO_AES && HAVE_AES_CBC */
  25710. #endif /* NO_RSA */
  25711. #if defined(HAVE_ECC)
  25712. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25713. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25714. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25715. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25716. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25717. #endif
  25718. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25719. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25720. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25721. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25722. #endif
  25723. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25724. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25725. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25726. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25727. #endif
  25728. #endif /* NO_AES && HAVE_AES_CBC*/
  25729. #endif /* END HAVE_ECC */
  25730. }; /* END pkcs7EnvelopedVector */
  25731. #ifdef ECC_TIMING_RESISTANT
  25732. ExpectIntEQ(wc_InitRng(&rng), 0);
  25733. #endif
  25734. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25735. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25736. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25737. for (i = 0; i < testSz; i++) {
  25738. #ifdef ASN_BER_TO_DER
  25739. encodeSignedDataStream strm;
  25740. /* test setting stream mode, the first one using IO callbacks */
  25741. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25742. (word32)(testVectors + i)->certSz), 0);
  25743. if (pkcs7 != NULL) {
  25744. #ifdef ECC_TIMING_RESISTANT
  25745. pkcs7->rng = &rng;
  25746. #endif
  25747. if (i != 0)
  25748. pkcs7->content = (byte*)(testVectors + i)->content;
  25749. pkcs7->contentSz = (testVectors + i)->contentSz;
  25750. pkcs7->contentOID = (testVectors + i)->contentOID;
  25751. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25752. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25753. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25754. pkcs7->privateKey = (testVectors + i)->privateKey;
  25755. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25756. }
  25757. if (i == 0) {
  25758. XMEMSET(&strm, 0, sizeof(strm));
  25759. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25760. StreamOutputCB, (void*)&strm), 0);
  25761. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25762. }
  25763. else {
  25764. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25765. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25766. (word32)sizeof(output));
  25767. }
  25768. switch ((testVectors + i)->encryptOID) {
  25769. #ifndef NO_DES3
  25770. case DES3b:
  25771. case DESb:
  25772. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25773. break;
  25774. #endif
  25775. #ifdef HAVE_AESCCM
  25776. #ifdef WOLFSSL_AES_128
  25777. case AES128CCMb:
  25778. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25779. break;
  25780. #endif
  25781. #ifdef WOLFSSL_AES_192
  25782. case AES192CCMb:
  25783. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25784. break;
  25785. #endif
  25786. #ifdef WOLFSSL_AES_256
  25787. case AES256CCMb:
  25788. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25789. break;
  25790. #endif
  25791. #endif
  25792. default:
  25793. ExpectIntGE(encodedSz, 0);
  25794. }
  25795. if (encodedSz > 0) {
  25796. if (i == 0) {
  25797. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25798. strm.out, (word32)encodedSz, decoded,
  25799. (word32)sizeof(decoded));
  25800. }
  25801. else {
  25802. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25803. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25804. }
  25805. ExpectIntGE(decodedSz, 0);
  25806. /* Verify the size of each buffer. */
  25807. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25808. }
  25809. wc_PKCS7_Free(pkcs7);
  25810. pkcs7 = NULL;
  25811. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25812. #endif
  25813. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25814. (word32)(testVectors + i)->certSz), 0);
  25815. if (pkcs7 != NULL) {
  25816. #ifdef ECC_TIMING_RESISTANT
  25817. pkcs7->rng = &rng;
  25818. #endif
  25819. pkcs7->content = (byte*)(testVectors + i)->content;
  25820. pkcs7->contentSz = (testVectors + i)->contentSz;
  25821. pkcs7->contentOID = (testVectors + i)->contentOID;
  25822. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25823. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25824. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25825. pkcs7->privateKey = (testVectors + i)->privateKey;
  25826. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25827. }
  25828. #ifdef ASN_BER_TO_DER
  25829. /* test without setting stream mode */
  25830. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25831. #endif
  25832. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25833. (word32)sizeof(output)), 0);
  25834. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25835. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25836. ExpectIntGE(decodedSz, 0);
  25837. /* Verify the size of each buffer. */
  25838. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25839. /* Don't free the last time through the loop. */
  25840. if (i < testSz - 1) {
  25841. wc_PKCS7_Free(pkcs7);
  25842. pkcs7 = NULL;
  25843. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25844. }
  25845. } /* END test loop. */
  25846. }
  25847. /* Test bad args. */
  25848. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25849. (word32)sizeof(output)), BAD_FUNC_ARG);
  25850. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25851. (word32)sizeof(output)), BAD_FUNC_ARG);
  25852. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25853. /* Decode. */
  25854. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25855. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25856. BAD_FUNC_ARG);
  25857. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25858. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25859. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25860. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25861. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25862. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25863. BAD_FUNC_ARG);
  25864. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25865. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25866. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25867. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25868. /* only a failure for KARI test cases */
  25869. if (pkcs7 != NULL) {
  25870. tempWrd32 = pkcs7->singleCertSz;
  25871. pkcs7->singleCertSz = 0;
  25872. }
  25873. #if defined(WOLFSSL_ASN_TEMPLATE)
  25874. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25875. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25876. BUFFER_E);
  25877. #else
  25878. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25879. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25880. ASN_PARSE_E);
  25881. #endif
  25882. if (pkcs7 != NULL) {
  25883. pkcs7->singleCertSz = tempWrd32;
  25884. tmpBytePtr = pkcs7->singleCert;
  25885. pkcs7->singleCert = NULL;
  25886. }
  25887. #ifndef NO_RSA
  25888. #if defined(NO_PKCS7_STREAM)
  25889. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25890. * is returned from kari parse which gets set to bad func arg */
  25891. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25892. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25893. BAD_FUNC_ARG);
  25894. #else
  25895. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25896. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25897. ASN_PARSE_E);
  25898. #endif
  25899. #endif /* !NO_RSA */
  25900. if (pkcs7 != NULL) {
  25901. pkcs7->singleCert = tmpBytePtr;
  25902. }
  25903. #endif
  25904. if (pkcs7 != NULL) {
  25905. tempWrd32 = pkcs7->privateKeySz;
  25906. pkcs7->privateKeySz = 0;
  25907. }
  25908. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25909. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25910. BAD_FUNC_ARG);
  25911. if (pkcs7 != NULL) {
  25912. pkcs7->privateKeySz = tempWrd32;
  25913. tmpBytePtr = pkcs7->privateKey;
  25914. pkcs7->privateKey = NULL;
  25915. }
  25916. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25917. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25918. BAD_FUNC_ARG);
  25919. if (pkcs7 != NULL) {
  25920. pkcs7->privateKey = tmpBytePtr;
  25921. }
  25922. wc_PKCS7_Free(pkcs7);
  25923. pkcs7 = NULL;
  25924. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25925. /* test of decrypt callback with KEKRI enveloped data */
  25926. {
  25927. int envelopedSz = 0;
  25928. const byte keyId[] = { 0x00 };
  25929. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25930. if (pkcs7 != NULL) {
  25931. pkcs7->content = (byte*)input;
  25932. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25933. pkcs7->contentOID = DATA;
  25934. pkcs7->encryptOID = AES256CBCb;
  25935. }
  25936. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25937. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25938. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25939. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25940. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25941. (word32)sizeof(output))), 0);
  25942. wc_PKCS7_Free(pkcs7);
  25943. pkcs7 = NULL;
  25944. /* decode envelopedData */
  25945. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25946. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25947. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25948. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25949. (word32)envelopedSz, decoded, sizeof(decoded))), 0);
  25950. wc_PKCS7_Free(pkcs7);
  25951. pkcs7 = NULL;
  25952. }
  25953. #endif /* !NO_AES && !NO_AES_256 */
  25954. #ifndef NO_RSA
  25955. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25956. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25957. #endif /* NO_RSA */
  25958. #ifdef HAVE_ECC
  25959. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25960. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25961. #endif /* HAVE_ECC */
  25962. #ifdef ECC_TIMING_RESISTANT
  25963. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25964. #endif
  25965. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25966. !defined(NO_RSA) && !defined(NO_SHA)
  25967. {
  25968. byte out[7];
  25969. byte *cms = NULL;
  25970. word32 cmsSz;
  25971. XFILE cmsFile = XBADFILE;
  25972. XMEMSET(out, 0, sizeof(out));
  25973. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25974. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25975. != XBADFILE);
  25976. cmsSz = (word32)FOURK_BUF;
  25977. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25978. DYNAMIC_TYPE_TMP_BUFFER));
  25979. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25980. if (cmsFile != XBADFILE)
  25981. XFCLOSE(cmsFile);
  25982. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25983. sizeof_client_cert_der_2048), 0);
  25984. if (pkcs7 != NULL) {
  25985. pkcs7->privateKey = (byte*)client_key_der_2048;
  25986. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25987. }
  25988. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25989. 2), 0);
  25990. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25991. sizeof(out)), 0);
  25992. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25993. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25994. wc_PKCS7_Free(pkcs7);
  25995. pkcs7 = NULL;
  25996. }
  25997. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25998. #endif /* HAVE_PKCS7 */
  25999. return EXPECT_RESULT();
  26000. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  26001. /*
  26002. * Testing wc_PKCS7_EncodeEncryptedData()
  26003. */
  26004. static int test_wc_PKCS7_EncodeEncryptedData(void)
  26005. {
  26006. EXPECT_DECLS;
  26007. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  26008. PKCS7* pkcs7 = NULL;
  26009. byte* tmpBytePtr = NULL;
  26010. byte encrypted[TWOK_BUF];
  26011. byte decoded[TWOK_BUF];
  26012. word32 tmpWrd32 = 0;
  26013. int tmpInt = 0;
  26014. int decodedSz = 0;
  26015. int encryptedSz = 0;
  26016. int testSz = 0;
  26017. int i = 0;
  26018. const byte data[] = { /* Hello World */
  26019. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  26020. 0x72,0x6c,0x64
  26021. };
  26022. #ifndef NO_DES3
  26023. byte desKey[] = {
  26024. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  26025. };
  26026. byte des3Key[] = {
  26027. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  26028. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  26029. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  26030. };
  26031. #endif
  26032. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26033. #ifndef NO_AES_128
  26034. byte aes128Key[] = {
  26035. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26036. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26037. };
  26038. #endif
  26039. #ifndef NO_AES_192
  26040. byte aes192Key[] = {
  26041. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26042. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26043. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26044. };
  26045. #endif
  26046. #ifndef NO_AES_256
  26047. byte aes256Key[] = {
  26048. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26049. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26050. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26051. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26052. };
  26053. #endif
  26054. #endif /* !NO_AES && HAVE_AES_CBC */
  26055. const pkcs7EncryptedVector testVectors[] =
  26056. {
  26057. #ifndef NO_DES3
  26058. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  26059. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  26060. #endif /* !NO_DES3 */
  26061. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26062. #ifndef NO_AES_128
  26063. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  26064. sizeof(aes128Key)},
  26065. #endif
  26066. #ifndef NO_AES_192
  26067. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  26068. sizeof(aes192Key)},
  26069. #endif
  26070. #ifndef NO_AES_256
  26071. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  26072. sizeof(aes256Key)},
  26073. #endif
  26074. #endif /* !NO_AES && HAVE_AES_CBC */
  26075. };
  26076. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  26077. for (i = 0; i < testSz; i++) {
  26078. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26079. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26080. if (pkcs7 != NULL) {
  26081. pkcs7->content = (byte*)testVectors[i].content;
  26082. pkcs7->contentSz = testVectors[i].contentSz;
  26083. pkcs7->contentOID = testVectors[i].contentOID;
  26084. pkcs7->encryptOID = testVectors[i].encryptOID;
  26085. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  26086. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  26087. pkcs7->heap = HEAP_HINT;
  26088. }
  26089. /* encode encryptedData */
  26090. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26091. sizeof(encrypted)), 0);
  26092. /* Decode encryptedData */
  26093. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  26094. (word32)encryptedSz, decoded, sizeof(decoded)), 0);
  26095. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  26096. /* Keep values for last itr. */
  26097. if (i < testSz - 1) {
  26098. wc_PKCS7_Free(pkcs7);
  26099. pkcs7 = NULL;
  26100. }
  26101. }
  26102. if (pkcs7 == NULL || testSz == 0) {
  26103. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26104. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26105. }
  26106. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  26107. sizeof(encrypted)),BAD_FUNC_ARG);
  26108. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  26109. sizeof(encrypted)), BAD_FUNC_ARG);
  26110. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26111. 0), BAD_FUNC_ARG);
  26112. /* Testing the struct. */
  26113. if (pkcs7 != NULL) {
  26114. tmpBytePtr = pkcs7->content;
  26115. pkcs7->content = NULL;
  26116. }
  26117. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26118. sizeof(encrypted)), BAD_FUNC_ARG);
  26119. if (pkcs7 != NULL) {
  26120. pkcs7->content = tmpBytePtr;
  26121. tmpWrd32 = pkcs7->contentSz;
  26122. pkcs7->contentSz = 0;
  26123. }
  26124. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26125. sizeof(encrypted)), BAD_FUNC_ARG);
  26126. if (pkcs7 != NULL) {
  26127. pkcs7->contentSz = tmpWrd32;
  26128. tmpInt = pkcs7->encryptOID;
  26129. pkcs7->encryptOID = 0;
  26130. }
  26131. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26132. sizeof(encrypted)), BAD_FUNC_ARG);
  26133. if (pkcs7 != NULL) {
  26134. pkcs7->encryptOID = tmpInt;
  26135. tmpBytePtr = pkcs7->encryptionKey;
  26136. pkcs7->encryptionKey = NULL;
  26137. }
  26138. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26139. sizeof(encrypted)), BAD_FUNC_ARG);
  26140. if (pkcs7 != NULL) {
  26141. pkcs7->encryptionKey = tmpBytePtr;
  26142. tmpWrd32 = pkcs7->encryptionKeySz;
  26143. pkcs7->encryptionKeySz = 0;
  26144. }
  26145. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26146. sizeof(encrypted)), BAD_FUNC_ARG);
  26147. if (pkcs7 != NULL) {
  26148. pkcs7->encryptionKeySz = tmpWrd32;
  26149. }
  26150. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, (word32)encryptedSz,
  26151. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26152. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, (word32)encryptedSz,
  26153. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26154. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  26155. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26156. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26157. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  26158. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26159. decoded, 0), BAD_FUNC_ARG);
  26160. /* Test struct fields */
  26161. if (pkcs7 != NULL) {
  26162. tmpBytePtr = pkcs7->encryptionKey;
  26163. pkcs7->encryptionKey = NULL;
  26164. }
  26165. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26166. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26167. if (pkcs7 != NULL) {
  26168. pkcs7->encryptionKey = tmpBytePtr;
  26169. pkcs7->encryptionKeySz = 0;
  26170. }
  26171. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26172. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26173. wc_PKCS7_Free(pkcs7);
  26174. #endif
  26175. return EXPECT_RESULT();
  26176. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26177. /*
  26178. * Testing wc_PKCS7_Degenerate()
  26179. */
  26180. static int test_wc_PKCS7_Degenerate(void)
  26181. {
  26182. EXPECT_DECLS;
  26183. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26184. PKCS7* pkcs7 = NULL;
  26185. char fName[] = "./certs/test-degenerate.p7b";
  26186. XFILE f = XBADFILE;
  26187. byte der[4096];
  26188. word32 derSz = 0;
  26189. #ifndef NO_PKCS7_STREAM
  26190. word32 z;
  26191. int ret;
  26192. #endif /* !NO_PKCS7_STREAM */
  26193. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26194. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26195. if (f != XBADFILE)
  26196. XFCLOSE(f);
  26197. /* test degenerate success */
  26198. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26199. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26200. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26201. #ifndef NO_RSA
  26202. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26203. #ifndef NO_PKCS7_STREAM
  26204. wc_PKCS7_Free(pkcs7);
  26205. pkcs7 = NULL;
  26206. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26207. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26208. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26209. /* test for streaming */
  26210. ret = -1;
  26211. for (z = 0; z < derSz && ret != 0; z++) {
  26212. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26213. if (ret < 0){
  26214. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26215. }
  26216. }
  26217. ExpectIntEQ(ret, 0);
  26218. #endif /* !NO_PKCS7_STREAM */
  26219. #else
  26220. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26221. #endif /* NO_RSA */
  26222. wc_PKCS7_Free(pkcs7);
  26223. pkcs7 = NULL;
  26224. /* test with turning off degenerate cases */
  26225. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26226. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26227. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26228. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26229. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  26230. PKCS7_NO_SIGNER_E);
  26231. #ifndef NO_PKCS7_STREAM
  26232. wc_PKCS7_Free(pkcs7);
  26233. pkcs7 = NULL;
  26234. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26235. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26236. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26237. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26238. /* test for streaming */
  26239. ret = -1;
  26240. for (z = 0; z < derSz && ret != 0; z++) {
  26241. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26242. if (ret == WC_PKCS7_WANT_READ_E){
  26243. continue;
  26244. }
  26245. else
  26246. break;
  26247. }
  26248. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  26249. #endif /* !NO_PKCS7_STREAM */
  26250. wc_PKCS7_Free(pkcs7);
  26251. #endif
  26252. return EXPECT_RESULT();
  26253. } /* END test_wc_PKCS7_Degenerate() */
  26254. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26255. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26256. static byte berContent[] = {
  26257. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26258. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26259. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26260. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26261. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26262. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26263. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26264. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26265. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26266. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26267. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26268. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26269. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26270. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26271. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26272. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26273. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26274. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26275. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26276. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26277. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26278. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26279. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26280. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26281. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26282. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26283. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26284. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26285. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26286. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26287. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26288. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26289. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26290. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26291. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26292. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26293. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26294. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26295. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26296. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26297. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26298. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26299. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26300. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26301. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26302. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26303. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26304. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26305. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26306. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26307. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26308. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26309. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26310. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26311. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26312. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26313. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26314. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26315. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26316. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26317. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26318. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26319. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26320. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26321. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26322. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26323. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26324. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26325. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26326. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26327. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26328. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26329. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26330. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26331. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26332. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26333. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26334. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26335. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26336. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26337. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26338. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26339. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26340. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26341. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26342. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26343. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26344. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26345. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26346. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26347. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26348. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26349. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26350. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26351. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26352. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26353. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26354. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26355. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26356. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26357. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26358. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26359. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26360. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26361. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26362. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26363. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26364. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26365. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26366. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26367. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26368. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26369. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26370. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26371. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26372. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26373. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26374. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26375. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26376. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26377. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26378. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26379. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26380. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26381. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26382. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26383. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26384. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26385. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26386. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26387. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26388. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26389. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26390. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26391. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26392. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26393. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26394. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26395. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26396. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26397. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26398. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26399. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26400. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26401. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26402. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26403. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26404. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26405. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26406. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26407. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26408. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26409. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26410. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26411. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26412. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26413. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26414. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26415. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26416. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26417. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26418. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26419. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26420. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26421. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26422. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26423. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26424. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26425. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26426. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26427. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26428. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26429. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26430. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26431. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26432. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26433. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26434. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26435. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26436. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26437. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26438. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26439. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26440. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26441. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26442. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26443. 0x00, 0x00, 0x00, 0x00, 0x00
  26444. };
  26445. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26446. * !NO_DES3 && !NO_SHA
  26447. */
  26448. /*
  26449. * Testing wc_PKCS7_BER()
  26450. */
  26451. static int test_wc_PKCS7_BER(void)
  26452. {
  26453. EXPECT_DECLS;
  26454. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26455. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26456. PKCS7* pkcs7 = NULL;
  26457. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26458. XFILE f = XBADFILE;
  26459. byte der[4096];
  26460. #ifndef NO_DES3
  26461. byte decoded[2048];
  26462. #endif
  26463. word32 derSz = 0;
  26464. #ifndef NO_PKCS7_STREAM
  26465. word32 z;
  26466. int ret;
  26467. #endif /* !NO_PKCS7_STREAM */
  26468. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26469. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26470. if (f != XBADFILE) {
  26471. XFCLOSE(f);
  26472. f = XBADFILE;
  26473. }
  26474. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26475. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26476. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26477. #ifndef NO_RSA
  26478. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26479. #ifndef NO_PKCS7_STREAM
  26480. wc_PKCS7_Free(pkcs7);
  26481. pkcs7 = NULL;
  26482. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26483. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26484. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26485. /* test for streaming */
  26486. ret = -1;
  26487. for (z = 0; z < derSz && ret != 0; z++) {
  26488. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26489. if (ret < 0){
  26490. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26491. }
  26492. }
  26493. ExpectIntEQ(ret, 0);
  26494. #endif /* !NO_PKCS7_STREAM */
  26495. #else
  26496. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26497. #endif
  26498. wc_PKCS7_Free(pkcs7);
  26499. pkcs7 = NULL;
  26500. #ifndef NO_DES3
  26501. /* decode BER content */
  26502. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26503. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26504. if (f != XBADFILE) {
  26505. XFCLOSE(f);
  26506. f = XBADFILE;
  26507. }
  26508. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26509. #ifndef NO_RSA
  26510. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26511. #else
  26512. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26513. #endif
  26514. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26515. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26516. if (f != XBADFILE) {
  26517. XFCLOSE(f);
  26518. f = XBADFILE;
  26519. }
  26520. if (pkcs7 != NULL) {
  26521. pkcs7->privateKey = der;
  26522. pkcs7->privateKeySz = derSz;
  26523. }
  26524. #ifndef NO_RSA
  26525. #ifdef WOLFSSL_SP_MATH
  26526. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26527. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26528. #else
  26529. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26530. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26531. #endif
  26532. #else
  26533. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26534. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26535. #endif
  26536. wc_PKCS7_Free(pkcs7);
  26537. #endif /* !NO_DES3 */
  26538. #endif
  26539. return EXPECT_RESULT();
  26540. } /* END test_wc_PKCS7_BER() */
  26541. static int test_wc_PKCS7_signed_enveloped(void)
  26542. {
  26543. EXPECT_DECLS;
  26544. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26545. !defined(NO_FILESYSTEM)
  26546. XFILE f = XBADFILE;
  26547. PKCS7* pkcs7 = NULL;
  26548. #ifdef HAVE_AES_CBC
  26549. PKCS7* inner = NULL;
  26550. #endif
  26551. WC_RNG rng;
  26552. unsigned char key[FOURK_BUF/2];
  26553. unsigned char cert[FOURK_BUF/2];
  26554. unsigned char env[FOURK_BUF];
  26555. int envSz = FOURK_BUF;
  26556. int keySz = 0;
  26557. int certSz = 0;
  26558. unsigned char sig[FOURK_BUF * 2];
  26559. int sigSz = FOURK_BUF * 2;
  26560. #ifdef HAVE_AES_CBC
  26561. unsigned char decoded[FOURK_BUF];
  26562. int decodedSz = FOURK_BUF;
  26563. #endif
  26564. #ifndef NO_PKCS7_STREAM
  26565. int z;
  26566. int ret;
  26567. #endif /* !NO_PKCS7_STREAM */
  26568. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26569. /* load cert */
  26570. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26571. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26572. if (f != XBADFILE) {
  26573. XFCLOSE(f);
  26574. f = XBADFILE;
  26575. }
  26576. /* load key */
  26577. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26578. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26579. if (f != XBADFILE) {
  26580. XFCLOSE(f);
  26581. f = XBADFILE;
  26582. }
  26583. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26584. /* sign cert for envelope */
  26585. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26586. ExpectIntEQ(wc_InitRng(&rng), 0);
  26587. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26588. if (pkcs7 != NULL) {
  26589. pkcs7->content = cert;
  26590. pkcs7->contentSz = (word32)certSz;
  26591. pkcs7->contentOID = DATA;
  26592. pkcs7->privateKey = key;
  26593. pkcs7->privateKeySz = (word32)keySz;
  26594. pkcs7->encryptOID = RSAk;
  26595. pkcs7->hashOID = SHA256h;
  26596. pkcs7->rng = &rng;
  26597. }
  26598. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26599. wc_PKCS7_Free(pkcs7);
  26600. pkcs7 = NULL;
  26601. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26602. #ifdef HAVE_AES_CBC
  26603. /* create envelope */
  26604. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26605. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26606. if (pkcs7 != NULL) {
  26607. pkcs7->content = sig;
  26608. pkcs7->contentSz = (word32)sigSz;
  26609. pkcs7->contentOID = DATA;
  26610. pkcs7->encryptOID = AES256CBCb;
  26611. pkcs7->privateKey = key;
  26612. pkcs7->privateKeySz = (word32)keySz;
  26613. }
  26614. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, (word32)envSz)), 0);
  26615. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26616. wc_PKCS7_Free(pkcs7);
  26617. pkcs7 = NULL;
  26618. #endif
  26619. /* create bad signed enveloped data */
  26620. sigSz = FOURK_BUF * 2;
  26621. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26622. ExpectIntEQ(wc_InitRng(&rng), 0);
  26623. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26624. if (pkcs7 != NULL) {
  26625. pkcs7->content = env;
  26626. pkcs7->contentSz = (word32)envSz;
  26627. pkcs7->contentOID = DATA;
  26628. pkcs7->privateKey = key;
  26629. pkcs7->privateKeySz = (word32)keySz;
  26630. pkcs7->encryptOID = RSAk;
  26631. pkcs7->hashOID = SHA256h;
  26632. pkcs7->rng = &rng;
  26633. }
  26634. /* Set no certs in bundle for this test. */
  26635. if (pkcs7 != NULL) {
  26636. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26637. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26638. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26639. }
  26640. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26641. wc_PKCS7_Free(pkcs7);
  26642. pkcs7 = NULL;
  26643. /* check verify fails */
  26644. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26645. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26646. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz),
  26647. PKCS7_SIGNEEDS_CHECK);
  26648. /* try verifying the signature manually */
  26649. {
  26650. RsaKey rKey;
  26651. word32 idx = 0;
  26652. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26653. WC_MAX_DIGEST_SIZE];
  26654. int digestSz = 0;
  26655. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26656. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, (word32)keySz), 0);
  26657. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26658. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26659. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26660. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26661. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26662. /* verify was success */
  26663. }
  26664. wc_PKCS7_Free(pkcs7);
  26665. pkcs7 = NULL;
  26666. /* initializing the PKCS7 struct with the signing certificate should pass */
  26667. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26668. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26669. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  26670. #ifndef NO_PKCS7_STREAM
  26671. wc_PKCS7_Free(pkcs7);
  26672. pkcs7 = NULL;
  26673. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26674. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26675. /* test for streaming */
  26676. ret = -1;
  26677. for (z = 0; z < sigSz && ret != 0; z++) {
  26678. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26679. if (ret < 0){
  26680. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26681. }
  26682. }
  26683. ExpectIntEQ(ret, 0);
  26684. #endif /* !NO_PKCS7_STREAM */
  26685. wc_PKCS7_Free(pkcs7);
  26686. pkcs7 = NULL;
  26687. /* create valid degenerate bundle */
  26688. sigSz = FOURK_BUF * 2;
  26689. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26690. if (pkcs7 != NULL) {
  26691. pkcs7->content = env;
  26692. pkcs7->contentSz = (word32)envSz;
  26693. pkcs7->contentOID = DATA;
  26694. pkcs7->privateKey = key;
  26695. pkcs7->privateKeySz = (word32)keySz;
  26696. pkcs7->encryptOID = RSAk;
  26697. pkcs7->hashOID = SHA256h;
  26698. pkcs7->rng = &rng;
  26699. }
  26700. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26701. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26702. wc_PKCS7_Free(pkcs7);
  26703. pkcs7 = NULL;
  26704. wc_FreeRng(&rng);
  26705. /* check verify */
  26706. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26707. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26708. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  26709. ExpectNotNull(pkcs7->content);
  26710. #ifndef NO_PKCS7_STREAM
  26711. wc_PKCS7_Free(pkcs7);
  26712. pkcs7 = NULL;
  26713. /* create valid degenerate bundle */
  26714. sigSz = FOURK_BUF * 2;
  26715. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26716. if (pkcs7 != NULL) {
  26717. pkcs7->content = env;
  26718. pkcs7->contentSz = (word32)envSz;
  26719. pkcs7->contentOID = DATA;
  26720. pkcs7->privateKey = key;
  26721. pkcs7->privateKeySz = (word32)keySz;
  26722. pkcs7->encryptOID = RSAk;
  26723. pkcs7->hashOID = SHA256h;
  26724. pkcs7->rng = &rng;
  26725. }
  26726. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26727. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26728. wc_PKCS7_Free(pkcs7);
  26729. pkcs7 = NULL;
  26730. wc_FreeRng(&rng);
  26731. /* check verify */
  26732. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26733. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26734. /* test for streaming */
  26735. ret = -1;
  26736. for (z = 0; z < sigSz && ret != 0; z++) {
  26737. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26738. if (ret < 0){
  26739. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26740. }
  26741. }
  26742. ExpectIntEQ(ret, 0);
  26743. #endif /* !NO_PKCS7_STREAM */
  26744. #ifdef HAVE_AES_CBC
  26745. /* check decode */
  26746. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26747. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, (word32)certSz), 0);
  26748. if (inner != NULL) {
  26749. inner->privateKey = key;
  26750. inner->privateKeySz = (word32)keySz;
  26751. }
  26752. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26753. pkcs7->contentSz, decoded, (word32)decodedSz)), 0);
  26754. wc_PKCS7_Free(inner);
  26755. inner = NULL;
  26756. #endif
  26757. wc_PKCS7_Free(pkcs7);
  26758. pkcs7 = NULL;
  26759. #ifdef HAVE_AES_CBC
  26760. /* check cert set */
  26761. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26762. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26763. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, (word32)decodedSz), 0);
  26764. ExpectNotNull(pkcs7->singleCert);
  26765. ExpectIntNE(pkcs7->singleCertSz, 0);
  26766. wc_PKCS7_Free(pkcs7);
  26767. pkcs7 = NULL;
  26768. #ifndef NO_PKCS7_STREAM
  26769. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26770. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26771. /* test for streaming */
  26772. ret = -1;
  26773. for (z = 0; z < decodedSz && ret != 0; z++) {
  26774. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26775. if (ret < 0){
  26776. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26777. }
  26778. }
  26779. ExpectIntEQ(ret, 0);
  26780. ExpectNotNull(pkcs7->singleCert);
  26781. ExpectIntNE(pkcs7->singleCertSz, 0);
  26782. wc_PKCS7_Free(pkcs7);
  26783. pkcs7 = NULL;
  26784. #endif /* !NO_PKCS7_STREAM */
  26785. #endif
  26786. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26787. return EXPECT_RESULT();
  26788. }
  26789. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26790. {
  26791. EXPECT_DECLS;
  26792. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26793. && !defined(NO_AES)
  26794. PKCS7* pkcs7 = NULL;
  26795. void* heap = NULL;
  26796. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26797. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26798. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26799. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26800. wc_PKCS7_Free(pkcs7);
  26801. #endif
  26802. return EXPECT_RESULT();
  26803. }
  26804. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26805. {
  26806. EXPECT_DECLS;
  26807. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26808. && !defined(NO_AES)
  26809. PKCS7* pkcs7 = NULL;
  26810. void* heap = NULL;
  26811. WOLFSSL_CTX* ctx = NULL;
  26812. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26813. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26814. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26815. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26816. wc_PKCS7_Free(pkcs7);
  26817. #endif
  26818. return EXPECT_RESULT();
  26819. }
  26820. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26821. {
  26822. EXPECT_DECLS;
  26823. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26824. && !defined(NO_AES)
  26825. PKCS7* pkcs7 = NULL;
  26826. void* heap = NULL;
  26827. WOLFSSL_CTX* ctx = NULL;
  26828. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26829. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26830. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26831. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26832. wc_PKCS7_Free(pkcs7);
  26833. #endif
  26834. return EXPECT_RESULT();
  26835. }
  26836. static int test_wc_PKCS7_DecodeCompressedData(void)
  26837. {
  26838. EXPECT_DECLS;
  26839. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26840. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26841. PKCS7* pkcs7 = NULL;
  26842. void* heap = NULL;
  26843. byte out[4096];
  26844. byte* decompressed = NULL;
  26845. int outSz;
  26846. int decompressedSz;
  26847. const char* cert = "./certs/client-cert.pem";
  26848. byte* cert_buf = NULL;
  26849. size_t cert_sz = 0;
  26850. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26851. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26852. DYNAMIC_TYPE_TMP_BUFFER)));
  26853. decompressedSz = (int)cert_sz;
  26854. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26855. if (pkcs7 != NULL) {
  26856. pkcs7->content = (byte*)cert_buf;
  26857. pkcs7->contentSz = (word32)cert_sz;
  26858. pkcs7->contentOID = DATA;
  26859. }
  26860. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26861. sizeof(out))), 0);
  26862. wc_PKCS7_Free(pkcs7);
  26863. pkcs7 = NULL;
  26864. /* compressed key should be smaller than when started */
  26865. ExpectIntLT(outSz, cert_sz);
  26866. /* test decompression */
  26867. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26868. ExpectIntEQ(pkcs7->contentOID, 0);
  26869. /* fail case with out buffer too small */
  26870. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26871. decompressed, outSz), 0);
  26872. /* success case */
  26873. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26874. decompressed, decompressedSz), cert_sz);
  26875. ExpectIntEQ(pkcs7->contentOID, DATA);
  26876. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26877. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26878. decompressed = NULL;
  26879. /* test decompression function with different 'max' inputs */
  26880. outSz = sizeof(out);
  26881. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26882. 0);
  26883. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26884. out, outSz, 0, heap), 0);
  26885. ExpectNull(decompressed);
  26886. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26887. out, outSz, 0, heap), 0);
  26888. ExpectNotNull(decompressed);
  26889. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26890. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26891. decompressed = NULL;
  26892. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26893. out, outSz, 0, heap), 0);
  26894. ExpectNotNull(decompressed);
  26895. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26896. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26897. if (cert_buf != NULL)
  26898. free(cert_buf);
  26899. wc_PKCS7_Free(pkcs7);
  26900. #endif
  26901. return EXPECT_RESULT();
  26902. }
  26903. static int test_wc_i2d_PKCS12(void)
  26904. {
  26905. EXPECT_DECLS;
  26906. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26907. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26908. && !defined(NO_AES) && !defined(NO_SHA)
  26909. WC_PKCS12* pkcs12 = NULL;
  26910. unsigned char der[FOURK_BUF * 2];
  26911. unsigned char* pt;
  26912. int derSz = 0;
  26913. unsigned char out[FOURK_BUF * 2];
  26914. int outSz = FOURK_BUF * 2;
  26915. const char p12_f[] = "./certs/test-servercert.p12";
  26916. XFILE f = XBADFILE;
  26917. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26918. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26919. if (f != XBADFILE)
  26920. XFCLOSE(f);
  26921. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26922. ExpectIntEQ(wc_d2i_PKCS12(der, (word32)derSz, pkcs12), 0);
  26923. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26924. ExpectIntEQ(outSz, derSz);
  26925. outSz = derSz - 1;
  26926. pt = out;
  26927. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26928. outSz = derSz;
  26929. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26930. ExpectIntEQ((pt == out), 0);
  26931. pt = NULL;
  26932. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26933. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26934. wc_PKCS12_free(pkcs12);
  26935. pkcs12 = NULL;
  26936. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26937. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26938. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26939. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26940. ExpectIntEQ(outSz, derSz);
  26941. wc_PKCS12_free(pkcs12);
  26942. pkcs12 = NULL;
  26943. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26944. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26945. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26946. ExpectIntEQ(outSz, derSz);
  26947. wc_PKCS12_free(pkcs12);
  26948. pkcs12 = NULL;
  26949. #endif
  26950. return EXPECT_RESULT();
  26951. }
  26952. /* Testing wc_SignatureGetSize() for signature type ECC */
  26953. static int test_wc_SignatureGetSize_ecc(void)
  26954. {
  26955. EXPECT_DECLS;
  26956. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26957. enum wc_SignatureType sig_type;
  26958. word32 key_len;
  26959. ecc_key ecc;
  26960. const char* qx =
  26961. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26962. const char* qy =
  26963. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26964. const char* d =
  26965. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26966. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26967. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26968. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26969. /* Input for signature type ECC */
  26970. sig_type = WC_SIGNATURE_TYPE_ECC;
  26971. key_len = sizeof(ecc_key);
  26972. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26973. /* Test bad args */
  26974. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26975. sig_type = (enum wc_SignatureType) 100;
  26976. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26977. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26978. sig_type = WC_SIGNATURE_TYPE_ECC;
  26979. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26980. key_len = (word32)0;
  26981. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26982. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26983. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26984. return EXPECT_RESULT();
  26985. } /* END test_wc_SignatureGetSize_ecc() */
  26986. /* Testing wc_SignatureGetSize() for signature type rsa */
  26987. static int test_wc_SignatureGetSize_rsa(void)
  26988. {
  26989. EXPECT_DECLS;
  26990. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26991. enum wc_SignatureType sig_type;
  26992. word32 key_len;
  26993. word32 idx = 0;
  26994. RsaKey rsa_key;
  26995. byte* tmp = NULL;
  26996. size_t bytes;
  26997. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26998. #ifdef USE_CERT_BUFFERS_1024
  26999. bytes = (size_t)sizeof_client_key_der_1024;
  27000. if (bytes < (size_t)sizeof_client_key_der_1024)
  27001. bytes = (size_t)sizeof_client_cert_der_1024;
  27002. #elif defined(USE_CERT_BUFFERS_2048)
  27003. bytes = (size_t)sizeof_client_key_der_2048;
  27004. if (bytes < (size_t)sizeof_client_cert_der_2048)
  27005. bytes = (size_t)sizeof_client_cert_der_2048;
  27006. #else
  27007. bytes = FOURK_BUF;
  27008. #endif
  27009. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  27010. DYNAMIC_TYPE_TMP_BUFFER));
  27011. if (tmp != NULL) {
  27012. #ifdef USE_CERT_BUFFERS_1024
  27013. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  27014. #elif defined(USE_CERT_BUFFERS_2048)
  27015. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  27016. #elif !defined(NO_FILESYSTEM)
  27017. XFILE file = XBADFILE;
  27018. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  27019. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  27020. if (file != XBADFILE)
  27021. XFCLOSE(file);
  27022. }
  27023. #else
  27024. ExpectFail();
  27025. #endif
  27026. }
  27027. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  27028. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  27029. /* Input for signature type RSA */
  27030. sig_type = WC_SIGNATURE_TYPE_RSA;
  27031. key_len = sizeof(RsaKey);
  27032. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  27033. /* Test bad args */
  27034. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  27035. sig_type = (enum wc_SignatureType)100;
  27036. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  27037. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  27038. sig_type = WC_SIGNATURE_TYPE_RSA;
  27039. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  27040. key_len = (word32)0;
  27041. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  27042. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  27043. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27044. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  27045. return EXPECT_RESULT();
  27046. } /* END test_wc_SignatureGetSize_rsa(void) */
  27047. /*----------------------------------------------------------------------------*
  27048. | hash.h Tests
  27049. *----------------------------------------------------------------------------*/
  27050. static int test_wc_HashInit(void)
  27051. {
  27052. EXPECT_DECLS;
  27053. int i; /* 0 indicates tests passed, 1 indicates failure */
  27054. wc_HashAlg hash;
  27055. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27056. enum wc_HashType enumArray[] = {
  27057. #ifndef NO_MD5
  27058. WC_HASH_TYPE_MD5,
  27059. #endif
  27060. #ifndef NO_SHA
  27061. WC_HASH_TYPE_SHA,
  27062. #endif
  27063. #ifdef WOLFSSL_SHA224
  27064. WC_HASH_TYPE_SHA224,
  27065. #endif
  27066. #ifndef NO_SHA256
  27067. WC_HASH_TYPE_SHA256,
  27068. #endif
  27069. #ifdef WOLFSSL_SHA384
  27070. WC_HASH_TYPE_SHA384,
  27071. #endif
  27072. #ifdef WOLFSSL_SHA512
  27073. WC_HASH_TYPE_SHA512,
  27074. #endif
  27075. };
  27076. /* dynamically finds the length */
  27077. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27078. /* For loop to test various arguments... */
  27079. for (i = 0; i < enumlen; i++) {
  27080. /* check for bad args */
  27081. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27082. wc_HashFree(&hash, enumArray[i]);
  27083. /* check for null ptr */
  27084. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  27085. } /* end of for loop */
  27086. return EXPECT_RESULT();
  27087. } /* end of test_wc_HashInit */
  27088. /*
  27089. * Unit test function for wc_HashSetFlags()
  27090. */
  27091. static int test_wc_HashSetFlags(void)
  27092. {
  27093. EXPECT_DECLS;
  27094. #ifdef WOLFSSL_HASH_FLAGS
  27095. wc_HashAlg hash;
  27096. word32 flags = 0;
  27097. int i, j;
  27098. int notSupportedLen;
  27099. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27100. enum wc_HashType enumArray[] = {
  27101. #ifndef NO_MD5
  27102. WC_HASH_TYPE_MD5,
  27103. #endif
  27104. #ifndef NO_SHA
  27105. WC_HASH_TYPE_SHA,
  27106. #endif
  27107. #ifdef WOLFSSL_SHA224
  27108. WC_HASH_TYPE_SHA224,
  27109. #endif
  27110. #ifndef NO_SHA256
  27111. WC_HASH_TYPE_SHA256,
  27112. #endif
  27113. #ifdef WOLFSSL_SHA384
  27114. WC_HASH_TYPE_SHA384,
  27115. #endif
  27116. #ifdef WOLFSSL_SHA512
  27117. WC_HASH_TYPE_SHA512,
  27118. #endif
  27119. #ifdef WOLFSSL_SHA3
  27120. WC_HASH_TYPE_SHA3_224,
  27121. #endif
  27122. };
  27123. enum wc_HashType notSupported[] = {
  27124. WC_HASH_TYPE_MD5_SHA,
  27125. WC_HASH_TYPE_MD2,
  27126. WC_HASH_TYPE_MD4,
  27127. WC_HASH_TYPE_BLAKE2B,
  27128. WC_HASH_TYPE_BLAKE2S,
  27129. WC_HASH_TYPE_NONE,
  27130. };
  27131. /* dynamically finds the length */
  27132. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27133. /* For loop to test various arguments... */
  27134. for (i = 0; i < enumlen; i++) {
  27135. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27136. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  27137. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27138. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  27139. wc_HashFree(&hash, enumArray[i]);
  27140. }
  27141. /* For loop to test not supported cases */
  27142. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27143. for (j = 0; j < notSupportedLen; j++) {
  27144. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27145. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  27146. BAD_FUNC_ARG);
  27147. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27148. }
  27149. #endif
  27150. return EXPECT_RESULT();
  27151. } /* END test_wc_HashSetFlags */
  27152. /*
  27153. * Unit test function for wc_HashGetFlags()
  27154. */
  27155. static int test_wc_HashGetFlags(void)
  27156. {
  27157. EXPECT_DECLS;
  27158. #ifdef WOLFSSL_HASH_FLAGS
  27159. wc_HashAlg hash;
  27160. word32 flags = 0;
  27161. int i, j;
  27162. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27163. enum wc_HashType enumArray[] = {
  27164. #ifndef NO_MD5
  27165. WC_HASH_TYPE_MD5,
  27166. #endif
  27167. #ifndef NO_SHA
  27168. WC_HASH_TYPE_SHA,
  27169. #endif
  27170. #ifdef WOLFSSL_SHA224
  27171. WC_HASH_TYPE_SHA224,
  27172. #endif
  27173. #ifndef NO_SHA256
  27174. WC_HASH_TYPE_SHA256,
  27175. #endif
  27176. #ifdef WOLFSSL_SHA384
  27177. WC_HASH_TYPE_SHA384,
  27178. #endif
  27179. #ifdef WOLFSSL_SHA512
  27180. WC_HASH_TYPE_SHA512,
  27181. #endif
  27182. #ifdef WOLFSSL_SHA3
  27183. WC_HASH_TYPE_SHA3_224,
  27184. #endif
  27185. };
  27186. enum wc_HashType notSupported[] = {
  27187. WC_HASH_TYPE_MD5_SHA,
  27188. WC_HASH_TYPE_MD2,
  27189. WC_HASH_TYPE_MD4,
  27190. WC_HASH_TYPE_BLAKE2B,
  27191. WC_HASH_TYPE_BLAKE2S,
  27192. WC_HASH_TYPE_NONE,
  27193. };
  27194. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27195. int notSupportedLen;
  27196. /* For loop to test various arguments... */
  27197. for (i = 0; i < enumlen; i++) {
  27198. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27199. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  27200. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27201. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  27202. wc_HashFree(&hash, enumArray[i]);
  27203. }
  27204. /* For loop to test not supported cases */
  27205. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27206. for (j = 0; j < notSupportedLen; j++) {
  27207. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27208. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  27209. BAD_FUNC_ARG);
  27210. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27211. }
  27212. #endif
  27213. return EXPECT_RESULT();
  27214. } /* END test_wc_HashGetFlags */
  27215. /*----------------------------------------------------------------------------*
  27216. | Compatibility Tests
  27217. *----------------------------------------------------------------------------*/
  27218. /*----------------------------------------------------------------------------*
  27219. | ASN.1 Tests
  27220. *----------------------------------------------------------------------------*/
  27221. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27222. {
  27223. EXPECT_DECLS;
  27224. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27225. ASN1_BIT_STRING* str = NULL;
  27226. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27227. /* Empty data testing. */
  27228. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27229. ASN1_BIT_STRING_free(str);
  27230. str = NULL;
  27231. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27232. /* Invalid parameter testing. */
  27233. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27234. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27235. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27236. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27237. /* No bit string - bit is always 0. */
  27238. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27239. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27240. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27241. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27242. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27243. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27244. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27245. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27246. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27247. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27248. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27249. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27250. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27251. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27252. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27253. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27254. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27255. ASN1_BIT_STRING_free(str);
  27256. ASN1_BIT_STRING_free(NULL);
  27257. #endif
  27258. return EXPECT_RESULT();
  27259. }
  27260. static int test_wolfSSL_ASN1_INTEGER(void)
  27261. {
  27262. EXPECT_DECLS;
  27263. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27264. ASN1_INTEGER* a = NULL;
  27265. ASN1_INTEGER* dup = NULL;
  27266. const unsigned char invalidLenDer[] = {
  27267. 0x02, 0x20, 0x00
  27268. };
  27269. const unsigned char longDer[] = {
  27270. 0x02, 0x20,
  27271. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27272. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27273. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27274. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27275. };
  27276. const unsigned char* p;
  27277. /* Invalid parameter testing. */
  27278. ASN1_INTEGER_free(NULL);
  27279. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27280. ExpectNotNull(a = ASN1_INTEGER_new());
  27281. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27282. ASN1_INTEGER_free(dup);
  27283. dup = NULL;
  27284. ASN1_INTEGER_free(a);
  27285. a = NULL;
  27286. p = longDer;
  27287. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27288. p = longDer;
  27289. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27290. ExpectPtrNE(p, longDer);
  27291. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27292. ASN1_INTEGER_free(dup);
  27293. ASN1_INTEGER_free(a);
  27294. #endif
  27295. return EXPECT_RESULT();
  27296. }
  27297. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27298. {
  27299. EXPECT_DECLS;
  27300. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27301. ASN1_INTEGER* a = NULL;
  27302. ASN1_INTEGER* b = NULL;
  27303. ExpectNotNull(a = ASN1_INTEGER_new());
  27304. ExpectNotNull(b = ASN1_INTEGER_new());
  27305. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27306. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27307. /* Invalid parameter testing. */
  27308. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27309. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27310. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27311. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27312. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27313. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27314. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27315. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27316. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27317. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27318. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27319. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27320. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27321. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27322. ASN1_INTEGER_free(b);
  27323. ASN1_INTEGER_free(a);
  27324. #endif
  27325. return EXPECT_RESULT();
  27326. }
  27327. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27328. {
  27329. EXPECT_DECLS;
  27330. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27331. ASN1_INTEGER* ai = NULL;
  27332. ASN1_INTEGER* ai2 = NULL;
  27333. BIGNUM* bn = NULL;
  27334. BIGNUM* bn2 = NULL;
  27335. ExpectNotNull(ai = ASN1_INTEGER_new());
  27336. ExpectNotNull(bn2 = BN_new());
  27337. /* Invalid parameter testing. */
  27338. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27339. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27340. /* at the moment hard setting since no set function */
  27341. if (ai != NULL) {
  27342. ai->data[0] = 0xff; /* No DER encoding. */
  27343. ai->length = 1;
  27344. }
  27345. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27346. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27347. BN_free(bn);
  27348. bn = NULL;
  27349. #else
  27350. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27351. #endif
  27352. if (ai != NULL) {
  27353. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27354. ai->data[1] = 0x04; /* bad length of integer */
  27355. ai->data[2] = 0x03;
  27356. ai->length = 3;
  27357. }
  27358. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27359. /* Interpreted as a number 0x020403. */
  27360. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27361. BN_free(bn);
  27362. bn = NULL;
  27363. #else
  27364. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27365. #endif
  27366. if (ai != NULL) {
  27367. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27368. ai->data[1] = 0x01; /* length of integer */
  27369. ai->data[2] = 0x03;
  27370. ai->length = 3;
  27371. }
  27372. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27373. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27374. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27375. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27376. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27377. if (ai != NULL) {
  27378. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27379. ai->data[1] = 0x02; /* length of integer */
  27380. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27381. ai->data[3] = 0xff;
  27382. ai->length = 4;
  27383. }
  27384. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27385. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27386. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27387. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27388. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27389. if (ai != NULL) {
  27390. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27391. ai->data[1] = 0x01; /* length of integer */
  27392. ai->data[2] = 0x00;
  27393. ai->length = 3;
  27394. }
  27395. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27396. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27397. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27398. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27399. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27400. if (ai != NULL) {
  27401. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27402. ai->data[1] = 0x01; /* length of integer */
  27403. ai->data[2] = 0x01;
  27404. ai->length = 3;
  27405. ai->negative = 1;
  27406. }
  27407. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27408. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27409. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27410. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27411. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27412. BN_free(bn2);
  27413. BN_free(bn);
  27414. ASN1_INTEGER_free(ai2);
  27415. ASN1_INTEGER_free(ai);
  27416. #endif
  27417. return EXPECT_RESULT();
  27418. }
  27419. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27420. {
  27421. EXPECT_DECLS;
  27422. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27423. ASN1_INTEGER *a = NULL;
  27424. long val;
  27425. ExpectNotNull(a = ASN1_INTEGER_new());
  27426. /* Invalid parameter testing. */
  27427. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27428. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27429. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27430. #else
  27431. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27432. #endif
  27433. ASN1_INTEGER_free(a);
  27434. a = NULL;
  27435. ExpectNotNull(a = ASN1_INTEGER_new());
  27436. val = 0;
  27437. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27438. ASN1_INTEGER_free(a);
  27439. a = NULL;
  27440. /* 0 */
  27441. ExpectNotNull(a = ASN1_INTEGER_new());
  27442. val = 0;
  27443. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27444. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27445. ASN1_INTEGER_free(a);
  27446. a = NULL;
  27447. /* 40 */
  27448. ExpectNotNull(a = ASN1_INTEGER_new());
  27449. val = 40;
  27450. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27451. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27452. ASN1_INTEGER_free(a);
  27453. a = NULL;
  27454. /* -40 */
  27455. ExpectNotNull(a = ASN1_INTEGER_new());
  27456. val = -40;
  27457. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27458. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27459. ASN1_INTEGER_free(a);
  27460. a = NULL;
  27461. /* 128 */
  27462. ExpectNotNull(a = ASN1_INTEGER_new());
  27463. val = 128;
  27464. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27465. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27466. ASN1_INTEGER_free(a);
  27467. a = NULL;
  27468. /* -128 */
  27469. ExpectNotNull(a = ASN1_INTEGER_new());
  27470. val = -128;
  27471. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27472. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27473. ASN1_INTEGER_free(a);
  27474. a = NULL;
  27475. /* 200 */
  27476. ExpectNotNull(a = ASN1_INTEGER_new());
  27477. val = 200;
  27478. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27479. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27480. ASN1_INTEGER_free(a);
  27481. a = NULL;
  27482. /* int max (2147483647) */
  27483. ExpectNotNull(a = ASN1_INTEGER_new());
  27484. val = 2147483647;
  27485. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27486. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27487. ASN1_INTEGER_free(a);
  27488. a = NULL;
  27489. /* int min (-2147483648) */
  27490. ExpectNotNull(a = ASN1_INTEGER_new());
  27491. val = -2147483647 - 1;
  27492. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27493. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27494. ASN1_INTEGER_free(a);
  27495. a = NULL;
  27496. /* long max positive */
  27497. ExpectNotNull(a = ASN1_INTEGER_new());
  27498. val = (long)(((unsigned long)-1) >> 1);
  27499. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27500. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27501. ASN1_INTEGER_free(a);
  27502. #endif
  27503. return EXPECT_RESULT();
  27504. }
  27505. #if defined(OPENSSL_EXTRA)
  27506. typedef struct ASN1IntTestVector {
  27507. const byte* der;
  27508. const size_t derSz;
  27509. const long value;
  27510. } ASN1IntTestVector;
  27511. #endif
  27512. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27513. {
  27514. EXPECT_DECLS;
  27515. #if defined(OPENSSL_EXTRA)
  27516. size_t i;
  27517. WOLFSSL_ASN1_INTEGER* a = NULL;
  27518. WOLFSSL_ASN1_INTEGER* b = NULL;
  27519. WOLFSSL_ASN1_INTEGER* c = NULL;
  27520. const byte* p = NULL;
  27521. byte* p2 = NULL;
  27522. byte* reEncoded = NULL;
  27523. int reEncodedSz = 0;
  27524. static const byte zeroDer[] = {
  27525. 0x02, 0x01, 0x00
  27526. };
  27527. static const byte oneDer[] = {
  27528. 0x02, 0x01, 0x01
  27529. };
  27530. static const byte negativeDer[] = {
  27531. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27532. };
  27533. static const byte positiveDer[] = {
  27534. 0x02, 0x03, 0x01, 0x00, 0x01
  27535. };
  27536. static const byte primeDer[] = {
  27537. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27538. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27539. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27540. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27541. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27542. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27543. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27544. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27545. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27546. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27547. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27548. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27549. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27550. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27551. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27552. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27553. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27554. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27555. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27556. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27557. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27558. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27559. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27560. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27561. };
  27562. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27563. static const ASN1IntTestVector testVectors[] = {
  27564. {zeroDer, sizeof(zeroDer), 0},
  27565. {oneDer, sizeof(oneDer), 1},
  27566. {negativeDer, sizeof(negativeDer), -4123123},
  27567. {positiveDer, sizeof(positiveDer), 65537},
  27568. {primeDer, sizeof(primeDer), 0}
  27569. };
  27570. static const size_t NUM_TEST_VECTORS =
  27571. sizeof(testVectors)/sizeof(testVectors[0]);
  27572. /* Check d2i error conditions */
  27573. /* NULL pointer to input. */
  27574. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27575. ExpectNull(b);
  27576. /* NULL input. */
  27577. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27578. ExpectNull(b);
  27579. /* 0 length. */
  27580. p = testVectors[0].der;
  27581. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27582. ExpectNull(b);
  27583. /* Negative length. */
  27584. p = testVectors[0].der;
  27585. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27586. ExpectNull(b);
  27587. /* Garbage DER input. */
  27588. p = garbageDer;
  27589. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27590. ExpectNull(b);
  27591. /* Check i2d error conditions */
  27592. /* NULL input. */
  27593. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27594. /* 0 length input data buffer (a->length == 0). */
  27595. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27596. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27597. if (a != NULL)
  27598. a->data = NULL;
  27599. /* NULL input data buffer. */
  27600. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27601. if (a != NULL) {
  27602. /* Reset a->data. */
  27603. a->data = a->intData;
  27604. }
  27605. /* Set a to valid value. */
  27606. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27607. /* NULL output buffer. */
  27608. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27609. wolfSSL_ASN1_INTEGER_free(a);
  27610. a = NULL;
  27611. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27612. p = testVectors[i].der;
  27613. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27614. testVectors[i].derSz));
  27615. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27616. if (testVectors[i].derSz <= sizeof(long)) {
  27617. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27618. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27619. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27620. wolfSSL_ASN1_INTEGER_free(c);
  27621. c = NULL;
  27622. }
  27623. /* Convert to DER without a pre-allocated output buffer. */
  27624. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27625. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27626. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27627. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27628. * output buffer pointer should be incremented just past the end of the
  27629. * encoded data. */
  27630. p2 = reEncoded;
  27631. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27632. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27633. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27634. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27635. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27636. reEncoded = NULL;
  27637. wolfSSL_ASN1_INTEGER_free(a);
  27638. a = NULL;
  27639. }
  27640. #endif /* OPENSSL_EXTRA */
  27641. return EXPECT_RESULT();
  27642. }
  27643. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27644. {
  27645. EXPECT_DECLS;
  27646. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27647. BIO* bio = NULL;
  27648. BIO* out = NULL;
  27649. BIO* fixed = NULL;
  27650. ASN1_INTEGER* ai = NULL;
  27651. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27652. char tmp[1024];
  27653. int tmpSz;
  27654. const char expected1[] = "123456";
  27655. const char expected2[] = "112345678912345678901234567890";
  27656. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27657. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27658. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27659. ExpectNotNull(ai = ASN1_INTEGER_new());
  27660. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27661. /* Invalid parameter testing. */
  27662. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27663. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27664. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27665. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27666. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27667. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27668. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27669. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27670. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27671. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27672. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27673. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27674. /* No data to read from BIO. */
  27675. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27676. /* read first line */
  27677. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27678. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27679. XMEMSET(tmp, 0, 1024);
  27680. tmpSz = BIO_read(out, tmp, 1024);
  27681. ExpectIntEQ(tmpSz, 6);
  27682. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27683. /* fail on second line (not % 2) */
  27684. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27685. /* read 3rd long line */
  27686. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27687. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27688. XMEMSET(tmp, 0, 1024);
  27689. tmpSz = BIO_read(out, tmp, 1024);
  27690. ExpectIntEQ(tmpSz, 30);
  27691. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27692. /* fail on empty line */
  27693. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27694. BIO_free(bio);
  27695. bio = NULL;
  27696. /* Make long integer, requiring dynamic memory, even longer. */
  27697. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27698. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27699. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27700. XMEMSET(tmp, 0, 1024);
  27701. tmpSz = BIO_read(out, tmp, 1024);
  27702. ExpectIntEQ(tmpSz, 48);
  27703. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27704. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27705. XMEMSET(tmp, 0, 1024);
  27706. tmpSz = BIO_read(out, tmp, 1024);
  27707. ExpectIntEQ(tmpSz, 56);
  27708. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27709. BIO_free(bio);
  27710. BIO_free(out);
  27711. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27712. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27713. /* Ensure there is 0 bytes available to write into. */
  27714. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27715. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27716. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27717. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27718. BIO_free(fixed);
  27719. ASN1_INTEGER_free(ai);
  27720. #endif
  27721. return EXPECT_RESULT();
  27722. }
  27723. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27724. {
  27725. EXPECT_DECLS;
  27726. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27727. ASN1_INTEGER *a = NULL;
  27728. unsigned char *pp = NULL,*tpp = NULL;
  27729. int ret = 0;
  27730. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27731. /* Invalid parameter testing. */
  27732. /* Set pp to an invalid value. */
  27733. pp = NULL;
  27734. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27735. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27736. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27737. /* 40 */
  27738. if (a != NULL) {
  27739. a->intData[0] = ASN_INTEGER;
  27740. a->intData[1] = 1;
  27741. a->intData[2] = 40;
  27742. }
  27743. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27744. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27745. DYNAMIC_TYPE_TMP_BUFFER));
  27746. tpp = pp;
  27747. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27748. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27749. tpp--;
  27750. ExpectIntEQ(*tpp, 40);
  27751. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27752. pp = NULL;
  27753. /* 128 */
  27754. if (a != NULL) {
  27755. a->intData[0] = ASN_INTEGER;
  27756. a->intData[1] = 1;
  27757. a->intData[2] = 128;
  27758. }
  27759. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27760. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27761. DYNAMIC_TYPE_TMP_BUFFER));
  27762. tpp = pp;
  27763. if (tpp != NULL) {
  27764. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27765. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27766. tpp--;
  27767. ExpectIntEQ(*(tpp--), 128);
  27768. ExpectIntEQ(*tpp, 0);
  27769. }
  27770. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27771. pp = NULL;
  27772. /* -40 */
  27773. if (a != NULL) {
  27774. a->intData[0] = ASN_INTEGER;
  27775. a->intData[1] = 1;
  27776. a->intData[2] = 40;
  27777. a->negative = 1;
  27778. }
  27779. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27780. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27781. DYNAMIC_TYPE_TMP_BUFFER));
  27782. tpp = pp;
  27783. if (tpp != NULL) {
  27784. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27785. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27786. tpp--;
  27787. ExpectIntEQ(*tpp, 216);
  27788. }
  27789. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27790. pp = NULL;
  27791. /* -128 */
  27792. if (a != NULL) {
  27793. a->intData[0] = ASN_INTEGER;
  27794. a->intData[1] = 1;
  27795. a->intData[2] = 128;
  27796. a->negative = 1;
  27797. }
  27798. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27799. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27800. DYNAMIC_TYPE_TMP_BUFFER));
  27801. tpp = pp;
  27802. if (tpp != NULL) {
  27803. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27804. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27805. tpp--;
  27806. ExpectIntEQ(*tpp, 128);
  27807. }
  27808. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27809. pp = NULL;
  27810. /* -200 */
  27811. if (a != NULL) {
  27812. a->intData[0] = ASN_INTEGER;
  27813. a->intData[1] = 1;
  27814. a->intData[2] = 200;
  27815. a->negative = 1;
  27816. }
  27817. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27818. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27819. DYNAMIC_TYPE_TMP_BUFFER));
  27820. tpp = pp;
  27821. if (tpp != NULL) {
  27822. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27823. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27824. tpp--;
  27825. ExpectIntEQ(*(tpp--), 56);
  27826. ExpectIntEQ(*tpp, 255);
  27827. }
  27828. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27829. pp = NULL;
  27830. /* Empty */
  27831. if (a != NULL) {
  27832. a->intData[0] = ASN_INTEGER;
  27833. a->intData[1] = 0;
  27834. a->negative = 0;
  27835. }
  27836. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27837. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27838. DYNAMIC_TYPE_TMP_BUFFER));
  27839. tpp = pp;
  27840. if (tpp != NULL) {
  27841. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27842. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27843. tpp--;
  27844. ExpectIntEQ(*tpp, 0);
  27845. }
  27846. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27847. pp = NULL;
  27848. /* 0 */
  27849. if (a != NULL) {
  27850. a->intData[0] = ASN_INTEGER;
  27851. a->intData[1] = 1;
  27852. a->intData[2] = 0;
  27853. a->negative = 1;
  27854. }
  27855. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27856. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27857. DYNAMIC_TYPE_TMP_BUFFER));
  27858. if (tpp != NULL) {
  27859. tpp = pp;
  27860. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27861. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27862. tpp--;
  27863. ExpectIntEQ(*tpp, 0);
  27864. }
  27865. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27866. pp = NULL;
  27867. /* 0x100 */
  27868. if (a != NULL) {
  27869. a->intData[0] = ASN_INTEGER;
  27870. a->intData[1] = 2;
  27871. a->intData[2] = 0x01;
  27872. a->intData[3] = 0x00;
  27873. a->negative = 0;
  27874. }
  27875. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27876. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27877. DYNAMIC_TYPE_TMP_BUFFER));
  27878. if (tpp != NULL) {
  27879. tpp = pp;
  27880. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27881. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27882. tpp -= 2;
  27883. ExpectIntEQ(tpp[0], 0x01);
  27884. ExpectIntEQ(tpp[1], 0x00);
  27885. }
  27886. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27887. pp = NULL;
  27888. /* -0x8000 => 0x8000 */
  27889. if (a != NULL) {
  27890. a->intData[0] = ASN_INTEGER;
  27891. a->intData[1] = 2;
  27892. a->intData[2] = 0x80;
  27893. a->intData[3] = 0x00;
  27894. a->negative = 1;
  27895. }
  27896. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27897. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27898. DYNAMIC_TYPE_TMP_BUFFER));
  27899. tpp = pp;
  27900. if (tpp != NULL) {
  27901. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27902. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27903. tpp -= 2;
  27904. ExpectIntEQ(tpp[0], 0x80);
  27905. ExpectIntEQ(tpp[1], 0x00);
  27906. }
  27907. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27908. pp = NULL;
  27909. /* -0x8001 => 0xFF7FFF */
  27910. if (a != NULL) {
  27911. a->intData[0] = ASN_INTEGER;
  27912. a->intData[1] = 2;
  27913. a->intData[2] = 0x80;
  27914. a->intData[3] = 0x01;
  27915. a->negative = 1;
  27916. }
  27917. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27918. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27919. DYNAMIC_TYPE_TMP_BUFFER));
  27920. tpp = pp;
  27921. if (tpp != NULL) {
  27922. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27923. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27924. tpp -= 3;
  27925. ExpectIntEQ(tpp[0], 0xFF);
  27926. ExpectIntEQ(tpp[1], 0x7F);
  27927. ExpectIntEQ(tpp[2], 0xFF);
  27928. }
  27929. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27930. wolfSSL_ASN1_INTEGER_free(a);
  27931. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27932. return EXPECT_RESULT();
  27933. }
  27934. static int test_wolfSSL_ASN1_OBJECT(void)
  27935. {
  27936. EXPECT_DECLS;
  27937. #if defined(OPENSSL_EXTRA)
  27938. ASN1_OBJECT* a = NULL;
  27939. ASN1_OBJECT s;
  27940. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27941. /* Invalid parameter testing. */
  27942. ASN1_OBJECT_free(NULL);
  27943. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27944. /* Test that a static ASN1_OBJECT can be freed. */
  27945. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27946. ASN1_OBJECT_free(&s);
  27947. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27948. ASN1_OBJECT_free(a);
  27949. a = NULL;
  27950. s.obj = der;
  27951. s.objSz = sizeof(der);
  27952. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27953. ASN1_OBJECT_free(a);
  27954. ASN1_OBJECT_free(&s);
  27955. #endif /* OPENSSL_EXTRA */
  27956. return EXPECT_RESULT();
  27957. }
  27958. static int test_wolfSSL_ASN1_get_object(void)
  27959. {
  27960. EXPECT_DECLS;
  27961. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27962. const unsigned char* derBuf = cliecc_cert_der_256;
  27963. const unsigned char* nullPtr = NULL;
  27964. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27965. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27966. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27967. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27968. const unsigned char* p;
  27969. unsigned char objDer[8];
  27970. unsigned char* der;
  27971. unsigned char* derPtr;
  27972. int len = sizeof_cliecc_cert_der_256;
  27973. long asnLen = 0;
  27974. int tag = 0;
  27975. int cls = 0;
  27976. ASN1_OBJECT* a = NULL;
  27977. ASN1_OBJECT s;
  27978. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27979. /* Invalid encoding at length. */
  27980. p = objDerInvalidLen;
  27981. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27982. 0x80);
  27983. p = objDerBadLen;
  27984. /* Error = 0x80, Constructed = 0x20 */
  27985. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27986. 0x80 | 0x20);
  27987. /* Read a couple TLV triplets and make sure they match the expected values
  27988. */
  27989. /* SEQUENCE */
  27990. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27991. ExpectIntEQ(asnLen, 861);
  27992. ExpectIntEQ(tag, 0x10);
  27993. ExpectIntEQ(cls, 0);
  27994. /* SEQUENCE */
  27995. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27996. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27997. ExpectIntEQ(asnLen, 772);
  27998. ExpectIntEQ(tag, 0x10);
  27999. ExpectIntEQ(cls, 0);
  28000. /* [0] */
  28001. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28002. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28003. ExpectIntEQ(asnLen, 3);
  28004. ExpectIntEQ(tag, 0);
  28005. ExpectIntEQ(cls, 0x80);
  28006. /* INTEGER */
  28007. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28008. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28009. ExpectIntEQ(asnLen, 1);
  28010. ExpectIntEQ(tag, 0x2);
  28011. ExpectIntEQ(cls, 0);
  28012. derBuf += asnLen;
  28013. /* INTEGER */
  28014. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28015. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28016. ExpectIntEQ(asnLen, 20);
  28017. ExpectIntEQ(tag, 0x2);
  28018. ExpectIntEQ(cls, 0);
  28019. derBuf += asnLen;
  28020. /* SEQUENCE */
  28021. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  28022. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  28023. ExpectIntEQ(asnLen, 10);
  28024. ExpectIntEQ(tag, 0x10);
  28025. ExpectIntEQ(cls, 0);
  28026. /* Found OBJECT_ID. */
  28027. /* Invalid parameter testing. */
  28028. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  28029. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  28030. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  28031. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  28032. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  28033. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  28034. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  28035. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  28036. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  28037. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  28038. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28039. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28040. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  28041. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  28042. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  28043. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  28044. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  28045. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  28046. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  28047. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  28048. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  28049. /* Invalid encoding at length. */
  28050. p = objDerInvalidLen;
  28051. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  28052. p = objDerBadLen;
  28053. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  28054. p = objDerNotObj;
  28055. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  28056. p = objDerNoData;
  28057. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  28058. /* Create an ASN OBJECT from content */
  28059. p = derBuf + 2;
  28060. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  28061. ASN1_OBJECT_free(a);
  28062. a = NULL;
  28063. /* Create an ASN OBJECT from DER */
  28064. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  28065. /* Invalid parameter testing. */
  28066. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  28067. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  28068. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  28069. der = NULL;
  28070. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  28071. derPtr = objDer;
  28072. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  28073. ExpectPtrNE(derPtr, objDer);
  28074. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  28075. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  28076. ASN1_OBJECT_free(a);
  28077. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  28078. return EXPECT_RESULT();
  28079. }
  28080. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  28081. {
  28082. EXPECT_DECLS;
  28083. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28084. ASN1_OBJECT* obj = NULL;
  28085. ASN1_OBJECT* a = NULL;
  28086. BIO *bio = NULL;
  28087. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  28088. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  28089. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  28090. const unsigned char* p;
  28091. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  28092. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  28093. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  28094. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  28095. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  28096. /* No DER encoding in ASN1_OBJECT. */
  28097. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  28098. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28099. ASN1_OBJECT_free(a);
  28100. a = NULL;
  28101. /* DER encoding - not OBJECT_ID */
  28102. p = notObjDer;
  28103. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28104. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28105. ASN1_OBJECT_free(a);
  28106. a = NULL;
  28107. /* Bad length encoding. */
  28108. p = badLenDer;
  28109. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28110. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28111. ASN1_OBJECT_free(a);
  28112. a = NULL;
  28113. /* Good encoding - but unknown. */
  28114. p = goodDer;
  28115. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28116. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28117. ASN1_OBJECT_free(a);
  28118. BIO_free(bio);
  28119. ASN1_OBJECT_free(obj);
  28120. #endif
  28121. return EXPECT_RESULT();
  28122. }
  28123. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  28124. {
  28125. EXPECT_DECLS;
  28126. #if defined(OPENSSL_EXTRA) && \
  28127. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  28128. char buf[50] = {0};
  28129. ASN1_OBJECT* obj;
  28130. const char* oid = "2.5.29.19";
  28131. const char* ln = "X509v3 Basic Constraints";
  28132. obj = NULL;
  28133. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  28134. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  28135. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  28136. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  28137. XMEMSET(buf, 0, sizeof(buf));
  28138. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  28139. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  28140. ASN1_OBJECT_free(obj);
  28141. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  28142. return EXPECT_RESULT();
  28143. }
  28144. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  28145. {
  28146. EXPECT_DECLS;
  28147. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  28148. WOLFSSL_STACK* sk = NULL;
  28149. WOLFSSL_ASN1_OBJECT* obj;
  28150. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28151. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28152. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28153. sk = NULL;
  28154. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28155. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  28156. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  28157. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  28158. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28159. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28160. sk = NULL;
  28161. /* obj freed in pop_free call. */
  28162. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28163. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28164. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28165. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28166. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28167. wolfSSL_ASN1_OBJECT_free(obj);
  28168. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28169. return EXPECT_RESULT();
  28170. }
  28171. static int test_wolfSSL_ASN1_STRING(void)
  28172. {
  28173. EXPECT_DECLS;
  28174. #if defined(OPENSSL_EXTRA)
  28175. ASN1_STRING* str = NULL;
  28176. ASN1_STRING* c = NULL;
  28177. const char data[] = "hello wolfSSL";
  28178. const char data2[] = "Same len data";
  28179. const char longData[] =
  28180. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28181. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28182. ASN1_STRING_free(str);
  28183. str = NULL;
  28184. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28185. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28186. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  28187. /* Check setting to NULL works. */
  28188. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28189. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28190. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28191. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28192. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28193. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28194. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28195. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28196. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  28197. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28198. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28199. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28200. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28201. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28202. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28203. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  28204. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28205. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28206. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28207. /* Check setting back to smaller size frees dynamic data. */
  28208. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28209. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  28210. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  28211. ExpectNull(ASN1_STRING_get0_data(NULL));
  28212. ExpectNotNull(ASN1_STRING_get0_data(str));
  28213. ExpectNull(ASN1_STRING_data(NULL));
  28214. ExpectNotNull(ASN1_STRING_data(str));
  28215. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  28216. ExpectIntGT(ASN1_STRING_length(str), 0);
  28217. ASN1_STRING_free(c);
  28218. ASN1_STRING_free(str);
  28219. ASN1_STRING_free(NULL);
  28220. #ifndef NO_WOLFSSL_STUB
  28221. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28222. #endif
  28223. #endif
  28224. return EXPECT_RESULT();
  28225. }
  28226. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28227. {
  28228. EXPECT_DECLS;
  28229. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  28230. !defined(NO_FILESYSTEM)
  28231. WOLFSSL_X509* x509 = NULL;
  28232. WOLFSSL_X509_NAME* subject = NULL;
  28233. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  28234. WOLFSSL_ASN1_STRING* a = NULL;
  28235. FILE* file = XBADFILE;
  28236. int idx = 0;
  28237. char targetOutput[16] = "www.wolfssl.com";
  28238. unsigned char* actual_output = NULL;
  28239. int len = 0;
  28240. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28241. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28242. if (file != NULL)
  28243. fclose(file);
  28244. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28245. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28246. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28247. NID_commonName, -1)), 5);
  28248. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28249. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28250. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28251. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0);
  28252. a = NULL;
  28253. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28254. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28255. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28256. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28257. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28258. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28259. wolfSSL_X509_free(x509);
  28260. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28261. ExpectNotNull(a = ASN1_STRING_new());
  28262. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28263. ASN1_STRING_free(a);
  28264. #endif
  28265. return EXPECT_RESULT();
  28266. }
  28267. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28268. {
  28269. EXPECT_DECLS;
  28270. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28271. WOLFSSL_ASN1_STRING* str = NULL;
  28272. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28273. char* ret = NULL;
  28274. ExpectNotNull(str = ASN1_STRING_new());
  28275. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28276. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28277. ret = NULL;
  28278. /* No data. */
  28279. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28280. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28281. ret = NULL;
  28282. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28283. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28284. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28285. ret = NULL;
  28286. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28287. /* No type. */
  28288. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28289. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28290. ASN1_STRING_free(str);
  28291. #endif
  28292. return EXPECT_RESULT();
  28293. }
  28294. static int test_wolfSSL_ASN1_STRING_canon(void)
  28295. {
  28296. EXPECT_DECLS;
  28297. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28298. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28299. defined(OPENSSL_EXTRA_X509_SMALL))
  28300. WOLFSSL_ASN1_STRING* orig = NULL;
  28301. WOLFSSL_ASN1_STRING* canon = NULL;
  28302. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28303. const char* whitespaceOnly = "\t\r\n";
  28304. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28305. const char* canonData = "\x01 \x02 \xff tt";
  28306. const char longData[] =
  28307. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28308. ExpectNotNull(orig = ASN1_STRING_new());
  28309. ExpectNotNull(canon = ASN1_STRING_new());
  28310. /* Invalid parameter testing. */
  28311. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28312. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28313. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28314. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28315. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28316. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28317. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28318. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28319. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28320. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28321. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28322. ASN1_STRING_free(orig);
  28323. orig = NULL;
  28324. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28325. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28326. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28327. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28328. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28329. ASN1_STRING_free(orig);
  28330. orig = NULL;
  28331. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28332. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28333. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28334. ASN1_STRING_free(orig);
  28335. orig = NULL;
  28336. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28337. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28338. ASN1_STRING_free(orig);
  28339. ASN1_STRING_free(canon);
  28340. #endif
  28341. #endif
  28342. return EXPECT_RESULT();
  28343. }
  28344. static int test_wolfSSL_ASN1_STRING_print(void)
  28345. {
  28346. EXPECT_DECLS;
  28347. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28348. !defined(NO_BIO)
  28349. ASN1_STRING* asnStr = NULL;
  28350. const char HELLO_DATA[]= \
  28351. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28352. #define MAX_UNPRINTABLE_CHAR 32
  28353. #define MAX_BUF 255
  28354. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28355. unsigned char expected[sizeof(unprintableData)+1];
  28356. unsigned char rbuf[MAX_BUF];
  28357. BIO *bio = NULL;
  28358. int p_len;
  28359. int i;
  28360. /* setup */
  28361. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28362. unprintableData[i] = (unsigned char)HELLO_DATA[i];
  28363. expected[i] = (unsigned char)HELLO_DATA[i];
  28364. }
  28365. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28366. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28367. if (i == (int)'\n' || i == (int)'\r')
  28368. expected[sizeof(HELLO_DATA)+i] = i;
  28369. else
  28370. expected[sizeof(HELLO_DATA)+i] = '.';
  28371. }
  28372. unprintableData[sizeof(unprintableData)-1] = '\0';
  28373. expected[sizeof(expected)-1] = '\0';
  28374. XMEMSET(rbuf, 0, MAX_BUF);
  28375. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28376. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28377. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28378. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28379. (int)sizeof(unprintableData)), 1);
  28380. /* test */
  28381. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28382. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28383. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28384. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28385. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28386. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28387. BIO_free(bio);
  28388. bio = NULL;
  28389. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28390. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28391. /* Ensure there is 0 bytes available to write into. */
  28392. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28393. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28394. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28395. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28396. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28397. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28398. BIO_free(bio);
  28399. ASN1_STRING_free(asnStr);
  28400. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28401. return EXPECT_RESULT();
  28402. }
  28403. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28404. {
  28405. EXPECT_DECLS;
  28406. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28407. ASN1_STRING* asn_str = NULL;
  28408. const char data[] = "Hello wolfSSL!";
  28409. ASN1_STRING* esc_str = NULL;
  28410. const char esc_data[] = "a+;<>";
  28411. ASN1_STRING* neg_int = NULL;
  28412. const char neg_int_data[] = "\xff";
  28413. ASN1_STRING* neg_enum = NULL;
  28414. const char neg_enum_data[] = "\xff";
  28415. BIO *bio = NULL;
  28416. BIO *fixed = NULL;
  28417. unsigned long flags;
  28418. int p_len;
  28419. unsigned char rbuf[255];
  28420. /* setup */
  28421. XMEMSET(rbuf, 0, 255);
  28422. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28423. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28424. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28425. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28426. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28427. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28428. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28429. sizeof(esc_data)), 1);
  28430. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28431. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28432. sizeof(neg_int_data) - 1), 1);
  28433. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28434. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28435. sizeof(neg_enum_data) - 1), 1);
  28436. /* Invalid parameter testing. */
  28437. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28438. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28439. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28440. /* no flags */
  28441. XMEMSET(rbuf, 0, 255);
  28442. flags = 0;
  28443. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28444. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28445. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28446. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28447. /* Ensure there is 0 bytes available to write into. */
  28448. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28449. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28450. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28451. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28452. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28453. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28454. /* RFC2253 Escape */
  28455. XMEMSET(rbuf, 0, 255);
  28456. flags = ASN1_STRFLGS_ESC_2253;
  28457. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28458. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28459. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28460. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28461. /* Ensure there is 0 bytes available to write into. */
  28462. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28463. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28464. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28465. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28466. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28467. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28468. /* Show type */
  28469. XMEMSET(rbuf, 0, 255);
  28470. flags = ASN1_STRFLGS_SHOW_TYPE;
  28471. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28472. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28473. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28474. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28475. /* Ensure there is 0 bytes available to write into. */
  28476. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28477. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28478. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28479. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28480. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28481. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28482. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28483. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28484. /* Dump All */
  28485. XMEMSET(rbuf, 0, 255);
  28486. flags = ASN1_STRFLGS_DUMP_ALL;
  28487. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28488. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28489. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28490. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28491. /* Ensure there is 0 bytes available to write into. */
  28492. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28493. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28494. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28495. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28496. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28497. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28498. /* Dump Der */
  28499. XMEMSET(rbuf, 0, 255);
  28500. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28501. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28502. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28503. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28504. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28505. /* Ensure there is 0 bytes available to write into. */
  28506. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28507. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28508. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28509. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28510. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28511. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28512. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28513. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28514. /* Dump All + Show type */
  28515. XMEMSET(rbuf, 0, 255);
  28516. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28517. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28518. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28519. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28520. /* Dump All + Show type - Negative Integer. */
  28521. XMEMSET(rbuf, 0, 255);
  28522. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28523. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28524. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28525. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28526. /* Dump All + Show type - Negative Enumerated. */
  28527. XMEMSET(rbuf, 0, 255);
  28528. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28529. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28530. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28531. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28532. BIO_free(fixed);
  28533. BIO_free(bio);
  28534. ASN1_STRING_free(asn_str);
  28535. ASN1_STRING_free(esc_str);
  28536. ASN1_STRING_free(neg_int);
  28537. ASN1_STRING_free(neg_enum);
  28538. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28539. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28540. #endif
  28541. return EXPECT_RESULT();
  28542. }
  28543. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28544. {
  28545. EXPECT_DECLS;
  28546. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28547. ASN1_STRING* asn1str_test = NULL;
  28548. ASN1_STRING* asn1str_answer = NULL;
  28549. /* Each character is encoded using 4 bytes */
  28550. char input[] = {
  28551. 0, 0, 0, 'T',
  28552. 0, 0, 0, 'e',
  28553. 0, 0, 0, 's',
  28554. 0, 0, 0, 't',
  28555. };
  28556. char output[] = "Test";
  28557. char badInput[] = {
  28558. 1, 0, 0, 'T',
  28559. 0, 1, 0, 'e',
  28560. 0, 0, 1, 's',
  28561. };
  28562. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28563. /* Test wrong type. */
  28564. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28565. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28566. ASN1_STRING_free(asn1str_test);
  28567. asn1str_test = NULL;
  28568. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28569. /* Test bad length. */
  28570. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28571. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28572. /* Test bad input. */
  28573. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28574. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28575. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28576. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28577. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28578. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28579. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28580. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28581. ExpectNotNull(
  28582. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28583. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28584. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28585. ASN1_STRING_free(asn1str_test);
  28586. ASN1_STRING_free(asn1str_answer);
  28587. #endif /* OPENSSL_ALL && !NO_ASN */
  28588. return EXPECT_RESULT();
  28589. }
  28590. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28591. {
  28592. EXPECT_DECLS;
  28593. #if defined(OPENSSL_EXTRA)
  28594. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28595. ExpectNotNull(asn1_gtime = ASN1_GENERALIZEDTIME_new());
  28596. if (asn1_gtime != NULL)
  28597. XMEMCPY(asn1_gtime->data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28598. ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28599. #endif /* OPENSSL_EXTRA */
  28600. return EXPECT_RESULT();
  28601. }
  28602. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28603. {
  28604. EXPECT_DECLS;
  28605. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28606. WOLFSSL_ASN1_GENERALIZEDTIME* gtime = NULL;
  28607. BIO* bio = NULL;
  28608. unsigned char buf[24];
  28609. int i;
  28610. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28611. BIO_set_write_buf_size(bio, 24);
  28612. ExpectNotNull(gtime = ASN1_GENERALIZEDTIME_new());
  28613. /* Type not set. */
  28614. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28615. ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
  28616. /* Invalid parameters testing. */
  28617. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28618. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28619. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), BAD_FUNC_ARG);
  28620. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
  28621. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28622. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28623. BIO_free(bio);
  28624. bio = NULL;
  28625. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28626. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28627. /* Ensure there is 0 bytes available to write into. */
  28628. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28629. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28630. for (i = 1; i < 20; i++) {
  28631. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28632. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28633. }
  28634. BIO_free(bio);
  28635. wolfSSL_ASN1_GENERALIZEDTIME_free(gtime);
  28636. #endif /* OPENSSL_EXTRA */
  28637. return EXPECT_RESULT();
  28638. }
  28639. static int test_wolfSSL_ASN1_TIME(void)
  28640. {
  28641. EXPECT_DECLS;
  28642. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28643. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28644. unsigned char *data;
  28645. ExpectNotNull(asn_time = ASN1_TIME_new());
  28646. #ifndef NO_WOLFSSL_STUB
  28647. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28648. #endif
  28649. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28650. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28651. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28652. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28653. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28654. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28655. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28656. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28657. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28658. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28659. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28660. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28661. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28662. ASN1_TIME_free(asn_time);
  28663. ASN1_TIME_free(NULL);
  28664. #endif
  28665. return EXPECT_RESULT();
  28666. }
  28667. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28668. {
  28669. EXPECT_DECLS;
  28670. #ifndef NO_ASN_TIME
  28671. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28672. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28673. WOLFSSL_ASN1_TIME* t = NULL;
  28674. char buf[ASN_GENERALIZED_TIME_SIZE];
  28675. ExpectNotNull((t = ASN1_TIME_new()));
  28676. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28677. /* Invalid parameter testing. */
  28678. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28679. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28680. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28681. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28682. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28683. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28684. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28685. /* Buffer needs to be longer than minimum of 5 characters. */
  28686. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28687. ASN1_TIME_free(t);
  28688. #endif
  28689. #endif /* NO_ASN_TIME */
  28690. return EXPECT_RESULT();
  28691. }
  28692. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28693. {
  28694. EXPECT_DECLS;
  28695. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28696. ASN1_TIME* fromTime = NULL;
  28697. ASN1_TIME* closeToTime = NULL;
  28698. ASN1_TIME* toTime = NULL;
  28699. ASN1_TIME* invalidTime = NULL;
  28700. int daysDiff = 0;
  28701. int secsDiff = 0;
  28702. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28703. /* Feb 22, 2003, 21:15:15 */
  28704. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28705. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28706. /* Feb 22, 2003, 21:16:15 */
  28707. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28708. ExpectNotNull((toTime = ASN1_TIME_new()));
  28709. /* Dec 19, 2010, 18:10:11 */
  28710. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28711. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28712. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28713. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28714. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28715. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28716. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28717. /* Error conditions. */
  28718. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28719. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28720. /* If both times are NULL, difference is 0. */
  28721. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28722. ExpectIntEQ(daysDiff, 0);
  28723. ExpectIntEQ(secsDiff, 0);
  28724. /* If one time is NULL, it defaults to the current time. */
  28725. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28726. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28727. /* Normal operation. Both times non-NULL. */
  28728. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28729. ExpectIntEQ(daysDiff, 2856);
  28730. ExpectIntEQ(secsDiff, 75296);
  28731. /* Swapping the times should return negative values. */
  28732. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28733. ExpectIntEQ(daysDiff, -2856);
  28734. ExpectIntEQ(secsDiff, -75296);
  28735. /* Compare with invalid time string. */
  28736. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28737. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28738. /* Compare with days difference of 0. */
  28739. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28740. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28741. /* Days and seconds differences not 0. */
  28742. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28743. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28744. /* Same time. */
  28745. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28746. /* Compare regression test: No seconds difference, just difference in days.
  28747. */
  28748. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28749. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28750. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28751. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28752. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28753. /* Edge case with Unix epoch. */
  28754. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28755. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28756. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28757. ExpectIntEQ(daysDiff, 3652);
  28758. ExpectIntEQ(secsDiff, 0);
  28759. /* Edge case with year > 2038 (year 2038 problem). */
  28760. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28761. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28762. ExpectIntEQ(daysDiff, 2932896);
  28763. ExpectIntEQ(secsDiff, 86399);
  28764. ASN1_TIME_free(fromTime);
  28765. ASN1_TIME_free(closeToTime);
  28766. ASN1_TIME_free(toTime);
  28767. ASN1_TIME_free(invalidTime);
  28768. #endif
  28769. return EXPECT_RESULT();
  28770. }
  28771. static int test_wolfSSL_ASN1_TIME_adj(void)
  28772. {
  28773. EXPECT_DECLS;
  28774. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28775. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28776. const int year = 365*24*60*60;
  28777. const int day = 24*60*60;
  28778. const int hour = 60*60;
  28779. const int mini = 60;
  28780. const byte asn_utc_time = ASN_UTC_TIME;
  28781. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28782. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28783. #endif
  28784. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28785. WOLFSSL_ASN1_TIME* s = NULL;
  28786. int offset_day;
  28787. long offset_sec;
  28788. char date_str[CTC_DATE_SIZE + 1];
  28789. time_t t;
  28790. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28791. /* UTC notation test */
  28792. /* 2000/2/15 20:30:00 */
  28793. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28794. offset_day = 7;
  28795. offset_sec = 45 * mini;
  28796. /* offset_sec = -45 * min;*/
  28797. ExpectNotNull(asn_time =
  28798. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28799. ExpectTrue(asn_time->type == asn_utc_time);
  28800. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28801. CTC_DATE_SIZE));
  28802. date_str[CTC_DATE_SIZE] = '\0';
  28803. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28804. /* negative offset */
  28805. offset_sec = -45 * mini;
  28806. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28807. ExpectNotNull(asn_time);
  28808. ExpectTrue(asn_time->type == asn_utc_time);
  28809. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28810. CTC_DATE_SIZE));
  28811. date_str[CTC_DATE_SIZE] = '\0';
  28812. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28813. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28814. s = NULL;
  28815. XMEMSET(date_str, 0, sizeof(date_str));
  28816. /* Generalized time will overflow time_t if not long */
  28817. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28818. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28819. DYNAMIC_TYPE_OPENSSL);
  28820. /* GeneralizedTime notation test */
  28821. /* 2055/03/01 09:00:00 */
  28822. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28823. offset_day = 12;
  28824. offset_sec = 10 * mini;
  28825. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28826. offset_sec));
  28827. ExpectTrue(asn_time->type == asn_gen_time);
  28828. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28829. CTC_DATE_SIZE));
  28830. date_str[CTC_DATE_SIZE] = '\0';
  28831. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28832. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28833. s = NULL;
  28834. XMEMSET(date_str, 0, sizeof(date_str));
  28835. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28836. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28837. s = NULL;
  28838. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28839. offset_day = 7;
  28840. offset_sec = 45 * mini;
  28841. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28842. offset_sec));
  28843. ExpectTrue(asn_time->type == asn_utc_time);
  28844. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28845. CTC_DATE_SIZE));
  28846. date_str[CTC_DATE_SIZE] = '\0';
  28847. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28848. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28849. asn_time = NULL;
  28850. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28851. offset_sec));
  28852. ExpectTrue(asn_time->type == asn_utc_time);
  28853. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28854. CTC_DATE_SIZE));
  28855. date_str[CTC_DATE_SIZE] = '\0';
  28856. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28857. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28858. #endif
  28859. return EXPECT_RESULT();
  28860. }
  28861. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28862. {
  28863. EXPECT_DECLS;
  28864. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28865. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28866. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28867. ASN1_TIME asnTime;
  28868. struct tm tm;
  28869. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28870. XMEMSET(&tm, 0, sizeof(struct tm));
  28871. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28872. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28873. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28874. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28875. ExpectIntEQ(tm.tm_sec, 15);
  28876. ExpectIntEQ(tm.tm_min, 15);
  28877. ExpectIntEQ(tm.tm_hour, 21);
  28878. ExpectIntEQ(tm.tm_mday, 22);
  28879. ExpectIntEQ(tm.tm_mon, 1);
  28880. ExpectIntEQ(tm.tm_year, 100);
  28881. ExpectIntEQ(tm.tm_isdst, 0);
  28882. #ifdef XMKTIME
  28883. ExpectIntEQ(tm.tm_wday, 2);
  28884. ExpectIntEQ(tm.tm_yday, 52);
  28885. #endif
  28886. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28887. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28888. ExpectIntEQ(tm.tm_year, 50);
  28889. /* Get current time. */
  28890. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28891. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28892. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28893. /* 0 length. */
  28894. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28895. /* No type. */
  28896. asnTime.length = 1;
  28897. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28898. /* Not UTCTIME length. */
  28899. asnTime.type = V_ASN1_UTCTIME;
  28900. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28901. /* Not GENERALIZEDTIME length. */
  28902. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28903. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28904. /* Not Zulu timezone. */
  28905. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28906. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28907. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28908. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28909. #ifdef XMKTIME
  28910. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28911. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28912. ExpectIntEQ(tm.tm_sec, 47);
  28913. ExpectIntEQ(tm.tm_min, 22);
  28914. ExpectIntEQ(tm.tm_hour, 21);
  28915. ExpectIntEQ(tm.tm_mday, 12);
  28916. ExpectIntEQ(tm.tm_mon, 4);
  28917. ExpectIntEQ(tm.tm_year, 123);
  28918. ExpectIntEQ(tm.tm_wday, 5);
  28919. ExpectIntEQ(tm.tm_yday, 131);
  28920. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28921. fields are zeroed out as expected. */
  28922. ExpectIntEQ(tm.tm_isdst, 0);
  28923. #endif
  28924. #endif
  28925. return EXPECT_RESULT();
  28926. }
  28927. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28928. {
  28929. EXPECT_DECLS;
  28930. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28931. WOLFSSL_ASN1_TIME *t = NULL;
  28932. WOLFSSL_ASN1_TIME *out = NULL;
  28933. WOLFSSL_ASN1_TIME *gtime = NULL;
  28934. int tlen = 0;
  28935. unsigned char *data = NULL;
  28936. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28937. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28938. /* type not set. */
  28939. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28940. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28941. t = NULL;
  28942. /* UTC Time test */
  28943. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28944. if (t != NULL) {
  28945. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28946. t->type = ASN_UTC_TIME;
  28947. t->length = ASN_UTC_TIME_SIZE;
  28948. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28949. }
  28950. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28951. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28952. out = NULL;
  28953. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28954. wolfSSL_ASN1_TIME_free(gtime);
  28955. gtime = NULL;
  28956. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28957. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28958. ExpectPtrEq(gtime, out);
  28959. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28960. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28961. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28962. /* Generalized Time test */
  28963. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28964. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28965. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28966. if (t != NULL) {
  28967. t->type = ASN_GENERALIZED_TIME;
  28968. t->length = ASN_GENERALIZED_TIME_SIZE;
  28969. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28970. }
  28971. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28972. ASN_GENERALIZED_TIME_SIZE);
  28973. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28974. "20050727123456Z");
  28975. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28976. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28977. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28978. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28979. /* UTC Time to Generalized Time 1900's test */
  28980. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28981. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28982. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28983. if (t != NULL) {
  28984. t->type = ASN_UTC_TIME;
  28985. t->length = ASN_UTC_TIME_SIZE;
  28986. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28987. }
  28988. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28989. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28990. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28991. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28992. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28993. /* Null parameter test */
  28994. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28995. gtime = NULL;
  28996. out = NULL;
  28997. if (t != NULL) {
  28998. t->type = ASN_UTC_TIME;
  28999. t->length = ASN_UTC_TIME_SIZE;
  29000. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  29001. }
  29002. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  29003. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  29004. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  29005. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  29006. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29007. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29008. #endif
  29009. return EXPECT_RESULT();
  29010. }
  29011. static int test_wolfSSL_ASN1_TIME_print(void)
  29012. {
  29013. EXPECT_DECLS;
  29014. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  29015. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  29016. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  29017. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  29018. !defined(NO_ASN_TIME)
  29019. BIO* bio = NULL;
  29020. BIO* fixed = NULL;
  29021. X509* x509 = NULL;
  29022. const unsigned char* der = client_cert_der_2048;
  29023. ASN1_TIME* notAfter = NULL;
  29024. ASN1_TIME* notBefore = NULL;
  29025. unsigned char buf[25];
  29026. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29027. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29028. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  29029. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  29030. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  29031. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  29032. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  29033. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  29034. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  29035. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29036. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  29037. /* Test BIO_write fails. */
  29038. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29039. /* Ensure there is 0 bytes available to write into. */
  29040. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  29041. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29042. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29043. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29044. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  29045. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  29046. /* create a bad time and test results */
  29047. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  29048. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  29049. if (EXPECT_SUCCESS()) {
  29050. notAfter->data[8] = 0;
  29051. notAfter->data[3] = 0;
  29052. }
  29053. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  29054. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29055. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29056. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  29057. BIO_free(bio);
  29058. BIO_free(fixed);
  29059. X509_free(x509);
  29060. #endif
  29061. return EXPECT_RESULT();
  29062. }
  29063. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  29064. {
  29065. EXPECT_DECLS;
  29066. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  29067. BIO* bio = NULL;
  29068. ASN1_UTCTIME* utc = NULL;
  29069. unsigned char buf[25];
  29070. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  29071. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  29072. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  29073. /* Valid date */
  29074. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29075. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  29076. DYNAMIC_TYPE_ASN1));
  29077. if (utc != NULL) {
  29078. utc->type = ASN_UTC_TIME;
  29079. utc->length = ASN_UTC_TIME_SIZE;
  29080. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  29081. }
  29082. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  29083. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  29084. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  29085. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  29086. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  29087. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  29088. XMEMSET(buf, 0, sizeof(buf));
  29089. BIO_free(bio);
  29090. bio = NULL;
  29091. /* Invalid format */
  29092. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29093. if (utc != NULL) {
  29094. utc->type = ASN_UTC_TIME;
  29095. utc->length = ASN_UTC_TIME_SIZE;
  29096. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  29097. }
  29098. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29099. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  29100. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  29101. /* Invalid type */
  29102. if (utc != NULL) {
  29103. utc->type = ASN_GENERALIZED_TIME;
  29104. utc->length = ASN_GENERALIZED_TIME_SIZE;
  29105. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  29106. }
  29107. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29108. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  29109. BIO_free(bio);
  29110. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  29111. return EXPECT_RESULT();
  29112. }
  29113. static int test_wolfSSL_ASN1_TYPE(void)
  29114. {
  29115. EXPECT_DECLS;
  29116. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  29117. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  29118. WOLFSSL_ASN1_TYPE* t = NULL;
  29119. WOLFSSL_ASN1_OBJECT* obj = NULL;
  29120. #ifndef NO_ASN_TIME
  29121. WOLFSSL_ASN1_TIME* time = NULL;
  29122. #endif
  29123. WOLFSSL_ASN1_STRING* str = NULL;
  29124. unsigned char data[] = { 0x00 };
  29125. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  29126. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29127. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  29128. wolfSSL_ASN1_TYPE_free(t);
  29129. t = NULL;
  29130. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29131. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  29132. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  29133. wolfSSL_ASN1_TYPE_free(t);
  29134. t = NULL;
  29135. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29136. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29137. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  29138. wolfSSL_ASN1_TYPE_free(t);
  29139. t = NULL;
  29140. #ifndef NO_ASN_TIME
  29141. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29142. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29143. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  29144. wolfSSL_ASN1_TYPE_free(t);
  29145. t = NULL;
  29146. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29147. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29148. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  29149. wolfSSL_ASN1_TYPE_free(t);
  29150. t = NULL;
  29151. #endif
  29152. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29153. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29154. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29155. wolfSSL_ASN1_TYPE_free(t);
  29156. t = NULL;
  29157. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29158. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29159. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29160. wolfSSL_ASN1_TYPE_free(t);
  29161. t = NULL;
  29162. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29163. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29164. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29165. wolfSSL_ASN1_TYPE_free(t);
  29166. t = NULL;
  29167. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29168. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29169. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29170. wolfSSL_ASN1_TYPE_free(t);
  29171. t = NULL;
  29172. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29173. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29174. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29175. wolfSSL_ASN1_TYPE_free(t);
  29176. t = NULL;
  29177. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29178. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29179. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29180. wolfSSL_ASN1_TYPE_free(t);
  29181. t = NULL;
  29182. #endif
  29183. return EXPECT_RESULT();
  29184. }
  29185. /* Testing code used in dpp.c in hostap */
  29186. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29187. typedef struct {
  29188. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29189. * as an OID identifying the curve */
  29190. X509_ALGOR *alg;
  29191. /* Compressed format public key per ANSI X9.63 */
  29192. ASN1_BIT_STRING *pub_key;
  29193. } DPP_BOOTSTRAPPING_KEY;
  29194. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29195. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29196. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29197. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29198. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29199. typedef struct {
  29200. ASN1_INTEGER *integer;
  29201. } TEST_ASN1;
  29202. ASN1_SEQUENCE(TEST_ASN1) = {
  29203. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29204. } ASN1_SEQUENCE_END(TEST_ASN1)
  29205. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29206. typedef struct {
  29207. ASN1_OCTET_STRING *octet_string;
  29208. } TEST_FAIL_ASN1;
  29209. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  29210. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  29211. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  29212. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  29213. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  29214. #endif
  29215. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29216. {
  29217. EXPECT_DECLS;
  29218. /* Testing code used in dpp.c in hostap */
  29219. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29220. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29221. EC_KEY *eckey = NULL;
  29222. EVP_PKEY *key = NULL;
  29223. size_t len = 0;
  29224. unsigned char *der = NULL;
  29225. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29226. const unsigned char *in = ecc_clikey_der_256;
  29227. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  29228. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  29229. const EC_GROUP *group = NULL;
  29230. const EC_POINT *point = NULL;
  29231. int nid;
  29232. TEST_ASN1 *test_asn1 = NULL;
  29233. TEST_FAIL_ASN1 test_fail_asn1;
  29234. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29235. const unsigned char goodObjDer[] = {
  29236. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29237. };
  29238. WOLFSSL_ASN1_ITEM emptyTemplate;
  29239. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29240. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29241. der = NULL;
  29242. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29243. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29244. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29245. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29246. (long)sizeof_ecc_clikey_der_256));
  29247. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29248. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29249. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29250. nid = EC_GROUP_get_curve_name(group);
  29251. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29252. group_obj = OBJ_nid2obj(nid);
  29253. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29254. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29255. group_obj), 1);
  29256. if (EXPECT_SUCCESS()) {
  29257. ec_obj = NULL;
  29258. group_obj = NULL;
  29259. }
  29260. }
  29261. wolfSSL_ASN1_OBJECT_free(group_obj);
  29262. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29263. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29264. #ifdef HAVE_COMP_KEY
  29265. ExpectIntGT((len = EC_POINT_point2oct(
  29266. group, point, POINT_CONVERSION_COMPRESSED,
  29267. NULL, 0, NULL)), 0);
  29268. #else
  29269. ExpectIntGT((len = EC_POINT_point2oct(
  29270. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29271. NULL, 0, NULL)), 0);
  29272. #endif
  29273. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29274. #ifdef HAVE_COMP_KEY
  29275. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29276. der, len-1, NULL), 0);
  29277. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29278. der, len, NULL), len);
  29279. #else
  29280. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29281. der, len-1, NULL), 0);
  29282. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29283. der, len, NULL), len);
  29284. #endif
  29285. if (EXPECT_SUCCESS()) {
  29286. bootstrap->pub_key->data = der;
  29287. bootstrap->pub_key->length = (int)len;
  29288. /* Not actually used */
  29289. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29290. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29291. }
  29292. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29293. der = NULL;
  29294. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29295. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29296. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29297. EVP_PKEY_free(key);
  29298. EC_KEY_free(eckey);
  29299. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29300. bootstrap = NULL;
  29301. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29302. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29303. * a NULL and an empty BIT_STRING. */
  29304. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29305. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29306. if (EXPECT_SUCCESS()) {
  29307. bootstrap->alg->algorithm->obj = badObjDer;
  29308. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29309. }
  29310. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29311. if (EXPECT_SUCCESS()) {
  29312. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29313. bootstrap->alg->parameter->value.ptr = NULL;
  29314. bootstrap->pub_key->data = NULL;
  29315. bootstrap->pub_key->length = 0;
  29316. /* Not actually used */
  29317. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29318. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29319. }
  29320. /* Encode with bad OBJECT_ID. */
  29321. der = NULL;
  29322. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29323. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29324. if (EXPECT_SUCCESS()) {
  29325. bootstrap->alg->algorithm->obj = goodObjDer;
  29326. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29327. bootstrap->alg->algorithm->grp = 2;
  29328. }
  29329. der = NULL;
  29330. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29331. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29332. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29333. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29334. /* Test integer */
  29335. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29336. der = NULL;
  29337. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29338. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29339. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29340. TEST_ASN1_free(test_asn1);
  29341. /* Test integer cases. */
  29342. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29343. TEST_ASN1_free(NULL);
  29344. /* Test error cases. */
  29345. ExpectNull(TEST_FAIL_ASN1_new());
  29346. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29347. TEST_FAIL_ASN1_free(NULL);
  29348. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29349. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29350. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29351. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29352. return EXPECT_RESULT();
  29353. }
  29354. static int test_wolfSSL_lhash(void)
  29355. {
  29356. EXPECT_DECLS;
  29357. #ifdef OPENSSL_ALL
  29358. const char testStr[] = "Like a true nature's child\n"
  29359. "We were born\n"
  29360. "Born to be wild";
  29361. #ifdef NO_SHA
  29362. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29363. #else
  29364. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29365. #endif
  29366. #endif
  29367. return EXPECT_RESULT();
  29368. }
  29369. static int test_wolfSSL_X509_NAME(void)
  29370. {
  29371. EXPECT_DECLS;
  29372. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29373. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29374. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29375. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29376. defined(OPENSSL_EXTRA))
  29377. X509* x509 = NULL;
  29378. const unsigned char* c = NULL;
  29379. unsigned char buf[4096];
  29380. int bytes = 0;
  29381. XFILE f = XBADFILE;
  29382. const X509_NAME* a = NULL;
  29383. const X509_NAME* b = NULL;
  29384. X509_NAME* d2i_name = NULL;
  29385. int sz = 0;
  29386. unsigned char* tmp = NULL;
  29387. char file[] = "./certs/ca-cert.der";
  29388. #ifndef OPENSSL_EXTRA_X509_SMALL
  29389. byte empty[] = { /* CN=empty emailAddress= */
  29390. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29391. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29392. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29393. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29394. 0x01, 0x16, 0x00
  29395. };
  29396. #endif
  29397. #ifndef OPENSSL_EXTRA_X509_SMALL
  29398. /* test compile of deprecated function, returns 0 */
  29399. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29400. #endif
  29401. ExpectNotNull(a = X509_NAME_new());
  29402. X509_NAME_free((X509_NAME*)a);
  29403. a = NULL;
  29404. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29405. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29406. if (f != XBADFILE)
  29407. XFCLOSE(f);
  29408. c = buf;
  29409. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29410. /* test cmp function */
  29411. ExpectNotNull(a = X509_get_issuer_name(x509));
  29412. ExpectNotNull(b = X509_get_subject_name(x509));
  29413. #ifndef OPENSSL_EXTRA_X509_SMALL
  29414. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29415. #endif
  29416. tmp = buf;
  29417. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29418. if (sz > 0 && tmp == buf) {
  29419. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29420. __LINE__);
  29421. fprintf(stderr, " Expected pointer to be incremented\n");
  29422. abort();
  29423. }
  29424. #ifndef OPENSSL_EXTRA_X509_SMALL
  29425. tmp = buf;
  29426. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29427. #endif
  29428. /* if output parameter is NULL, should still return required size. */
  29429. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29430. /* retry but with the function creating a buffer */
  29431. tmp = NULL;
  29432. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29433. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29434. tmp = NULL;
  29435. #ifdef WOLFSSL_CERT_NAME_ALL
  29436. /* test for givenName and name */
  29437. {
  29438. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29439. const byte gName[] = "test-given-name";
  29440. const byte name[] = "test-name";
  29441. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29442. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29443. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29444. 1);
  29445. wolfSSL_X509_NAME_ENTRY_free(entry);
  29446. entry = NULL;
  29447. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29448. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29449. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29450. 1);
  29451. wolfSSL_X509_NAME_ENTRY_free(entry);
  29452. tmp = NULL;
  29453. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29454. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29455. }
  29456. #endif
  29457. b = NULL;
  29458. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29459. #ifndef OPENSSL_EXTRA_X509_SMALL
  29460. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29461. #endif
  29462. X509_NAME_free((X509_NAME*)b);
  29463. X509_NAME_free(d2i_name);
  29464. d2i_name = NULL;
  29465. X509_free(x509);
  29466. #ifndef OPENSSL_EXTRA_X509_SMALL
  29467. /* test with an empty domain component */
  29468. tmp = empty;
  29469. sz = sizeof(empty);
  29470. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29471. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29472. /* size of empty emailAddress will be 0 */
  29473. tmp = buf;
  29474. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29475. (char*)tmp, sizeof(buf)), 0);
  29476. /* should contain no organization name */
  29477. tmp = buf;
  29478. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29479. (char*)tmp, sizeof(buf)), -1);
  29480. X509_NAME_free(d2i_name);
  29481. #endif
  29482. #endif
  29483. return EXPECT_RESULT();
  29484. }
  29485. static int test_wolfSSL_X509_NAME_hash(void)
  29486. {
  29487. EXPECT_DECLS;
  29488. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29489. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29490. BIO* bio = NULL;
  29491. X509* x509 = NULL;
  29492. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29493. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29494. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29495. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29496. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29497. X509_free(x509);
  29498. BIO_free(bio);
  29499. #endif
  29500. return EXPECT_RESULT();
  29501. }
  29502. static int test_wolfSSL_X509_NAME_print_ex(void)
  29503. {
  29504. EXPECT_DECLS;
  29505. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29506. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29507. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29508. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29509. !defined(NO_BIO) && !defined(NO_RSA)
  29510. int memSz = 0;
  29511. byte* mem = NULL;
  29512. BIO* bio = NULL;
  29513. BIO* membio = NULL;
  29514. X509* x509 = NULL;
  29515. X509_NAME* name = NULL;
  29516. const char* expNormal = "C=US, CN=wolfssl.com";
  29517. const char* expReverse = "CN=wolfssl.com, C=US";
  29518. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29519. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29520. const char* expRFC5523 =
  29521. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29522. /* Test with real cert (svrCertFile) first */
  29523. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29524. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29525. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29526. ExpectNotNull(name = X509_get_subject_name(x509));
  29527. /* Test without flags */
  29528. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29529. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29530. BIO_free(membio);
  29531. membio = NULL;
  29532. /* Test flag: XN_FLAG_RFC2253 */
  29533. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29534. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29535. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29536. BIO_free(membio);
  29537. membio = NULL;
  29538. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29539. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29540. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29541. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29542. BIO_free(membio);
  29543. membio = NULL;
  29544. X509_free(x509);
  29545. BIO_free(bio);
  29546. name = NULL;
  29547. /* Test normal case without escaped characters */
  29548. {
  29549. /* Create name: "/C=US/CN=wolfssl.com" */
  29550. ExpectNotNull(name = X509_NAME_new());
  29551. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29552. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29553. WOLFSSL_SUCCESS);
  29554. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29555. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29556. WOLFSSL_SUCCESS);
  29557. /* Test without flags */
  29558. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29559. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29560. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29561. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29562. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29563. BIO_free(membio);
  29564. membio = NULL;
  29565. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29566. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29567. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29568. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29569. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29570. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29571. BIO_free(membio);
  29572. membio = NULL;
  29573. /* Test flags: XN_FLAG_DN_REV - reversed */
  29574. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29575. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29576. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29577. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29578. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29579. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29580. BIO_free(membio);
  29581. membio = NULL;
  29582. X509_NAME_free(name);
  29583. name = NULL;
  29584. }
  29585. /* Test RFC2253 characters are escaped with backslashes */
  29586. {
  29587. ExpectNotNull(name = X509_NAME_new());
  29588. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29589. /* space at beginning and end, and: ,+"\ */
  29590. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29591. WOLFSSL_SUCCESS);
  29592. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29593. /* # at beginning, and: <>;*/
  29594. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29595. WOLFSSL_SUCCESS);
  29596. /* Test without flags */
  29597. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29598. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29599. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29600. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29601. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29602. XSTRLEN(expNotEscaped)), 0);
  29603. BIO_free(membio);
  29604. membio = NULL;
  29605. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29606. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29607. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29608. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29609. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29610. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29611. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29612. BIO_free(membio);
  29613. membio = NULL;
  29614. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29615. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29616. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29617. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29618. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29619. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29620. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29621. XSTRLEN(expNotEscapedRev)), 0);
  29622. BIO_free(membio);
  29623. X509_NAME_free(name);
  29624. }
  29625. #endif
  29626. return EXPECT_RESULT();
  29627. }
  29628. #ifndef NO_BIO
  29629. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29630. {
  29631. EXPECT_DECLS;
  29632. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29633. STACK_OF(X509_INFO) *info_stack = NULL;
  29634. X509_INFO *info = NULL;
  29635. int len;
  29636. int i;
  29637. const char* files[] = {
  29638. cliCertFile,
  29639. cliKeyFile,
  29640. /* This needs to be the order as svrCertFile contains the
  29641. * intermediate cert as well. */
  29642. svrKeyFile,
  29643. svrCertFile,
  29644. NULL,
  29645. };
  29646. const char** curFile;
  29647. BIO *fileBIO = NULL;
  29648. BIO *concatBIO = NULL;
  29649. byte tmp[FOURK_BUF];
  29650. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29651. * to group objects together. */
  29652. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29653. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29654. int fileLen = 0;
  29655. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29656. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29657. if (EXPECT_SUCCESS()) {
  29658. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29659. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29660. fileLen -= len;
  29661. if (EXPECT_FAIL())
  29662. break;
  29663. }
  29664. /* Make sure we read the entire file */
  29665. ExpectIntEQ(fileLen, 0);
  29666. }
  29667. BIO_free(fileBIO);
  29668. fileBIO = NULL;
  29669. }
  29670. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29671. NULL));
  29672. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29673. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29674. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29675. ExpectNotNull(info->x509);
  29676. ExpectNull(info->crl);
  29677. if (i != 0) {
  29678. ExpectNotNull(info->x_pkey);
  29679. ExpectIntEQ(X509_check_private_key(info->x509,
  29680. info->x_pkey->dec_pkey), 1);
  29681. }
  29682. else {
  29683. ExpectNull(info->x_pkey);
  29684. }
  29685. }
  29686. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29687. BIO_free(concatBIO);
  29688. #endif
  29689. return EXPECT_RESULT();
  29690. }
  29691. #endif
  29692. #ifndef NO_BIO
  29693. static int test_wolfSSL_X509_INFO(void)
  29694. {
  29695. EXPECT_DECLS;
  29696. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29697. STACK_OF(X509_INFO) *info_stack = NULL;
  29698. X509_INFO *info = NULL;
  29699. BIO *cert = NULL;
  29700. int i;
  29701. /* PEM in hex format to avoid null terminator */
  29702. byte data[] = {
  29703. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29704. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29705. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29706. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29707. 0x2d, 0x2d, 0x2d
  29708. };
  29709. /* PEM in hex format to avoid null terminator */
  29710. byte data2[] = {
  29711. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29712. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29713. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29714. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29715. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29716. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29717. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29718. };
  29719. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29720. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29721. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29722. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29723. ExpectNotNull(info->x509);
  29724. ExpectNull(info->crl);
  29725. ExpectNull(info->x_pkey);
  29726. }
  29727. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29728. info_stack = NULL;
  29729. BIO_free(cert);
  29730. cert = NULL;
  29731. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29732. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29733. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29734. info_stack = NULL;
  29735. BIO_free(cert);
  29736. cert = NULL;
  29737. /* This case should fail due to invalid input. */
  29738. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29739. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29740. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29741. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29742. info_stack = NULL;
  29743. BIO_free(cert);
  29744. cert = NULL;
  29745. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29746. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29747. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29748. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29749. BIO_free(cert);
  29750. #endif
  29751. return EXPECT_RESULT();
  29752. }
  29753. #endif
  29754. static int test_wolfSSL_X509_subject_name_hash(void)
  29755. {
  29756. EXPECT_DECLS;
  29757. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29758. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29759. X509* x509 = NULL;
  29760. X509_NAME* subjectName = NULL;
  29761. unsigned long ret1 = 0;
  29762. unsigned long ret2 = 0;
  29763. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29764. SSL_FILETYPE_PEM));
  29765. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29766. /* These two
  29767. * - X509_subject_name_hash(x509)
  29768. * - X509_NAME_hash(X509_get_subject_name(x509))
  29769. * should give the same hash, if !defined(NO_SHA) is true. */
  29770. ret1 = X509_subject_name_hash(x509);
  29771. ExpectIntNE(ret1, 0);
  29772. #if !defined(NO_SHA)
  29773. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29774. ExpectIntNE(ret2, 0);
  29775. ExpectIntEQ(ret1, ret2);
  29776. #else
  29777. (void) ret2;
  29778. #endif
  29779. X509_free(x509);
  29780. #endif
  29781. return EXPECT_RESULT();
  29782. }
  29783. static int test_wolfSSL_X509_issuer_name_hash(void)
  29784. {
  29785. EXPECT_DECLS;
  29786. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29787. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29788. X509* x509 = NULL;
  29789. X509_NAME* issuertName = NULL;
  29790. unsigned long ret1 = 0;
  29791. unsigned long ret2 = 0;
  29792. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29793. SSL_FILETYPE_PEM));
  29794. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29795. /* These two
  29796. * - X509_issuer_name_hash(x509)
  29797. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29798. * should give the same hash, if !defined(NO_SHA) is true. */
  29799. ret1 = X509_issuer_name_hash(x509);
  29800. ExpectIntNE(ret1, 0);
  29801. #if !defined(NO_SHA)
  29802. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29803. ExpectIntNE(ret2, 0);
  29804. ExpectIntEQ(ret1, ret2);
  29805. #else
  29806. (void) ret2;
  29807. #endif
  29808. X509_free(x509);
  29809. #endif
  29810. return EXPECT_RESULT();
  29811. }
  29812. static int test_wolfSSL_X509_check_host(void)
  29813. {
  29814. EXPECT_DECLS;
  29815. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29816. && !defined(NO_SHA) && !defined(NO_RSA)
  29817. X509* x509 = NULL;
  29818. const char altName[] = "example.com";
  29819. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29820. SSL_FILETYPE_PEM));
  29821. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29822. WOLFSSL_SUCCESS);
  29823. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29824. WOLFSSL_FAILURE);
  29825. X509_free(x509);
  29826. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29827. WOLFSSL_FAILURE);
  29828. #endif
  29829. return EXPECT_RESULT();
  29830. }
  29831. static int test_wolfSSL_X509_check_email(void)
  29832. {
  29833. EXPECT_DECLS;
  29834. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29835. X509* x509 = NULL;
  29836. const char goodEmail[] = "info@wolfssl.com";
  29837. const char badEmail[] = "disinfo@wolfssl.com";
  29838. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29839. SSL_FILETYPE_PEM));
  29840. /* Should fail on non-matching email address */
  29841. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29842. WOLFSSL_FAILURE);
  29843. /* Should succeed on matching email address */
  29844. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29845. WOLFSSL_SUCCESS);
  29846. /* Should compute length internally when not provided */
  29847. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29848. WOLFSSL_SUCCESS);
  29849. /* Should fail when email address is NULL */
  29850. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29851. WOLFSSL_FAILURE);
  29852. X509_free(x509);
  29853. /* Should fail when x509 is NULL */
  29854. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29855. WOLFSSL_FAILURE);
  29856. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29857. return EXPECT_RESULT();
  29858. }
  29859. static int test_wc_PemToDer(void)
  29860. {
  29861. EXPECT_DECLS;
  29862. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29863. int ret;
  29864. DerBuffer* pDer = NULL;
  29865. const char* ca_cert = "./certs/server-cert.pem";
  29866. byte* cert_buf = NULL;
  29867. size_t cert_sz = 0;
  29868. int eccKey = 0;
  29869. EncryptedInfo info;
  29870. XMEMSET(&info, 0, sizeof(info));
  29871. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29872. ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
  29873. &info, &eccKey), 0);
  29874. wc_FreeDer(&pDer);
  29875. pDer = NULL;
  29876. if (cert_buf != NULL) {
  29877. free(cert_buf);
  29878. cert_buf = NULL;
  29879. }
  29880. #ifdef HAVE_ECC
  29881. {
  29882. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29883. byte key_buf[256] = {0};
  29884. /* Test fail of loading a key with cert type */
  29885. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29886. key_buf[0] = '\n';
  29887. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29888. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29889. &pDer, NULL, &info, &eccKey)), 0);
  29890. #ifdef OPENSSL_EXTRA
  29891. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29892. &pDer, NULL, &info, &eccKey)), 0);
  29893. #endif
  29894. wc_FreeDer(&pDer);
  29895. if (cert_buf != NULL)
  29896. free(cert_buf);
  29897. }
  29898. #endif
  29899. #endif
  29900. return EXPECT_RESULT();
  29901. }
  29902. static int test_wc_AllocDer(void)
  29903. {
  29904. EXPECT_DECLS;
  29905. #if !defined(NO_CERTS)
  29906. DerBuffer* pDer = NULL;
  29907. word32 testSize = 1024;
  29908. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29909. BAD_FUNC_ARG);
  29910. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29911. ExpectNotNull(pDer);
  29912. wc_FreeDer(&pDer);
  29913. #endif
  29914. return EXPECT_RESULT();
  29915. }
  29916. static int test_wc_CertPemToDer(void)
  29917. {
  29918. EXPECT_DECLS;
  29919. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29920. const char* ca_cert = "./certs/ca-cert.pem";
  29921. byte* cert_buf = NULL;
  29922. size_t cert_sz = 0;
  29923. size_t cert_dersz = 0;
  29924. byte* cert_der = NULL;
  29925. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29926. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29927. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29928. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29929. (int)cert_dersz, CERT_TYPE), 0);
  29930. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29931. BAD_FUNC_ARG);
  29932. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29933. BAD_FUNC_ARG);
  29934. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29935. BAD_FUNC_ARG);
  29936. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29937. CERT_TYPE), BAD_FUNC_ARG);
  29938. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29939. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29940. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29941. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29942. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29943. CERT_TYPE), BAD_FUNC_ARG);
  29944. if (cert_der != NULL)
  29945. free(cert_der);
  29946. if (cert_buf != NULL)
  29947. free(cert_buf);
  29948. #endif
  29949. return EXPECT_RESULT();
  29950. }
  29951. static int test_wc_KeyPemToDer(void)
  29952. {
  29953. EXPECT_DECLS;
  29954. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29955. int ret = 0;
  29956. const byte cert_buf[] = \
  29957. "-----BEGIN PRIVATE KEY-----\n"
  29958. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29959. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29960. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29961. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29962. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29963. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29964. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29965. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29966. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29967. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29968. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29969. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29970. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29971. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29972. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29973. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29974. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29975. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29976. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29977. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29978. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29979. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29980. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29981. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29982. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29983. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29984. "-----END PRIVATE KEY-----\n";
  29985. const int cert_sz = sizeof(cert_buf);
  29986. const char cert_pw[] = "password";
  29987. int cert_dersz = 0;
  29988. byte* cert_der = NULL;
  29989. /* Bad arg: Cert buffer is NULL */
  29990. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29991. BAD_FUNC_ARG);
  29992. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29993. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29994. BAD_FUNC_ARG);
  29995. /* Test normal operation */
  29996. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29997. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  29998. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29999. cert_pw), 0);
  30000. ExpectIntLE(ret, cert_sz);
  30001. if (cert_der != NULL) {
  30002. free(cert_der);
  30003. cert_der = NULL;
  30004. }
  30005. /* Test NULL for DER buffer to return needed DER buffer size */
  30006. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  30007. ExpectIntLE(ret, cert_sz);
  30008. if (EXPECT_SUCCESS())
  30009. cert_dersz = ret;
  30010. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  30011. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  30012. cert_pw), 0);
  30013. ExpectIntLE(ret, cert_sz);
  30014. if (cert_der != NULL)
  30015. free(cert_der);
  30016. #endif
  30017. return EXPECT_RESULT();
  30018. }
  30019. static int test_wc_PubKeyPemToDer(void)
  30020. {
  30021. EXPECT_DECLS;
  30022. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  30023. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30024. int ret = 0;
  30025. const char* key = "./certs/ecc-client-keyPub.pem";
  30026. byte* cert_buf = NULL;
  30027. size_t cert_sz = 0, cert_dersz = 0;
  30028. byte* cert_der = NULL;
  30029. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  30030. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  30031. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  30032. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  30033. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30034. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30035. (int)cert_dersz), 0);
  30036. if (cert_der != NULL) {
  30037. free(cert_der);
  30038. cert_der = NULL;
  30039. }
  30040. /* Test NULL for DER buffer to return needed DER buffer size */
  30041. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  30042. ExpectIntLE(ret, cert_sz);
  30043. cert_dersz = (size_t)ret;
  30044. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30045. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  30046. (int)cert_dersz), 0);
  30047. if (cert_der != NULL) {
  30048. free(cert_der);
  30049. }
  30050. if (cert_buf != NULL) {
  30051. free(cert_buf);
  30052. }
  30053. #endif
  30054. return EXPECT_RESULT();
  30055. }
  30056. static int test_wc_PemPubKeyToDer(void)
  30057. {
  30058. EXPECT_DECLS;
  30059. #if !defined(NO_FILESYSTEM) && \
  30060. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  30061. const char* key = "./certs/ecc-client-keyPub.pem";
  30062. size_t cert_dersz = 1024;
  30063. byte* cert_der = NULL;
  30064. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  30065. BAD_FUNC_ARG);
  30066. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30067. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  30068. if (cert_der != NULL) {
  30069. free(cert_der);
  30070. }
  30071. #endif
  30072. return EXPECT_RESULT();
  30073. }
  30074. static int test_wc_GetPubKeyDerFromCert(void)
  30075. {
  30076. EXPECT_DECLS;
  30077. #if !defined(NO_RSA) || defined(HAVE_ECC)
  30078. int ret;
  30079. word32 idx = 0;
  30080. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30081. word32 keyDerSz = (word32)sizeof(keyDer);
  30082. DecodedCert decoded;
  30083. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30084. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  30085. word32 certBufSz = sizeof(certBuf);
  30086. #endif
  30087. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  30088. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  30089. XFILE fp = XBADFILE;
  30090. #endif
  30091. #ifndef NO_RSA
  30092. RsaKey rsaKey;
  30093. #if defined(USE_CERT_BUFFERS_2048)
  30094. byte* rsaCertDer = (byte*)client_cert_der_2048;
  30095. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  30096. #elif defined(USE_CERT_BUFFERS_1024)
  30097. byte* rsaCertDer = (byte*)client_cert_der_1024;
  30098. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  30099. #else
  30100. unsigned char rsaCertDer[TWOK_BUF];
  30101. word32 rsaCertDerSz;
  30102. #endif
  30103. #endif
  30104. #ifdef HAVE_ECC
  30105. ecc_key eccKey;
  30106. #if defined(USE_CERT_BUFFERS_256)
  30107. byte* eccCert = (byte*)cliecc_cert_der_256;
  30108. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  30109. #else
  30110. unsigned char eccCert[ONEK_BUF];
  30111. word32 eccCertSz;
  30112. XFILE fp2 = XBADFILE;
  30113. #endif
  30114. #endif
  30115. #ifndef NO_RSA
  30116. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  30117. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  30118. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  30119. fp), 0);
  30120. if (fp != XBADFILE) {
  30121. XFCLOSE(fp);
  30122. fp = XBADFILE;
  30123. }
  30124. #endif
  30125. /* good test case - RSA DER cert */
  30126. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  30127. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30128. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30129. ExpectIntGT(keyDerSz, 0);
  30130. /* sanity check, verify we can import DER public key */
  30131. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30132. ExpectIntEQ(ret, 0);
  30133. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30134. if (ret == 0) {
  30135. wc_FreeRsaKey(&rsaKey);
  30136. }
  30137. /* test LENGTH_ONLY_E case */
  30138. keyDerSz = 0;
  30139. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30140. LENGTH_ONLY_E);
  30141. ExpectIntGT(keyDerSz, 0);
  30142. /* bad args: DecodedCert NULL */
  30143. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  30144. /* bad args: output key buff size */
  30145. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  30146. /* bad args: zero size output key buffer */
  30147. keyDerSz = 0;
  30148. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30149. BAD_FUNC_ARG);
  30150. wc_FreeDecodedCert(&decoded);
  30151. /* Certificate Request Tests */
  30152. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30153. {
  30154. XMEMSET(certBuf, 0, sizeof(certBuf));
  30155. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  30156. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  30157. if (fp != XBADFILE) {
  30158. XFCLOSE(fp);
  30159. }
  30160. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  30161. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  30162. /* good test case - RSA DER certificate request */
  30163. keyDerSz = sizeof(keyDer);
  30164. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30165. 0);
  30166. ExpectIntGT(keyDerSz, 0);
  30167. /* sanity check, verify we can import DER public key */
  30168. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30169. ExpectIntEQ(ret, 0);
  30170. idx = 0;
  30171. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30172. if (ret == 0) {
  30173. wc_FreeRsaKey(&rsaKey);
  30174. }
  30175. wc_FreeDecodedCert(&decoded);
  30176. }
  30177. #endif /* WOLFSSL_CERT_REQ */
  30178. #endif /* NO_RSA */
  30179. #ifdef HAVE_ECC
  30180. #ifndef USE_CERT_BUFFERS_256
  30181. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  30182. XBADFILE);
  30183. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  30184. if (fp2 != XBADFILE) {
  30185. XFCLOSE(fp2);
  30186. }
  30187. #endif
  30188. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30189. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30190. /* good test case - ECC */
  30191. XMEMSET(keyDer, 0, sizeof(keyDer));
  30192. keyDerSz = sizeof(keyDer);
  30193. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30194. ExpectIntGT(keyDerSz, 0);
  30195. /* sanity check, verify we can import DER public key */
  30196. ret = wc_ecc_init(&eccKey);
  30197. ExpectIntEQ(ret, 0);
  30198. idx = 0; /* reset idx to 0, used above in RSA case */
  30199. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  30200. if (ret == 0) {
  30201. wc_ecc_free(&eccKey);
  30202. }
  30203. /* test LENGTH_ONLY_E case */
  30204. keyDerSz = 0;
  30205. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30206. LENGTH_ONLY_E);
  30207. ExpectIntGT(keyDerSz, 0);
  30208. wc_FreeDecodedCert(&decoded);
  30209. #endif
  30210. #endif /* !NO_RSA || HAVE_ECC */
  30211. return EXPECT_RESULT();
  30212. }
  30213. static int test_wc_CheckCertSigPubKey(void)
  30214. {
  30215. EXPECT_DECLS;
  30216. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30217. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30218. int ret = 0;
  30219. const char* ca_cert = "./certs/ca-cert.pem";
  30220. byte* cert_buf = NULL;
  30221. size_t cert_sz = 0;
  30222. byte* cert_der = NULL;
  30223. word32 cert_dersz = 0;
  30224. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30225. word32 keyDerSz = (word32)sizeof(keyDer);
  30226. DecodedCert decoded;
  30227. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30228. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30229. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30230. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  30231. (int)cert_dersz, CERT_TYPE), 0);
  30232. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30233. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30234. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30235. ExpectIntGT(keyDerSz, 0);
  30236. /* Good test case. */
  30237. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30238. keyDerSz, RSAk), 0);
  30239. /* No certificate. */
  30240. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30241. ECDSAk), BAD_FUNC_ARG);
  30242. /* Bad cert size. */
  30243. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30244. RSAk), 0);
  30245. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30246. /* No public key. */
  30247. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30248. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30249. /* Bad public key size. */
  30250. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30251. RSAk), BAD_FUNC_ARG);
  30252. /* Wrong aglo. */
  30253. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30254. keyDerSz, ECDSAk), ASN_PARSE_E);
  30255. wc_FreeDecodedCert(&decoded);
  30256. if (cert_der != NULL)
  30257. free(cert_der);
  30258. if (cert_buf != NULL)
  30259. free(cert_buf);
  30260. #endif
  30261. return EXPECT_RESULT();
  30262. }
  30263. static int test_wolfSSL_certs(void)
  30264. {
  30265. EXPECT_DECLS;
  30266. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30267. !defined(NO_RSA)
  30268. X509* x509ext = NULL;
  30269. #ifdef OPENSSL_ALL
  30270. X509* x509 = NULL;
  30271. WOLFSSL_X509_EXTENSION* ext = NULL;
  30272. ASN1_OBJECT* obj = NULL;
  30273. #endif
  30274. WOLFSSL* ssl = NULL;
  30275. WOLFSSL_CTX* ctx = NULL;
  30276. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30277. ASN1_STRING* asn1_str = NULL;
  30278. AUTHORITY_KEYID* akey = NULL;
  30279. BASIC_CONSTRAINTS* bc = NULL;
  30280. int crit = 0;
  30281. #ifndef NO_WOLFSSL_SERVER
  30282. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30283. #else
  30284. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30285. #endif
  30286. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30287. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30288. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30289. #if !defined(NO_CHECK_PRIVATE_KEY)
  30290. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30291. #endif
  30292. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30293. #if !defined(NO_CHECK_PRIVATE_KEY)
  30294. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30295. #endif
  30296. ExpectNotNull(ssl = SSL_new(ctx));
  30297. #if !defined(NO_CHECK_PRIVATE_KEY)
  30298. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30299. #endif
  30300. #ifdef HAVE_PK_CALLBACKS
  30301. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30302. #endif /* HAVE_PK_CALLBACKS */
  30303. /* create and use x509 */
  30304. #ifdef OPENSSL_ALL
  30305. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30306. WOLFSSL_FILETYPE_PEM));
  30307. #endif
  30308. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30309. WOLFSSL_FILETYPE_PEM));
  30310. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30311. #if !defined(NO_CHECK_PRIVATE_KEY)
  30312. /* with loading in a new cert the check on private key should now fail */
  30313. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30314. #endif
  30315. #if defined(USE_CERT_BUFFERS_2048)
  30316. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30317. (unsigned char*)server_cert_der_2048,
  30318. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30319. #endif
  30320. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30321. /************* Get Digest of Certificate ******************/
  30322. {
  30323. byte digest[64]; /* max digest size */
  30324. word32 digestSz;
  30325. XMEMSET(digest, 0, sizeof(digest));
  30326. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30327. WOLFSSL_SUCCESS);
  30328. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30329. WOLFSSL_SUCCESS);
  30330. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30331. WOLFSSL_FAILURE);
  30332. }
  30333. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30334. /* test and checkout X509 extensions */
  30335. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30336. NID_basic_constraints, &crit, NULL));
  30337. ExpectIntEQ(crit, 0);
  30338. #ifdef OPENSSL_ALL
  30339. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30340. X509_EXTENSION_free(ext);
  30341. ext = NULL;
  30342. ExpectNotNull(ext = X509_EXTENSION_new());
  30343. X509_EXTENSION_set_critical(ext, 1);
  30344. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30345. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30346. ASN1_OBJECT_free(obj);
  30347. obj = NULL;
  30348. X509_EXTENSION_free(ext);
  30349. ext = NULL;
  30350. ExpectNotNull(ext = X509_EXTENSION_new());
  30351. X509_EXTENSION_set_critical(ext, 0);
  30352. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30353. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30354. NULL);
  30355. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30356. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30357. * and X509_get_ext_d2i has created new */
  30358. asn1_str = NULL;
  30359. X509_EXTENSION_free(ext);
  30360. ext = NULL;
  30361. #endif
  30362. BASIC_CONSTRAINTS_free(bc);
  30363. bc = NULL;
  30364. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30365. NID_key_usage, &crit, NULL));
  30366. ExpectIntEQ(crit, 1);
  30367. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30368. #ifdef OPENSSL_ALL
  30369. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30370. X509_EXTENSION_free(ext);
  30371. ext = NULL;
  30372. #endif
  30373. ASN1_STRING_free(asn1_str);
  30374. asn1_str = NULL;
  30375. #ifdef OPENSSL_ALL
  30376. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30377. NID_ext_key_usage, &crit, NULL));
  30378. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30379. X509_EXTENSION_free(ext);
  30380. ext = NULL;
  30381. EXTENDED_KEY_USAGE_free(sk);
  30382. sk = NULL;
  30383. #else
  30384. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30385. &crit, NULL);
  30386. ExpectNull(sk);
  30387. #endif
  30388. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30389. NID_authority_key_identifier, &crit, NULL));
  30390. #ifdef OPENSSL_ALL
  30391. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30392. akey));
  30393. X509_EXTENSION_free(ext);
  30394. ext = NULL;
  30395. #endif
  30396. wolfSSL_AUTHORITY_KEYID_free(akey);
  30397. akey = NULL;
  30398. /* NID not yet supported */
  30399. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30400. NID_private_key_usage_period, &crit, NULL));
  30401. ExpectIntEQ(crit, -1);
  30402. sk_ASN1_OBJECT_free(sk);
  30403. sk = NULL;
  30404. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30405. NID_subject_alt_name, &crit, NULL));
  30406. {
  30407. int i;
  30408. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30409. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30410. ExpectIntEQ(gen->type, GEN_DNS);
  30411. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30412. }
  30413. }
  30414. sk_GENERAL_NAME_free(sk);
  30415. sk = NULL;
  30416. /* NID not yet supported */
  30417. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30418. NID_issuer_alt_name, &crit, NULL));
  30419. ExpectIntEQ(crit, -1);
  30420. sk_ASN1_OBJECT_free(sk);
  30421. sk = NULL;
  30422. /* NID not yet supported */
  30423. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30424. NID_info_access, &crit, NULL));
  30425. sk_ASN1_OBJECT_free(sk);
  30426. sk = NULL;
  30427. /* NID not yet supported */
  30428. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30429. NID_sinfo_access, &crit, NULL));
  30430. ExpectIntEQ(crit, -1);
  30431. sk_ASN1_OBJECT_free(sk);
  30432. sk = NULL;
  30433. /* NID not yet supported */
  30434. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30435. NID_name_constraints, &crit, NULL));
  30436. ExpectIntEQ(crit, -1);
  30437. sk_ASN1_OBJECT_free(sk);
  30438. sk = NULL;
  30439. /* no cert policy set */
  30440. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30441. NID_certificate_policies, &crit, NULL));
  30442. sk_ASN1_OBJECT_free(sk);
  30443. sk = NULL;
  30444. /* NID not yet supported */
  30445. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30446. NID_policy_mappings, &crit, NULL));
  30447. ExpectIntEQ(crit, -1);
  30448. sk_ASN1_OBJECT_free(sk);
  30449. sk = NULL;
  30450. /* NID not yet supported */
  30451. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30452. NID_policy_constraints, &crit, NULL));
  30453. ExpectIntEQ(crit, -1);
  30454. sk_ASN1_OBJECT_free(sk);
  30455. sk = NULL;
  30456. /* NID not yet supported */
  30457. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30458. NID_inhibit_any_policy, &crit, NULL));
  30459. ExpectIntEQ(crit, -1);
  30460. sk_ASN1_OBJECT_free(sk);
  30461. sk = NULL;
  30462. /* NID not yet supported */
  30463. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30464. NID_tlsfeature, &crit, NULL));
  30465. ExpectIntEQ(crit, -1);
  30466. sk_ASN1_OBJECT_free(sk);
  30467. sk = NULL;
  30468. /* test invalid cases */
  30469. crit = 0;
  30470. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30471. NULL));
  30472. ExpectIntEQ(crit, -1);
  30473. /* NULL passed for criticality. */
  30474. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30475. NID_tlsfeature, NULL, NULL));
  30476. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30477. #ifdef OPENSSL_ALL
  30478. X509_free(x509);
  30479. #endif
  30480. X509_free(x509ext);
  30481. SSL_free(ssl);
  30482. SSL_CTX_free(ctx);
  30483. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30484. return EXPECT_RESULT();
  30485. }
  30486. static int test_wolfSSL_X509_check_private_key(void)
  30487. {
  30488. EXPECT_DECLS;
  30489. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30490. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30491. X509* x509 = NULL;
  30492. EVP_PKEY* pkey = NULL;
  30493. const byte* key;
  30494. /* Check with correct key */
  30495. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30496. SSL_FILETYPE_PEM)));
  30497. key = client_key_der_2048;
  30498. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30499. (long)sizeof_client_key_der_2048));
  30500. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30501. EVP_PKEY_free(pkey);
  30502. pkey = NULL;
  30503. /* Check with wrong key */
  30504. key = server_key_der_2048;
  30505. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30506. (long)sizeof_server_key_der_2048));
  30507. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30508. /* test for incorrect parameter */
  30509. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30510. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30511. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30512. EVP_PKEY_free(pkey);
  30513. X509_free(x509);
  30514. #endif
  30515. return EXPECT_RESULT();
  30516. }
  30517. static int test_wolfSSL_private_keys(void)
  30518. {
  30519. EXPECT_DECLS;
  30520. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30521. !defined(NO_FILESYSTEM)
  30522. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30523. WOLFSSL* ssl = NULL;
  30524. WOLFSSL_CTX* ctx = NULL;
  30525. EVP_PKEY* pkey = NULL;
  30526. OpenSSL_add_all_digests();
  30527. OpenSSL_add_all_algorithms();
  30528. #ifndef NO_RSA
  30529. #ifndef NO_WOLFSSL_SERVER
  30530. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30531. #else
  30532. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30533. #endif
  30534. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30535. /* Have to load a cert before you can check the private key against that
  30536. * certificates public key! */
  30537. #if !defined(NO_CHECK_PRIVATE_KEY)
  30538. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30539. #endif
  30540. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30541. #if !defined(NO_CHECK_PRIVATE_KEY)
  30542. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30543. #endif
  30544. ExpectNotNull(ssl = SSL_new(ctx));
  30545. #if !defined(NO_CHECK_PRIVATE_KEY)
  30546. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30547. #endif
  30548. #ifdef USE_CERT_BUFFERS_2048
  30549. {
  30550. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30551. unsigned char buf[FOURK_BUF];
  30552. word32 bufSz;
  30553. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30554. (unsigned char*)client_key_der_2048,
  30555. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30556. #if !defined(NO_CHECK_PRIVATE_KEY)
  30557. /* Should mismatch now that a different private key loaded */
  30558. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30559. #endif
  30560. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30561. (unsigned char*)server_key,
  30562. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30563. #if !defined(NO_CHECK_PRIVATE_KEY)
  30564. /* After loading back in DER format of original key, should match */
  30565. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30566. #endif
  30567. /* test loading private key to the WOLFSSL_CTX */
  30568. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30569. (unsigned char*)client_key_der_2048,
  30570. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30571. #if !defined(NO_CHECK_PRIVATE_KEY)
  30572. /* Should mismatch now that a different private key loaded */
  30573. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30574. #endif
  30575. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30576. (unsigned char*)server_key,
  30577. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30578. #if !defined(NO_CHECK_PRIVATE_KEY)
  30579. /* After loading back in DER format of original key, should match */
  30580. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30581. #endif
  30582. /* pkey not set yet, expecting to fail */
  30583. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30584. /* set PKEY and test again */
  30585. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30586. &server_key, (long)sizeof_server_key_der_2048));
  30587. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30588. /* reuse PKEY structure and test
  30589. * this should be checked with a memory management sanity checker */
  30590. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30591. server_key = (const unsigned char*)server_key_der_2048;
  30592. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30593. &server_key, (long)sizeof_server_key_der_2048));
  30594. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30595. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30596. bufSz = FOURK_BUF;
  30597. ExpectIntGT((bufSz = (word32)wc_CreatePKCS8Key(buf, &bufSz,
  30598. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30599. RSAk, NULL, 0)), 0);
  30600. server_key = (const unsigned char*)buf;
  30601. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30602. (long)bufSz));
  30603. }
  30604. #endif
  30605. EVP_PKEY_free(pkey);
  30606. pkey = NULL;
  30607. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30608. ssl = NULL;
  30609. SSL_CTX_free(ctx);
  30610. ctx = NULL;
  30611. #endif /* end of RSA private key match tests */
  30612. #ifdef HAVE_ECC
  30613. #ifndef NO_WOLFSSL_SERVER
  30614. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30615. #else
  30616. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30617. #endif
  30618. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30619. WOLFSSL_FILETYPE_PEM));
  30620. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30621. WOLFSSL_FILETYPE_PEM));
  30622. ExpectNotNull(ssl = SSL_new(ctx));
  30623. #if !defined(NO_CHECK_PRIVATE_KEY)
  30624. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30625. #endif
  30626. SSL_free(ssl);
  30627. ssl = NULL;
  30628. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30629. WOLFSSL_FILETYPE_PEM));
  30630. ExpectNotNull(ssl = SSL_new(ctx));
  30631. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30632. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30633. #endif
  30634. SSL_free(ssl);
  30635. ssl = NULL;
  30636. SSL_CTX_free(ctx);
  30637. ctx = NULL;
  30638. #endif /* end of ECC private key match tests */
  30639. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30640. #ifndef NO_WOLFSSL_SERVER
  30641. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30642. #else
  30643. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30644. #endif
  30645. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30646. WOLFSSL_FILETYPE_PEM));
  30647. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30648. WOLFSSL_FILETYPE_PEM));
  30649. ExpectNotNull(ssl = SSL_new(ctx));
  30650. #if !defined(NO_CHECK_PRIVATE_KEY)
  30651. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30652. #endif
  30653. SSL_free(ssl);
  30654. ssl = NULL;
  30655. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30656. WOLFSSL_FILETYPE_PEM));
  30657. ExpectNotNull(ssl = SSL_new(ctx));
  30658. #if !defined(NO_CHECK_PRIVATE_KEY)
  30659. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30660. #endif
  30661. SSL_free(ssl);
  30662. ssl = NULL;
  30663. SSL_CTX_free(ctx);
  30664. ctx = NULL;
  30665. #endif /* end of Ed25519 private key match tests */
  30666. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30667. #ifndef NO_WOLFSSL_SERVER
  30668. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30669. #else
  30670. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30671. #endif
  30672. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30673. WOLFSSL_FILETYPE_PEM));
  30674. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30675. WOLFSSL_FILETYPE_PEM));
  30676. ExpectNotNull(ssl = SSL_new(ctx));
  30677. #if !defined(NO_CHECK_PRIVATE_KEY)
  30678. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30679. #endif
  30680. SSL_free(ssl);
  30681. ssl = NULL;
  30682. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30683. WOLFSSL_FILETYPE_PEM));
  30684. ExpectNotNull(ssl = SSL_new(ctx));
  30685. #if !defined(NO_CHECK_PRIVATE_KEY)
  30686. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30687. #endif
  30688. SSL_free(ssl);
  30689. ssl = NULL;
  30690. SSL_CTX_free(ctx);
  30691. ctx = NULL;
  30692. #endif /* end of Ed448 private key match tests */
  30693. EVP_cleanup();
  30694. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30695. CONF_modules_free();
  30696. ENGINE_cleanup();
  30697. CONF_modules_unload();
  30698. (void)ssl;
  30699. (void)ctx;
  30700. (void)pkey;
  30701. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30702. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30703. return EXPECT_RESULT();
  30704. }
  30705. static int test_wolfSSL_PEM_def_callback(void)
  30706. {
  30707. EXPECT_DECLS;
  30708. #ifdef OPENSSL_EXTRA
  30709. char buf[10];
  30710. const char* defpwd = "DEF PWD";
  30711. int defpwdLen = (int)XSTRLEN(defpwd);
  30712. int smallLen = 1;
  30713. /* Bad parameters. */
  30714. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30715. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30716. 0);
  30717. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30718. XMEMSET(buf, 0, sizeof(buf));
  30719. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30720. defpwdLen);
  30721. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30722. ExpectIntEQ(buf[defpwdLen], 0);
  30723. /* Size of buffer is smaller than default password. */
  30724. XMEMSET(buf, 0, sizeof(buf));
  30725. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30726. smallLen);
  30727. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30728. ExpectIntEQ(buf[smallLen], 0);
  30729. #endif /* OPENSSL_EXTRA */
  30730. return EXPECT_RESULT();
  30731. }
  30732. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30733. {
  30734. EXPECT_DECLS;
  30735. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30736. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30737. XFILE file = XBADFILE;
  30738. #if !defined(NO_RSA)
  30739. const char* fname_rsa = "./certs/server-key.pem";
  30740. RSA* rsa = NULL;
  30741. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30742. unsigned char* sig = NULL;
  30743. size_t sigLen = 0;
  30744. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30745. size_t tbsLen = sizeof(tbs);
  30746. #endif
  30747. #if !defined(NO_DSA)
  30748. const char* fname_dsa = "./certs/dsa2048.pem";
  30749. #endif
  30750. #if defined(HAVE_ECC)
  30751. const char* fname_ec = "./certs/ecc-key.pem";
  30752. #endif
  30753. #if !defined(NO_DH)
  30754. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30755. #endif
  30756. EVP_PKEY* pkey = NULL;
  30757. /* Check error case. */
  30758. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30759. /* not a PEM key. */
  30760. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30761. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30762. if (file != XBADFILE)
  30763. XFCLOSE(file);
  30764. file = XBADFILE;
  30765. #ifndef NO_RSA
  30766. /* Read in an RSA key. */
  30767. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30768. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30769. if (file != XBADFILE)
  30770. XFCLOSE(file);
  30771. file = XBADFILE;
  30772. /* Make sure the key is usable by signing some data with it. */
  30773. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30774. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30775. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30776. DYNAMIC_TYPE_TMP_BUFFER));
  30777. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30778. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30779. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30780. WOLFSSL_SUCCESS);
  30781. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30782. EVP_PKEY_CTX_free(ctx);
  30783. EVP_PKEY_free(pkey);
  30784. pkey = NULL;
  30785. #endif
  30786. #ifndef NO_DSA
  30787. /* Read in a DSA key. */
  30788. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30789. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30790. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30791. EVP_PKEY_free(pkey);
  30792. pkey = NULL;
  30793. #else
  30794. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30795. #endif
  30796. if (file != XBADFILE)
  30797. XFCLOSE(file);
  30798. file = XBADFILE;
  30799. #endif
  30800. #ifdef HAVE_ECC
  30801. /* Read in an EC key. */
  30802. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30803. ExpectNotNull(pkey = EVP_PKEY_new());
  30804. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30805. if (file != XBADFILE)
  30806. XFCLOSE(file);
  30807. file = XBADFILE;
  30808. EVP_PKEY_free(pkey);
  30809. pkey = NULL;
  30810. #endif
  30811. #ifndef NO_DH
  30812. /* Read in a DH key. */
  30813. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30814. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30815. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30816. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30817. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30818. EVP_PKEY_free(pkey);
  30819. pkey = NULL;
  30820. #else
  30821. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30822. #endif
  30823. if (file != XBADFILE)
  30824. XFCLOSE(file);
  30825. file = XBADFILE;
  30826. #endif
  30827. #endif
  30828. return EXPECT_RESULT();
  30829. }
  30830. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30831. {
  30832. EXPECT_DECLS;
  30833. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30834. && !defined(NO_FILESYSTEM)
  30835. XFILE file = XBADFILE;
  30836. const char* fname = "./certs/client-keyPub.pem";
  30837. EVP_PKEY* pkey = NULL;
  30838. /* Check error case. */
  30839. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30840. /* Read in an RSA key. */
  30841. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30842. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30843. EVP_PKEY_free(pkey);
  30844. pkey = NULL;
  30845. if (file != XBADFILE)
  30846. XFCLOSE(file);
  30847. file = XBADFILE;
  30848. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30849. ExpectNotNull(pkey = EVP_PKEY_new());
  30850. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30851. EVP_PKEY_free(pkey);
  30852. if (file != XBADFILE)
  30853. XFCLOSE(file);
  30854. #endif
  30855. return EXPECT_RESULT();
  30856. }
  30857. /* test loading RSA key using BIO */
  30858. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30859. {
  30860. EXPECT_DECLS;
  30861. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30862. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30863. !defined(NO_BIO)
  30864. BIO* bio = NULL;
  30865. XFILE file = XBADFILE;
  30866. const char* fname = "./certs/server-key.pem";
  30867. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30868. EVP_PKEY* pkey = NULL;
  30869. size_t sz = 0;
  30870. byte* buf = NULL;
  30871. EVP_PKEY* pkey2 = NULL;
  30872. EVP_PKEY* pkey3 = NULL;
  30873. RSA* rsa_key = NULL;
  30874. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30875. unsigned char extra[10];
  30876. int i;
  30877. BIO* pub_bio = NULL;
  30878. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30879. #endif
  30880. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30881. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30882. ExpectIntGT(sz = XFTELL(file), 0);
  30883. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30884. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30885. if (buf != NULL) {
  30886. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30887. }
  30888. if (file != XBADFILE) {
  30889. XFCLOSE(file);
  30890. file = XBADFILE;
  30891. }
  30892. /* Test using BIO new mem and loading PEM private key */
  30893. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30894. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30895. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30896. buf = NULL;
  30897. BIO_free(bio);
  30898. bio = NULL;
  30899. /* New empty EVP_PKEY */
  30900. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30901. if (pkey2 != NULL) {
  30902. pkey2->type = EVP_PKEY_RSA;
  30903. }
  30904. /* Test parameter copy */
  30905. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30906. EVP_PKEY_free(pkey2);
  30907. EVP_PKEY_free(pkey);
  30908. pkey = NULL;
  30909. /* Qt unit test case : rsa pkcs8 key */
  30910. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30911. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30912. ExpectIntGT(sz = XFTELL(file), 0);
  30913. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30914. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30915. if (buf) {
  30916. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30917. }
  30918. if (file != XBADFILE) {
  30919. XFCLOSE(file);
  30920. file = XBADFILE;
  30921. }
  30922. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30923. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30924. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30925. buf = NULL;
  30926. BIO_free(bio);
  30927. bio = NULL;
  30928. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30929. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30930. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30931. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30932. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30933. #else
  30934. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30935. #endif
  30936. RSA_free(rsa_key);
  30937. EVP_PKEY_free(pkey3);
  30938. EVP_PKEY_free(pkey);
  30939. pkey = NULL;
  30940. pkey2 = NULL;
  30941. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30942. #define BIO_PEM_TEST_CHAR 'a'
  30943. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30944. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30945. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30946. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30947. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30948. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30949. (long)sizeof_server_key_der_2048));
  30950. ExpectNull(pkey);
  30951. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30952. (long)sizeof_server_key_der_2048));
  30953. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30954. WOLFSSL_FAILURE);
  30955. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30956. WOLFSSL_FAILURE);
  30957. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30958. WOLFSSL_SUCCESS);
  30959. ExpectIntGT(BIO_pending(bio), 0);
  30960. ExpectIntEQ(BIO_pending(bio), 1679);
  30961. /* Check if the pubkey API writes only the public key */
  30962. #ifdef WOLFSSL_KEY_GEN
  30963. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30964. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30965. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30966. ExpectIntGT(BIO_pending(pub_bio), 0);
  30967. /* Previously both the private key and the pubkey calls would write
  30968. * out the private key and the PEM header was the only difference.
  30969. * The public PEM should be significantly shorter than the
  30970. * private key versison. */
  30971. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30972. #else
  30973. /* Not supported. */
  30974. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30975. #endif
  30976. /* test creating new EVP_PKEY with good args */
  30977. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30978. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30979. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30980. pkey->pkey_sz), 0);
  30981. }
  30982. /* test of reuse of EVP_PKEY */
  30983. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30984. ExpectIntEQ(BIO_pending(bio), 0);
  30985. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30986. SSL_SUCCESS);
  30987. /* add 10 extra bytes after PEM */
  30988. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30989. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30990. ExpectNotNull(pkey);
  30991. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30992. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30993. pkey->pkey_sz), 0);
  30994. }
  30995. /* check 10 extra bytes still there */
  30996. ExpectIntEQ(BIO_pending(bio), 10);
  30997. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30998. for (i = 0; i < 10; i++) {
  30999. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  31000. }
  31001. BIO_free(pub_bio);
  31002. BIO_free(bio);
  31003. bio = NULL;
  31004. EVP_PKEY_free(pkey);
  31005. pkey = NULL;
  31006. EVP_PKEY_free(pkey2);
  31007. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  31008. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  31009. * !NO_FILESYSTEM && !NO_BIO */
  31010. return EXPECT_RESULT();
  31011. }
  31012. /* test loading ECC key using BIO */
  31013. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  31014. {
  31015. EXPECT_DECLS;
  31016. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  31017. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31018. BIO* bio = NULL;
  31019. EVP_PKEY* pkey = NULL;
  31020. XFILE file = XBADFILE;
  31021. const char* fname = "./certs/ecc-key.pem";
  31022. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  31023. size_t sz = 0;
  31024. byte* buf = NULL;
  31025. EVP_PKEY* pkey2 = NULL;
  31026. EVP_PKEY* pkey3 = NULL;
  31027. EC_KEY* ec_key = NULL;
  31028. int nid = 0;
  31029. BIO* pub_bio = NULL;
  31030. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31031. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31032. ExpectIntGT(sz = XFTELL(file), 0);
  31033. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31034. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31035. if (buf) {
  31036. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31037. }
  31038. if (file != XBADFILE) {
  31039. XFCLOSE(file);
  31040. file = XBADFILE;
  31041. }
  31042. /* Test using BIO new mem and loading PEM private key */
  31043. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31044. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31045. BIO_free(bio);
  31046. bio = NULL;
  31047. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31048. buf = NULL;
  31049. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31050. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31051. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31052. WOLFSSL_SUCCESS);
  31053. ExpectIntGT(BIO_pending(bio), 0);
  31054. /* No parmeters. */
  31055. ExpectIntEQ(BIO_pending(bio), 227);
  31056. /* Check if the pubkey API writes only the public key */
  31057. #ifdef WOLFSSL_KEY_GEN
  31058. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  31059. ExpectIntGT(BIO_pending(pub_bio), 0);
  31060. /* Previously both the private key and the pubkey calls would write
  31061. * out the private key and the PEM header was the only difference.
  31062. * The public PEM should be significantly shorter than the
  31063. * private key versison. */
  31064. ExpectIntEQ(BIO_pending(pub_bio), 178);
  31065. #endif
  31066. BIO_free(pub_bio);
  31067. BIO_free(bio);
  31068. bio = NULL;
  31069. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31070. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31071. if (pkey2 != NULL) {
  31072. pkey2->type = EVP_PKEY_EC;
  31073. }
  31074. /* Test parameter copy */
  31075. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  31076. /* Qt unit test case 1*/
  31077. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31078. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31079. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31080. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31081. #else
  31082. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31083. #endif
  31084. /* Test default digest */
  31085. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  31086. ExpectIntEQ(nid, NID_sha256);
  31087. EC_KEY_free(ec_key);
  31088. ec_key = NULL;
  31089. EVP_PKEY_free(pkey3);
  31090. pkey3 = NULL;
  31091. EVP_PKEY_free(pkey2);
  31092. pkey2 = NULL;
  31093. EVP_PKEY_free(pkey);
  31094. pkey = NULL;
  31095. /* Qt unit test case ec pkcs8 key */
  31096. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  31097. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  31098. ExpectIntGT(sz = XFTELL(file), 0);
  31099. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  31100. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31101. if (buf) {
  31102. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31103. }
  31104. if (file != XBADFILE) {
  31105. XFCLOSE(file);
  31106. file = XBADFILE;
  31107. }
  31108. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31109. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31110. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31111. buf = NULL;
  31112. BIO_free(bio);
  31113. bio = NULL;
  31114. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31115. /* Qt unit test case */
  31116. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31117. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31118. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31119. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31120. #else
  31121. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31122. #endif
  31123. EC_KEY_free(ec_key);
  31124. EVP_PKEY_free(pkey3);
  31125. EVP_PKEY_free(pkey);
  31126. pkey = NULL;
  31127. #endif
  31128. return EXPECT_RESULT();
  31129. }
  31130. /* test loading DSA key using BIO */
  31131. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  31132. {
  31133. EXPECT_DECLS;
  31134. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  31135. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31136. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  31137. BIO* bio = NULL;
  31138. EVP_PKEY* pkey = NULL;
  31139. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  31140. /* Private DSA EVP_PKEY */
  31141. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31142. NULL));
  31143. BIO_free(bio);
  31144. bio = NULL;
  31145. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31146. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31147. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31148. NULL), 0);
  31149. #endif
  31150. #ifdef WOLFSSL_KEY_GEN
  31151. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  31152. ExpectIntEQ(BIO_pending(bio), 1178);
  31153. BIO_reset(bio);
  31154. #endif
  31155. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31156. 1);
  31157. ExpectIntEQ(BIO_pending(bio), 1196);
  31158. BIO_free(bio);
  31159. bio = NULL;
  31160. EVP_PKEY_free(pkey);
  31161. pkey = NULL;
  31162. #endif
  31163. #endif
  31164. return EXPECT_RESULT();
  31165. }
  31166. /* test loading DH key using BIO */
  31167. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  31168. {
  31169. EXPECT_DECLS;
  31170. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  31171. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31172. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  31173. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  31174. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  31175. BIO* bio = NULL;
  31176. EVP_PKEY* pkey = NULL;
  31177. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  31178. /* Private DH EVP_PKEY */
  31179. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31180. NULL));
  31181. BIO_free(bio);
  31182. bio = NULL;
  31183. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31184. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31185. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31186. NULL), 0);
  31187. #endif
  31188. #ifdef WOLFSSL_KEY_GEN
  31189. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  31190. #endif
  31191. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31192. 1);
  31193. ExpectIntEQ(BIO_pending(bio), 806);
  31194. BIO_free(bio);
  31195. bio = NULL;
  31196. EVP_PKEY_free(pkey);
  31197. pkey = NULL;
  31198. #endif
  31199. #endif
  31200. return EXPECT_RESULT();
  31201. }
  31202. static int test_wolfSSL_PEM_PrivateKey(void)
  31203. {
  31204. EXPECT_DECLS;
  31205. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31206. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  31207. #ifndef NO_BIO
  31208. BIO* bio = NULL;
  31209. #endif
  31210. EVP_PKEY* pkey = NULL;
  31211. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31212. #ifndef NO_BIO
  31213. /* test creating new EVP_PKEY with bad arg */
  31214. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  31215. /* Test bad EVP_PKEY type. */
  31216. /* New HMAC EVP_PKEY */
  31217. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  31218. ExpectNotNull(pkey = EVP_PKEY_new());
  31219. if (pkey != NULL) {
  31220. pkey->type = EVP_PKEY_HMAC;
  31221. }
  31222. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31223. 0);
  31224. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31225. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31226. NULL), 0);
  31227. #endif
  31228. #ifdef WOLFSSL_KEY_GEN
  31229. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  31230. #endif
  31231. EVP_PKEY_free(pkey);
  31232. pkey = NULL;
  31233. BIO_free(bio);
  31234. bio = NULL;
  31235. /* key is DES encrypted */
  31236. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31237. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31238. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31239. {
  31240. XFILE f = XBADFILE;
  31241. wc_pem_password_cb* passwd_cb = NULL;
  31242. void* passwd_cb_userdata;
  31243. SSL_CTX* ctx = NULL;
  31244. char passwd[] = "bad password";
  31245. #ifndef WOLFSSL_NO_TLS12
  31246. #ifndef NO_WOLFSSL_SERVER
  31247. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31248. #else
  31249. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31250. #endif
  31251. #else
  31252. #ifndef NO_WOLFSSL_SERVER
  31253. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31254. #else
  31255. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31256. #endif
  31257. #endif
  31258. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31259. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31260. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31261. ExpectNull(passwd_cb_userdata =
  31262. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31263. /* fail case with password call back */
  31264. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31265. (void*)passwd));
  31266. BIO_free(bio);
  31267. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31268. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31269. (void*)passwd));
  31270. BIO_free(bio);
  31271. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31272. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31273. if ((bio == NULL) && (f != XBADFILE)) {
  31274. XFCLOSE(f);
  31275. }
  31276. /* use callback that works */
  31277. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31278. (void*)"yassl123"));
  31279. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31280. EVP_PKEY_free(pkey);
  31281. pkey = NULL;
  31282. BIO_free(bio);
  31283. bio = NULL;
  31284. SSL_CTX_free(ctx);
  31285. }
  31286. #endif /* !defined(NO_DES3) */
  31287. #endif /* !NO_BIO */
  31288. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31289. {
  31290. unsigned char buf[2048];
  31291. size_t bytes = 0;
  31292. XFILE f = XBADFILE;
  31293. SSL_CTX* ctx = NULL;
  31294. #ifndef WOLFSSL_NO_TLS12
  31295. #ifndef NO_WOLFSSL_SERVER
  31296. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31297. #else
  31298. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31299. #endif
  31300. #else
  31301. #ifndef NO_WOLFSSL_SERVER
  31302. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31303. #else
  31304. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31305. #endif
  31306. #endif
  31307. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31308. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31309. if (f != XBADFILE)
  31310. XFCLOSE(f);
  31311. server_key = buf;
  31312. pkey = NULL;
  31313. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, (long int)bytes));
  31314. ExpectNull(pkey);
  31315. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, (long int)bytes));
  31316. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31317. EVP_PKEY_free(pkey);
  31318. pkey = NULL;
  31319. SSL_CTX_free(ctx);
  31320. server_key = NULL;
  31321. }
  31322. #endif
  31323. #ifndef NO_BIO
  31324. (void)bio;
  31325. #endif
  31326. (void)pkey;
  31327. (void)server_key;
  31328. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31329. return EXPECT_RESULT();
  31330. }
  31331. static int test_wolfSSL_PEM_file_RSAKey(void)
  31332. {
  31333. EXPECT_DECLS;
  31334. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31335. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31336. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31337. RSA* rsa = NULL;
  31338. XFILE fp = XBADFILE;
  31339. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31340. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31341. if (fp != XBADFILE)
  31342. XFCLOSE(fp);
  31343. ExpectIntEQ(RSA_size(rsa), 256);
  31344. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31345. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31346. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31347. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31348. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31349. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31350. RSA_free(rsa);
  31351. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31352. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31353. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31354. return EXPECT_RESULT();
  31355. }
  31356. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31357. {
  31358. EXPECT_DECLS;
  31359. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31360. !defined(NO_FILESYSTEM) && \
  31361. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31362. RSA* rsa = NULL;
  31363. XFILE f = NULL;
  31364. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31365. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31366. ExpectIntEQ(RSA_size(rsa), 256);
  31367. if (f != XBADFILE) {
  31368. XFCLOSE(f);
  31369. f = XBADFILE;
  31370. }
  31371. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31372. NULL), WOLFSSL_FAILURE);
  31373. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31374. NULL), WOLFSSL_FAILURE);
  31375. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31376. WOLFSSL_SUCCESS);
  31377. RSA_free(rsa);
  31378. #ifdef HAVE_ECC
  31379. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31380. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31381. if (f != XBADFILE)
  31382. XFCLOSE(f);
  31383. #endif /* HAVE_ECC */
  31384. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31385. return EXPECT_RESULT();
  31386. }
  31387. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31388. {
  31389. EXPECT_DECLS;
  31390. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31391. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31392. XFILE file = XBADFILE;
  31393. const char* fname = "./certs/client-keyPub.pem";
  31394. RSA *rsa = NULL;
  31395. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31396. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31397. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31398. ExpectIntEQ(RSA_size(rsa), 256);
  31399. RSA_free(rsa);
  31400. if (file != XBADFILE)
  31401. XFCLOSE(file);
  31402. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31403. return EXPECT_RESULT();
  31404. }
  31405. #ifndef NO_BIO
  31406. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31407. {
  31408. EXPECT_DECLS;
  31409. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31410. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31411. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31412. RSA* rsa = NULL;
  31413. BIO* bio = NULL;
  31414. /* PrivateKey */
  31415. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31416. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31417. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31418. ExpectNotNull(rsa);
  31419. ExpectIntEQ(RSA_size(rsa), 256);
  31420. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31421. NULL), WOLFSSL_FAILURE);
  31422. BIO_free(bio);
  31423. bio = NULL;
  31424. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31425. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31426. NULL), WOLFSSL_SUCCESS);
  31427. BIO_free(bio);
  31428. bio = NULL;
  31429. RSA_free(rsa);
  31430. rsa = NULL;
  31431. /* PUBKEY */
  31432. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31433. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31434. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31435. ExpectIntEQ(RSA_size(rsa), 256);
  31436. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31437. BIO_free(bio);
  31438. bio = NULL;
  31439. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31440. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31441. BIO_free(bio);
  31442. bio = NULL;
  31443. RSA_free(rsa);
  31444. rsa = NULL;
  31445. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31446. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31447. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31448. BIO_free(bio);
  31449. bio = NULL;
  31450. RSA_free(rsa);
  31451. rsa = NULL;
  31452. #ifdef HAVE_ECC
  31453. /* ensure that non-rsa keys do not work */
  31454. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31455. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31456. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31457. BIO_free(bio);
  31458. bio = NULL;
  31459. RSA_free(rsa);
  31460. rsa = NULL;
  31461. #endif /* HAVE_ECC */
  31462. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31463. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31464. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31465. return EXPECT_RESULT();
  31466. }
  31467. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31468. {
  31469. EXPECT_DECLS;
  31470. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31471. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31472. RSA* rsa = NULL;
  31473. RSA* rsa_dup = NULL;
  31474. BIO* bio = NULL;
  31475. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31476. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31477. ExpectIntEQ(RSA_size(rsa), 256);
  31478. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31479. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31480. /* Test duplicating empty key. */
  31481. ExpectNotNull(rsa_dup = RSA_new());
  31482. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31483. RSA_free(rsa_dup);
  31484. rsa_dup = NULL;
  31485. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31486. ExpectPtrNE(rsa_dup, rsa);
  31487. #endif
  31488. /* test if valgrind complains about unreleased memory */
  31489. RSA_up_ref(rsa);
  31490. RSA_free(rsa);
  31491. BIO_free(bio);
  31492. bio = NULL;
  31493. RSA_free(rsa);
  31494. rsa = NULL;
  31495. RSA_free(rsa_dup);
  31496. rsa_dup = NULL;
  31497. #ifdef HAVE_ECC
  31498. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31499. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31500. BIO_free(bio);
  31501. #endif /* HAVE_ECC */
  31502. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31503. return EXPECT_RESULT();
  31504. }
  31505. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31506. {
  31507. EXPECT_DECLS;
  31508. #ifndef HAVE_SELFTEST
  31509. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31510. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31511. DSA* dsa = NULL;
  31512. BIO* bio = NULL;
  31513. /* PrivateKey */
  31514. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31515. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31516. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31517. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31518. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31519. NULL), WOLFSSL_FAILURE);
  31520. BIO_free(bio);
  31521. bio = NULL;
  31522. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31523. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31524. NULL), WOLFSSL_SUCCESS);
  31525. BIO_free(bio);
  31526. bio = NULL;
  31527. DSA_free(dsa);
  31528. dsa = NULL;
  31529. /* PUBKEY */
  31530. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31531. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31532. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31533. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31534. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31535. BIO_free(bio);
  31536. bio = NULL;
  31537. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31538. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31539. BIO_free(bio);
  31540. bio = NULL;
  31541. DSA_free(dsa);
  31542. dsa = NULL;
  31543. #ifdef HAVE_ECC
  31544. /* ensure that non-dsa keys do not work */
  31545. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31546. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31547. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31548. BIO_free(bio);
  31549. bio = NULL;
  31550. DSA_free(dsa);
  31551. dsa = NULL;
  31552. #endif /* HAVE_ECC */
  31553. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31554. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31555. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31556. #endif /* HAVE_SELFTEST */
  31557. return EXPECT_RESULT();
  31558. }
  31559. static int test_wolfSSL_PEM_bio_ECKey(void)
  31560. {
  31561. EXPECT_DECLS;
  31562. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31563. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31564. EC_KEY* ec = NULL;
  31565. EC_KEY* ec2;
  31566. BIO* bio = NULL;
  31567. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31568. unsigned char* pem = NULL;
  31569. int pLen;
  31570. #endif
  31571. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31572. "MAA=\n"
  31573. "-----END PUBLIC KEY-----";
  31574. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31575. "MAA=\n"
  31576. "-----END EC PRIVATE KEY-----";
  31577. /* PrivateKey */
  31578. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31579. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31580. ec2 = NULL;
  31581. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31582. ExpectIntEQ(ec == ec2, 1);
  31583. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31584. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31585. NULL), WOLFSSL_FAILURE);
  31586. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31587. NULL), WOLFSSL_FAILURE);
  31588. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31589. WOLFSSL_FAILURE);
  31590. BIO_free(bio);
  31591. bio = NULL;
  31592. /* Public key data - fail. */
  31593. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31594. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31595. BIO_free(bio);
  31596. bio = NULL;
  31597. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31598. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31599. NULL), WOLFSSL_SUCCESS);
  31600. BIO_free(bio);
  31601. bio = NULL;
  31602. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31603. NULL),WOLFSSL_FAILURE);
  31604. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31605. WOLFSSL_FAILURE);
  31606. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31607. WOLFSSL_FAILURE);
  31608. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31609. WOLFSSL_SUCCESS);
  31610. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31611. NULL), 0);
  31612. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31613. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31614. NULL), 0);
  31615. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31616. NULL), 0);
  31617. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31618. &pLen), 0);
  31619. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31620. &pLen), 0);
  31621. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31622. &pLen), 0);
  31623. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31624. NULL), 0);
  31625. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31626. &pLen), 1);
  31627. ExpectIntGT(pLen, 0);
  31628. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31629. #endif
  31630. EC_KEY_free(ec);
  31631. ec = NULL;
  31632. /* PUBKEY */
  31633. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31634. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31635. ec2 = NULL;
  31636. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31637. ExpectIntEQ(ec == ec2, 1);
  31638. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31639. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31640. BIO_free(bio);
  31641. bio = NULL;
  31642. /* Test 0x30, 0x00 fails. */
  31643. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31644. sizeof(ec_key_bad_1)));
  31645. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31646. BIO_free(bio);
  31647. bio = NULL;
  31648. /* Private key data - fail. */
  31649. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31650. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31651. BIO_free(bio);
  31652. bio = NULL;
  31653. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31654. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31655. BIO_free(bio);
  31656. bio = NULL;
  31657. /* Same test as above, but with a file pointer rather than a BIO. */
  31658. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31659. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31660. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31661. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31662. EC_KEY_free(ec);
  31663. ec = NULL;
  31664. #ifndef NO_RSA
  31665. /* ensure that non-ec keys do not work */
  31666. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31667. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31668. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31669. BIO_free(bio);
  31670. bio = NULL;
  31671. EC_KEY_free(ec);
  31672. ec = NULL;
  31673. #endif /* !NO_RSA */
  31674. /* Test 0x30, 0x00 fails. */
  31675. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31676. sizeof(ec_priv_key_bad_1)));
  31677. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31678. BIO_free(bio);
  31679. bio = NULL;
  31680. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31681. return EXPECT_RESULT();
  31682. }
  31683. static int test_wolfSSL_PEM_PUBKEY(void)
  31684. {
  31685. EXPECT_DECLS;
  31686. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31687. BIO* bio = NULL;
  31688. EVP_PKEY* pkey = NULL;
  31689. /* test creating new EVP_PKEY with bad arg */
  31690. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31691. /* test loading ECC key using BIO */
  31692. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31693. {
  31694. XFILE file = XBADFILE;
  31695. const char* fname = "./certs/ecc-client-keyPub.pem";
  31696. size_t sz = 0;
  31697. byte* buf = NULL;
  31698. EVP_PKEY* pkey2 = NULL;
  31699. EC_KEY* ec_key = NULL;
  31700. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31701. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31702. ExpectIntGT(sz = XFTELL(file), 0);
  31703. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31704. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31705. if (buf != NULL) {
  31706. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31707. }
  31708. if (file != XBADFILE) {
  31709. XFCLOSE(file);
  31710. }
  31711. /* Test using BIO new mem and loading PEM private key */
  31712. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31713. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31714. BIO_free(bio);
  31715. bio = NULL;
  31716. EVP_PKEY_free(pkey);
  31717. pkey = NULL;
  31718. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31719. ExpectNotNull(pkey = EVP_PKEY_new());
  31720. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31721. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31722. BIO_free(bio);
  31723. bio = NULL;
  31724. /* Qt unit test case*/
  31725. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31726. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31727. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31728. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31729. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31730. #else
  31731. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31732. #endif
  31733. EC_KEY_free(ec_key);
  31734. EVP_PKEY_free(pkey2);
  31735. EVP_PKEY_free(pkey);
  31736. pkey = NULL;
  31737. }
  31738. #endif
  31739. (void)bio;
  31740. (void)pkey;
  31741. #endif
  31742. return EXPECT_RESULT();
  31743. }
  31744. #endif /* !NO_BIO */
  31745. static int test_DSA_do_sign_verify(void)
  31746. {
  31747. EXPECT_DECLS;
  31748. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31749. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31750. !defined(NO_DSA)
  31751. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31752. DSA_SIG* sig = NULL;
  31753. DSA* dsa = NULL;
  31754. word32 bytes;
  31755. byte sigBin[DSA_SIG_SIZE];
  31756. int dsacheck;
  31757. #ifdef USE_CERT_BUFFERS_1024
  31758. byte tmp[ONEK_BUF];
  31759. XMEMSET(tmp, 0, sizeof(tmp));
  31760. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31761. bytes = sizeof_dsa_key_der_1024;
  31762. #elif defined(USE_CERT_BUFFERS_2048)
  31763. byte tmp[TWOK_BUF];
  31764. XMEMSET(tmp, 0, sizeof(tmp));
  31765. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31766. bytes = sizeof_dsa_key_der_2048;
  31767. #else
  31768. byte tmp[TWOK_BUF];
  31769. XFILE fp = XBADFILE;
  31770. XMEMSET(tmp, 0, sizeof(tmp));
  31771. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31772. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31773. if (fp != XBADFILE)
  31774. XFCLOSE(fp);
  31775. #endif /* END USE_CERT_BUFFERS_1024 */
  31776. XMEMSET(digest, 202, sizeof(digest));
  31777. ExpectNotNull(dsa = DSA_new());
  31778. ExpectIntEQ(DSA_LoadDer(dsa, tmp, (int)bytes), 1);
  31779. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31780. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31781. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31782. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31783. DSA_SIG_free(sig);
  31784. DSA_free(dsa);
  31785. #endif
  31786. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31787. return EXPECT_RESULT();
  31788. }
  31789. static int test_wolfSSL_tmp_dh(void)
  31790. {
  31791. EXPECT_DECLS;
  31792. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31793. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31794. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31795. byte buff[6000];
  31796. char file[] = "./certs/dsaparams.pem";
  31797. XFILE f = XBADFILE;
  31798. int bytes = 0;
  31799. DSA* dsa = NULL;
  31800. DH* dh = NULL;
  31801. #if defined(WOLFSSL_DH_EXTRA) && \
  31802. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31803. DH* dh2 = NULL;
  31804. #endif
  31805. BIO* bio = NULL;
  31806. SSL* ssl = NULL;
  31807. SSL_CTX* ctx = NULL;
  31808. #ifndef NO_WOLFSSL_SERVER
  31809. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31810. #else
  31811. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31812. #endif
  31813. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31814. WOLFSSL_FILETYPE_PEM));
  31815. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31816. WOLFSSL_FILETYPE_PEM));
  31817. ExpectNotNull(ssl = SSL_new(ctx));
  31818. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31819. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31820. if (f != XBADFILE)
  31821. XFCLOSE(f);
  31822. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31823. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31824. ExpectNotNull(dsa);
  31825. dh = wolfSSL_DSA_dup_DH(dsa);
  31826. ExpectNotNull(dh);
  31827. #if defined(WOLFSSL_DH_EXTRA) && \
  31828. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31829. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31830. #endif
  31831. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31832. #ifndef NO_WOLFSSL_SERVER
  31833. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31834. #else
  31835. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31836. #endif
  31837. BIO_free(bio);
  31838. DSA_free(dsa);
  31839. DH_free(dh);
  31840. dh = NULL;
  31841. #if defined(WOLFSSL_DH_EXTRA) && \
  31842. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31843. DH_free(dh2);
  31844. dh2 = NULL;
  31845. #endif
  31846. SSL_free(ssl);
  31847. SSL_CTX_free(ctx);
  31848. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31849. #endif
  31850. return EXPECT_RESULT();
  31851. }
  31852. static int test_wolfSSL_ctrl(void)
  31853. {
  31854. EXPECT_DECLS;
  31855. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31856. byte buff[6000];
  31857. BIO* bio = NULL;
  31858. int bytes;
  31859. BUF_MEM* ptr = NULL;
  31860. XMEMSET(buff, 0, sizeof(buff));
  31861. bytes = sizeof(buff);
  31862. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31863. ExpectNotNull(BIO_s_socket());
  31864. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31865. /* needs tested after stubs filled out @TODO
  31866. SSL_ctrl
  31867. SSL_CTX_ctrl
  31868. */
  31869. BIO_free(bio);
  31870. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31871. return EXPECT_RESULT();
  31872. }
  31873. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31874. {
  31875. EXPECT_DECLS;
  31876. #ifdef OPENSSL_EXTRA
  31877. static const unsigned char pw[] = "password";
  31878. static const int pwSz = sizeof(pw) - 1;
  31879. size_t checkPwSz = 0;
  31880. const unsigned char* checkPw = NULL;
  31881. WOLFSSL_EVP_PKEY* key = NULL;
  31882. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31883. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31884. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31885. pwSz));
  31886. if (key != NULL) {
  31887. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31888. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31889. ExpectIntEQ(key->pkey_sz, pwSz);
  31890. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31891. }
  31892. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31893. ExpectIntEQ((int)checkPwSz, pwSz);
  31894. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31895. wolfSSL_EVP_PKEY_free(key);
  31896. key = NULL;
  31897. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31898. 0));
  31899. ExpectIntEQ(key->pkey_sz, 0);
  31900. if (EXPECT_SUCCESS()) {
  31901. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31902. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31903. }
  31904. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31905. ExpectIntEQ((int)checkPwSz, 0);
  31906. wolfSSL_EVP_PKEY_free(key);
  31907. key = NULL;
  31908. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31909. 0));
  31910. ExpectIntEQ(key->pkey_sz, 0);
  31911. if (EXPECT_SUCCESS()) {
  31912. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31913. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31914. }
  31915. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31916. ExpectIntEQ((int)checkPwSz, 0);
  31917. wolfSSL_EVP_PKEY_free(key);
  31918. key = NULL;
  31919. #endif /* OPENSSL_EXTRA */
  31920. return EXPECT_RESULT();
  31921. }
  31922. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31923. {
  31924. EXPECT_DECLS;
  31925. #ifdef OPENSSL_EXTRA
  31926. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31927. const char *priv = "ABCDEFGHIJKLMNOP";
  31928. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31929. WOLFSSL_EVP_PKEY* key = NULL;
  31930. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31931. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31932. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31933. NULL, (const unsigned char *)priv, 0, cipher));
  31934. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31935. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31936. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31937. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31938. wolfSSL_EVP_PKEY_free(key);
  31939. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31940. #endif /* OPENSSL_EXTRA */
  31941. return EXPECT_RESULT();
  31942. }
  31943. static int test_wolfSSL_EVP_Digest(void)
  31944. {
  31945. EXPECT_DECLS;
  31946. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31947. const char* in = "abc";
  31948. int inLen = (int)XSTRLEN(in);
  31949. byte out[WC_SHA256_DIGEST_SIZE];
  31950. unsigned int outLen;
  31951. const char* expOut =
  31952. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31953. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31954. "\x15\xAD";
  31955. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31956. "SHA256", NULL), 1);
  31957. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31958. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31959. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31960. return EXPECT_RESULT();
  31961. }
  31962. static int test_wolfSSL_EVP_Digest_all(void)
  31963. {
  31964. EXPECT_DECLS;
  31965. #ifdef OPENSSL_EXTRA
  31966. const char* digests[] = {
  31967. #ifndef NO_MD5
  31968. "MD5",
  31969. #endif
  31970. #ifndef NO_SHA
  31971. "SHA",
  31972. #endif
  31973. #ifdef WOLFSSL_SHA224
  31974. "SHA224",
  31975. #endif
  31976. #ifndef NO_SHA256
  31977. "SHA256",
  31978. #endif
  31979. #ifdef WOLFSSL_SHA384
  31980. "SHA384",
  31981. #endif
  31982. #ifdef WOLFSSL_SHA512
  31983. "SHA512",
  31984. #endif
  31985. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31986. "SHA512_224",
  31987. #endif
  31988. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31989. "SHA512_256",
  31990. #endif
  31991. #ifdef WOLFSSL_SHA3
  31992. #ifndef WOLFSSL_NOSHA3_224
  31993. "SHA3_224",
  31994. #endif
  31995. #ifndef WOLFSSL_NOSHA3_256
  31996. "SHA3_256",
  31997. #endif
  31998. "SHA3_384",
  31999. #ifndef WOLFSSL_NOSHA3_512
  32000. "SHA3_512",
  32001. #endif
  32002. #endif /* WOLFSSL_SHA3 */
  32003. NULL
  32004. };
  32005. const char** d;
  32006. const unsigned char in[] = "abc";
  32007. int inLen = XSTR_SIZEOF(in);
  32008. byte out[WC_MAX_DIGEST_SIZE];
  32009. unsigned int outLen;
  32010. for (d = digests; *d != NULL; d++) {
  32011. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  32012. ExpectIntGT(outLen, 0);
  32013. ExpectIntEQ(EVP_MD_size(*d), outLen);
  32014. }
  32015. #endif
  32016. return EXPECT_RESULT();
  32017. }
  32018. static int test_wolfSSL_EVP_MD_size(void)
  32019. {
  32020. EXPECT_DECLS;
  32021. #ifdef OPENSSL_EXTRA
  32022. WOLFSSL_EVP_MD_CTX mdCtx;
  32023. #ifdef WOLFSSL_SHA3
  32024. #ifndef WOLFSSL_NOSHA3_224
  32025. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32026. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  32027. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  32028. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  32029. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32030. #endif
  32031. #ifndef WOLFSSL_NOSHA3_256
  32032. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32033. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  32034. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  32035. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  32036. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32037. #endif
  32038. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32039. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  32040. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  32041. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  32042. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32043. #ifndef WOLFSSL_NOSHA3_512
  32044. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32045. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  32046. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  32047. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  32048. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32049. #endif
  32050. #endif /* WOLFSSL_SHA3 */
  32051. #ifndef NO_SHA256
  32052. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32053. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  32054. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32055. WC_SHA256_DIGEST_SIZE);
  32056. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32057. WC_SHA256_BLOCK_SIZE);
  32058. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  32059. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  32060. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32061. #endif
  32062. #ifndef NO_MD5
  32063. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32064. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  32065. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32066. WC_MD5_DIGEST_SIZE);
  32067. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32068. WC_MD5_BLOCK_SIZE);
  32069. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  32070. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  32071. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32072. #endif
  32073. #ifdef WOLFSSL_SHA224
  32074. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32075. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  32076. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32077. WC_SHA224_DIGEST_SIZE);
  32078. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32079. WC_SHA224_BLOCK_SIZE);
  32080. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  32081. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  32082. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32083. #endif
  32084. #ifdef WOLFSSL_SHA384
  32085. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32086. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  32087. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32088. WC_SHA384_DIGEST_SIZE);
  32089. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32090. WC_SHA384_BLOCK_SIZE);
  32091. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  32092. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  32093. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32094. #endif
  32095. #ifdef WOLFSSL_SHA512
  32096. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32097. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  32098. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32099. WC_SHA512_DIGEST_SIZE);
  32100. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32101. WC_SHA512_BLOCK_SIZE);
  32102. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  32103. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  32104. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32105. #endif
  32106. #ifndef NO_SHA
  32107. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32108. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  32109. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32110. WC_SHA_DIGEST_SIZE);
  32111. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32112. WC_SHA_BLOCK_SIZE);
  32113. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32114. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32115. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32116. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32117. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  32118. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32119. WC_SHA_DIGEST_SIZE);
  32120. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32121. WC_SHA_BLOCK_SIZE);
  32122. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32123. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32124. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32125. #endif
  32126. /* error case */
  32127. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32128. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  32129. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32130. BAD_FUNC_ARG);
  32131. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  32132. /* Cleanup is valid on uninit'ed struct */
  32133. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32134. #endif /* OPENSSL_EXTRA */
  32135. return EXPECT_RESULT();
  32136. }
  32137. static int test_wolfSSL_EVP_MD_pkey_type(void)
  32138. {
  32139. EXPECT_DECLS;
  32140. #ifdef OPENSSL_EXTRA
  32141. const WOLFSSL_EVP_MD* md;
  32142. #ifndef NO_MD5
  32143. ExpectNotNull(md = EVP_md5());
  32144. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  32145. #endif
  32146. #ifndef NO_SHA
  32147. ExpectNotNull(md = EVP_sha1());
  32148. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  32149. #endif
  32150. #ifdef WOLFSSL_SHA224
  32151. ExpectNotNull(md = EVP_sha224());
  32152. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  32153. #endif
  32154. ExpectNotNull(md = EVP_sha256());
  32155. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  32156. #ifdef WOLFSSL_SHA384
  32157. ExpectNotNull(md = EVP_sha384());
  32158. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  32159. #endif
  32160. #ifdef WOLFSSL_SHA512
  32161. ExpectNotNull(md = EVP_sha512());
  32162. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  32163. #endif
  32164. #endif
  32165. return EXPECT_RESULT();
  32166. }
  32167. #ifdef OPENSSL_EXTRA
  32168. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  32169. size_t testKeySz, const char* testData, size_t testDataSz,
  32170. const byte* testResult, size_t testResultSz)
  32171. {
  32172. EXPECT_DECLS;
  32173. unsigned char check[WC_MAX_DIGEST_SIZE];
  32174. size_t checkSz = -1;
  32175. WOLFSSL_EVP_PKEY* key = NULL;
  32176. WOLFSSL_EVP_MD_CTX mdCtx;
  32177. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  32178. testKey, (int)testKeySz));
  32179. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32180. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32181. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32182. (unsigned int)testDataSz), 1);
  32183. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32184. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32185. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32186. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32187. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32188. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32189. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32190. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32191. (unsigned int)testDataSz), 1);
  32192. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32193. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32194. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32195. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32196. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32197. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32198. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32199. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32200. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32201. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32202. (unsigned int)testDataSz - 4), 1);
  32203. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32204. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32205. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32206. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32207. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32208. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32209. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32210. (unsigned int)testDataSz - 4), 1);
  32211. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32212. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32213. wolfSSL_EVP_PKEY_free(key);
  32214. return EXPECT_RESULT();
  32215. }
  32216. #endif
  32217. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32218. {
  32219. EXPECT_DECLS;
  32220. #ifdef OPENSSL_EXTRA
  32221. static const unsigned char testKey[] =
  32222. {
  32223. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32224. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32225. 0x0b, 0x0b, 0x0b, 0x0b
  32226. };
  32227. static const char testData[] = "Hi There";
  32228. #ifdef WOLFSSL_SHA224
  32229. static const unsigned char testResultSha224[] =
  32230. {
  32231. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32232. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32233. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32234. 0x53, 0x68, 0x4b, 0x22
  32235. };
  32236. #endif
  32237. #ifndef NO_SHA256
  32238. static const unsigned char testResultSha256[] =
  32239. {
  32240. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32241. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32242. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32243. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32244. };
  32245. #endif
  32246. #ifdef WOLFSSL_SHA384
  32247. static const unsigned char testResultSha384[] =
  32248. {
  32249. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32250. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32251. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32252. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32253. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32254. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32255. };
  32256. #endif
  32257. #ifdef WOLFSSL_SHA512
  32258. static const unsigned char testResultSha512[] =
  32259. {
  32260. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32261. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32262. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32263. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32264. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32265. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32266. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32267. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32268. };
  32269. #endif
  32270. #ifdef WOLFSSL_SHA3
  32271. #ifndef WOLFSSL_NOSHA3_224
  32272. static const unsigned char testResultSha3_224[] =
  32273. {
  32274. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32275. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32276. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32277. 0xf3, 0xc8, 0x60, 0xf7
  32278. };
  32279. #endif
  32280. #ifndef WOLFSSL_NOSHA3_256
  32281. static const unsigned char testResultSha3_256[] =
  32282. {
  32283. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32284. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32285. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32286. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32287. };
  32288. #endif
  32289. #ifndef WOLFSSL_NOSHA3_384
  32290. static const unsigned char testResultSha3_384[] =
  32291. {
  32292. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32293. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32294. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32295. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32296. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32297. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32298. };
  32299. #endif
  32300. #ifndef WOLFSSL_NOSHA3_512
  32301. static const unsigned char testResultSha3_512[] =
  32302. {
  32303. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32304. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32305. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32306. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32307. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32308. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32309. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32310. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32311. };
  32312. #endif
  32313. #endif
  32314. #ifndef NO_SHA256
  32315. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32316. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32317. sizeof(testResultSha256)), TEST_SUCCESS);
  32318. #endif
  32319. #ifdef WOLFSSL_SHA224
  32320. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32321. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32322. sizeof(testResultSha224)), TEST_SUCCESS);
  32323. #endif
  32324. #ifdef WOLFSSL_SHA384
  32325. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32326. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32327. sizeof(testResultSha384)), TEST_SUCCESS);
  32328. #endif
  32329. #ifdef WOLFSSL_SHA512
  32330. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32331. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32332. sizeof(testResultSha512)), TEST_SUCCESS);
  32333. #endif
  32334. #ifdef WOLFSSL_SHA3
  32335. #ifndef WOLFSSL_NOSHA3_224
  32336. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32337. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32338. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32339. #endif
  32340. #ifndef WOLFSSL_NOSHA3_256
  32341. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32342. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32343. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32344. #endif
  32345. #ifndef WOLFSSL_NOSHA3_384
  32346. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32347. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32348. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32349. #endif
  32350. #ifndef WOLFSSL_NOSHA3_512
  32351. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32352. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32353. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32354. #endif
  32355. #endif
  32356. #endif /* OPENSSL_EXTRA */
  32357. return EXPECT_RESULT();
  32358. }
  32359. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32360. {
  32361. EXPECT_DECLS;
  32362. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32363. WOLFSSL_EVP_PKEY* privKey = NULL;
  32364. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32365. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32366. const char testData[] = "Hi There";
  32367. WOLFSSL_EVP_MD_CTX mdCtx;
  32368. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32369. int ret;
  32370. size_t checkSz = -1;
  32371. int sz = 2048 / 8;
  32372. const unsigned char* cp;
  32373. const unsigned char* p;
  32374. unsigned char check[2048/8];
  32375. size_t i;
  32376. int paddings[] = {
  32377. RSA_PKCS1_PADDING,
  32378. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32379. RSA_PKCS1_PSS_PADDING,
  32380. #endif
  32381. };
  32382. cp = client_key_der_2048;
  32383. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32384. sizeof_client_key_der_2048)));
  32385. p = client_keypub_der_2048;
  32386. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32387. sizeof_client_keypub_der_2048)));
  32388. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32389. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32390. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32391. NULL, privKey), 1);
  32392. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32393. (unsigned int)XSTRLEN(testData)), 1);
  32394. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32395. ExpectIntEQ((int)checkSz, sz);
  32396. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32397. ExpectIntEQ((int)checkSz,sz);
  32398. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32399. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32400. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32401. ExpectIntEQ(ret, 1);
  32402. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32403. ExpectIntEQ(ret, 1);
  32404. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32405. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32406. NULL, pubKey), 1);
  32407. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32408. (unsigned int)XSTRLEN(testData)),
  32409. 1);
  32410. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32411. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32412. ExpectIntEQ(ret, 1);
  32413. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32414. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32415. NULL, privKey), 1);
  32416. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32417. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32418. ExpectIntEQ((int)checkSz, sz);
  32419. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32420. ExpectIntEQ((int)checkSz, sz);
  32421. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32422. (unsigned int)XSTRLEN(testData) - 4), 1);
  32423. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32424. ExpectIntEQ((int)checkSz, sz);
  32425. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32426. ExpectIntEQ(ret, 1);
  32427. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32428. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32429. NULL, pubKey), 1);
  32430. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32431. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32432. (unsigned int)XSTRLEN(testData) - 4),
  32433. 1);
  32434. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32435. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32436. ExpectIntEQ(ret, 1);
  32437. /* Check all signing padding types */
  32438. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32439. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32440. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32441. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32442. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32443. paddings[i]), 1);
  32444. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32445. (unsigned int)XSTRLEN(testData)), 1);
  32446. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32447. ExpectIntEQ((int)checkSz, sz);
  32448. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32449. ExpectIntEQ((int)checkSz,sz);
  32450. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32451. ExpectIntEQ(ret, 1);
  32452. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32453. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32454. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32455. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32456. paddings[i]), 1);
  32457. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32458. (unsigned int)XSTRLEN(testData)), 1);
  32459. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32460. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32461. ExpectIntEQ(ret, 1);
  32462. }
  32463. wolfSSL_EVP_PKEY_free(pubKey);
  32464. wolfSSL_EVP_PKEY_free(privKey);
  32465. #endif
  32466. return EXPECT_RESULT();
  32467. }
  32468. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32469. {
  32470. EXPECT_DECLS;
  32471. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32472. WOLFSSL_EVP_PKEY* privKey = NULL;
  32473. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32474. const char testData[] = "Hi There";
  32475. WOLFSSL_EVP_MD_CTX mdCtx;
  32476. int ret;
  32477. size_t checkSz = -1;
  32478. const unsigned char* cp;
  32479. const unsigned char* p;
  32480. unsigned char check[2048/8];
  32481. cp = ecc_clikey_der_256;
  32482. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32483. sizeof_ecc_clikey_der_256));
  32484. p = ecc_clikeypub_der_256;
  32485. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32486. sizeof_ecc_clikeypub_der_256)));
  32487. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32488. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32489. NULL, privKey), 1);
  32490. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32491. (unsigned int)XSTRLEN(testData)), 1);
  32492. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32493. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32494. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32495. ExpectIntEQ(ret, 1);
  32496. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32497. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32498. NULL, pubKey), 1);
  32499. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32500. (unsigned int)XSTRLEN(testData)),
  32501. 1);
  32502. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32503. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32504. ExpectIntEQ(ret, 1);
  32505. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32506. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32507. NULL, privKey), 1);
  32508. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32509. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32510. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32511. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32512. (unsigned int)XSTRLEN(testData) - 4), 1);
  32513. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32514. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32515. ExpectIntEQ(ret, 1);
  32516. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32517. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32518. NULL, pubKey), 1);
  32519. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32520. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32521. (unsigned int)XSTRLEN(testData) - 4),
  32522. 1);
  32523. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32524. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32525. ExpectIntEQ(ret, 1);
  32526. wolfSSL_EVP_PKEY_free(pubKey);
  32527. wolfSSL_EVP_PKEY_free(privKey);
  32528. #endif
  32529. return EXPECT_RESULT();
  32530. }
  32531. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32532. {
  32533. EXPECT_DECLS;
  32534. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32535. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32536. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32537. char caFile[] = "./certs/client-ca.pem";
  32538. char clientFile[] = "./certs/client-cert.pem";
  32539. SSL_CTX* ctx = NULL;
  32540. X509* x509 = NULL;
  32541. BIO *bio = NULL;
  32542. X509 *cert = NULL;
  32543. X509 *ca = NULL;
  32544. STACK_OF(X509) *chain = NULL;
  32545. STACK_OF(X509) *chain2 = NULL;
  32546. #ifndef NO_WOLFSSL_SERVER
  32547. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32548. #else
  32549. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32550. #endif
  32551. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32552. WOLFSSL_FILETYPE_PEM));
  32553. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32554. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32555. WOLFSSL_FILETYPE_PEM));
  32556. /* additional test of getting EVP_PKEY key size from X509
  32557. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32558. * allowed with user RSA */
  32559. {
  32560. EVP_PKEY* pkey = NULL;
  32561. #if defined(HAVE_ECC)
  32562. X509* ecX509 = NULL;
  32563. #endif /* HAVE_ECC */
  32564. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32565. /* current RSA key is 2048 bit (256 bytes) */
  32566. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32567. EVP_PKEY_free(pkey);
  32568. pkey = NULL;
  32569. #if defined(HAVE_ECC)
  32570. #if defined(USE_CERT_BUFFERS_256)
  32571. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32572. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32573. SSL_FILETYPE_ASN1));
  32574. #else
  32575. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32576. cliEccCertFile, SSL_FILETYPE_PEM));
  32577. #endif
  32578. pkey = X509_get_pubkey(ecX509);
  32579. ExpectNotNull(pkey);
  32580. /* current ECC key is 256 bit (32 bytes) */
  32581. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32582. X509_free(ecX509);
  32583. ecX509 = NULL;
  32584. EVP_PKEY_free(pkey);
  32585. pkey = NULL;
  32586. #endif /* HAVE_ECC */
  32587. }
  32588. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32589. if (EXPECT_SUCCESS()) {
  32590. x509 = NULL;
  32591. }
  32592. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32593. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32594. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32595. #endif
  32596. SSL_CTX_free(ctx);
  32597. ctx = NULL;
  32598. #ifndef NO_WOLFSSL_SERVER
  32599. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32600. #else
  32601. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32602. #endif
  32603. /* Test haproxy use case */
  32604. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32605. /* Read Certificate */
  32606. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32607. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32608. ExpectNotNull(chain = sk_X509_new_null());
  32609. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32610. if (EXPECT_SUCCESS()) {
  32611. ca = NULL;
  32612. }
  32613. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32614. ExpectNotNull(ca = sk_X509_shift(chain2));
  32615. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32616. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32617. if (EXPECT_SUCCESS()) {
  32618. ca = NULL;
  32619. }
  32620. BIO_free(bio);
  32621. X509_free(cert);
  32622. X509_free(ca);
  32623. X509_free(x509);
  32624. sk_X509_pop_free(chain, X509_free);
  32625. sk_X509_pop_free(chain2, X509_free);
  32626. SSL_CTX_free(ctx);
  32627. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32628. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32629. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32630. return EXPECT_RESULT();
  32631. }
  32632. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32633. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32634. {
  32635. EXPECT_DECLS;
  32636. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32637. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32638. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32639. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32640. callback_functions client_cb;
  32641. callback_functions server_cb;
  32642. int line = 0;
  32643. int flag = ERR_TXT_STRING;
  32644. const char* file = NULL;
  32645. const char* data = NULL;
  32646. /* create a failed connection and inspect the error */
  32647. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32648. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32649. client_cb.method = wolfTLSv1_1_client_method;
  32650. server_cb.method = wolfTLSv1_2_server_method;
  32651. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32652. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32653. ExpectNotNull(data);
  32654. /* check clearing error state */
  32655. ERR_remove_state(0);
  32656. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32657. ERR_peek_last_error_line(NULL, &line);
  32658. ExpectIntEQ(line, 0);
  32659. ERR_peek_last_error_line(&file, NULL);
  32660. ExpectNull(file);
  32661. /* retry connection to fill error queue */
  32662. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32663. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32664. client_cb.method = wolfTLSv1_1_client_method;
  32665. server_cb.method = wolfTLSv1_2_server_method;
  32666. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32667. /* check that error code was stored */
  32668. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32669. ERR_peek_last_error_line(NULL, &line);
  32670. ExpectIntNE(line, 0);
  32671. ERR_peek_last_error_line(&file, NULL);
  32672. ExpectNotNull(file);
  32673. fprintf(stderr, "\nTesting error print out\n");
  32674. ERR_print_errors_fp(stderr);
  32675. fprintf(stderr, "Done testing print out\n\n");
  32676. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32677. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32678. return EXPECT_RESULT();
  32679. }
  32680. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32681. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32682. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32683. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32684. {
  32685. (void) ok;
  32686. (void) ctx;
  32687. fprintf(stderr, "ENTER verify_cb\n");
  32688. return SSL_SUCCESS;
  32689. }
  32690. #endif
  32691. static int test_wolfSSL_X509_Name_canon(void)
  32692. {
  32693. EXPECT_DECLS;
  32694. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32695. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32696. defined(WOLFSSL_CERT_GEN) && \
  32697. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32698. const long ex_hash1 = 0x0fdb2da4;
  32699. const long ex_hash2 = 0x9f3e8c9e;
  32700. X509_NAME *name = NULL;
  32701. X509 *x509 = NULL;
  32702. XFILE file = XBADFILE;
  32703. unsigned long hash = 0;
  32704. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32705. byte *pbuf = NULL;
  32706. word32 len = 0;
  32707. (void) ex_hash2;
  32708. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32709. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32710. ExpectNotNull(name = X509_get_issuer_name(x509));
  32711. /* When output buffer is NULL, should return necessary output buffer
  32712. * length.*/
  32713. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32714. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32715. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32716. hash = (((unsigned long)digest[3] << 24) |
  32717. ((unsigned long)digest[2] << 16) |
  32718. ((unsigned long)digest[1] << 8) |
  32719. ((unsigned long)digest[0]));
  32720. ExpectIntEQ(hash, ex_hash1);
  32721. if (file != XBADFILE) {
  32722. XFCLOSE(file);
  32723. file = XBADFILE;
  32724. }
  32725. X509_free(x509);
  32726. x509 = NULL;
  32727. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32728. pbuf = NULL;
  32729. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32730. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32731. ExpectNotNull(name = X509_get_issuer_name(x509));
  32732. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32733. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32734. hash = (((unsigned long)digest[3] << 24) |
  32735. ((unsigned long)digest[2] << 16) |
  32736. ((unsigned long)digest[1] << 8) |
  32737. ((unsigned long)digest[0]));
  32738. ExpectIntEQ(hash, ex_hash2);
  32739. if (file != XBADFILE)
  32740. XFCLOSE(file);
  32741. X509_free(x509);
  32742. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32743. #endif
  32744. return EXPECT_RESULT();
  32745. }
  32746. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32747. {
  32748. EXPECT_DECLS;
  32749. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32750. const int MAX_DIR = 4;
  32751. const char paths[][32] = {
  32752. "./certs/ed25519",
  32753. "./certs/ecc",
  32754. "./certs/crl",
  32755. "./certs/",
  32756. };
  32757. char CertCrl_path[MAX_FILENAME_SZ];
  32758. char *p;
  32759. X509_STORE* str = NULL;
  32760. X509_LOOKUP* lookup = NULL;
  32761. WOLFSSL_STACK* sk = NULL;
  32762. int len, total_len, i;
  32763. (void)sk;
  32764. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32765. /* illegal string */
  32766. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32767. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32768. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32769. SSL_FILETYPE_PEM,NULL), 0);
  32770. /* free store */
  32771. X509_STORE_free(str);
  32772. str = NULL;
  32773. /* short folder string */
  32774. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32775. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32776. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32777. SSL_FILETYPE_PEM,NULL), 1);
  32778. #if defined(WOLFSSL_INT_H)
  32779. /* only available when including internal.h */
  32780. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32781. #endif
  32782. /* free store */
  32783. X509_STORE_free(str);
  32784. str = NULL;
  32785. /* typical function check */
  32786. p = &CertCrl_path[0];
  32787. total_len = 0;
  32788. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32789. len = (int)XSTRLEN((const char*)&paths[i]);
  32790. total_len += len;
  32791. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32792. p += len;
  32793. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32794. }
  32795. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32796. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32797. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32798. SSL_FILETYPE_PEM,NULL), 1);
  32799. #if defined(WOLFSSL_INT_H)
  32800. /* only available when including internal.h */
  32801. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32802. #endif
  32803. X509_STORE_free(str);
  32804. #endif
  32805. return EXPECT_RESULT();
  32806. }
  32807. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32808. {
  32809. EXPECT_DECLS;
  32810. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32811. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32812. defined(WOLFSSL_SIGNER_DER_CERT)
  32813. X509_STORE_CTX* ctx = NULL;
  32814. X509_STORE* str = NULL;
  32815. X509_LOOKUP* lookup = NULL;
  32816. X509* cert1 = NULL;
  32817. X509* x509Ca = NULL;
  32818. X509* x509Svr = NULL;
  32819. X509* issuer = NULL;
  32820. WOLFSSL_STACK* sk = NULL;
  32821. X509_NAME* caName = NULL;
  32822. X509_NAME* issuerName = NULL;
  32823. XFILE file1 = XBADFILE;
  32824. int i;
  32825. int cert_count = 0;
  32826. int cmp;
  32827. char der[] = "certs/ca-cert.der";
  32828. #ifdef HAVE_CRL
  32829. char pem[][100] = {
  32830. "./certs/crl/crl.pem",
  32831. "./certs/crl/crl2.pem",
  32832. "./certs/crl/caEccCrl.pem",
  32833. "./certs/crl/eccCliCRL.pem",
  32834. "./certs/crl/eccSrvCRL.pem",
  32835. ""
  32836. };
  32837. #endif
  32838. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32839. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32840. if (file1 != XBADFILE)
  32841. XFCLOSE(file1);
  32842. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32843. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32844. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32845. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32846. SSL_FILETYPE_PEM,NULL), 1);
  32847. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32848. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32849. /* check if CA cert is loaded into the store */
  32850. for (i = 0; i < cert_count; i++) {
  32851. x509Ca = sk_X509_value(sk, i);
  32852. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32853. }
  32854. ExpectNotNull((x509Svr =
  32855. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32856. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32857. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32858. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32859. ExpectNotNull(issuer);
  32860. caName = X509_get_subject_name(x509Ca);
  32861. ExpectNotNull(caName);
  32862. issuerName = X509_get_subject_name(issuer);
  32863. ExpectNotNull(issuerName);
  32864. cmp = X509_NAME_cmp(caName, issuerName);
  32865. ExpectIntEQ(cmp, 0);
  32866. /* load der format */
  32867. X509_free(issuer);
  32868. issuer = NULL;
  32869. X509_STORE_CTX_free(ctx);
  32870. ctx = NULL;
  32871. X509_STORE_free(str);
  32872. str = NULL;
  32873. sk_X509_pop_free(sk, NULL);
  32874. sk = NULL;
  32875. X509_free(x509Svr);
  32876. x509Svr = NULL;
  32877. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32878. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32879. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32880. SSL_FILETYPE_ASN1,NULL), 1);
  32881. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32882. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32883. /* check if CA cert is loaded into the store */
  32884. for (i = 0; i < cert_count; i++) {
  32885. x509Ca = sk_X509_value(sk, i);
  32886. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32887. }
  32888. X509_STORE_free(str);
  32889. str = NULL;
  32890. sk_X509_pop_free(sk, NULL);
  32891. sk = NULL;
  32892. X509_free(cert1);
  32893. cert1 = NULL;
  32894. #ifdef HAVE_CRL
  32895. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32896. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32897. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32898. SSL_FILETYPE_PEM,NULL), 1);
  32899. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32900. "certs/server-revoked-cert.pem",
  32901. SSL_FILETYPE_PEM,NULL), 1);
  32902. if (str) {
  32903. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32904. WOLFSSL_FILETYPE_PEM), 1);
  32905. /* since store hasn't yet known the revoked cert*/
  32906. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32907. "certs/server-revoked-cert.pem",
  32908. WOLFSSL_FILETYPE_PEM), 1);
  32909. }
  32910. for (i = 0; pem[i][0] != '\0'; i++)
  32911. {
  32912. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32913. SSL_FILETYPE_PEM, NULL), 1);
  32914. }
  32915. if (str) {
  32916. /* since store knows crl list */
  32917. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32918. "certs/server-revoked-cert.pem",
  32919. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32920. }
  32921. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32922. X509_STORE_free(str);
  32923. #endif
  32924. #endif
  32925. return EXPECT_RESULT();
  32926. }
  32927. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32928. {
  32929. int res = TEST_SKIPPED;
  32930. #if defined(OPENSSL_EXTRA)
  32931. X509_STORE_CTX_cleanup(NULL);
  32932. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32933. res = TEST_SUCCESS;
  32934. #endif
  32935. return res;
  32936. }
  32937. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32938. {
  32939. EXPECT_DECLS;
  32940. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32941. X509_STORE_CTX* ctx = NULL;
  32942. X509_STORE* str = NULL;
  32943. X509* x509Ca = NULL;
  32944. X509* x509Svr = NULL;
  32945. X509* issuer = NULL;
  32946. X509_NAME* caName = NULL;
  32947. X509_NAME* issuerName = NULL;
  32948. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32949. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32950. ExpectNotNull((x509Ca =
  32951. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32952. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32953. ExpectNotNull((x509Svr =
  32954. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32955. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32956. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32957. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32958. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32959. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32960. #ifdef WOLFSSL_SIGNER_DER_CERT
  32961. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32962. #endif
  32963. X509_free(issuer);
  32964. X509_STORE_CTX_free(ctx);
  32965. X509_free(x509Svr);
  32966. X509_STORE_free(str);
  32967. X509_free(x509Ca);
  32968. #endif
  32969. return EXPECT_RESULT();
  32970. }
  32971. static int test_wolfSSL_PKCS7_certs(void)
  32972. {
  32973. EXPECT_DECLS;
  32974. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32975. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32976. STACK_OF(X509)* sk = NULL;
  32977. STACK_OF(X509_INFO)* info_sk = NULL;
  32978. PKCS7 *p7 = NULL;
  32979. BIO* bio = NULL;
  32980. const byte* p = NULL;
  32981. int buflen = 0;
  32982. int i;
  32983. /* Test twice. Once with d2i and once without to test
  32984. * that everything is free'd correctly. */
  32985. for (i = 0; i < 2; i++) {
  32986. ExpectNotNull(p7 = PKCS7_new());
  32987. if (p7 != NULL) {
  32988. p7->version = 1;
  32989. #ifdef NO_SHA
  32990. p7->hashOID = SHA256h;
  32991. #else
  32992. p7->hashOID = SHAh;
  32993. #endif
  32994. }
  32995. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32996. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32997. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32998. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32999. ExpectNotNull(sk = sk_X509_new_null());
  33000. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  33001. X509_INFO* info = NULL;
  33002. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  33003. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  33004. if (EXPECT_SUCCESS() && (info != NULL)) {
  33005. info->x509 = NULL;
  33006. }
  33007. X509_INFO_free(info);
  33008. }
  33009. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  33010. info_sk = NULL;
  33011. BIO_free(bio);
  33012. bio = NULL;
  33013. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33014. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  33015. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  33016. sk_X509_pop_free(sk, X509_free);
  33017. }
  33018. sk = NULL;
  33019. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  33020. if (i == 0) {
  33021. PKCS7_free(p7);
  33022. p7 = NULL;
  33023. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  33024. if (p7 != NULL) {
  33025. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  33026. * them */
  33027. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  33028. }
  33029. /* PKCS7_free free's the certs */
  33030. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  33031. }
  33032. BIO_free(bio);
  33033. bio = NULL;
  33034. PKCS7_free(p7);
  33035. p7 = NULL;
  33036. }
  33037. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33038. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  33039. return EXPECT_RESULT();
  33040. }
  33041. static int test_wolfSSL_X509_STORE_CTX(void)
  33042. {
  33043. EXPECT_DECLS;
  33044. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33045. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33046. X509_STORE_CTX* ctx = NULL;
  33047. X509_STORE* str = NULL;
  33048. X509* x509 = NULL;
  33049. #ifdef OPENSSL_ALL
  33050. X509* x5092 = NULL;
  33051. STACK_OF(X509) *sk = NULL;
  33052. STACK_OF(X509) *sk2 = NULL;
  33053. STACK_OF(X509) *sk3 = NULL;
  33054. #endif
  33055. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33056. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  33057. ExpectNotNull((x509 =
  33058. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  33059. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  33060. #ifdef OPENSSL_ALL
  33061. /* sk_X509_new only in OPENSSL_ALL */
  33062. sk = sk_X509_new_null();
  33063. ExpectNotNull(sk);
  33064. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  33065. #else
  33066. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  33067. #endif
  33068. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  33069. X509_STORE_CTX_set_error(ctx, -5);
  33070. X509_STORE_CTX_set_error(NULL, -5);
  33071. X509_STORE_CTX_free(ctx);
  33072. ctx = NULL;
  33073. #ifdef OPENSSL_ALL
  33074. sk_X509_pop_free(sk, NULL);
  33075. sk = NULL;
  33076. #endif
  33077. X509_STORE_free(str);
  33078. str = NULL;
  33079. X509_free(x509);
  33080. x509 = NULL;
  33081. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33082. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  33083. X509_STORE_CTX_free(ctx);
  33084. ctx = NULL;
  33085. #ifdef OPENSSL_ALL
  33086. /* test X509_STORE_CTX_get(1)_chain */
  33087. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  33088. SSL_FILETYPE_PEM)));
  33089. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  33090. SSL_FILETYPE_PEM)));
  33091. ExpectNotNull((sk = sk_X509_new_null()));
  33092. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  33093. if (EXPECT_FAIL()) {
  33094. X509_free(x509);
  33095. x509 = NULL;
  33096. }
  33097. ExpectNotNull((str = X509_STORE_new()));
  33098. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  33099. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  33100. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  33101. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  33102. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  33103. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  33104. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  33105. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  33106. X509_STORE_CTX_free(ctx);
  33107. ctx = NULL;
  33108. X509_STORE_free(str);
  33109. str = NULL;
  33110. /* CTX certs not freed yet */
  33111. X509_free(x5092);
  33112. x5092 = NULL;
  33113. sk_X509_pop_free(sk, NULL);
  33114. sk = NULL;
  33115. /* sk3 is dup so free here */
  33116. sk_X509_pop_free(sk3, NULL);
  33117. sk3 = NULL;
  33118. #endif
  33119. /* test X509_STORE_CTX_get/set_ex_data */
  33120. {
  33121. int i = 0, tmpData = 5;
  33122. void* tmpDataRet;
  33123. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33124. #ifdef HAVE_EX_DATA
  33125. for (i = 0; i < MAX_EX_DATA; i++) {
  33126. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33127. WOLFSSL_SUCCESS);
  33128. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33129. ExpectNotNull(tmpDataRet);
  33130. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33131. }
  33132. #else
  33133. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33134. WOLFSSL_FAILURE);
  33135. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33136. ExpectNull(tmpDataRet);
  33137. #endif
  33138. X509_STORE_CTX_free(ctx);
  33139. ctx = NULL;
  33140. }
  33141. /* test X509_STORE_get/set_ex_data */
  33142. {
  33143. int i = 0, tmpData = 99;
  33144. void* tmpDataRet;
  33145. ExpectNotNull(str = X509_STORE_new());
  33146. #ifdef HAVE_EX_DATA
  33147. for (i = 0; i < MAX_EX_DATA; i++) {
  33148. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33149. WOLFSSL_SUCCESS);
  33150. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33151. ExpectNotNull(tmpDataRet);
  33152. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33153. }
  33154. #else
  33155. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33156. WOLFSSL_FAILURE);
  33157. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33158. ExpectNull(tmpDataRet);
  33159. #endif
  33160. X509_STORE_free(str);
  33161. str = NULL;
  33162. }
  33163. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33164. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33165. return EXPECT_RESULT();
  33166. }
  33167. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33168. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  33169. STACK_OF(X509)* chain)
  33170. {
  33171. EXPECT_DECLS;
  33172. XFILE fp = XBADFILE;
  33173. X509* cert = NULL;
  33174. ExpectTrue((fp = XFOPEN(filename, "rb"))
  33175. != XBADFILE);
  33176. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33177. if (fp != XBADFILE) {
  33178. XFCLOSE(fp);
  33179. fp = XBADFILE;
  33180. }
  33181. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  33182. if (EXPECT_FAIL())
  33183. X509_free(cert);
  33184. return EXPECT_RESULT();
  33185. }
  33186. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  33187. int err, int loadCA)
  33188. {
  33189. EXPECT_DECLS;
  33190. X509_STORE_CTX* ctx = NULL;
  33191. X509_STORE* str = NULL;
  33192. XFILE fp = XBADFILE;
  33193. X509* cert = NULL;
  33194. STACK_OF(X509)* untrusted = NULL;
  33195. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  33196. != XBADFILE);
  33197. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33198. if (fp != XBADFILE) {
  33199. XFCLOSE(fp);
  33200. fp = XBADFILE;
  33201. }
  33202. ExpectNotNull(str = X509_STORE_new());
  33203. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33204. ExpectNotNull(untrusted = sk_X509_new_null());
  33205. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  33206. if (loadCA) {
  33207. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  33208. 1);
  33209. }
  33210. for (; *filenames; filenames++) {
  33211. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  33212. untrusted), TEST_SUCCESS);
  33213. }
  33214. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  33215. ExpectIntEQ(X509_verify_cert(ctx), ret);
  33216. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  33217. X509_free(cert);
  33218. X509_STORE_free(str);
  33219. X509_STORE_CTX_free(ctx);
  33220. sk_X509_pop_free(untrusted, NULL);
  33221. return EXPECT_RESULT();
  33222. }
  33223. #endif
  33224. static int test_X509_STORE_untrusted(void)
  33225. {
  33226. EXPECT_DECLS;
  33227. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33228. const char* untrusted1[] = {
  33229. "./certs/intermediate/ca-int2-cert.pem",
  33230. NULL
  33231. };
  33232. const char* untrusted2[] = {
  33233. "./certs/intermediate/ca-int-cert.pem",
  33234. "./certs/intermediate/ca-int2-cert.pem",
  33235. NULL
  33236. };
  33237. const char* untrusted3[] = {
  33238. "./certs/intermediate/ca-int-cert.pem",
  33239. "./certs/intermediate/ca-int2-cert.pem",
  33240. "./certs/ca-cert.pem",
  33241. NULL
  33242. };
  33243. /* Adding unrelated certs that should be ignored */
  33244. const char* untrusted4[] = {
  33245. "./certs/client-ca.pem",
  33246. "./certs/intermediate/ca-int-cert.pem",
  33247. "./certs/server-cert.pem",
  33248. "./certs/intermediate/ca-int2-cert.pem",
  33249. NULL
  33250. };
  33251. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33252. * to loaded CA. */
  33253. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33254. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33255. /* Succeeds because path to loaded CA is available. */
  33256. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33257. TEST_SUCCESS);
  33258. /* Fails because root CA is in the untrusted stack */
  33259. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33260. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33261. /* Succeeds because path to loaded CA is available. */
  33262. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33263. TEST_SUCCESS);
  33264. #endif
  33265. return EXPECT_RESULT();
  33266. }
  33267. static int test_wolfSSL_X509_STORE_set_flags(void)
  33268. {
  33269. EXPECT_DECLS;
  33270. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33271. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33272. X509_STORE* store = NULL;
  33273. X509* x509 = NULL;
  33274. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33275. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33276. WOLFSSL_FILETYPE_PEM)));
  33277. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33278. #ifdef HAVE_CRL
  33279. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33280. WOLFSSL_SUCCESS);
  33281. #else
  33282. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33283. NOT_COMPILED_IN);
  33284. #endif
  33285. wolfSSL_X509_free(x509);
  33286. wolfSSL_X509_STORE_free(store);
  33287. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33288. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33289. return EXPECT_RESULT();
  33290. }
  33291. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33292. {
  33293. EXPECT_DECLS;
  33294. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33295. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33296. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33297. WOLFSSL_X509_STORE* store = NULL;
  33298. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33299. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33300. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33301. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33302. X509_FILETYPE_PEM), 1);
  33303. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33304. X509_FILETYPE_PEM), 1);
  33305. if (store != NULL) {
  33306. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33307. WOLFSSL_FILETYPE_PEM), 1);
  33308. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33309. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33310. }
  33311. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33312. X509_FILETYPE_PEM), 1);
  33313. if (store != NULL) {
  33314. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33315. WOLFSSL_FILETYPE_PEM), 1);
  33316. }
  33317. wolfSSL_X509_STORE_free(store);
  33318. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33319. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33320. return EXPECT_RESULT();
  33321. }
  33322. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33323. {
  33324. EXPECT_DECLS;
  33325. #if defined(OPENSSL_EXTRA)
  33326. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33327. time_t c_time;
  33328. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33329. c_time = 365*24*60*60;
  33330. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33331. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33332. WOLFSSL_USE_CHECK_TIME);
  33333. ExpectTrue(ctx->param->check_time == c_time);
  33334. wolfSSL_X509_STORE_CTX_free(ctx);
  33335. #endif /* OPENSSL_EXTRA */
  33336. return EXPECT_RESULT();
  33337. }
  33338. static int test_wolfSSL_CTX_get0_set1_param(void)
  33339. {
  33340. EXPECT_DECLS;
  33341. #if defined(OPENSSL_EXTRA)
  33342. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33343. SSL_CTX* ctx = NULL;
  33344. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33345. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33346. char testIPv4[] = "127.0.0.1";
  33347. char testhostName[] = "foo.hoge.com";
  33348. #ifndef NO_WOLFSSL_SERVER
  33349. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33350. #else
  33351. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33352. #endif
  33353. ExpectNull(SSL_CTX_get0_param(NULL));
  33354. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33355. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33356. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33357. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33358. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33359. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33360. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33361. WOLFSSL_SUCCESS);
  33362. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33363. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33364. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33365. (int)XSTRLEN(testhostName)));
  33366. ExpectIntEQ(0x01, pParam->hostFlags);
  33367. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33368. /* test for incorrect parameter */
  33369. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33370. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33371. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33372. SSL_CTX_free(ctx);
  33373. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33374. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33375. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33376. return EXPECT_RESULT();
  33377. }
  33378. static int test_wolfSSL_get0_param(void)
  33379. {
  33380. EXPECT_DECLS;
  33381. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33382. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33383. SSL_CTX* ctx = NULL;
  33384. SSL* ssl = NULL;
  33385. #ifndef NO_WOLFSSL_SERVER
  33386. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33387. #else
  33388. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33389. #endif
  33390. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33391. SSL_FILETYPE_PEM));
  33392. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33393. ExpectNotNull(ssl = SSL_new(ctx));
  33394. ExpectNotNull(SSL_get0_param(ssl));
  33395. SSL_free(ssl);
  33396. SSL_CTX_free(ctx);
  33397. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33398. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33399. return EXPECT_RESULT();
  33400. }
  33401. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33402. {
  33403. EXPECT_DECLS;
  33404. #if defined(OPENSSL_EXTRA)
  33405. const char host[] = "www.example.com";
  33406. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33407. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33408. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33409. if (pParam != NULL) {
  33410. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33411. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33412. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33413. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33414. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33415. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33416. }
  33417. #endif /* OPENSSL_EXTRA */
  33418. return EXPECT_RESULT();
  33419. }
  33420. static int test_wolfSSL_set1_host(void)
  33421. {
  33422. EXPECT_DECLS;
  33423. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33424. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33425. const char host[] = "www.test_wolfSSL_set1_host.com";
  33426. const char emptyStr[] = "";
  33427. SSL_CTX* ctx = NULL;
  33428. SSL* ssl = NULL;
  33429. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33430. #ifndef NO_WOLFSSL_SERVER
  33431. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33432. #else
  33433. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33434. #endif
  33435. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33436. SSL_FILETYPE_PEM));
  33437. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33438. ExpectNotNull(ssl = SSL_new(ctx));
  33439. pParam = SSL_get0_param(ssl);
  33440. /* we should get back host string */
  33441. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33442. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33443. /* we should get back empty string */
  33444. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33445. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33446. /* we should get back host string */
  33447. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33448. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33449. /* we should get back empty string */
  33450. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33451. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33452. SSL_free(ssl);
  33453. SSL_CTX_free(ctx);
  33454. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33455. #endif /* OPENSSL_EXTRA */
  33456. return EXPECT_RESULT();
  33457. }
  33458. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33459. {
  33460. EXPECT_DECLS;
  33461. #if defined(OPENSSL_EXTRA)
  33462. unsigned char buf[16] = {0};
  33463. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33464. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33465. /* test 127.0.0.1 */
  33466. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33467. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33468. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33469. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33470. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33471. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33472. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33473. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33474. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33475. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33476. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33477. /* test 2001:db8:: */
  33478. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33479. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33480. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33481. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33482. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33483. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33484. /* test ::1234:5678 */
  33485. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33486. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33487. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33488. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33489. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33490. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33491. /* test 2001:db8::1234:5678 */
  33492. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33493. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33494. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33495. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33496. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33497. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33498. sizeof(param->ipasc)), 0);
  33499. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33500. /* 2001:db8:1::ab9:c0a8:102 */
  33501. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33502. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33503. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33504. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33505. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33506. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33507. sizeof(param->ipasc)), 0);
  33508. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33509. #endif /* OPENSSL_EXTRA */
  33510. return EXPECT_RESULT();
  33511. }
  33512. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33513. {
  33514. EXPECT_DECLS;
  33515. #if defined(OPENSSL_EXTRA)
  33516. X509_STORE* store = NULL;
  33517. X509_STORE_CTX* ctx = NULL;
  33518. X509_STORE_CTX* ctx_no_init = NULL;
  33519. ExpectNotNull((store = X509_STORE_new()));
  33520. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33521. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33522. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33523. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33524. /* should return NULL if ctx has not bee initialized */
  33525. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33526. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33527. wolfSSL_X509_STORE_CTX_free(ctx);
  33528. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33529. X509_STORE_free(store);
  33530. #endif /* OPENSSL_EXTRA */
  33531. return EXPECT_RESULT();
  33532. }
  33533. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33534. {
  33535. EXPECT_DECLS;
  33536. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33537. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33538. WOLFSSL_CTX* ctx = NULL;
  33539. WOLFSSL* ssl = NULL;
  33540. X509_NAME* name = NULL;
  33541. STACK_OF(X509_NAME)* names = NULL;
  33542. STACK_OF(X509_NAME)* ca_list = NULL;
  33543. int names_len = 0;
  33544. int i;
  33545. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33546. /* Send two X501 names in cert request */
  33547. names = SSL_load_client_CA_file(cliCertFile);
  33548. ExpectNotNull(names);
  33549. ca_list = SSL_load_client_CA_file(caCertFile);
  33550. ExpectNotNull(ca_list);
  33551. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33552. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33553. if (EXPECT_FAIL()) {
  33554. wolfSSL_X509_NAME_free(name);
  33555. name = NULL;
  33556. }
  33557. SSL_CTX_set_client_CA_list(ctx, names);
  33558. /* This should only free the stack structure */
  33559. sk_X509_NAME_free(ca_list);
  33560. ca_list = NULL;
  33561. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33562. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33563. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33564. for (i = 0; i < names_len; i++) {
  33565. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33566. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33567. }
  33568. /* Needed to be able to create ssl object */
  33569. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33570. SSL_FILETYPE_PEM));
  33571. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33572. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33573. /* load again as old names are responsibility of ctx to free*/
  33574. names = SSL_load_client_CA_file(cliCertFile);
  33575. ExpectNotNull(names);
  33576. SSL_set_client_CA_list(ssl, names);
  33577. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33578. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33579. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33580. for (i = 0; i < names_len; i++) {
  33581. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33582. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33583. }
  33584. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33585. {
  33586. tcp_ready ready;
  33587. func_args server_args;
  33588. callback_functions server_cb;
  33589. THREAD_TYPE serverThread;
  33590. WOLFSSL* ssl_client = NULL;
  33591. WOLFSSL_CTX* ctx_client = NULL;
  33592. SOCKET_T sockfd = 0;
  33593. /* wolfSSL_get_client_CA_list() with handshake */
  33594. StartTCP();
  33595. InitTcpReady(&ready);
  33596. XMEMSET(&server_args, 0, sizeof(func_args));
  33597. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33598. server_args.signal = &ready;
  33599. server_args.callbacks = &server_cb;
  33600. /* we are responsible for free'ing WOLFSSL_CTX */
  33601. server_cb.ctx = ctx;
  33602. server_cb.isSharedCtx = 1;
  33603. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33604. cliCertFile, 0));
  33605. start_thread(test_server_nofail, &server_args, &serverThread);
  33606. wait_tcp_ready(&server_args);
  33607. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33608. ExpectNotNull(ctx_client =
  33609. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33610. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33611. ctx_client, caCertFile, 0));
  33612. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33613. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33614. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33615. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33616. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33617. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33618. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33619. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33620. /* We are expecting two cert names to be sent */
  33621. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33622. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33623. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33624. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33625. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33626. }
  33627. wolfSSL_shutdown(ssl_client);
  33628. wolfSSL_free(ssl_client);
  33629. wolfSSL_CTX_free(ctx_client);
  33630. CloseSocket(sockfd);
  33631. join_thread(serverThread);
  33632. FreeTcpReady(&ready);
  33633. }
  33634. #endif
  33635. wolfSSL_free(ssl);
  33636. wolfSSL_CTX_free(ctx);
  33637. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33638. * !NO_BIO */
  33639. return EXPECT_RESULT();
  33640. }
  33641. static int test_wolfSSL_CTX_add_client_CA(void)
  33642. {
  33643. EXPECT_DECLS;
  33644. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33645. !defined(NO_WOLFSSL_CLIENT)
  33646. WOLFSSL_CTX* ctx = NULL;
  33647. WOLFSSL_X509* x509 = NULL;
  33648. WOLFSSL_X509* x509_a = NULL;
  33649. STACK_OF(X509_NAME)* ca_list = NULL;
  33650. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33651. /* Add client cert */
  33652. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33653. SSL_FILETYPE_PEM));
  33654. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33655. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33656. /* Add another client cert */
  33657. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33658. SSL_FILETYPE_PEM));
  33659. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33660. /* test for incorrect parameter */
  33661. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33662. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33663. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33664. X509_free(x509);
  33665. X509_free(x509_a);
  33666. SSL_CTX_free(ctx);
  33667. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33668. return EXPECT_RESULT();
  33669. }
  33670. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33671. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33672. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33673. {
  33674. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33675. WOLFSSL_CTX* ctx = callbacks->ctx;
  33676. WOLFSSL* ssl = NULL;
  33677. SOCKET_T sfd = 0;
  33678. SOCKET_T cfd = 0;
  33679. word16 port;
  33680. char input[1024];
  33681. int idx;
  33682. int ret, err = 0;
  33683. const char* privateName = "ech-private-name.com";
  33684. int privateNameLen = (int)XSTRLEN(privateName);
  33685. ((func_args*)args)->return_code = TEST_FAIL;
  33686. port = ((func_args*)args)->signal->port;
  33687. AssertIntEQ(WOLFSSL_SUCCESS,
  33688. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33689. AssertIntEQ(WOLFSSL_SUCCESS,
  33690. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33691. WOLFSSL_FILETYPE_PEM));
  33692. AssertIntEQ(WOLFSSL_SUCCESS,
  33693. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33694. WOLFSSL_FILETYPE_PEM));
  33695. if (callbacks->ctx_ready)
  33696. callbacks->ctx_ready(ctx);
  33697. ssl = wolfSSL_new(ctx);
  33698. /* set the sni for the server */
  33699. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33700. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33701. CloseSocket(sfd);
  33702. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33703. if (callbacks->ssl_ready)
  33704. callbacks->ssl_ready(ssl);
  33705. do {
  33706. err = 0; /* Reset error */
  33707. ret = wolfSSL_accept(ssl);
  33708. if (ret != WOLFSSL_SUCCESS) {
  33709. err = wolfSSL_get_error(ssl, 0);
  33710. }
  33711. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33712. if (ret != WOLFSSL_SUCCESS) {
  33713. char buff[WOLFSSL_MAX_ERROR_SZ];
  33714. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33715. }
  33716. else {
  33717. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33718. input[idx] = 0;
  33719. fprintf(stderr, "Client message: %s\n", input);
  33720. }
  33721. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33722. privateNameLen));
  33723. ((func_args*)args)->return_code = TEST_SUCCESS;
  33724. }
  33725. if (callbacks->on_result)
  33726. callbacks->on_result(ssl);
  33727. wolfSSL_shutdown(ssl);
  33728. wolfSSL_free(ssl);
  33729. wolfSSL_CTX_free(ctx);
  33730. CloseSocket(cfd);
  33731. #ifdef FP_ECC
  33732. wc_ecc_fp_free();
  33733. #endif
  33734. WOLFSSL_RETURN_FROM_THREAD(0);
  33735. }
  33736. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33737. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33738. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33739. {
  33740. AssertNotNull(ssl);
  33741. AssertNotNull(line);
  33742. XFILE fp;
  33743. const byte lf = '\n';
  33744. fp = XFOPEN("./MyKeyLog.txt", "a");
  33745. XFWRITE( line, 1, strlen(line),fp);
  33746. XFWRITE( (void*)&lf,1,1,fp);
  33747. XFFLUSH(fp);
  33748. XFCLOSE(fp);
  33749. }
  33750. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33751. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33752. {
  33753. EXPECT_DECLS;
  33754. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33755. !defined(NO_WOLFSSL_CLIENT)
  33756. SSL_CTX* ctx = NULL;
  33757. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33758. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33759. SSL_CTX_free(ctx);
  33760. SSL_CTX_set_keylog_callback(NULL, NULL);
  33761. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33762. return EXPECT_RESULT();
  33763. }
  33764. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33765. {
  33766. EXPECT_DECLS;
  33767. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33768. !defined(NO_WOLFSSL_CLIENT)
  33769. SSL_CTX* ctx = NULL;
  33770. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33771. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33772. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33773. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33774. SSL_CTX_set_keylog_callback(ctx, NULL );
  33775. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33776. SSL_CTX_free(ctx);
  33777. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33778. return EXPECT_RESULT();
  33779. }
  33780. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33781. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33782. {
  33783. /* set keylog callback */
  33784. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33785. return TEST_SUCCESS;
  33786. }
  33787. #endif
  33788. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33789. {
  33790. EXPECT_DECLS;
  33791. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33792. /* This test is intended for checking whether keylog callback is called
  33793. * in client during TLS handshake between the client and a server.
  33794. */
  33795. test_ssl_cbf server_cbf;
  33796. test_ssl_cbf client_cbf;
  33797. XFILE fp = XBADFILE;
  33798. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33799. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33800. server_cbf.method = wolfTLSv1_2_server_method;
  33801. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33802. /* clean up keylog file */
  33803. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33804. if (fp != XBADFILE) {
  33805. XFFLUSH(fp);
  33806. XFCLOSE(fp);
  33807. fp = XBADFILE;
  33808. }
  33809. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33810. &server_cbf, NULL), TEST_SUCCESS);
  33811. XSLEEP_MS(100);
  33812. /* check if the keylog file exists */
  33813. char buff[300] = {0};
  33814. int found = 0;
  33815. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33816. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33817. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33818. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33819. found = 1;
  33820. break;
  33821. }
  33822. }
  33823. if (fp != XBADFILE) {
  33824. XFCLOSE(fp);
  33825. }
  33826. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33827. ExpectIntEQ(found, 1);
  33828. /* clean up */
  33829. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33830. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33831. return EXPECT_RESULT();
  33832. }
  33833. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33834. defined(HAVE_SECRET_CALLBACK)
  33835. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33836. {
  33837. /* set keylog callback */
  33838. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33839. return TEST_SUCCESS;
  33840. }
  33841. #endif
  33842. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33843. {
  33844. EXPECT_DECLS;
  33845. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33846. defined(HAVE_SECRET_CALLBACK)
  33847. /* This test is intended for checking whether keylog callback is called
  33848. * in client during TLS handshake between the client and a server.
  33849. */
  33850. test_ssl_cbf server_cbf;
  33851. test_ssl_cbf client_cbf;
  33852. XFILE fp = XBADFILE;
  33853. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33854. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33855. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33856. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33857. /* clean up keylog file */
  33858. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33859. if (fp != XBADFILE) {
  33860. XFCLOSE(fp);
  33861. fp = XBADFILE;
  33862. }
  33863. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33864. &server_cbf, NULL), TEST_SUCCESS);
  33865. /* check if the keylog file exists */
  33866. {
  33867. char buff[300] = {0};
  33868. int found[4] = {0};
  33869. int numfnd = 0;
  33870. int i;
  33871. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33872. while (EXPECT_SUCCESS() &&
  33873. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33874. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33875. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33876. found[0] = 1;
  33877. continue;
  33878. }
  33879. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33880. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33881. found[1] = 1;
  33882. continue;
  33883. }
  33884. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33885. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33886. found[2] = 1;
  33887. continue;
  33888. }
  33889. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33890. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33891. found[3] = 1;
  33892. continue;
  33893. }
  33894. }
  33895. if (fp != XBADFILE)
  33896. XFCLOSE(fp);
  33897. for (i = 0; i < 4; i++) {
  33898. if (found[i] != 0)
  33899. numfnd++;
  33900. }
  33901. ExpectIntEQ(numfnd, 4);
  33902. }
  33903. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33904. return EXPECT_RESULT();
  33905. }
  33906. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33907. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33908. static int test_wolfSSL_Tls13_ECH_params(void)
  33909. {
  33910. EXPECT_DECLS;
  33911. #if !defined(NO_WOLFSSL_CLIENT)
  33912. word32 outputLen = 0;
  33913. byte testBuf[72];
  33914. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33915. WOLFSSL *ssl = wolfSSL_new(ctx);
  33916. ExpectNotNull(ctx);
  33917. ExpectNotNull(ssl);
  33918. /* invalid ctx */
  33919. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33920. "ech-public-name.com", 0, 0, 0));
  33921. /* invalid public name */
  33922. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33923. 0, 0));
  33924. /* invalid algorithms */
  33925. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33926. "ech-public-name.com", 1000, 1000, 1000));
  33927. /* invalid ctx */
  33928. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33929. &outputLen));
  33930. /* invalid output len */
  33931. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33932. /* invalid ssl */
  33933. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33934. (char*)testBuf, sizeof(testBuf)));
  33935. /* invalid configs64 */
  33936. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33937. sizeof(testBuf)));
  33938. /* invalid size */
  33939. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33940. (char*)testBuf, 0));
  33941. /* invalid ssl */
  33942. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33943. sizeof(testBuf)));
  33944. /* invalid configs */
  33945. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33946. sizeof(testBuf)));
  33947. /* invalid size */
  33948. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33949. /* invalid ssl */
  33950. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33951. /* invalid size */
  33952. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33953. wolfSSL_free(ssl);
  33954. wolfSSL_CTX_free(ctx);
  33955. #endif /* !NO_WOLFSSL_CLIENT */
  33956. return EXPECT_RESULT();
  33957. }
  33958. static int test_wolfSSL_Tls13_ECH(void)
  33959. {
  33960. EXPECT_DECLS;
  33961. tcp_ready ready;
  33962. func_args client_args;
  33963. func_args server_args;
  33964. THREAD_TYPE serverThread;
  33965. callback_functions server_cbf;
  33966. callback_functions client_cbf;
  33967. SOCKET_T sockfd = 0;
  33968. WOLFSSL_CTX* ctx = NULL;
  33969. WOLFSSL* ssl = NULL;
  33970. const char* publicName = "ech-public-name.com";
  33971. const char* privateName = "ech-private-name.com";
  33972. int privateNameLen = 20;
  33973. char reply[1024];
  33974. int replyLen = 0;
  33975. byte rawEchConfig[128];
  33976. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33977. InitTcpReady(&ready);
  33978. ready.port = 22222;
  33979. XMEMSET(&client_args, 0, sizeof(func_args));
  33980. XMEMSET(&server_args, 0, sizeof(func_args));
  33981. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33982. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33983. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33984. /* create the server context here so we can get the ech config */
  33985. ExpectNotNull(server_cbf.ctx =
  33986. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33987. /* generate ech config */
  33988. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33989. publicName, 0, 0, 0));
  33990. /* get the config for the client to use */
  33991. ExpectIntEQ(WOLFSSL_SUCCESS,
  33992. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33993. &rawEchConfigLen));
  33994. server_args.callbacks = &server_cbf;
  33995. server_args.signal = &ready;
  33996. /* start server task */
  33997. start_thread(server_task_ech, &server_args, &serverThread);
  33998. wait_tcp_ready(&server_args);
  33999. /* run as a TLS1.3 client */
  34000. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  34001. ExpectIntEQ(WOLFSSL_SUCCESS,
  34002. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34003. ExpectIntEQ(WOLFSSL_SUCCESS,
  34004. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34005. ExpectIntEQ(WOLFSSL_SUCCESS,
  34006. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34007. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  34008. /* get connected the server task */
  34009. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34010. /* set the ech configs for the client */
  34011. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  34012. rawEchConfigLen));
  34013. /* set the sni for the client */
  34014. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  34015. privateName, privateNameLen));
  34016. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34017. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  34018. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  34019. privateNameLen);
  34020. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  34021. /* add th null terminator for string compare */
  34022. reply[replyLen] = 0;
  34023. /* check that the server replied with the private name */
  34024. ExpectStrEQ(privateName, reply);
  34025. wolfSSL_free(ssl);
  34026. wolfSSL_CTX_free(ctx);
  34027. CloseSocket(sockfd);
  34028. join_thread(serverThread);
  34029. FreeTcpReady(&ready);
  34030. return EXPECT_RESULT();
  34031. }
  34032. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  34033. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34034. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34035. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34036. static int post_auth_version_cb(WOLFSSL* ssl)
  34037. {
  34038. EXPECT_DECLS;
  34039. /* do handshake and then test version error */
  34040. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  34041. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  34042. return EXPECT_RESULT();
  34043. }
  34044. static int post_auth_version_client_cb(WOLFSSL* ssl)
  34045. {
  34046. EXPECT_DECLS;
  34047. /* do handshake and then test version error */
  34048. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  34049. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  34050. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  34051. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  34052. /* check was added to error queue */
  34053. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  34054. /* check the string matches expected string */
  34055. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  34056. "WRONG_SSL_VERSION");
  34057. #endif
  34058. return EXPECT_RESULT();
  34059. }
  34060. static int post_auth_cb(WOLFSSL* ssl)
  34061. {
  34062. EXPECT_DECLS;
  34063. WOLFSSL_X509* x509 = NULL;
  34064. /* do handshake and then test version error */
  34065. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  34066. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  34067. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  34068. wolfSSL_X509_free(x509);
  34069. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  34070. return EXPECT_RESULT();
  34071. }
  34072. static int set_post_auth_cb(WOLFSSL* ssl)
  34073. {
  34074. if (!wolfSSL_is_server(ssl)) {
  34075. EXPECT_DECLS;
  34076. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  34077. return EXPECT_RESULT();
  34078. }
  34079. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  34080. return TEST_SUCCESS;
  34081. }
  34082. #endif
  34083. static int test_wolfSSL_Tls13_postauth(void)
  34084. {
  34085. EXPECT_DECLS;
  34086. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34087. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34088. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  34089. test_ssl_cbf server_cbf;
  34090. test_ssl_cbf client_cbf;
  34091. /* test version failure doing post auth with TLS 1.2 connection */
  34092. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  34093. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  34094. server_cbf.method = wolfTLSv1_2_server_method;
  34095. server_cbf.ssl_ready = set_post_auth_cb;
  34096. server_cbf.on_result = post_auth_version_cb;
  34097. client_cbf.ssl_ready = set_post_auth_cb;
  34098. client_cbf.on_result = post_auth_version_client_cb;
  34099. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34100. &server_cbf, NULL), TEST_SUCCESS);
  34101. /* tests on post auth with TLS 1.3 */
  34102. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  34103. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  34104. server_cbf.method = wolfTLSv1_3_server_method;
  34105. server_cbf.ssl_ready = set_post_auth_cb;
  34106. client_cbf.ssl_ready = set_post_auth_cb;
  34107. server_cbf.on_result = post_auth_cb;
  34108. client_cbf.on_result = NULL;
  34109. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34110. &server_cbf, NULL), TEST_SUCCESS);
  34111. #endif
  34112. return EXPECT_RESULT();
  34113. }
  34114. static int test_wolfSSL_X509_NID(void)
  34115. {
  34116. EXPECT_DECLS;
  34117. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  34118. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  34119. int sigType;
  34120. int nameSz;
  34121. X509* cert = NULL;
  34122. EVP_PKEY* pubKeyTmp = NULL;
  34123. X509_NAME* name = NULL;
  34124. char commonName[80];
  34125. char countryName[80];
  34126. char localityName[80];
  34127. char stateName[80];
  34128. char orgName[80];
  34129. char orgUnit[80];
  34130. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  34131. /* convert cert from DER to internal WOLFSSL_X509 struct */
  34132. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  34133. sizeof_client_cert_der_2048, HEAP_HINT));
  34134. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  34135. /* extract PUBLIC KEY from cert */
  34136. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  34137. /* extract signatureType */
  34138. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  34139. /* extract subjectName info */
  34140. ExpectNotNull(name = X509_get_subject_name(cert));
  34141. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  34142. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34143. NULL, 0)), 0);
  34144. ExpectIntEQ(nameSz, 15);
  34145. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34146. commonName, sizeof(commonName))), 0);
  34147. ExpectIntEQ(nameSz, 15);
  34148. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  34149. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34150. commonName, 9)), 0);
  34151. ExpectIntEQ(nameSz, 8);
  34152. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  34153. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  34154. countryName, sizeof(countryName))), 0);
  34155. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  34156. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  34157. localityName, sizeof(localityName))), 0);
  34158. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  34159. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34160. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  34161. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  34162. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  34163. orgName, sizeof(orgName))), 0);
  34164. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  34165. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34166. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  34167. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  34168. EVP_PKEY_free(pubKeyTmp);
  34169. X509_free(cert);
  34170. #endif
  34171. return EXPECT_RESULT();
  34172. }
  34173. static int test_wolfSSL_CTX_set_srp_username(void)
  34174. {
  34175. EXPECT_DECLS;
  34176. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34177. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34178. WOLFSSL_CTX* ctx = NULL;
  34179. WOLFSSL* ssl = NULL;
  34180. const char *username = "TESTUSER";
  34181. const char *password = "TESTPASSWORD";
  34182. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34183. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34184. SSL_SUCCESS);
  34185. wolfSSL_CTX_free(ctx);
  34186. ctx = NULL;
  34187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34188. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34189. SSL_SUCCESS);
  34190. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34191. SSL_SUCCESS);
  34192. ExpectNotNull(ssl = SSL_new(ctx));
  34193. ExpectNotNull(SSL_get_srp_username(ssl));
  34194. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  34195. wolfSSL_free(ssl);
  34196. wolfSSL_CTX_free(ctx);
  34197. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34198. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34199. return EXPECT_RESULT();
  34200. }
  34201. static int test_wolfSSL_CTX_set_srp_password(void)
  34202. {
  34203. EXPECT_DECLS;
  34204. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  34205. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34206. WOLFSSL_CTX* ctx = NULL;
  34207. const char *username = "TESTUSER";
  34208. const char *password = "TESTPASSWORD";
  34209. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34210. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34211. SSL_SUCCESS);
  34212. wolfSSL_CTX_free(ctx);
  34213. ctx = NULL;
  34214. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34215. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34216. SSL_SUCCESS);
  34217. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34218. SSL_SUCCESS);
  34219. wolfSSL_CTX_free(ctx);
  34220. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34221. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34222. return EXPECT_RESULT();
  34223. }
  34224. static int test_wolfSSL_X509_STORE(void)
  34225. {
  34226. EXPECT_DECLS;
  34227. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34228. X509_STORE *store = NULL;
  34229. #ifdef HAVE_CRL
  34230. X509_STORE_CTX *storeCtx = NULL;
  34231. X509_CRL *crl = NULL;
  34232. X509 *ca = NULL;
  34233. X509 *cert = NULL;
  34234. const char crlPem[] = "./certs/crl/crl.revoked";
  34235. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34236. const char caCert[] = "./certs/ca-cert.pem";
  34237. XFILE fp = XBADFILE;
  34238. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34239. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34240. SSL_FILETYPE_PEM)));
  34241. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34242. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34243. SSL_FILETYPE_PEM)));
  34244. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34245. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34246. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34247. X509_STORE_free(store);
  34248. store = NULL;
  34249. X509_STORE_CTX_free(storeCtx);
  34250. storeCtx = NULL;
  34251. X509_free(cert);
  34252. cert = NULL;
  34253. X509_free(ca);
  34254. ca = NULL;
  34255. /* should fail to verify now after adding in CRL */
  34256. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34257. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34258. SSL_FILETYPE_PEM)));
  34259. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34260. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34261. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34262. NULL, NULL));
  34263. if (fp != XBADFILE)
  34264. XFCLOSE(fp);
  34265. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34266. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34267. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34268. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34269. SSL_FILETYPE_PEM)));
  34270. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34271. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34272. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34273. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34274. X509_CRL_free(crl);
  34275. crl = NULL;
  34276. X509_STORE_free(store);
  34277. store = NULL;
  34278. X509_STORE_CTX_free(storeCtx);
  34279. storeCtx = NULL;
  34280. X509_free(cert);
  34281. cert = NULL;
  34282. X509_free(ca);
  34283. ca = NULL;
  34284. #endif /* HAVE_CRL */
  34285. #ifndef WOLFCRYPT_ONLY
  34286. {
  34287. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34288. SSL_CTX* ctx = NULL;
  34289. SSL* ssl = NULL;
  34290. int i;
  34291. for (i = 0; i < 2; i++) {
  34292. #ifndef NO_WOLFSSL_SERVER
  34293. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34294. #else
  34295. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34296. #endif
  34297. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34298. SSL_CTX_set_cert_store(ctx, store);
  34299. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34300. SSL_CTX_set_cert_store(ctx, store);
  34301. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34302. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34303. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34304. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34305. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34306. ExpectNotNull(ssl = SSL_new(ctx));
  34307. if (i == 0) {
  34308. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34309. SSL_SUCCESS);
  34310. }
  34311. else {
  34312. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34313. #ifdef OPENSSL_ALL
  34314. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34315. #endif
  34316. }
  34317. if (EXPECT_FAIL() || (i == 1)) {
  34318. X509_STORE_free(store);
  34319. store = NULL;
  34320. }
  34321. SSL_free(ssl);
  34322. ssl = NULL;
  34323. SSL_CTX_free(ctx);
  34324. ctx = NULL;
  34325. }
  34326. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34327. }
  34328. #endif
  34329. #endif
  34330. return EXPECT_RESULT();
  34331. }
  34332. static int test_wolfSSL_X509_STORE_load_locations(void)
  34333. {
  34334. EXPECT_DECLS;
  34335. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34336. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34337. SSL_CTX *ctx = NULL;
  34338. X509_STORE *store = NULL;
  34339. const char ca_file[] = "./certs/ca-cert.pem";
  34340. const char client_pem_file[] = "./certs/client-cert.pem";
  34341. const char client_der_file[] = "./certs/client-cert.der";
  34342. const char ecc_file[] = "./certs/ecc-key.pem";
  34343. const char certs_path[] = "./certs/";
  34344. const char bad_path[] = "./bad-path/";
  34345. #ifdef HAVE_CRL
  34346. const char crl_path[] = "./certs/crl/";
  34347. const char crl_file[] = "./certs/crl/crl.pem";
  34348. #endif
  34349. #ifndef NO_WOLFSSL_SERVER
  34350. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34351. #else
  34352. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34353. #endif
  34354. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34355. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34356. WOLFSSL_SUCCESS);
  34357. /* Test bad arguments */
  34358. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34359. WOLFSSL_FAILURE);
  34360. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34361. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34362. WOLFSSL_FAILURE);
  34363. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34364. WOLFSSL_FAILURE);
  34365. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34366. WOLFSSL_FAILURE);
  34367. #ifdef HAVE_CRL
  34368. /* Test with CRL */
  34369. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34370. WOLFSSL_SUCCESS);
  34371. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34372. WOLFSSL_SUCCESS);
  34373. #endif
  34374. /* Test with CA */
  34375. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34376. WOLFSSL_SUCCESS);
  34377. /* Test with client_cert and certs path */
  34378. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34379. WOLFSSL_SUCCESS);
  34380. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34381. WOLFSSL_SUCCESS);
  34382. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34383. /* Clear nodes */
  34384. ERR_clear_error();
  34385. #endif
  34386. SSL_CTX_free(ctx);
  34387. #endif
  34388. return EXPECT_RESULT();
  34389. }
  34390. static int test_X509_STORE_get0_objects(void)
  34391. {
  34392. EXPECT_DECLS;
  34393. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34394. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34395. X509_STORE *store = NULL;
  34396. X509_STORE *store_cpy = NULL;
  34397. SSL_CTX *ctx = NULL;
  34398. X509_OBJECT *obj = NULL;
  34399. STACK_OF(X509_OBJECT) *objs = NULL;
  34400. int i;
  34401. /* Setup store */
  34402. #ifndef NO_WOLFSSL_SERVER
  34403. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34404. #else
  34405. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34406. #endif
  34407. ExpectNotNull(store_cpy = X509_STORE_new());
  34408. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34409. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34410. WOLFSSL_SUCCESS);
  34411. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34412. WOLFSSL_SUCCESS);
  34413. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34414. WOLFSSL_SUCCESS);
  34415. #ifdef HAVE_CRL
  34416. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34417. WOLFSSL_SUCCESS);
  34418. #endif
  34419. /* Store ready */
  34420. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34421. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34422. #ifdef HAVE_CRL
  34423. #ifdef WOLFSSL_SIGNER_DER_CERT
  34424. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34425. #else
  34426. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34427. #endif
  34428. #else
  34429. #ifdef WOLFSSL_SIGNER_DER_CERT
  34430. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34431. #else
  34432. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34433. #endif
  34434. #endif
  34435. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34436. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34437. switch (X509_OBJECT_get_type(obj)) {
  34438. case X509_LU_X509:
  34439. {
  34440. WOLFSSL_X509* x509;
  34441. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34442. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34443. break;
  34444. }
  34445. case X509_LU_CRL:
  34446. #ifdef HAVE_CRL
  34447. {
  34448. WOLFSSL_CRL* crl = NULL;
  34449. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34450. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34451. break;
  34452. }
  34453. #endif
  34454. case X509_LU_NONE:
  34455. default:
  34456. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34457. "(when built with crl support)"),
  34458. ("Unrecognized X509_OBJECT type or none"));
  34459. }
  34460. }
  34461. X509_STORE_free(store_cpy);
  34462. SSL_CTX_free(ctx);
  34463. #endif
  34464. return EXPECT_RESULT();
  34465. }
  34466. static int test_wolfSSL_BN_CTX(void)
  34467. {
  34468. EXPECT_DECLS;
  34469. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34470. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34471. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34472. WOLFSSL_BIGNUM* t = NULL;
  34473. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34474. /* No implementation. */
  34475. BN_CTX_init(NULL);
  34476. ExpectNotNull(t = BN_CTX_get(NULL));
  34477. BN_free(t);
  34478. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34479. BN_free(t);
  34480. #ifndef NO_WOLFSSL_STUB
  34481. /* No implementation. */
  34482. BN_CTX_start(NULL);
  34483. BN_CTX_start(bn_ctx);
  34484. #endif
  34485. BN_CTX_free(NULL);
  34486. BN_CTX_free(bn_ctx);
  34487. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34488. return EXPECT_RESULT();
  34489. }
  34490. static int test_wolfSSL_BN(void)
  34491. {
  34492. EXPECT_DECLS;
  34493. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34494. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34495. BIGNUM* a = NULL;
  34496. BIGNUM* b = NULL;
  34497. BIGNUM* c = NULL;
  34498. BIGNUM* d = NULL;
  34499. BIGNUM emptyBN;
  34500. /* Setup */
  34501. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34502. /* internal not set emptyBN. */
  34503. ExpectNotNull(a = BN_new());
  34504. ExpectNotNull(b = BN_new());
  34505. ExpectNotNull(c = BN_dup(b));
  34506. ExpectNotNull(d = BN_new());
  34507. /* Invalid parameter testing. */
  34508. BN_free(NULL);
  34509. ExpectNull(BN_dup(NULL));
  34510. ExpectNull(BN_dup(&emptyBN));
  34511. ExpectNull(BN_copy(NULL, NULL));
  34512. ExpectNull(BN_copy(b, NULL));
  34513. ExpectNull(BN_copy(NULL, c));
  34514. ExpectNull(BN_copy(b, &emptyBN));
  34515. ExpectNull(BN_copy(&emptyBN, c));
  34516. BN_clear(NULL);
  34517. BN_clear(&emptyBN);
  34518. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34519. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34520. ExpectIntEQ(BN_num_bits(NULL), 0);
  34521. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34522. ExpectIntEQ(BN_is_negative(NULL), 0);
  34523. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34524. /* END Invalid Parameters */
  34525. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34526. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34527. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34528. ExpectIntEQ(BN_num_bits(a), 2);
  34529. ExpectIntEQ(BN_num_bytes(a), 1);
  34530. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34531. defined(WOLFSSL_SP_INT_NEGATIVE))
  34532. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34533. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34534. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34535. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34536. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34537. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34538. {
  34539. /* Do additional tests on negative BN conversions. */
  34540. char* ret = NULL;
  34541. ASN1_INTEGER* asn1 = NULL;
  34542. BIGNUM* tmp = NULL;
  34543. /* Sanity check we have a negative BN. */
  34544. ExpectIntEQ(BN_is_negative(c), 1);
  34545. ExpectNotNull(ret = BN_bn2dec(c));
  34546. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34547. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34548. ret = NULL;
  34549. /* Convert to ASN1_INTEGER and back to BN. */
  34550. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34551. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34552. /* After converting back BN should be negative and correct. */
  34553. ExpectIntEQ(BN_is_negative(tmp), 1);
  34554. ExpectNotNull(ret = BN_bn2dec(tmp));
  34555. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34556. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34557. ASN1_INTEGER_free(asn1);
  34558. BN_free(tmp);
  34559. }
  34560. #endif
  34561. ExpectIntEQ(BN_get_word(c), 4);
  34562. #endif
  34563. ExpectIntEQ(BN_set_word(a, 3), 1);
  34564. ExpectIntEQ(BN_set_word(b, 3), 1);
  34565. ExpectIntEQ(BN_set_word(c, 4), 1);
  34566. /* NULL == NULL, NULL < num, num > NULL */
  34567. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34568. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34569. ExpectIntLT(BN_cmp(NULL, b), 0);
  34570. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34571. ExpectIntGT(BN_cmp(a, NULL), 0);
  34572. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34573. ExpectIntEQ(BN_cmp(a, b), 0);
  34574. ExpectIntLT(BN_cmp(a, c), 0);
  34575. ExpectIntGT(BN_cmp(c, b), 0);
  34576. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34577. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34578. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34579. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34580. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34581. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34582. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34583. #endif
  34584. BN_clear(a);
  34585. BN_free(a);
  34586. BN_free(b);
  34587. BN_free(c);
  34588. BN_clear_free(d);
  34589. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34590. return EXPECT_RESULT();
  34591. }
  34592. static int test_wolfSSL_BN_init(void)
  34593. {
  34594. EXPECT_DECLS;
  34595. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34596. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34597. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34598. BIGNUM* ap = NULL;
  34599. BIGNUM bv;
  34600. BIGNUM cv;
  34601. BIGNUM dv;
  34602. ExpectNotNull(ap = BN_new());
  34603. BN_init(NULL);
  34604. XMEMSET(&bv, 0, sizeof(bv));
  34605. ExpectNull(BN_dup(&bv));
  34606. BN_init(&bv);
  34607. BN_init(&cv);
  34608. BN_init(&dv);
  34609. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34610. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34611. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34612. /* a^b mod c = */
  34613. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34614. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34615. /* check result 3^2 mod 5 */
  34616. ExpectIntEQ(BN_get_word(&dv), 4);
  34617. /* a*b mod c = */
  34618. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34619. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34620. /* check result 3*2 mod 5 */
  34621. ExpectIntEQ(BN_get_word(&dv), 1);
  34622. BN_free(ap);
  34623. #endif
  34624. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34625. return EXPECT_RESULT();
  34626. }
  34627. static int test_wolfSSL_BN_enc_dec(void)
  34628. {
  34629. EXPECT_DECLS;
  34630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34631. BIGNUM* a = NULL;
  34632. BIGNUM* b = NULL;
  34633. BIGNUM* c = NULL;
  34634. BIGNUM emptyBN;
  34635. char* str = NULL;
  34636. const char* emptyStr = "";
  34637. const char* numberStr = "12345";
  34638. const char* badStr = "g12345";
  34639. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34640. const char* twoStr = "2";
  34641. #endif
  34642. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34643. unsigned char outNum[5];
  34644. /* Setup */
  34645. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34646. ExpectNotNull(a = BN_new());
  34647. ExpectNotNull(b = BN_new());
  34648. ExpectIntEQ(BN_set_word(a, 2), 1);
  34649. /* Invalid parameters */
  34650. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34651. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34652. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34653. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34654. ExpectNull(BN_bn2hex(NULL));
  34655. ExpectNull(BN_bn2hex(&emptyBN));
  34656. ExpectNull(BN_bn2dec(NULL));
  34657. ExpectNull(BN_bn2dec(&emptyBN));
  34658. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34659. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34660. ExpectNull(BN_bin2bn(binNum, -1, a));
  34661. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34662. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34663. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34664. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34665. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34666. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34667. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34668. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34669. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34670. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34671. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34672. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34673. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34674. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34675. ExpectIntEQ(BN_set_word(a, 2), 1);
  34676. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34677. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34678. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34679. ExpectIntEQ(BN_cmp(a, b), 0);
  34680. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34681. ExpectIntEQ(BN_cmp(a, b), -1);
  34682. ExpectNotNull(str = BN_bn2hex(a));
  34683. ExpectNotNull(BN_hex2bn(&b, str));
  34684. ExpectIntEQ(BN_cmp(a, b), 0);
  34685. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34686. ExpectIntEQ(BN_cmp(a, b), -1);
  34687. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34688. str = NULL;
  34689. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34690. ExpectNotNull(str = BN_bn2dec(a));
  34691. ExpectStrEQ(str, twoStr);
  34692. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34693. str = NULL;
  34694. #ifndef NO_RSA
  34695. ExpectNotNull(str = BN_bn2dec(a));
  34696. ExpectNotNull(BN_dec2bn(&b, str));
  34697. ExpectIntEQ(BN_cmp(a, b), 0);
  34698. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34699. ExpectIntEQ(BN_cmp(a, b), -1);
  34700. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34701. str = NULL;
  34702. #else
  34703. /* No implementation - fail with good parameters. */
  34704. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34705. #endif
  34706. #endif
  34707. BN_free(b);
  34708. BN_free(a);
  34709. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34710. return EXPECT_RESULT();
  34711. }
  34712. static int test_wolfSSL_BN_word(void)
  34713. {
  34714. EXPECT_DECLS;
  34715. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34716. BIGNUM* a = NULL;
  34717. BIGNUM* b = NULL;
  34718. BIGNUM* c = NULL;
  34719. BIGNUM av;
  34720. ExpectNotNull(a = BN_new());
  34721. ExpectNotNull(b = BN_new());
  34722. ExpectNotNull(c = BN_new());
  34723. XMEMSET(&av, 0, sizeof(av));
  34724. /* Invalid parameter. */
  34725. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34726. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34727. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34728. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34729. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34730. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34731. ExpectIntEQ(BN_get_word(NULL), 0);
  34732. ExpectIntEQ(BN_get_word(&av), 0);
  34733. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34734. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34735. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34736. !defined(NO_DSA))
  34737. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34738. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34739. #endif
  34740. ExpectIntEQ(BN_one(NULL), 0);
  34741. ExpectIntEQ(BN_one(&av), 0);
  34742. BN_zero(NULL);
  34743. BN_zero(&av);
  34744. ExpectIntEQ(BN_is_one(NULL), 0);
  34745. ExpectIntEQ(BN_is_one(&av), 0);
  34746. ExpectIntEQ(BN_is_zero(NULL), 0);
  34747. ExpectIntEQ(BN_is_zero(&av), 0);
  34748. ExpectIntEQ(BN_set_word(a, 3), 1);
  34749. ExpectIntEQ(BN_set_word(b, 2), 1);
  34750. ExpectIntEQ(BN_set_word(c, 5), 1);
  34751. /* a + 3 = */
  34752. ExpectIntEQ(BN_add_word(a, 3), 1);
  34753. /* check result 3 + 3*/
  34754. ExpectIntEQ(BN_get_word(a), 6);
  34755. ExpectIntEQ(BN_is_word(a, 6), 1);
  34756. ExpectIntEQ(BN_is_word(a, 5), 0);
  34757. /* set a back to 3 */
  34758. ExpectIntEQ(BN_set_word(a, 3), 1);
  34759. /* a - 3 = */
  34760. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34761. /* check result 3 - 3*/
  34762. ExpectIntEQ(BN_get_word(a), 0);
  34763. ExpectIntEQ(BN_one(a), 1);
  34764. ExpectIntEQ(BN_is_word(a, 1), 1);
  34765. ExpectIntEQ(BN_is_word(a, 0), 0);
  34766. ExpectIntEQ(BN_is_one(a), 1);
  34767. ExpectIntEQ(BN_is_zero(a), 0);
  34768. BN_zero(a);
  34769. ExpectIntEQ(BN_is_word(a, 0), 1);
  34770. ExpectIntEQ(BN_is_word(a, 1), 0);
  34771. ExpectIntEQ(BN_is_zero(a), 1);
  34772. ExpectIntEQ(BN_is_one(a), 0);
  34773. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34774. !defined(NO_DSA))
  34775. ExpectIntEQ(BN_set_word(a, 5), 1);
  34776. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34777. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34778. #endif
  34779. BN_free(c);
  34780. BN_free(b);
  34781. BN_free(a);
  34782. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34783. return EXPECT_RESULT();
  34784. }
  34785. static int test_wolfSSL_BN_bits(void)
  34786. {
  34787. EXPECT_DECLS;
  34788. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34789. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34790. BIGNUM* a = NULL;
  34791. BIGNUM emptyBN;
  34792. /* Setup */
  34793. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34794. ExpectNotNull(a = BN_new());
  34795. /* Invalid parameters. */
  34796. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34797. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34798. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34799. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34800. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34801. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34802. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34803. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34804. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34805. ExpectIntEQ(BN_is_odd(NULL), 0);
  34806. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34807. ExpectIntEQ(BN_set_word(a, 0), 1);
  34808. ExpectIntEQ(BN_is_zero(a), 1);
  34809. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34810. ExpectIntEQ(BN_is_zero(a), 0);
  34811. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34812. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34813. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34814. ExpectIntEQ(BN_is_zero(a), 1);
  34815. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34816. ExpectIntEQ(BN_is_odd(a), 1);
  34817. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34818. ExpectIntEQ(BN_is_odd(a), 0);
  34819. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34820. ExpectIntEQ(BN_is_odd(a), 0);
  34821. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34822. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34823. #ifndef NO_WOLFSSL_STUB
  34824. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34825. #endif
  34826. BN_free(a);
  34827. #endif
  34828. return EXPECT_RESULT();
  34829. }
  34830. static int test_wolfSSL_BN_shift(void)
  34831. {
  34832. EXPECT_DECLS;
  34833. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34834. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34835. BIGNUM* a = NULL;
  34836. BIGNUM* b = NULL;
  34837. BIGNUM emptyBN;
  34838. /* Setup */
  34839. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34840. ExpectNotNull(a = BN_new());
  34841. ExpectNotNull(b = BN_new());
  34842. /* Invalid parameters. */
  34843. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34844. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34845. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34846. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34847. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34848. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34849. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34850. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34851. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34852. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34853. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34854. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34855. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34856. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34857. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34858. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34859. ExpectIntEQ(BN_set_word(a, 1), 1);
  34860. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34861. ExpectIntEQ(BN_is_word(b, 2), 1);
  34862. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34863. ExpectIntEQ(BN_is_word(a, 2), 1);
  34864. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34865. ExpectIntEQ(BN_is_word(b, 1), 1);
  34866. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34867. ExpectIntEQ(BN_is_word(a, 1), 1);
  34868. BN_free(b);
  34869. BN_free(a);
  34870. #endif
  34871. return EXPECT_RESULT();
  34872. }
  34873. static int test_wolfSSL_BN_math(void)
  34874. {
  34875. EXPECT_DECLS;
  34876. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34877. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34878. BIGNUM* a = NULL;
  34879. BIGNUM* b = NULL;
  34880. BIGNUM* r = NULL;
  34881. BIGNUM* rem = NULL;
  34882. BIGNUM emptyBN;
  34883. BN_ULONG val1;
  34884. BN_ULONG val2;
  34885. /* Setup */
  34886. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34887. ExpectNotNull(a = BN_new());
  34888. ExpectNotNull(b = BN_new());
  34889. ExpectNotNull(r = BN_new());
  34890. ExpectNotNull(rem = BN_new());
  34891. /* Invalid parameters. */
  34892. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34893. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34894. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34895. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34896. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34897. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34898. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34899. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34900. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34901. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34902. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34903. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34904. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34905. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34906. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34907. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34908. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34909. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34910. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34911. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34912. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34913. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34914. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34915. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34916. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34917. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34918. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34919. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34920. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34921. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34922. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34923. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34924. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34925. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34926. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34927. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34928. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34929. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34930. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34931. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34932. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34933. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34934. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34935. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34936. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34937. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34938. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34939. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34940. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34941. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34942. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34943. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34944. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34945. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34946. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34947. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34948. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34949. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34950. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34951. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34952. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34953. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34954. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34955. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34956. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34957. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34958. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34959. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34960. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34961. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34962. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34963. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34964. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34965. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34966. /* END Invalid parameters. */
  34967. val1 = 8;
  34968. val2 = 3;
  34969. ExpectIntEQ(BN_set_word(a, val1), 1);
  34970. ExpectIntEQ(BN_set_word(b, val2), 1);
  34971. ExpectIntEQ(BN_add(r, a, b), 1);
  34972. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34973. ExpectIntEQ(BN_sub(r, a, b), 1);
  34974. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34975. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34976. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34977. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34978. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34979. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34980. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34981. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34982. BN_free(rem);
  34983. BN_free(r);
  34984. BN_free(b);
  34985. BN_free(a);
  34986. #endif
  34987. return EXPECT_RESULT();
  34988. }
  34989. static int test_wolfSSL_BN_math_mod(void)
  34990. {
  34991. EXPECT_DECLS;
  34992. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34993. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34994. BIGNUM* a = NULL;
  34995. BIGNUM* b = NULL;
  34996. BIGNUM* m = NULL;
  34997. BIGNUM* r = NULL;
  34998. BIGNUM* t = NULL;
  34999. BIGNUM emptyBN;
  35000. BN_ULONG val1;
  35001. BN_ULONG val2;
  35002. BN_ULONG val3;
  35003. /* Setup */
  35004. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35005. ExpectNotNull(a = BN_new());
  35006. ExpectNotNull(b = BN_new());
  35007. ExpectNotNull(m = BN_new());
  35008. ExpectNotNull(r = BN_new());
  35009. /* Invalid parameters. */
  35010. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  35011. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  35012. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  35013. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  35014. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  35015. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  35016. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  35017. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  35018. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  35019. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35020. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35021. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35022. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35023. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35024. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  35025. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  35026. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  35027. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  35028. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  35029. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  35030. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  35031. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  35032. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  35033. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  35034. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  35035. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  35036. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  35037. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35038. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35039. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35040. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35041. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35042. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  35043. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  35044. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  35045. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  35046. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  35047. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  35048. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  35049. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  35050. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  35051. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  35052. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  35053. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  35054. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  35055. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35056. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35057. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  35058. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  35059. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  35060. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  35061. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  35062. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  35063. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  35064. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  35065. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  35066. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  35067. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  35068. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  35069. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  35070. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  35071. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  35072. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  35073. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  35074. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  35075. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  35076. /* END Invalid parameters. */
  35077. val1 = 9;
  35078. val2 = 13;
  35079. val3 = 5;
  35080. ExpectIntEQ(BN_set_word(a, val1), 1);
  35081. ExpectIntEQ(BN_set_word(b, val2), 1);
  35082. ExpectIntEQ(BN_set_word(m, val3), 1);
  35083. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  35084. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  35085. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  35086. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  35087. ExpectIntEQ(BN_set_word(a, 2), 1);
  35088. ExpectIntEQ(BN_set_word(b, 3), 1);
  35089. ExpectIntEQ(BN_set_word(m, 5), 1);
  35090. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  35091. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  35092. ExpectIntEQ(BN_is_word(r, 3), 1);
  35093. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  35094. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  35095. ExpectIntEQ(BN_is_word(r, 3), 1);
  35096. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  35097. ExpectIntEQ(BN_is_word(t, 3), 1);
  35098. BN_free(t);
  35099. /* No inverse case. No inverse when a divides b. */
  35100. ExpectIntEQ(BN_set_word(a, 3), 1);
  35101. ExpectIntEQ(BN_set_word(m, 9), 1);
  35102. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  35103. BN_free(r);
  35104. BN_free(m);
  35105. BN_free(b);
  35106. BN_free(a);
  35107. #endif
  35108. return EXPECT_RESULT();
  35109. }
  35110. static int test_wolfSSL_BN_math_other(void)
  35111. {
  35112. EXPECT_DECLS;
  35113. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35114. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35115. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35116. BIGNUM* a = NULL;
  35117. BIGNUM* b = NULL;
  35118. BIGNUM* r = NULL;
  35119. BIGNUM emptyBN;
  35120. /* Setup */
  35121. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35122. ExpectNotNull(a = BN_new());
  35123. ExpectNotNull(b = BN_new());
  35124. ExpectNotNull(r = BN_new());
  35125. /* Invalid parameters. */
  35126. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  35127. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  35128. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  35129. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  35130. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  35131. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  35132. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  35133. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35134. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  35135. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  35136. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  35137. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  35138. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  35139. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  35140. /* END Invalid parameters. */
  35141. /* No common factors between 2 and 3. */
  35142. ExpectIntEQ(BN_set_word(a, 2), 1);
  35143. ExpectIntEQ(BN_set_word(b, 3), 1);
  35144. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35145. ExpectIntEQ(BN_is_word(r, 1), 1);
  35146. /* 3 is largest value that divides both 6 and 9. */
  35147. ExpectIntEQ(BN_set_word(a, 6), 1);
  35148. ExpectIntEQ(BN_set_word(b, 9), 1);
  35149. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35150. ExpectIntEQ(BN_is_word(r, 3), 1);
  35151. /* GCD of 0 and 0 is undefined. */
  35152. ExpectIntEQ(BN_set_word(a, 0), 1);
  35153. ExpectIntEQ(BN_set_word(b, 0), 1);
  35154. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  35155. /* Teardown */
  35156. BN_free(r);
  35157. BN_free(b);
  35158. BN_free(a);
  35159. #endif
  35160. #endif
  35161. return EXPECT_RESULT();
  35162. }
  35163. static int test_wolfSSL_BN_rand(void)
  35164. {
  35165. EXPECT_DECLS;
  35166. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  35167. BIGNUM* bn = NULL;
  35168. BIGNUM* range = NULL;
  35169. BIGNUM emptyBN;
  35170. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35171. ExpectNotNull(bn = BN_new());
  35172. ExpectNotNull(range = BN_new());
  35173. /* Invalid parameters. */
  35174. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  35175. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35176. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  35177. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  35178. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35179. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  35180. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  35181. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35182. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  35183. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  35184. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35185. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  35186. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  35187. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  35188. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  35189. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  35190. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  35191. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  35192. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  35193. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35194. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35195. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35196. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35197. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35198. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35199. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35200. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35201. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35202. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35203. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35204. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35205. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35206. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35207. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35208. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35209. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35210. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35211. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35212. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35213. /* 1 bit random value must have no more than one top bit set. */
  35214. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35215. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35216. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35217. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35218. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35219. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35220. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35221. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35222. /* END Invalid parameters. */
  35223. /* 0 bit random: 0. */
  35224. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35225. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35226. ExpectIntEQ(BN_is_zero(bn), 1);
  35227. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35228. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35229. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35230. ExpectIntEQ(BN_is_zero(bn), 1);
  35231. /* 1 bit random: 0 or 1. */
  35232. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35233. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35234. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35235. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35236. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35237. ExpectIntEQ(BN_get_word(bn), 1);
  35238. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35239. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35240. ExpectIntEQ(BN_get_word(bn), 1);
  35241. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35242. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35243. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35244. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35245. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35246. ExpectIntEQ(BN_get_word(bn), 1);
  35247. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35248. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35249. ExpectIntEQ(BN_get_word(bn), 1);
  35250. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35251. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35252. ExpectIntEQ(BN_num_bits(bn), 8);
  35253. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35254. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35255. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35256. ExpectIntEQ(BN_num_bits(bn), 8);
  35257. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35258. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35259. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35260. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35261. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35262. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35263. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35264. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35265. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35266. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35267. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35268. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35269. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35270. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35271. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35272. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35273. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35274. * case, requesting a 13-bit random number would actually return a 16-bit
  35275. * random number. */
  35276. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35277. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35278. ExpectIntEQ(BN_num_bits(bn), 13);
  35279. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35280. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35281. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35282. ExpectIntEQ(BN_set_word(range, 0), 1);
  35283. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35284. ExpectIntEQ(BN_set_word(range, 1), 1);
  35285. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35286. BN_free(bn);
  35287. BN_free(range);
  35288. #endif
  35289. return EXPECT_RESULT();
  35290. }
  35291. static int test_wolfSSL_BN_prime(void)
  35292. {
  35293. EXPECT_DECLS;
  35294. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35295. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35296. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35297. BIGNUM* a = NULL;
  35298. BIGNUM* add = NULL;
  35299. BIGNUM* rem = NULL;
  35300. BIGNUM emptyBN;
  35301. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35302. ExpectNotNull(a = BN_new());
  35303. ExpectNotNull(add = BN_new());
  35304. ExpectNotNull(rem = BN_new());
  35305. /* Invalid parameters. */
  35306. /* BN_generate_prime_ex()
  35307. * prime - must have valid BIGNUM
  35308. * bits - Greater then 0
  35309. * safe - not supported, must be 0
  35310. * add - not supported, must be NULL
  35311. * rem - not supported, must be NULL
  35312. * cb - anything
  35313. */
  35314. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35315. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35316. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35317. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35318. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35319. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35320. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35321. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35322. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35323. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35324. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35325. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35326. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35327. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35328. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35329. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35330. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35331. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35332. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35333. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35334. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35335. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35336. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35337. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35338. /* END Invalid parameters. */
  35339. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35340. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35341. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35342. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35343. BN_free(rem);
  35344. BN_free(add);
  35345. BN_free(a);
  35346. #endif
  35347. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35348. return EXPECT_RESULT();
  35349. }
  35350. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35351. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35352. #define TEST_ARG 0x1234
  35353. static void msg_cb(int write_p, int version, int content_type,
  35354. const void *buf, size_t len, SSL *ssl, void *arg)
  35355. {
  35356. (void)write_p;
  35357. (void)version;
  35358. (void)content_type;
  35359. (void)buf;
  35360. (void)len;
  35361. (void)ssl;
  35362. AssertTrue(arg == (void*)TEST_ARG);
  35363. }
  35364. #endif
  35365. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35366. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35367. #if defined(SESSION_CERTS)
  35368. #include "wolfssl/internal.h"
  35369. #endif
  35370. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35371. {
  35372. EXPECT_DECLS;
  35373. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35374. STACK_OF(X509)* sk = NULL;
  35375. X509* x509 = NULL;
  35376. int i, num;
  35377. BIO* bio = NULL;
  35378. #endif
  35379. ExpectNotNull(ctx);
  35380. ExpectNotNull(ssl);
  35381. fprintf(stderr, "\n===== msgcb called ====\n");
  35382. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35383. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35384. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35385. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35386. #endif
  35387. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35388. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35389. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35390. if (sk == NULL) {
  35391. BIO_free(bio);
  35392. return TEST_FAIL;
  35393. }
  35394. num = sk_X509_num(sk);
  35395. ExpectTrue(num > 0);
  35396. for (i = 0; i < num; i++) {
  35397. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35398. if (x509 == NULL)
  35399. break;
  35400. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35401. X509_print(bio,x509);
  35402. fprintf(stderr, "\n\n");
  35403. }
  35404. BIO_free(bio);
  35405. #endif
  35406. return EXPECT_RESULT();
  35407. }
  35408. #endif
  35409. static int test_wolfSSL_msgCb(void)
  35410. {
  35411. EXPECT_DECLS;
  35412. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35413. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35414. test_ssl_cbf client_cb;
  35415. test_ssl_cbf server_cb;
  35416. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35417. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35418. #ifndef WOLFSSL_NO_TLS12
  35419. client_cb.method = wolfTLSv1_2_client_method;
  35420. server_cb.method = wolfTLSv1_2_server_method;
  35421. #else
  35422. client_cb.method = wolfTLSv1_3_client_method;
  35423. server_cb.method = wolfTLSv1_3_server_method;
  35424. #endif
  35425. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35426. &server_cb, msgCb), TEST_SUCCESS);
  35427. #endif
  35428. return EXPECT_RESULT();
  35429. }
  35430. static int test_wolfSSL_either_side(void)
  35431. {
  35432. EXPECT_DECLS;
  35433. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35434. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35435. test_ssl_cbf client_cb;
  35436. test_ssl_cbf server_cb;
  35437. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35438. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35439. /* Use different CTX for client and server */
  35440. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35441. ExpectNotNull(client_cb.ctx);
  35442. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35443. ExpectNotNull(server_cb.ctx);
  35444. /* we are responsible for free'ing WOLFSSL_CTX */
  35445. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35446. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35447. &server_cb, NULL), TEST_SUCCESS);
  35448. wolfSSL_CTX_free(client_cb.ctx);
  35449. wolfSSL_CTX_free(server_cb.ctx);
  35450. #endif
  35451. return EXPECT_RESULT();
  35452. }
  35453. static int test_wolfSSL_DTLS_either_side(void)
  35454. {
  35455. EXPECT_DECLS;
  35456. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35457. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35458. test_ssl_cbf client_cb;
  35459. test_ssl_cbf server_cb;
  35460. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35461. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35462. /* Use different CTX for client and server */
  35463. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35464. ExpectNotNull(client_cb.ctx);
  35465. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35466. ExpectNotNull(server_cb.ctx);
  35467. /* we are responsible for free'ing WOLFSSL_CTX */
  35468. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35469. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35470. &server_cb, NULL), TEST_SUCCESS);
  35471. wolfSSL_CTX_free(client_cb.ctx);
  35472. wolfSSL_CTX_free(server_cb.ctx);
  35473. #endif
  35474. return EXPECT_RESULT();
  35475. }
  35476. static int test_generate_cookie(void)
  35477. {
  35478. EXPECT_DECLS;
  35479. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35480. SSL_CTX* ctx = NULL;
  35481. SSL* ssl = NULL;
  35482. byte buf[FOURK_BUF] = {0};
  35483. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35484. ExpectNotNull(ssl = SSL_new(ctx));
  35485. /* Test unconnected */
  35486. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35487. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35488. wolfSSL_SetCookieCtx(ssl, ctx);
  35489. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35490. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35491. SSL_free(ssl);
  35492. SSL_CTX_free(ctx);
  35493. #endif
  35494. return EXPECT_RESULT();
  35495. }
  35496. static int test_wolfSSL_set_options(void)
  35497. {
  35498. EXPECT_DECLS;
  35499. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35500. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35501. WOLFSSL* ssl = NULL;
  35502. WOLFSSL_CTX* ctx = NULL;
  35503. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35504. char appData[] = "extra msg";
  35505. #endif
  35506. #ifdef OPENSSL_EXTRA
  35507. unsigned char protos[] = {
  35508. 7, 't', 'l', 's', '/', '1', '.', '2',
  35509. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35510. };
  35511. unsigned int len = sizeof(protos);
  35512. void *arg = (void *)TEST_ARG;
  35513. #endif
  35514. #ifndef NO_WOLFSSL_SERVER
  35515. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35516. #else
  35517. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35518. #endif
  35519. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35520. WOLFSSL_FILETYPE_PEM));
  35521. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35522. WOLFSSL_FILETYPE_PEM));
  35523. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35524. == WOLFSSL_OP_NO_TLSv1);
  35525. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35526. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35527. WOLFSSL_OP_NO_SSLv2)), 0);
  35528. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35529. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35530. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35531. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35532. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35533. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35534. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35535. WOLFSSL_OP_NO_COMPRESSION));
  35536. wolfSSL_CTX_free(ctx);
  35537. ctx = NULL;
  35538. #ifndef NO_WOLFSSL_SERVER
  35539. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35540. #else
  35541. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35542. #endif
  35543. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35544. WOLFSSL_FILETYPE_PEM));
  35545. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35546. WOLFSSL_FILETYPE_PEM));
  35547. #ifdef OPENSSL_EXTRA
  35548. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35549. #endif
  35550. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35551. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35552. #ifdef HAVE_EX_DATA
  35553. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35554. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35555. if (ssl != NULL) {
  35556. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35557. appData, sizeof(appData)), 0);
  35558. }
  35559. #else
  35560. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35561. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35562. #endif
  35563. #endif
  35564. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35565. WOLFSSL_OP_NO_TLSv1);
  35566. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35567. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35568. WOLFSSL_OP_NO_SSLv2)), 0);
  35569. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35570. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35571. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35572. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35573. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35574. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35575. #ifdef OPENSSL_EXTRA
  35576. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35577. WOLFSSL_OP_NO_COMPRESSION));
  35578. #endif
  35579. #ifdef OPENSSL_EXTRA
  35580. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35581. wolfSSL_set_msg_callback_arg(ssl, arg);
  35582. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35583. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35584. #else
  35585. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35586. #endif
  35587. #endif
  35588. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35589. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35590. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35591. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35592. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35593. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35594. #else
  35595. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35596. #endif
  35597. #endif /* HAVE_ALPN && !NO_BIO */
  35598. #endif
  35599. wolfSSL_free(ssl);
  35600. wolfSSL_CTX_free(ctx);
  35601. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35602. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35603. return EXPECT_RESULT();
  35604. }
  35605. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35606. {
  35607. EXPECT_DECLS;
  35608. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35609. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35610. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35611. SSL* ssl = NULL;
  35612. SSL_CTX* ctx = NULL;
  35613. STACK_OF(SSL_CIPHER) *sk = NULL;
  35614. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35615. #ifndef NO_WOLFSSL_SERVER
  35616. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35617. #else
  35618. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35619. #endif
  35620. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35621. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35622. ExpectNotNull(ssl = SSL_new(ctx));
  35623. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35624. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35625. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35626. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35627. /* error case because connection has not been established yet */
  35628. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35629. sk_SSL_CIPHER_free(dupSk);
  35630. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35631. SSL_free(ssl);
  35632. SSL_CTX_free(ctx);
  35633. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35634. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35635. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35636. return EXPECT_RESULT();
  35637. }
  35638. static int test_wolfSSL_set1_curves_list(void)
  35639. {
  35640. EXPECT_DECLS;
  35641. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35642. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35643. SSL* ssl = NULL;
  35644. SSL_CTX* ctx = NULL;
  35645. #ifndef NO_WOLFSSL_SERVER
  35646. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35647. #else
  35648. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35649. #endif
  35650. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35651. SSL_FILETYPE_PEM));
  35652. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35653. ExpectNotNull(ssl = SSL_new(ctx));
  35654. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35655. #ifdef HAVE_ECC
  35656. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35657. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35658. #endif
  35659. #ifdef HAVE_CURVE25519
  35660. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35661. #else
  35662. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35663. #endif
  35664. #ifdef HAVE_CURVE448
  35665. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35666. #else
  35667. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35668. #endif
  35669. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35670. #ifdef HAVE_ECC
  35671. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35672. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35673. #endif
  35674. #ifdef HAVE_CURVE25519
  35675. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35676. #else
  35677. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35678. #endif
  35679. #ifdef HAVE_CURVE448
  35680. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35681. #else
  35682. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35683. #endif
  35684. SSL_free(ssl);
  35685. SSL_CTX_free(ctx);
  35686. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35687. #endif
  35688. return EXPECT_RESULT();
  35689. }
  35690. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  35691. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  35692. static int test_wolfSSL_curves_mismatch_ctx_ready(WOLFSSL_CTX* ctx)
  35693. {
  35694. static int counter = 0;
  35695. EXPECT_DECLS;
  35696. if (counter % 2) {
  35697. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-256"),
  35698. WOLFSSL_SUCCESS);
  35699. }
  35700. else {
  35701. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-384"),
  35702. WOLFSSL_SUCCESS);
  35703. }
  35704. /* Ciphersuites that require curves */
  35705. wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384:"
  35706. "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-GCM-SHA256:"
  35707. "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
  35708. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  35709. "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:"
  35710. "ECDHE-ECDSA-CHACHA20-POLY1305");
  35711. counter++;
  35712. return EXPECT_RESULT();
  35713. }
  35714. #endif
  35715. static int test_wolfSSL_curves_mismatch(void)
  35716. {
  35717. EXPECT_DECLS;
  35718. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  35719. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  35720. test_ssl_cbf func_cb_client;
  35721. test_ssl_cbf func_cb_server;
  35722. size_t i;
  35723. struct {
  35724. method_provider client_meth;
  35725. method_provider server_meth;
  35726. const char* desc;
  35727. int client_last_err;
  35728. int server_last_err;
  35729. } test_params[] = {
  35730. #ifdef WOLFSSL_TLS13
  35731. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3",
  35732. FATAL_ERROR, BAD_KEY_SHARE_DATA},
  35733. #endif
  35734. #ifndef WOLFSSL_NO_TLS12
  35735. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2",
  35736. FATAL_ERROR, MATCH_SUITE_ERROR},
  35737. #endif
  35738. #ifndef NO_OLD_TLS
  35739. {wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLS 1.1",
  35740. FATAL_ERROR, MATCH_SUITE_ERROR},
  35741. #endif
  35742. };
  35743. for (i = 0; i < XELEM_CNT(test_params) && !EXPECT_FAIL(); i++) {
  35744. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35745. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35746. printf("\tTesting with %s...\n", test_params[i].desc);
  35747. func_cb_client.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  35748. func_cb_server.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  35749. func_cb_client.method = test_params[i].client_meth;
  35750. func_cb_server.method = test_params[i].server_meth;
  35751. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35752. &func_cb_server, NULL), TEST_FAIL);
  35753. ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
  35754. ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
  35755. if (!EXPECT_SUCCESS())
  35756. break;
  35757. printf("\t%s passed\n", test_params[i].desc);
  35758. }
  35759. #endif
  35760. return EXPECT_RESULT();
  35761. }
  35762. static int test_wolfSSL_set1_sigalgs_list(void)
  35763. {
  35764. EXPECT_DECLS;
  35765. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35766. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35767. SSL* ssl = NULL;
  35768. SSL_CTX* ctx = NULL;
  35769. #ifndef NO_WOLFSSL_SERVER
  35770. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35771. #else
  35772. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35773. #endif
  35774. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35775. SSL_FILETYPE_PEM));
  35776. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35777. ExpectNotNull(ssl = SSL_new(ctx));
  35778. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35779. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35780. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35781. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35782. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35783. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35784. #ifndef NO_RSA
  35785. #ifndef NO_SHA256
  35786. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35787. WOLFSSL_FAILURE);
  35788. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35789. WOLFSSL_FAILURE);
  35790. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35791. WOLFSSL_SUCCESS);
  35792. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35793. WOLFSSL_SUCCESS);
  35794. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35795. WOLFSSL_FAILURE);
  35796. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35797. WOLFSSL_FAILURE);
  35798. #ifdef WC_RSA_PSS
  35799. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35800. WOLFSSL_SUCCESS);
  35801. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35802. WOLFSSL_SUCCESS);
  35803. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35804. WOLFSSL_SUCCESS);
  35805. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35806. WOLFSSL_SUCCESS);
  35807. #endif
  35808. #ifdef WOLFSSL_SHA512
  35809. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35810. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35811. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35812. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35813. #elif defined(WOLFSSL_SHA384)
  35814. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35815. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35816. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35817. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35818. #endif
  35819. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35820. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35821. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35822. WOLFSSL_FAILURE);
  35823. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35824. WOLFSSL_FAILURE);
  35825. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35826. WOLFSSL_FAILURE);
  35827. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35828. WOLFSSL_FAILURE);
  35829. #endif
  35830. #endif
  35831. #ifdef HAVE_ECC
  35832. #ifndef NO_SHA256
  35833. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35834. WOLFSSL_SUCCESS);
  35835. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35836. WOLFSSL_SUCCESS);
  35837. #ifdef WOLFSSL_SHA512
  35838. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35839. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35840. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35841. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35842. #elif defined(WOLFSSL_SHA384)
  35843. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35844. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35845. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35846. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35847. #endif
  35848. #endif
  35849. #endif
  35850. #ifdef HAVE_ED25519
  35851. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35852. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35853. #endif
  35854. #ifdef HAVE_ED448
  35855. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35856. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35857. #endif
  35858. #ifndef NO_DSA
  35859. #ifndef NO_SHA256
  35860. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35861. WOLFSSL_SUCCESS);
  35862. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35863. WOLFSSL_SUCCESS);
  35864. #endif
  35865. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35866. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35867. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35868. WOLFSSL_SUCCESS);
  35869. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35870. WOLFSSL_SUCCESS);
  35871. #endif
  35872. #endif
  35873. SSL_free(ssl);
  35874. SSL_CTX_free(ctx);
  35875. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35876. #endif
  35877. return EXPECT_RESULT();
  35878. }
  35879. /* Testing wolfSSL_set_tlsext_status_type function.
  35880. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35881. */
  35882. static int test_wolfSSL_set_tlsext_status_type(void)
  35883. {
  35884. EXPECT_DECLS;
  35885. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35886. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35887. SSL* ssl = NULL;
  35888. SSL_CTX* ctx = NULL;
  35889. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35890. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35891. SSL_FILETYPE_PEM));
  35892. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35893. ExpectNotNull(ssl = SSL_new(ctx));
  35894. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35895. SSL_SUCCESS);
  35896. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35897. SSL_free(ssl);
  35898. SSL_CTX_free(ctx);
  35899. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35900. return EXPECT_RESULT();
  35901. }
  35902. #ifndef NO_BIO
  35903. static int test_wolfSSL_PEM_read_bio(void)
  35904. {
  35905. EXPECT_DECLS;
  35906. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35907. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35908. byte buff[6000];
  35909. XFILE f = XBADFILE;
  35910. int bytes;
  35911. X509* x509 = NULL;
  35912. BIO* bio = NULL;
  35913. BUF_MEM* buf = NULL;
  35914. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35915. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35916. if (f != XBADFILE)
  35917. XFCLOSE(f);
  35918. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35919. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35920. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35921. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35922. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35923. /* BIO should return the set EOF value */
  35924. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35925. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35926. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35927. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35928. BIO_free(bio);
  35929. BUF_MEM_free(buf);
  35930. X509_free(x509);
  35931. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35932. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35933. return EXPECT_RESULT();
  35934. }
  35935. #if defined(OPENSSL_EXTRA)
  35936. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35937. long argl, long ret)
  35938. {
  35939. (void)bio;
  35940. (void)cmd;
  35941. (void)argp;
  35942. (void)argi;
  35943. (void)argl;
  35944. return ret;
  35945. }
  35946. #endif
  35947. static int test_wolfSSL_BIO(void)
  35948. {
  35949. EXPECT_DECLS;
  35950. #if defined(OPENSSL_EXTRA)
  35951. const unsigned char* p = NULL;
  35952. byte buff[20];
  35953. BIO* bio1 = NULL;
  35954. BIO* bio2 = NULL;
  35955. BIO* bio3 = NULL;
  35956. char* bufPt = NULL;
  35957. int i;
  35958. for (i = 0; i < 20; i++) {
  35959. buff[i] = i;
  35960. }
  35961. /* test BIO_free with NULL */
  35962. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35963. /* Creating and testing type BIO_s_bio */
  35964. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35965. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35966. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35967. /* read/write before set up */
  35968. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35969. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35970. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35971. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35972. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35973. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35974. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35975. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35976. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35977. /* write buffer full */
  35978. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35979. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35980. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35981. /* write the other direction with pair */
  35982. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35983. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35984. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35985. /* try read */
  35986. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35987. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35988. /* try read using ctrl function */
  35989. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35990. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35991. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35992. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35993. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35994. for (i = 0; i < 20; i++) {
  35995. ExpectIntEQ((int)bufPt[i], i);
  35996. }
  35997. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35998. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35999. for (i = 0; i < 8; i++) {
  36000. ExpectIntEQ((int)bufPt[i], i);
  36001. }
  36002. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  36003. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  36004. /* new pair */
  36005. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  36006. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  36007. bio2 = NULL;
  36008. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  36009. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  36010. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  36011. /* test wrap around... */
  36012. ExpectIntEQ(BIO_reset(bio1), 0);
  36013. ExpectIntEQ(BIO_reset(bio3), 0);
  36014. /* fill write buffer, read only small amount then write again */
  36015. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36016. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36017. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  36018. for (i = 0; i < 4; i++) {
  36019. ExpectIntEQ(bufPt[i], i);
  36020. }
  36021. /* try writing over read index */
  36022. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  36023. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  36024. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  36025. /* read and write 0 bytes */
  36026. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  36027. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  36028. /* should read only to end of write buffer then need to read again */
  36029. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  36030. for (i = 0; i < 16; i++) {
  36031. ExpectIntEQ(bufPt[i], buff[4 + i]);
  36032. }
  36033. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  36034. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  36035. for (i = 0; i < 4; i++) {
  36036. ExpectIntEQ(bufPt[i], 0);
  36037. }
  36038. /* read index should not have advanced with nread0 */
  36039. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  36040. for (i = 0; i < 4; i++) {
  36041. ExpectIntEQ(bufPt[i], 0);
  36042. }
  36043. /* write and fill up buffer checking reset of index state */
  36044. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36045. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36046. /* test reset on data in bio1 write buffer */
  36047. ExpectIntEQ(BIO_reset(bio1), 0);
  36048. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  36049. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  36050. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  36051. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  36052. ExpectNotNull(p);
  36053. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  36054. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  36055. for (i = 0; i < 6; i++) {
  36056. ExpectIntEQ(bufPt[i], i);
  36057. }
  36058. /* test case of writing twice with offset read index */
  36059. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  36060. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  36061. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36062. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  36063. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36064. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  36065. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  36066. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  36067. BIO_free(bio1);
  36068. bio1 = NULL;
  36069. BIO_free(bio3);
  36070. bio3 = NULL;
  36071. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  36072. {
  36073. BIO* bioA = NULL;
  36074. BIO* bioB = NULL;
  36075. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  36076. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  36077. BIO_free(bioA);
  36078. bioA = NULL;
  36079. BIO_free(bioB);
  36080. bioB = NULL;
  36081. }
  36082. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36083. /* BIOs with file pointers */
  36084. #if !defined(NO_FILESYSTEM)
  36085. {
  36086. XFILE f1 = XBADFILE;
  36087. XFILE f2 = XBADFILE;
  36088. BIO* f_bio1 = NULL;
  36089. BIO* f_bio2 = NULL;
  36090. unsigned char cert[300];
  36091. char testFile[] = "tests/bio_write_test.txt";
  36092. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  36093. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  36094. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  36095. /* Failure due to wrong BIO type */
  36096. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36097. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  36098. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  36099. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  36100. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  36101. WOLFSSL_SUCCESS);
  36102. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36103. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  36104. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  36105. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  36106. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  36107. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  36108. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  36109. ExpectIntEQ(BIO_reset(f_bio2), 0);
  36110. ExpectIntEQ(BIO_tell(NULL),-1);
  36111. ExpectIntEQ(BIO_tell(f_bio2),0);
  36112. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  36113. ExpectIntEQ(BIO_tell(f_bio2),4);
  36114. BIO_free(f_bio1);
  36115. f_bio1 = NULL;
  36116. BIO_free(f_bio2);
  36117. f_bio2 = NULL;
  36118. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  36119. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36120. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36121. BIO_free(f_bio1);
  36122. f_bio1 = NULL;
  36123. }
  36124. #endif /* !defined(NO_FILESYSTEM) */
  36125. /* BIO info callback */
  36126. {
  36127. const char* testArg = "test";
  36128. BIO* cb_bio = NULL;
  36129. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  36130. BIO_set_callback(cb_bio, bioCallback);
  36131. ExpectNotNull(BIO_get_callback(cb_bio));
  36132. BIO_set_callback(cb_bio, NULL);
  36133. ExpectNull(BIO_get_callback(cb_bio));
  36134. BIO_set_callback_arg(cb_bio, (char*)testArg);
  36135. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  36136. ExpectNull(BIO_get_callback_arg(NULL));
  36137. BIO_free(cb_bio);
  36138. cb_bio = NULL;
  36139. }
  36140. /* BIO_vfree */
  36141. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  36142. BIO_vfree(NULL);
  36143. BIO_vfree(bio1);
  36144. #endif
  36145. return EXPECT_RESULT();
  36146. }
  36147. static int test_wolfSSL_BIO_BIO_ring_read(void)
  36148. {
  36149. EXPECT_DECLS;
  36150. #if defined(OPENSSL_ALL)
  36151. BIO* bio1 = NULL;
  36152. BIO* bio2 = NULL;
  36153. byte data[50];
  36154. byte tmp[50];
  36155. XMEMSET(data, 42, sizeof(data));
  36156. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  36157. SSL_SUCCESS);
  36158. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  36159. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  36160. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  36161. ExpectBufEQ(tmp, data, 20);
  36162. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  36163. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  36164. ExpectBufEQ(tmp, data, 40);
  36165. BIO_free(bio1);
  36166. BIO_free(bio2);
  36167. #endif
  36168. return EXPECT_RESULT();
  36169. }
  36170. #endif /* !NO_BIO */
  36171. static int test_wolfSSL_a2i_IPADDRESS(void)
  36172. {
  36173. EXPECT_DECLS;
  36174. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  36175. const unsigned char* data = NULL;
  36176. int dataSz = 0;
  36177. ASN1_OCTET_STRING *st = NULL;
  36178. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  36179. const unsigned char ipv6_exp[] = {
  36180. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36181. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  36182. };
  36183. const unsigned char ipv6_home[] = {
  36184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  36186. };
  36187. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  36188. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  36189. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36190. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  36191. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  36192. ASN1_STRING_free(st);
  36193. st = NULL;
  36194. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  36195. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36196. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36197. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  36198. ASN1_STRING_free(st);
  36199. st = NULL;
  36200. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  36201. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36202. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36203. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  36204. ASN1_STRING_free(st);
  36205. #endif
  36206. return EXPECT_RESULT();
  36207. }
  36208. static int test_wolfSSL_X509_cmp_time(void)
  36209. {
  36210. EXPECT_DECLS;
  36211. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  36212. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  36213. WOLFSSL_ASN1_TIME asn_time;
  36214. time_t t;
  36215. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  36216. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  36217. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  36218. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  36219. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  36220. #endif
  36221. return EXPECT_RESULT();
  36222. }
  36223. static int test_wolfSSL_X509_time_adj(void)
  36224. {
  36225. EXPECT_DECLS;
  36226. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36227. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36228. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36229. !defined(NO_ASN_TIME)
  36230. X509* x509 = NULL;
  36231. time_t t;
  36232. time_t not_before;
  36233. time_t not_after;
  36234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36235. client_cert_der_2048, sizeof_client_cert_der_2048,
  36236. WOLFSSL_FILETYPE_ASN1));
  36237. t = 0;
  36238. not_before = wc_Time(0);
  36239. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36240. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36241. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36242. /* Check X509_gmtime_adj, too. */
  36243. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36244. X509_free(x509);
  36245. #endif
  36246. return EXPECT_RESULT();
  36247. }
  36248. static int test_wolfSSL_X509_bad_altname(void)
  36249. {
  36250. EXPECT_DECLS;
  36251. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36252. const unsigned char malformed_alt_name_cert[] = {
  36253. 0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
  36254. 0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  36255. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
  36256. 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
  36257. 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
  36258. 0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
  36259. 0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
  36260. 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
  36261. 0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  36262. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  36263. 0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
  36264. 0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
  36265. 0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
  36266. 0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
  36267. 0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
  36268. 0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
  36269. 0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
  36270. 0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
  36271. 0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
  36272. 0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
  36273. 0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
  36274. 0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
  36275. 0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
  36276. 0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
  36277. 0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
  36278. 0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
  36279. 0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
  36280. 0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
  36281. 0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
  36282. 0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
  36283. 0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
  36284. 0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
  36285. 0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
  36286. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  36287. 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
  36288. 0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  36289. 0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
  36290. 0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
  36291. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  36292. 0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
  36293. 0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
  36294. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  36295. 0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
  36296. 0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
  36297. 0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
  36298. 0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
  36299. 0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
  36300. 0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
  36301. 0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
  36302. 0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
  36303. 0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
  36304. 0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
  36305. 0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
  36306. 0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
  36307. 0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
  36308. 0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
  36309. 0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
  36310. 0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
  36311. 0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
  36312. 0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
  36313. 0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
  36314. 0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
  36315. 0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
  36316. 0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
  36317. };
  36318. X509* x509 = NULL;
  36319. int certSize = (int)sizeof(malformed_alt_name_cert) / sizeof(unsigned char);
  36320. const char *name = "aaaaa";
  36321. int nameLen = (int)XSTRLEN(name);
  36322. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36323. malformed_alt_name_cert, certSize, SSL_FILETYPE_ASN1));
  36324. /* malformed_alt_name_cert has a malformed alternative
  36325. * name of "a*\0*". Ensure that it does not match "aaaaa" */
  36326. ExpectIntNE(wolfSSL_X509_check_host(x509, name, nameLen,
  36327. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  36328. X509_free(x509);
  36329. #endif
  36330. return EXPECT_RESULT();
  36331. }
  36332. static int test_wolfSSL_X509_name_match(void)
  36333. {
  36334. EXPECT_DECLS;
  36335. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36336. /* A certificate with the subject alternative name a* */
  36337. const unsigned char cert_der[] = {
  36338. 0x30, 0x82, 0x03, 0xac, 0x30, 0x82, 0x02, 0x94, 0xa0, 0x03, 0x02, 0x01,
  36339. 0x02, 0x02, 0x14, 0x0f, 0xa5, 0x10, 0x85, 0xef, 0x58, 0x10, 0x59, 0xfc,
  36340. 0x0f, 0x20, 0x1f, 0x53, 0xf5, 0x30, 0x39, 0x34, 0x49, 0x54, 0x05, 0x30,
  36341. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  36342. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  36343. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  36344. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  36345. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  36346. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  36347. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  36348. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  36349. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  36350. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  36351. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  36352. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  36353. 0x30, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  36354. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x30,
  36355. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  36356. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  36357. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  36358. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  36359. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  36360. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  36361. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  36362. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  36363. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  36364. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  36365. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  36366. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  36367. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xf4, 0xca, 0x3d,
  36368. 0xd4, 0xbc, 0x9b, 0xea, 0x74, 0xfe, 0x73, 0xf4, 0x16, 0x23, 0x0b, 0x4a,
  36369. 0x09, 0x54, 0xf6, 0x7b, 0x10, 0x99, 0x11, 0x93, 0xb2, 0xdb, 0x4d, 0x7d,
  36370. 0x23, 0xab, 0xf9, 0xcd, 0xf6, 0x54, 0xd4, 0xf6, 0x39, 0x57, 0xee, 0x97,
  36371. 0xb2, 0xb9, 0xfc, 0x7e, 0x9c, 0xb3, 0xfb, 0x56, 0xb6, 0x84, 0xd6, 0x2d,
  36372. 0x59, 0x1c, 0xed, 0xda, 0x9b, 0x19, 0xf5, 0x8a, 0xa7, 0x8a, 0x89, 0xd6,
  36373. 0xa1, 0xc0, 0xe6, 0x16, 0xad, 0x04, 0xcf, 0x5a, 0x1f, 0xdf, 0x62, 0x6c,
  36374. 0x68, 0x45, 0xe9, 0x55, 0x2e, 0x42, 0xa3, 0x1b, 0x3b, 0x86, 0x23, 0x22,
  36375. 0xa1, 0x20, 0x48, 0xd1, 0x52, 0xc0, 0x8b, 0xab, 0xe2, 0x8a, 0x15, 0x68,
  36376. 0xbd, 0x89, 0x6f, 0x9f, 0x45, 0x75, 0xb4, 0x27, 0xc1, 0x72, 0x41, 0xfd,
  36377. 0x79, 0x89, 0xb0, 0x74, 0xa2, 0xe9, 0x61, 0x48, 0x4c, 0x54, 0xad, 0x6b,
  36378. 0x61, 0xbf, 0x0e, 0x27, 0x58, 0xb4, 0xf6, 0x9c, 0x2c, 0x9f, 0xc2, 0x3e,
  36379. 0x3b, 0xb3, 0x90, 0x41, 0xbc, 0x61, 0xcd, 0x01, 0x57, 0x90, 0x82, 0xec,
  36380. 0x46, 0xba, 0x4f, 0x89, 0x8e, 0x7f, 0x49, 0x4f, 0x46, 0x69, 0x37, 0x8b,
  36381. 0xa0, 0xba, 0x85, 0xe8, 0x42, 0xff, 0x9a, 0xa1, 0x53, 0x81, 0x5c, 0xf3,
  36382. 0x8e, 0x85, 0x1c, 0xd4, 0x90, 0x60, 0xa0, 0x37, 0x59, 0x04, 0x65, 0xa6,
  36383. 0xb5, 0x12, 0x00, 0xc3, 0x04, 0x51, 0xa7, 0x83, 0x96, 0x62, 0x3d, 0x49,
  36384. 0x97, 0xe8, 0x6b, 0x9a, 0x5d, 0x51, 0x24, 0xee, 0xad, 0x45, 0x18, 0x0f,
  36385. 0x3f, 0x97, 0xec, 0xdf, 0xcf, 0x42, 0x8a, 0x96, 0xc7, 0xd8, 0x82, 0x87,
  36386. 0x7f, 0x57, 0x70, 0x22, 0xfb, 0x29, 0x3e, 0x3c, 0xa3, 0xc1, 0xd5, 0x71,
  36387. 0xb3, 0x84, 0x06, 0x53, 0xa3, 0x86, 0x20, 0x35, 0xe3, 0x41, 0xb9, 0xd8,
  36388. 0x00, 0x22, 0x4f, 0x6d, 0xe6, 0xfd, 0xf0, 0xf4, 0xa2, 0x39, 0x0a, 0x1a,
  36389. 0x23, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x30, 0x30, 0x2e, 0x30, 0x0d,
  36390. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x06, 0x30, 0x04, 0x82, 0x02, 0x61,
  36391. 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  36392. 0x45, 0x05, 0xf3, 0x4d, 0x3e, 0x7e, 0x9c, 0xf5, 0x08, 0xee, 0x2c, 0x13,
  36393. 0x32, 0xe3, 0xf2, 0x14, 0xe8, 0x0e, 0x71, 0x21, 0x30, 0x0d, 0x06, 0x09,
  36394. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  36395. 0x82, 0x01, 0x01, 0x00, 0xa8, 0x28, 0xe5, 0x22, 0x65, 0xcf, 0x47, 0xfe,
  36396. 0x82, 0x17, 0x99, 0x20, 0xdb, 0xb1, 0x57, 0xd4, 0x91, 0x1a, 0x83, 0xde,
  36397. 0xc1, 0xaf, 0xc4, 0x1f, 0xfb, 0xa4, 0x6a, 0xad, 0xdc, 0x58, 0x72, 0xd9,
  36398. 0x9b, 0xab, 0xa5, 0xbb, 0xf4, 0x98, 0xd4, 0xdf, 0x36, 0xcb, 0xb5, 0x78,
  36399. 0xce, 0x4b, 0x25, 0x5b, 0x24, 0x92, 0xfe, 0xe8, 0xd4, 0xe4, 0xbd, 0x6f,
  36400. 0x71, 0x1a, 0x81, 0x2a, 0x6f, 0x35, 0x93, 0xf7, 0xcc, 0xed, 0xe5, 0x06,
  36401. 0xd2, 0x96, 0x41, 0xb5, 0xa9, 0x8a, 0xc0, 0xc9, 0x17, 0xe3, 0x13, 0x5e,
  36402. 0x94, 0x5e, 0xfa, 0xfc, 0xf0, 0x00, 0x2e, 0xe1, 0xd8, 0x1b, 0x23, 0x3f,
  36403. 0x7c, 0x4d, 0x9f, 0xfb, 0xb7, 0x95, 0xc1, 0x94, 0x7f, 0x7f, 0xb5, 0x4f,
  36404. 0x93, 0x6d, 0xc3, 0x2b, 0xb2, 0x28, 0x36, 0xd2, 0x7c, 0x01, 0x3c, 0xae,
  36405. 0x35, 0xdb, 0xc8, 0x95, 0x1b, 0x5f, 0x6c, 0x0f, 0x57, 0xb3, 0xcc, 0x97,
  36406. 0x98, 0x80, 0x06, 0xaa, 0xe4, 0x93, 0x1f, 0xb7, 0xa0, 0x54, 0xf1, 0x4f,
  36407. 0x6f, 0x11, 0xdf, 0xab, 0xd3, 0xbf, 0xf0, 0x3a, 0x81, 0x60, 0xaf, 0x7a,
  36408. 0xf7, 0x09, 0xd5, 0xae, 0x0c, 0x7d, 0xae, 0x8d, 0x47, 0x06, 0xbe, 0x11,
  36409. 0x6e, 0xf8, 0x7e, 0x49, 0xf8, 0xac, 0x24, 0x0a, 0x4b, 0xc2, 0xf6, 0xe8,
  36410. 0x2c, 0xec, 0x35, 0xef, 0xa9, 0x13, 0xb8, 0xd2, 0x9c, 0x92, 0x61, 0x91,
  36411. 0xec, 0x7b, 0x0c, 0xea, 0x9a, 0x71, 0x36, 0x15, 0x34, 0x2b, 0x7a, 0x25,
  36412. 0xac, 0xfe, 0xc7, 0x26, 0x89, 0x70, 0x3e, 0x64, 0x68, 0x97, 0x4b, 0xaa,
  36413. 0xc1, 0x24, 0x14, 0xbd, 0x45, 0x2f, 0xe0, 0xfe, 0xf4, 0x2b, 0x8e, 0x08,
  36414. 0x3e, 0xe4, 0xb5, 0x3d, 0x5d, 0xf4, 0xc3, 0xd6, 0x9c, 0xb5, 0x33, 0x1b,
  36415. 0x3b, 0xda, 0x6e, 0x99, 0x7b, 0x09, 0xd1, 0x30, 0x97, 0x23, 0x52, 0x6d,
  36416. 0x1b, 0x71, 0x3a, 0xf4, 0x54, 0xf0, 0xe5, 0x9e
  36417. };
  36418. WOLFSSL_X509* x509 = NULL;
  36419. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  36420. const char *name1 = "aaaaa";
  36421. int nameLen1 = (int)(XSTRLEN(name1));
  36422. const char *name2 = "a";
  36423. int nameLen2 = (int)(XSTRLEN(name2));
  36424. const char *name3 = "abbbb";
  36425. int nameLen3 = (int)(XSTRLEN(name3));
  36426. const char *name4 = "bbb";
  36427. int nameLen4 = (int)(XSTRLEN(name4));
  36428. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36429. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  36430. /* Ensure that "a*" matches "aaaaa" */
  36431. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  36432. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36433. /* Ensure that "a*" matches "a" */
  36434. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  36435. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36436. /* Ensure that "a*" matches "abbbb" */
  36437. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  36438. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36439. /* Ensure that "a*" does not match "bbb" */
  36440. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  36441. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  36442. wolfSSL_X509_free(x509);
  36443. #endif
  36444. return EXPECT_RESULT();
  36445. }
  36446. static int test_wolfSSL_X509_name_match2(void)
  36447. {
  36448. EXPECT_DECLS;
  36449. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36450. /* A certificate with the subject alternative name a*b* */
  36451. const unsigned char cert_der[] = {
  36452. 0x30, 0x82, 0x03, 0xae, 0x30, 0x82, 0x02, 0x96, 0xa0, 0x03, 0x02, 0x01,
  36453. 0x02, 0x02, 0x14, 0x41, 0x8c, 0x8b, 0xaa, 0x0e, 0xd8, 0x5a, 0xc0, 0x52,
  36454. 0x46, 0x0e, 0xe5, 0xd8, 0xb9, 0x48, 0x93, 0x7e, 0x8a, 0x7c, 0x65, 0x30,
  36455. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  36456. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  36457. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  36458. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  36459. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  36460. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  36461. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  36462. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  36463. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  36464. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  36465. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  36466. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  36467. 0x30, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  36468. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x30,
  36469. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  36470. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  36471. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  36472. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  36473. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  36474. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  36475. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  36476. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  36477. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  36478. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  36479. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  36480. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  36481. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa5, 0x60, 0x80,
  36482. 0xf3, 0xee, 0x19, 0xd2, 0xe4, 0x15, 0x94, 0x54, 0x12, 0x88, 0xee, 0xda,
  36483. 0x11, 0x11, 0x87, 0x99, 0x88, 0xb3, 0x71, 0xc7, 0x97, 0x78, 0x1b, 0x57,
  36484. 0x37, 0x1d, 0x0b, 0x1f, 0x2f, 0x2c, 0x35, 0x13, 0x75, 0xd3, 0x31, 0x3e,
  36485. 0x6f, 0x80, 0x21, 0xa5, 0xa3, 0xad, 0x10, 0x81, 0xb6, 0x37, 0xd4, 0x55,
  36486. 0x2e, 0xc1, 0xb8, 0x37, 0xa3, 0x3c, 0xe8, 0x81, 0x03, 0x3c, 0xda, 0x5f,
  36487. 0x6f, 0x45, 0x32, 0x2b, 0x0e, 0x99, 0x27, 0xfd, 0xe5, 0x6c, 0x07, 0xd9,
  36488. 0x4e, 0x0a, 0x8b, 0x23, 0x74, 0x96, 0x25, 0x97, 0xae, 0x6d, 0x19, 0xba,
  36489. 0xbf, 0x0f, 0xc8, 0xa1, 0xe5, 0xea, 0xa8, 0x00, 0x09, 0xc3, 0x9a, 0xef,
  36490. 0x09, 0x33, 0xc1, 0x33, 0x2e, 0x7b, 0x6d, 0xa7, 0x66, 0x87, 0xb6, 0x3a,
  36491. 0xb9, 0xdb, 0x4c, 0x5e, 0xb5, 0x55, 0x69, 0x37, 0x17, 0x92, 0x1f, 0xe3,
  36492. 0x53, 0x1a, 0x2d, 0x25, 0xd0, 0xcf, 0x72, 0x37, 0xc2, 0x89, 0x83, 0x78,
  36493. 0xcf, 0xac, 0x2e, 0x46, 0x92, 0x5c, 0x4a, 0xba, 0x7d, 0xa0, 0x22, 0x34,
  36494. 0xb1, 0x22, 0x26, 0x99, 0xda, 0xe8, 0x97, 0xe2, 0x0c, 0xd3, 0xbc, 0x97,
  36495. 0x7e, 0xa8, 0xb9, 0xe3, 0xe2, 0x7f, 0x56, 0xef, 0x22, 0xee, 0x15, 0x95,
  36496. 0xa6, 0xd1, 0xf4, 0xa7, 0xac, 0x4a, 0xab, 0xc1, 0x1a, 0xda, 0xc5, 0x5f,
  36497. 0xa5, 0x5e, 0x2f, 0x15, 0x9c, 0x36, 0xbe, 0xd3, 0x47, 0xb6, 0x86, 0xb9,
  36498. 0xc6, 0x59, 0x39, 0x36, 0xad, 0x84, 0x53, 0x95, 0x72, 0x91, 0x89, 0x51,
  36499. 0x32, 0x77, 0xf1, 0xa5, 0x93, 0xfe, 0xf0, 0x41, 0x7c, 0x64, 0xf1, 0xb0,
  36500. 0x8b, 0x81, 0x8d, 0x3a, 0x2c, 0x9e, 0xbe, 0x2e, 0x8b, 0xf7, 0x80, 0x63,
  36501. 0x35, 0x32, 0xfa, 0x26, 0xe0, 0x63, 0xbf, 0x5e, 0xaf, 0xf0, 0x08, 0xe0,
  36502. 0x80, 0x65, 0x38, 0xfa, 0x21, 0xaa, 0x91, 0x34, 0x48, 0x3d, 0x32, 0x5c,
  36503. 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x32, 0x30, 0x30, 0x30, 0x0f,
  36504. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82, 0x04, 0x61,
  36505. 0x2a, 0x62, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16,
  36506. 0x04, 0x14, 0x3d, 0x55, 0x74, 0xf8, 0x3a, 0x26, 0x03, 0x8c, 0x6a, 0x2e,
  36507. 0x91, 0x0e, 0x18, 0x70, 0xb4, 0xa4, 0xcc, 0x04, 0x00, 0xd3, 0x30, 0x0d,
  36508. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  36509. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x8f, 0x3b, 0xff, 0x46, 0x0c, 0xb5,
  36510. 0x21, 0xdc, 0xcf, 0x61, 0x9a, 0x25, 0x93, 0x99, 0x68, 0x2f, 0x16, 0x71,
  36511. 0x15, 0x00, 0x5f, 0xb0, 0x9b, 0x43, 0x5c, 0x47, 0xe2, 0x8e, 0xc8, 0xea,
  36512. 0xb3, 0x30, 0x4d, 0x87, 0x90, 0xcf, 0x24, 0x37, 0x5c, 0xfd, 0xc8, 0xc6,
  36513. 0x09, 0x36, 0xb2, 0xfb, 0xfd, 0xc1, 0x82, 0x92, 0x77, 0x5b, 0x9d, 0xeb,
  36514. 0xac, 0x47, 0xbc, 0xda, 0x7c, 0x89, 0x19, 0x03, 0x9e, 0xcd, 0x96, 0x2a,
  36515. 0x90, 0x55, 0x23, 0x19, 0xac, 0x9d, 0x49, 0xfb, 0xa0, 0x31, 0x7d, 0x6b,
  36516. 0x1a, 0x16, 0x13, 0xb1, 0xa9, 0xc9, 0xc4, 0xaf, 0xf1, 0xb4, 0xa7, 0x9b,
  36517. 0x08, 0x64, 0x6a, 0x09, 0xcd, 0x4a, 0x03, 0x4c, 0x93, 0xb6, 0xcf, 0x29,
  36518. 0xdb, 0x56, 0x88, 0x8e, 0xed, 0x08, 0x6d, 0x8d, 0x76, 0xa3, 0xd7, 0xc6,
  36519. 0x69, 0xa1, 0xf5, 0xd2, 0xd0, 0x0a, 0x4b, 0xfa, 0x88, 0x66, 0x6c, 0xe5,
  36520. 0x4a, 0xee, 0x13, 0xad, 0xad, 0x22, 0x25, 0x73, 0x39, 0x56, 0x74, 0x0e,
  36521. 0xda, 0xcd, 0x35, 0x67, 0xe3, 0x81, 0x5c, 0xc5, 0xae, 0x3c, 0x4f, 0x47,
  36522. 0x3e, 0x97, 0xde, 0xac, 0xf6, 0xe1, 0x26, 0xe2, 0xe0, 0x66, 0x48, 0x20,
  36523. 0x7c, 0x02, 0x81, 0x3e, 0x7d, 0x34, 0xb7, 0x73, 0x3e, 0x2e, 0xd6, 0x20,
  36524. 0x1c, 0xdf, 0xf1, 0xae, 0x86, 0x8b, 0xb2, 0xc2, 0x9b, 0x68, 0x9c, 0xf6,
  36525. 0x1a, 0x5e, 0x30, 0x06, 0x39, 0x0a, 0x1f, 0x7b, 0xd7, 0x18, 0x4b, 0x06,
  36526. 0x9d, 0xff, 0x84, 0x57, 0xcc, 0x92, 0xad, 0x81, 0x0a, 0x19, 0x11, 0xc4,
  36527. 0xac, 0x59, 0x00, 0xe8, 0x5a, 0x70, 0x78, 0xd6, 0x9f, 0xe0, 0x82, 0x2a,
  36528. 0x1f, 0x09, 0x36, 0x1c, 0x52, 0x98, 0xf7, 0x95, 0x8f, 0xf9, 0x48, 0x4f,
  36529. 0x30, 0x52, 0xb5, 0xf3, 0x8d, 0x13, 0x93, 0x27, 0xbe, 0xb4, 0x75, 0x39,
  36530. 0x65, 0xc6, 0x48, 0x4e, 0x32, 0xd7, 0xf4, 0xc3, 0x26, 0x8d
  36531. };
  36532. WOLFSSL_X509* x509 = NULL;
  36533. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  36534. const char *name1 = "ab";
  36535. int nameLen1 = (int)(XSTRLEN(name1));
  36536. const char *name2 = "acccbccc";
  36537. int nameLen2 = (int)(XSTRLEN(name2));
  36538. const char *name3 = "accb";
  36539. int nameLen3 = (int)(XSTRLEN(name3));
  36540. const char *name4 = "accda";
  36541. int nameLen4 = (int)(XSTRLEN(name4));
  36542. const char *name5 = "acc\0bcc";
  36543. int nameLen5 = 7;
  36544. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36545. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  36546. /* Ensure that "a*b*" matches "ab" */
  36547. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  36548. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36549. /* Ensure that "a*b*" matches "acccbccc" */
  36550. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  36551. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36552. /* Ensure that "a*b*" matches "accb" */
  36553. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  36554. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36555. /* Ensure that "a*b*" does not match "accda" */
  36556. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  36557. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36558. /* Ensure that "a*b*" matches "ab", testing openssl behavior replication
  36559. * on check len input handling, 0 for len is OK as it should then use
  36560. * strlen(name1) */
  36561. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, 0,
  36562. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36563. /* Openssl also allows for len to include NULL terminator */
  36564. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1 + 1,
  36565. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36566. /* Ensure that check string with NULL terminator in middle is
  36567. * rejected */
  36568. ExpectIntNE(wolfSSL_X509_check_host(x509, name5, nameLen5,
  36569. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36570. wolfSSL_X509_free(x509);
  36571. #endif
  36572. return EXPECT_RESULT();
  36573. }
  36574. static int test_wolfSSL_X509_name_match3(void)
  36575. {
  36576. EXPECT_DECLS;
  36577. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36578. /* A certificate with the subject alternative name *.example.com */
  36579. const unsigned char cert_der[] = {
  36580. 0x30, 0x82, 0x03, 0xb7, 0x30, 0x82, 0x02, 0x9f, 0xa0, 0x03, 0x02, 0x01,
  36581. 0x02, 0x02, 0x14, 0x59, 0xbb, 0xf6, 0xde, 0xb8, 0x3d, 0x0e, 0x8c, 0xe4,
  36582. 0xbd, 0x98, 0xa3, 0xbe, 0x3e, 0x8f, 0xdc, 0xbd, 0x7f, 0xcc, 0xae, 0x30,
  36583. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  36584. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  36585. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  36586. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  36587. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  36588. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  36589. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  36590. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  36591. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  36592. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  36593. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  36594. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  36595. 0x31, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  36596. 0x30, 0x35, 0x32, 0x39, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x30,
  36597. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  36598. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  36599. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  36600. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  36601. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  36602. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  36603. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  36604. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  36605. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  36606. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  36607. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  36608. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  36609. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0x78, 0x16,
  36610. 0x05, 0x65, 0xf2, 0x85, 0xf2, 0x61, 0x7f, 0xb1, 0x4d, 0x73, 0xe2, 0x82,
  36611. 0xb5, 0x3d, 0xf7, 0x9d, 0x05, 0x65, 0xed, 0x9d, 0xc3, 0x29, 0x7a, 0x92,
  36612. 0x2c, 0x06, 0x5f, 0xc8, 0x13, 0x55, 0x42, 0x4e, 0xbd, 0xe2, 0x56, 0x2a,
  36613. 0x4b, 0xac, 0xe6, 0x1b, 0x10, 0xc9, 0xdb, 0x9a, 0x45, 0x36, 0xed, 0xf3,
  36614. 0x26, 0x8c, 0x22, 0x88, 0x1e, 0x6d, 0x2b, 0x41, 0xfa, 0x0d, 0x43, 0x88,
  36615. 0x88, 0xde, 0x8d, 0x2e, 0xca, 0x6e, 0x7c, 0x62, 0x66, 0x3e, 0xfa, 0x4e,
  36616. 0x71, 0xea, 0x7d, 0x3b, 0x32, 0x33, 0x5c, 0x7a, 0x7e, 0xea, 0x74, 0xbd,
  36617. 0xb6, 0x8f, 0x4c, 0x1c, 0x7a, 0x79, 0x94, 0xf1, 0xe8, 0x02, 0x67, 0x98,
  36618. 0x25, 0xb4, 0x31, 0x80, 0xc1, 0xae, 0xbf, 0xef, 0xf2, 0x6c, 0x78, 0x42,
  36619. 0xef, 0xb5, 0xc6, 0x01, 0x47, 0x79, 0x8d, 0x92, 0xce, 0xc1, 0xb5, 0x98,
  36620. 0x76, 0xf0, 0x84, 0xa2, 0x53, 0x90, 0xe5, 0x39, 0xc7, 0xbd, 0xf2, 0xbb,
  36621. 0xe3, 0x3f, 0x00, 0xf6, 0xf0, 0x46, 0x86, 0xee, 0x55, 0xbd, 0x2c, 0x1f,
  36622. 0x97, 0x24, 0x7c, 0xbc, 0xda, 0x2f, 0x1b, 0x53, 0xef, 0x26, 0x56, 0xcc,
  36623. 0xb7, 0xd8, 0xca, 0x17, 0x20, 0x4e, 0x62, 0x03, 0x66, 0x32, 0xb3, 0xd1,
  36624. 0x71, 0x26, 0x6c, 0xff, 0xd1, 0x9e, 0x44, 0x86, 0x2a, 0xae, 0xba, 0x43,
  36625. 0x00, 0x13, 0x7e, 0x50, 0xdd, 0x3e, 0x27, 0x39, 0x70, 0x1c, 0x0c, 0x0b,
  36626. 0xe8, 0xa2, 0xae, 0x03, 0x09, 0x2e, 0xd8, 0x71, 0xee, 0x7b, 0x1a, 0x09,
  36627. 0x2d, 0xe1, 0xd5, 0xde, 0xf5, 0xa3, 0x36, 0x77, 0x90, 0x97, 0x99, 0xd7,
  36628. 0x6c, 0xb7, 0x5c, 0x9d, 0xf7, 0x7e, 0x41, 0x89, 0xfe, 0xe4, 0x08, 0xc6,
  36629. 0x0b, 0xe4, 0x9b, 0x5f, 0x51, 0xa6, 0x08, 0xb8, 0x99, 0x81, 0xe9, 0xce,
  36630. 0xb4, 0x2d, 0xb2, 0x92, 0x9f, 0xe5, 0x1a, 0x98, 0x76, 0x20, 0x70, 0x54,
  36631. 0x93, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x18,
  36632. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x11, 0x30, 0x0f, 0x82, 0x0d, 0x2a,
  36633. 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
  36634. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x60,
  36635. 0xd4, 0x26, 0xbb, 0xcc, 0x7c, 0x29, 0xa2, 0x88, 0x3c, 0x76, 0x7d, 0xb4,
  36636. 0x86, 0x8b, 0x47, 0x64, 0x5b, 0x87, 0xe0, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  36637. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  36638. 0x01, 0x01, 0x00, 0xc3, 0x0d, 0x03, 0x67, 0xbb, 0x47, 0x8b, 0xf3, 0x20,
  36639. 0xdc, 0x7d, 0x2e, 0xe1, 0xd9, 0xf0, 0x01, 0xc4, 0x66, 0xc2, 0xe1, 0xcd,
  36640. 0xc3, 0x4a, 0x72, 0xf0, 0x6e, 0x38, 0xcf, 0x63, 0x01, 0x96, 0x9e, 0x84,
  36641. 0xb9, 0xce, 0x1d, 0xba, 0x4b, 0xe0, 0x70, 0x86, 0x2b, 0x5a, 0xab, 0xec,
  36642. 0xbf, 0xc2, 0xaa, 0x64, 0xa2, 0x6c, 0xd2, 0x42, 0x52, 0xd4, 0xbe, 0x8a,
  36643. 0xca, 0x9c, 0x03, 0xf3, 0xd6, 0x5f, 0xcd, 0x23, 0x9f, 0xf5, 0xa9, 0x04,
  36644. 0x40, 0x5b, 0x66, 0x78, 0xc0, 0xac, 0xa1, 0xdb, 0x5d, 0xd1, 0x94, 0xfc,
  36645. 0x47, 0x94, 0xf5, 0x45, 0xe3, 0x70, 0x13, 0x3f, 0x66, 0x6d, 0xdd, 0x73,
  36646. 0x68, 0x68, 0xe2, 0xd2, 0x89, 0xcb, 0x7f, 0xc6, 0xca, 0xd6, 0x96, 0x0b,
  36647. 0xcc, 0xdd, 0xa1, 0x74, 0xda, 0x33, 0xe8, 0x9e, 0xda, 0xb7, 0xd9, 0x12,
  36648. 0xab, 0x85, 0x9d, 0x0c, 0xde, 0xa0, 0x7d, 0x7e, 0xa1, 0x91, 0xed, 0xe5,
  36649. 0x32, 0x7c, 0xc5, 0xea, 0x1d, 0x4a, 0xb5, 0x38, 0x63, 0x17, 0xf3, 0x4f,
  36650. 0x2c, 0x4a, 0x58, 0x86, 0x09, 0x33, 0x86, 0xc4, 0xe7, 0x56, 0x6f, 0x32,
  36651. 0x71, 0xb7, 0xd0, 0x83, 0x12, 0x9e, 0x26, 0x0a, 0x3a, 0x45, 0xcb, 0xd7,
  36652. 0x4e, 0xab, 0xa4, 0xc3, 0xee, 0x4c, 0xc0, 0x38, 0xa1, 0xfa, 0xba, 0xfa,
  36653. 0xb7, 0x80, 0x69, 0x67, 0xa3, 0xef, 0x89, 0xba, 0xce, 0x89, 0x91, 0x3d,
  36654. 0x6a, 0x76, 0xe9, 0x3b, 0x32, 0x86, 0x76, 0x85, 0x6b, 0x4f, 0x7f, 0xbc,
  36655. 0x7a, 0x5b, 0x31, 0x92, 0x79, 0x35, 0xf8, 0xb9, 0xb1, 0xd7, 0xdb, 0xa9,
  36656. 0x6a, 0x8a, 0x91, 0x60, 0x65, 0xd4, 0x76, 0x54, 0x55, 0x57, 0xb9, 0x35,
  36657. 0xe0, 0xf5, 0xbb, 0x8f, 0xd4, 0x40, 0x75, 0xbb, 0x47, 0xa8, 0xf9, 0x0f,
  36658. 0xea, 0xc9, 0x6e, 0x84, 0xd5, 0xf5, 0x58, 0x2d, 0xe5, 0x76, 0x7b, 0xdf,
  36659. 0x97, 0x05, 0x5e, 0xaf, 0x50, 0xf5, 0x48
  36660. };
  36661. WOLFSSL_X509* x509 = NULL;
  36662. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  36663. const char *name1 = "foo.example.com";
  36664. int nameLen1 = (int)(XSTRLEN(name1));
  36665. const char *name2 = "x.y.example.com";
  36666. int nameLen2 = (int)(XSTRLEN(name2));
  36667. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36668. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  36669. /* Ensure that "*.example.com" matches "foo.example.com" */
  36670. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  36671. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36672. /* Ensure that "*.example.com" does NOT match "x.y.example.com" */
  36673. ExpectIntNE(wolfSSL_X509_check_host(x509, name2, nameLen2,
  36674. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  36675. wolfSSL_X509_free(x509);
  36676. #endif
  36677. return EXPECT_RESULT();
  36678. }
  36679. static int test_wolfSSL_X509_max_altnames(void)
  36680. {
  36681. EXPECT_DECLS;
  36682. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  36683. /* Only test if max alt names has not been modified */
  36684. #if WOLFSSL_MAX_ALT_NAMES == 128
  36685. WOLFSSL_CTX* ctx = NULL;
  36686. /* File contains a certificate encoded with 130 subject alternative names */
  36687. const char* over_max_altnames_cert = \
  36688. "./certs/test/cert-over-max-altnames.pem";
  36689. #ifndef NO_WOLFSSL_SERVER
  36690. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36691. #else
  36692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36693. #endif
  36694. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx,
  36695. over_max_altnames_cert, NULL, WOLFSSL_LOAD_FLAG_NONE),
  36696. WOLFSSL_SUCCESS);
  36697. wolfSSL_CTX_free(ctx);
  36698. #endif
  36699. #endif
  36700. return EXPECT_RESULT();
  36701. }
  36702. static int test_wolfSSL_X509_max_name_constraints(void)
  36703. {
  36704. EXPECT_DECLS;
  36705. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  36706. !defined(IGNORE_NAME_CONSTRAINTS)
  36707. /* Only test if max name constraints has not been modified */
  36708. #if WOLFSSL_MAX_NAME_CONSTRAINTS == 128
  36709. WOLFSSL_CTX* ctx = NULL;
  36710. /* File contains a certificate with 130 name constraints */
  36711. const char* over_max_nc = "./certs/test/cert-over-max-nc.pem";
  36712. #ifndef NO_WOLFSSL_SERVER
  36713. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36714. #else
  36715. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36716. #endif
  36717. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, over_max_nc,
  36718. NULL, WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  36719. wolfSSL_CTX_free(ctx);
  36720. #endif
  36721. #endif
  36722. return EXPECT_RESULT();
  36723. }
  36724. static int test_wolfSSL_X509(void)
  36725. {
  36726. EXPECT_DECLS;
  36727. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36728. !defined(NO_RSA)
  36729. X509* x509 = NULL;
  36730. #ifndef NO_BIO
  36731. BIO* bio = NULL;
  36732. X509_STORE_CTX* ctx = NULL;
  36733. X509_STORE* store = NULL;
  36734. #endif
  36735. char der[] = "certs/ca-cert.der";
  36736. XFILE fp = XBADFILE;
  36737. ExpectNotNull(x509 = X509_new());
  36738. X509_free(x509);
  36739. x509 = NULL;
  36740. #ifndef NO_BIO
  36741. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36742. SSL_FILETYPE_PEM));
  36743. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36744. #ifdef WOLFSSL_CERT_GEN
  36745. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36746. #endif
  36747. ExpectNotNull(ctx = X509_STORE_CTX_new());
  36748. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36749. ExpectNotNull(store = X509_STORE_new());
  36750. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36751. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36752. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36753. X509_STORE_CTX_free(ctx);
  36754. X509_STORE_free(store);
  36755. X509_free(x509);
  36756. x509 = NULL;
  36757. BIO_free(bio);
  36758. #endif
  36759. /** d2i_X509_fp test **/
  36760. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36761. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36762. ExpectNotNull(x509);
  36763. X509_free(x509);
  36764. x509 = NULL;
  36765. if (fp != XBADFILE) {
  36766. XFCLOSE(fp);
  36767. fp = XBADFILE;
  36768. }
  36769. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36770. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36771. ExpectNotNull(x509);
  36772. X509_free(x509);
  36773. if (fp != XBADFILE)
  36774. XFCLOSE(fp);
  36775. /* X509_up_ref test */
  36776. ExpectIntEQ(X509_up_ref(NULL), 0);
  36777. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  36778. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36779. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36780. X509_free(x509); /* refCount = 2 */
  36781. X509_free(x509); /* refCount = 1 */
  36782. X509_free(x509); /* refCount = 0, free */
  36783. #endif
  36784. return EXPECT_RESULT();
  36785. }
  36786. static int test_wolfSSL_X509_get_ext_count(void)
  36787. {
  36788. EXPECT_DECLS;
  36789. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36790. !defined(NO_RSA)
  36791. int ret = 0;
  36792. WOLFSSL_X509* x509 = NULL;
  36793. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36794. XFILE f = XBADFILE;
  36795. /* NULL parameter check */
  36796. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36797. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36798. SSL_FILETYPE_PEM));
  36799. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36800. wolfSSL_X509_free(x509);
  36801. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36802. SSL_FILETYPE_PEM));
  36803. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36804. wolfSSL_X509_free(x509);
  36805. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36806. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36807. if (f != XBADFILE)
  36808. XFCLOSE(f);
  36809. /* wolfSSL_X509_get_ext_count() valid input */
  36810. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36811. /* wolfSSL_X509_get_ext_count() NULL argument */
  36812. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36813. wolfSSL_X509_free(x509);
  36814. #endif
  36815. return EXPECT_RESULT();
  36816. }
  36817. static int test_wolfSSL_X509_sign2(void)
  36818. {
  36819. EXPECT_DECLS;
  36820. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36821. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36822. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36823. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36824. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36825. defined(WOLFSSL_IP_ALT_NAME))
  36826. WOLFSSL_X509 *x509 = NULL;
  36827. WOLFSSL_X509 *ca = NULL;
  36828. const unsigned char *der = NULL;
  36829. const unsigned char *pt = NULL;
  36830. WOLFSSL_EVP_PKEY *priv = NULL;
  36831. WOLFSSL_X509_NAME *name = NULL;
  36832. int derSz;
  36833. #ifndef NO_ASN_TIME
  36834. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36835. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36836. const int year = 365*24*60*60;
  36837. const int day = 24*60*60;
  36838. const int hour = 60*60;
  36839. const int mini = 60;
  36840. time_t t;
  36841. #endif
  36842. const unsigned char expected[] = {
  36843. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36844. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36845. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36846. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36847. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36848. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36849. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36850. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36851. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36852. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36853. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36854. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36855. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36856. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36857. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36858. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36859. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36860. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36861. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36862. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36863. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36864. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36865. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36866. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36867. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36868. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36869. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36870. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36871. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36872. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36873. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36874. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36875. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36876. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36877. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36878. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36879. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36880. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36881. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36882. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36883. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36884. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36885. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36886. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36887. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36888. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36889. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36890. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36891. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36892. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36893. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36894. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36895. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36896. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36897. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36898. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36899. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36900. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36901. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36902. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36903. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36904. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36905. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36906. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36907. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36908. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36909. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36910. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36911. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36912. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36913. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36914. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36915. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36916. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36917. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36918. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36919. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36920. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36921. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36922. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36923. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36924. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36925. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36926. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36927. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36928. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36929. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36930. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36931. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36932. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36933. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36934. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36935. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36936. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36937. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36938. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36939. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36940. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36941. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36942. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36943. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36944. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36945. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36946. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36947. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36948. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36949. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36950. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36951. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36952. };
  36953. pt = ca_key_der_2048;
  36954. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36955. sizeof_ca_key_der_2048));
  36956. pt = client_cert_der_2048;
  36957. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36958. sizeof_client_cert_der_2048));
  36959. pt = ca_cert_der_2048;
  36960. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36961. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36962. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36963. #ifndef NO_ASN_TIME
  36964. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36965. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36966. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36967. ExpectIntEQ(notAfter->length, 13);
  36968. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36969. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36970. #endif
  36971. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36972. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36973. ExpectIntEQ(derSz, sizeof(expected));
  36974. #ifndef NO_ASN_TIME
  36975. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36976. #endif
  36977. wolfSSL_X509_free(ca);
  36978. wolfSSL_X509_free(x509);
  36979. wolfSSL_EVP_PKEY_free(priv);
  36980. #ifndef NO_ASN_TIME
  36981. wolfSSL_ASN1_TIME_free(notBefore);
  36982. wolfSSL_ASN1_TIME_free(notAfter);
  36983. #endif
  36984. #endif
  36985. return EXPECT_RESULT();
  36986. }
  36987. static int test_wolfSSL_X509_sign(void)
  36988. {
  36989. EXPECT_DECLS;
  36990. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36991. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36992. int ret = 0;
  36993. char *cn = NULL;
  36994. word32 cnSz = 0;
  36995. X509_NAME *name = NULL;
  36996. X509 *x509 = NULL;
  36997. X509 *ca = NULL;
  36998. DecodedCert dCert;
  36999. EVP_PKEY *pub = NULL;
  37000. EVP_PKEY *priv = NULL;
  37001. EVP_MD_CTX *mctx = NULL;
  37002. #if defined(USE_CERT_BUFFERS_1024)
  37003. const unsigned char* rsaPriv = client_key_der_1024;
  37004. const unsigned char* rsaPub = client_keypub_der_1024;
  37005. const unsigned char* certIssuer = client_cert_der_1024;
  37006. long clientKeySz = (long)sizeof_client_key_der_1024;
  37007. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  37008. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  37009. #elif defined(USE_CERT_BUFFERS_2048)
  37010. const unsigned char* rsaPriv = client_key_der_2048;
  37011. const unsigned char* rsaPub = client_keypub_der_2048;
  37012. const unsigned char* certIssuer = client_cert_der_2048;
  37013. long clientKeySz = (long)sizeof_client_key_der_2048;
  37014. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  37015. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  37016. #endif
  37017. byte sn[16];
  37018. int snSz = sizeof(sn);
  37019. /* Set X509_NAME fields */
  37020. ExpectNotNull(name = X509_NAME_new());
  37021. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  37022. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  37023. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  37024. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  37025. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  37026. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  37027. /* Get private and public keys */
  37028. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  37029. clientKeySz));
  37030. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  37031. ExpectNotNull(x509 = X509_new());
  37032. /* Set version 3 */
  37033. ExpectIntNE(X509_set_version(x509, 2L), 0);
  37034. /* Set subject name, add pubkey, and sign certificate */
  37035. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  37036. X509_NAME_free(name);
  37037. name = NULL;
  37038. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  37039. #ifdef WOLFSSL_ALT_NAMES
  37040. /* Add some subject alt names */
  37041. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  37042. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  37043. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37044. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  37045. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37046. "sphygmomanometer",
  37047. ASN_DNS_TYPE), SSL_SUCCESS);
  37048. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37049. "supercalifragilisticexpialidocious",
  37050. ASN_DNS_TYPE), SSL_SUCCESS);
  37051. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  37052. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  37053. ASN_DNS_TYPE), SSL_SUCCESS);
  37054. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37055. {
  37056. unsigned char ip4_type[] = {127,128,0,255};
  37057. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  37058. 0xff, 0xee, 0x99, 0x88,
  37059. 0x77, 0x66, 0x55, 0x44,
  37060. 0x00, 0x33, 0x22, 0x11};
  37061. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  37062. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  37063. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  37064. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  37065. }
  37066. #endif
  37067. #endif /* WOLFSSL_ALT_NAMES */
  37068. {
  37069. ASN1_UTCTIME* infinite_past = NULL;
  37070. ExpectNotNull(infinite_past = ASN1_UTCTIME_set(NULL, 0));
  37071. ExpectIntEQ(X509_set1_notBefore(x509, infinite_past), 1);
  37072. ASN1_UTCTIME_free(infinite_past);
  37073. }
  37074. /* test valid sign case */
  37075. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  37076. /* test valid X509_sign_ctx case */
  37077. ExpectNotNull(mctx = EVP_MD_CTX_new());
  37078. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  37079. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  37080. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  37081. ExpectIntEQ(X509_get_ext_count(x509), 1);
  37082. #endif
  37083. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  37084. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  37085. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  37086. #endif
  37087. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  37088. WOLFSSL_SUCCESS);
  37089. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  37090. /* Variation in size depends on ASN.1 encoding when MSB is set.
  37091. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  37092. * with the MSB set. See GenerateInteger in asn.c */
  37093. #ifndef USE_CERT_BUFFERS_1024
  37094. #ifndef WOLFSSL_ALT_NAMES
  37095. /* Valid case - size should be 781-786 with 16 byte serial number */
  37096. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  37097. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37098. /* Valid case - size should be 955-960 with 16 byte serial number */
  37099. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  37100. #else
  37101. /* Valid case - size should be 926-931 with 16 byte serial number */
  37102. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  37103. #endif
  37104. #else
  37105. #ifndef WOLFSSL_ALT_NAMES
  37106. /* Valid case - size should be 537-542 with 16 byte serial number */
  37107. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  37108. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  37109. /* Valid case - size should be 695-670 with 16 byte serial number */
  37110. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  37111. #else
  37112. /* Valid case - size should be 666-671 with 16 byte serial number */
  37113. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  37114. #endif
  37115. #endif
  37116. /* check that issuer name is as expected after signature */
  37117. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  37118. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  37119. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  37120. ExpectNotNull(name = X509_get_subject_name(ca));
  37121. cnSz = X509_NAME_get_sz(name);
  37122. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  37123. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  37124. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  37125. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37126. cn = NULL;
  37127. #ifdef WOLFSSL_MULTI_ATTRIB
  37128. /* test adding multiple OU's to the signer */
  37129. ExpectNotNull(name = X509_get_subject_name(ca));
  37130. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  37131. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  37132. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  37133. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  37134. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  37135. #endif
  37136. ExpectNotNull(name = X509_get_subject_name(ca));
  37137. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  37138. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  37139. ExpectNotNull(name = X509_get_issuer_name(x509));
  37140. cnSz = X509_NAME_get_sz(name);
  37141. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  37142. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  37143. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  37144. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  37145. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  37146. cn = NULL;
  37147. FreeDecodedCert(&dCert);
  37148. /* Test invalid parameters */
  37149. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  37150. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  37151. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  37152. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  37153. EVP_MD_CTX_free(mctx);
  37154. mctx = NULL;
  37155. ExpectNotNull(mctx = EVP_MD_CTX_new());
  37156. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  37157. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  37158. /* test invalid version number */
  37159. #if defined(OPENSSL_ALL)
  37160. ExpectIntNE(X509_set_version(x509, 6L), 0);
  37161. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  37162. /* uses ParseCert which fails on bad version number */
  37163. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  37164. #endif
  37165. EVP_MD_CTX_free(mctx);
  37166. EVP_PKEY_free(priv);
  37167. EVP_PKEY_free(pub);
  37168. X509_free(x509);
  37169. X509_free(ca);
  37170. #endif
  37171. return EXPECT_RESULT();
  37172. }
  37173. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  37174. {
  37175. EXPECT_DECLS;
  37176. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  37177. X509* x509 = NULL;
  37178. const X509_ALGOR* alg;
  37179. ExpectNotNull(x509 = X509_new());
  37180. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  37181. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  37182. X509_free(x509);
  37183. #endif
  37184. return EXPECT_RESULT();
  37185. }
  37186. static int test_wolfSSL_X509_ALGOR_get0(void)
  37187. {
  37188. EXPECT_DECLS;
  37189. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  37190. !defined(NO_SHA256) && !defined(NO_RSA)
  37191. X509* x509 = NULL;
  37192. const ASN1_OBJECT* obj = NULL;
  37193. const X509_ALGOR* alg = NULL;
  37194. int pptype = 0;
  37195. const void *ppval = NULL;
  37196. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  37197. SSL_FILETYPE_PEM));
  37198. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  37199. /* Invalid case */
  37200. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  37201. ExpectNull(obj);
  37202. /* Valid case */
  37203. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  37204. ExpectNotNull(obj);
  37205. ExpectNull(ppval);
  37206. ExpectIntNE(pptype, 0);
  37207. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  37208. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  37209. X509_free(x509);
  37210. #endif
  37211. return EXPECT_RESULT();
  37212. }
  37213. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  37214. {
  37215. EXPECT_DECLS;
  37216. #if defined(OPENSSL_EXTRA)
  37217. X509_VERIFY_PARAM *paramTo = NULL;
  37218. X509_VERIFY_PARAM *paramFrom = NULL;
  37219. char testIPv4[] = "127.0.0.1";
  37220. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  37221. char testhostName1[] = "foo.hoge.com";
  37222. char testhostName2[] = "foobar.hoge.com";
  37223. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  37224. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  37225. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  37226. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  37227. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  37228. (int)XSTRLEN(testhostName1)), 1);
  37229. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  37230. (int)XSTRLEN(testhostName1)));
  37231. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  37232. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  37233. ExpectIntEQ(0x01, paramFrom->hostFlags);
  37234. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  37235. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  37236. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  37237. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  37238. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  37239. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37240. /* null pointer */
  37241. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  37242. /* in the case of "from" null, returns success */
  37243. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  37244. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  37245. /* inherit flags test : VPARAM_DEFAULT */
  37246. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37247. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37248. (int)XSTRLEN(testhostName1)));
  37249. ExpectIntEQ(0x01, paramTo->hostFlags);
  37250. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37251. /* inherit flags test : VPARAM OVERWRITE */
  37252. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37253. (int)XSTRLEN(testhostName2)), 1);
  37254. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37255. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  37256. if (paramTo != NULL) {
  37257. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  37258. }
  37259. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37260. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37261. (int)XSTRLEN(testhostName1)));
  37262. ExpectIntEQ(0x01, paramTo->hostFlags);
  37263. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37264. /* inherit flags test : VPARAM_RESET_FLAGS */
  37265. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37266. (int)XSTRLEN(testhostName2)), 1);
  37267. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37268. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  37269. if (paramTo != NULL) {
  37270. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  37271. }
  37272. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37273. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  37274. (int)XSTRLEN(testhostName1)));
  37275. ExpectIntEQ(0x01, paramTo->hostFlags);
  37276. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  37277. /* inherit flags test : VPARAM_LOCKED */
  37278. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  37279. (int)XSTRLEN(testhostName2)), 1);
  37280. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  37281. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  37282. if (paramTo != NULL) {
  37283. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  37284. }
  37285. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  37286. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  37287. (int)XSTRLEN(testhostName2)));
  37288. ExpectIntEQ(0x00, paramTo->hostFlags);
  37289. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  37290. /* test for incorrect parameters */
  37291. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  37292. 0);
  37293. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  37294. /* inherit flags test : VPARAM_ONCE, not testable yet */
  37295. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  37296. 1);
  37297. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  37298. X509_V_FLAG_CRL_CHECK_ALL);
  37299. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  37300. X509_V_FLAG_CRL_CHECK_ALL), 1);
  37301. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  37302. X509_VERIFY_PARAM_free(paramTo);
  37303. X509_VERIFY_PARAM_free(paramFrom);
  37304. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  37305. #endif
  37306. return EXPECT_RESULT();
  37307. }
  37308. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37309. !defined(WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY)
  37310. static int test_wolfSSL_check_domain_verify_count = 0;
  37311. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  37312. WOLFSSL_X509_STORE_CTX* store)
  37313. {
  37314. EXPECT_DECLS;
  37315. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  37316. ExpectIntEQ(preverify, 1);
  37317. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  37318. return EXPECT_SUCCESS();
  37319. }
  37320. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  37321. {
  37322. EXPECT_DECLS;
  37323. X509_VERIFY_PARAM *param = NULL;
  37324. ExpectNotNull(param = SSL_get0_param(ssl));
  37325. /* Domain check should only be done on the leaf cert */
  37326. X509_VERIFY_PARAM_set_hostflags(param,
  37327. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  37328. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  37329. "wolfSSL Server Chain", 0), 1);
  37330. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  37331. test_wolfSSL_check_domain_verify_cb);
  37332. return EXPECT_RESULT();
  37333. }
  37334. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  37335. {
  37336. EXPECT_DECLS;
  37337. /* Use a cert with different domains in chain */
  37338. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  37339. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  37340. return EXPECT_RESULT();
  37341. }
  37342. static int test_wolfSSL_check_domain(void)
  37343. {
  37344. EXPECT_DECLS;
  37345. test_ssl_cbf func_cb_client;
  37346. test_ssl_cbf func_cb_server;
  37347. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  37348. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  37349. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  37350. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  37351. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  37352. &func_cb_server, NULL), TEST_SUCCESS);
  37353. /* Should have been called once for each cert in sent chain */
  37354. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  37355. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  37356. #else
  37357. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  37358. #endif
  37359. return EXPECT_RESULT();
  37360. }
  37361. #else
  37362. static int test_wolfSSL_check_domain(void)
  37363. {
  37364. EXPECT_DECLS;
  37365. return EXPECT_RESULT();
  37366. }
  37367. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  37368. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  37369. {
  37370. EXPECT_DECLS;
  37371. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  37372. X509* x509 = NULL;
  37373. X509_PUBKEY* pubKey;
  37374. ExpectNotNull(x509 = X509_new());
  37375. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  37376. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  37377. X509_free(x509);
  37378. #endif
  37379. return EXPECT_RESULT();
  37380. }
  37381. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  37382. {
  37383. EXPECT_DECLS;
  37384. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  37385. !defined(NO_SHA256) && !defined(NO_RSA)
  37386. X509* x509 = NULL;
  37387. ASN1_OBJECT* obj = NULL;
  37388. const ASN1_OBJECT* pa_oid = NULL;
  37389. X509_PUBKEY* pubKey = NULL;
  37390. X509_PUBKEY* pubKey2 = NULL;
  37391. EVP_PKEY* evpKey = NULL;
  37392. const unsigned char *pk = NULL;
  37393. int ppklen;
  37394. int pptype;
  37395. X509_ALGOR *pa = NULL;
  37396. const void *pval;
  37397. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  37398. SSL_FILETYPE_PEM));
  37399. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  37400. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37401. ExpectNotNull(pk);
  37402. ExpectNotNull(pa);
  37403. ExpectNotNull(pubKey);
  37404. ExpectIntGT(ppklen, 0);
  37405. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  37406. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  37407. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  37408. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  37409. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  37410. ExpectNotNull(pk);
  37411. ExpectNotNull(pa);
  37412. ExpectIntGT(ppklen, 0);
  37413. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37414. ExpectNotNull(pa_oid);
  37415. ExpectNull(pval);
  37416. ExpectIntEQ(pptype, V_ASN1_NULL);
  37417. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  37418. X509_PUBKEY_free(pubKey2);
  37419. X509_free(x509);
  37420. EVP_PKEY_free(evpKey);
  37421. #endif
  37422. return EXPECT_RESULT();
  37423. }
  37424. static int test_wolfSSL_X509_PUBKEY_EC(void)
  37425. {
  37426. EXPECT_DECLS;
  37427. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  37428. X509* x509 = NULL;
  37429. ASN1_OBJECT* obj = NULL;
  37430. ASN1_OBJECT* poid = NULL;
  37431. const ASN1_OBJECT* pa_oid = NULL;
  37432. X509_PUBKEY* pubKey = NULL;
  37433. X509_PUBKEY* pubKey2 = NULL;
  37434. EVP_PKEY* evpKey = NULL;
  37435. const unsigned char *pk = NULL;
  37436. int ppklen;
  37437. int pptype;
  37438. X509_ALGOR *pa = NULL;
  37439. const void *pval;
  37440. char buf[50];
  37441. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  37442. SSL_FILETYPE_PEM));
  37443. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  37444. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  37445. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  37446. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  37447. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  37448. ExpectNotNull(pk);
  37449. ExpectNotNull(pa);
  37450. ExpectIntGT(ppklen, 0);
  37451. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37452. ExpectNotNull(pa_oid);
  37453. ExpectNotNull(pval);
  37454. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  37455. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  37456. poid = (ASN1_OBJECT *)pval;
  37457. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  37458. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  37459. X509_PUBKEY_free(pubKey2);
  37460. X509_free(x509);
  37461. EVP_PKEY_free(evpKey);
  37462. #endif
  37463. return EXPECT_RESULT();
  37464. }
  37465. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  37466. {
  37467. EXPECT_DECLS;
  37468. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  37469. word32 bytes;
  37470. #ifdef USE_CERT_BUFFERS_1024
  37471. byte tmp[ONEK_BUF];
  37472. #elif defined(USE_CERT_BUFFERS_2048)
  37473. byte tmp[TWOK_BUF];
  37474. #else
  37475. byte tmp[TWOK_BUF];
  37476. #endif /* END USE_CERT_BUFFERS_1024 */
  37477. const unsigned char* dsaKeyDer = tmp;
  37478. ASN1_OBJECT* obj = NULL;
  37479. ASN1_STRING* str;
  37480. const ASN1_OBJECT* pa_oid = NULL;
  37481. X509_PUBKEY* pubKey = NULL;
  37482. EVP_PKEY* evpKey = NULL;
  37483. const unsigned char *pk = NULL;
  37484. int ppklen, pptype;
  37485. X509_ALGOR *pa = NULL;
  37486. const void *pval;
  37487. #ifdef USE_CERT_BUFFERS_1024
  37488. XMEMSET(tmp, 0, sizeof(tmp));
  37489. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  37490. bytes = sizeof_dsa_key_der_1024;
  37491. #elif defined(USE_CERT_BUFFERS_2048)
  37492. XMEMSET(tmp, 0, sizeof(tmp));
  37493. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  37494. bytes = sizeof_dsa_key_der_2048;
  37495. #else
  37496. {
  37497. XFILE fp = XBADFILE;
  37498. XMEMSET(tmp, 0, sizeof(tmp));
  37499. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  37500. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  37501. if (fp != XBADFILE)
  37502. XFCLOSE(fp);
  37503. }
  37504. #endif
  37505. /* Initialize pkey with der format dsa key */
  37506. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  37507. ExpectNotNull(pubKey = X509_PUBKEY_new());
  37508. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  37509. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37510. ExpectNotNull(pk);
  37511. ExpectNotNull(pa);
  37512. ExpectIntGT(ppklen, 0);
  37513. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37514. ExpectNotNull(pa_oid);
  37515. ExpectNotNull(pval);
  37516. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  37517. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  37518. str = (ASN1_STRING *)pval;
  37519. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  37520. #ifdef USE_CERT_BUFFERS_1024
  37521. ExpectIntEQ(ASN1_STRING_length(str), 291);
  37522. #else
  37523. ExpectIntEQ(ASN1_STRING_length(str), 549);
  37524. #endif /* END USE_CERT_BUFFERS_1024 */
  37525. X509_PUBKEY_free(pubKey);
  37526. EVP_PKEY_free(evpKey);
  37527. #endif
  37528. return EXPECT_RESULT();
  37529. }
  37530. static int test_wolfSSL_BUF(void)
  37531. {
  37532. EXPECT_DECLS;
  37533. #if defined(OPENSSL_EXTRA)
  37534. BUF_MEM* buf = NULL;
  37535. ExpectNotNull(buf = BUF_MEM_new());
  37536. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  37537. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  37538. BUF_MEM_free(buf);
  37539. #endif
  37540. return EXPECT_RESULT();
  37541. }
  37542. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37543. static int stub_rand_seed(const void *buf, int num)
  37544. {
  37545. (void)buf;
  37546. (void)num;
  37547. return 123;
  37548. }
  37549. static int stub_rand_bytes(unsigned char *buf, int num)
  37550. {
  37551. (void)buf;
  37552. (void)num;
  37553. return 456;
  37554. }
  37555. static byte* was_stub_rand_cleanup_called(void)
  37556. {
  37557. static byte was_called = 0;
  37558. return &was_called;
  37559. }
  37560. static void stub_rand_cleanup(void)
  37561. {
  37562. byte* was_called = was_stub_rand_cleanup_called();
  37563. *was_called = 1;
  37564. return;
  37565. }
  37566. static byte* was_stub_rand_add_called(void)
  37567. {
  37568. static byte was_called = 0;
  37569. return &was_called;
  37570. }
  37571. static int stub_rand_add(const void *buf, int num, double entropy)
  37572. {
  37573. byte* was_called = was_stub_rand_add_called();
  37574. (void)buf;
  37575. (void)num;
  37576. (void)entropy;
  37577. *was_called = 1;
  37578. return 0;
  37579. }
  37580. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  37581. {
  37582. (void)buf;
  37583. (void)num;
  37584. return 9876;
  37585. }
  37586. static int stub_rand_status(void)
  37587. {
  37588. return 5432;
  37589. }
  37590. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37591. static int test_wolfSSL_RAND_set_rand_method(void)
  37592. {
  37593. EXPECT_DECLS;
  37594. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37595. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  37596. unsigned char* buf = NULL;
  37597. int num = 0;
  37598. double entropy = 0;
  37599. int ret;
  37600. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  37601. byte* was_add_called = was_stub_rand_add_called();
  37602. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  37603. DYNAMIC_TYPE_TMP_BUFFER));
  37604. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  37605. ExpectIntEQ(*was_cleanup_called, 0);
  37606. RAND_cleanup();
  37607. ExpectIntEQ(*was_cleanup_called, 0);
  37608. rand_methods.seed = &stub_rand_seed;
  37609. rand_methods.bytes = &stub_rand_bytes;
  37610. rand_methods.cleanup = &stub_rand_cleanup;
  37611. rand_methods.add = &stub_rand_add;
  37612. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  37613. rand_methods.status = &stub_rand_status;
  37614. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  37615. ExpectIntEQ(RAND_seed(buf, num), 123);
  37616. ExpectIntEQ(RAND_bytes(buf, num), 456);
  37617. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  37618. ExpectIntEQ(RAND_status(), 5432);
  37619. ExpectIntEQ(*was_add_called, 0);
  37620. /* The function pointer for RAND_add returns int, but RAND_add itself
  37621. * returns void. */
  37622. RAND_add(buf, num, entropy);
  37623. ExpectIntEQ(*was_add_called, 1);
  37624. was_add_called = 0;
  37625. ExpectIntEQ(*was_cleanup_called, 0);
  37626. RAND_cleanup();
  37627. ExpectIntEQ(*was_cleanup_called, 1);
  37628. *was_cleanup_called = 0;
  37629. ret = RAND_set_rand_method(NULL);
  37630. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  37631. ExpectIntNE(RAND_status(), 5432);
  37632. ExpectIntEQ(*was_cleanup_called, 0);
  37633. RAND_cleanup();
  37634. ExpectIntEQ(*was_cleanup_called, 0);
  37635. RAND_set_rand_method(NULL);
  37636. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37637. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37638. return EXPECT_RESULT();
  37639. }
  37640. static int test_wolfSSL_RAND_bytes(void)
  37641. {
  37642. EXPECT_DECLS;
  37643. #if defined(OPENSSL_EXTRA)
  37644. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  37645. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  37646. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  37647. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  37648. int max_bufsize;
  37649. byte *my_buf = NULL;
  37650. /* sanity check */
  37651. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  37652. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  37653. max_bufsize = size4;
  37654. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  37655. DYNAMIC_TYPE_TMP_BUFFER));
  37656. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  37657. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  37658. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  37659. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  37660. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  37661. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  37662. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  37663. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37664. #endif
  37665. return EXPECT_RESULT();
  37666. }
  37667. static int test_wolfSSL_RAND(void)
  37668. {
  37669. EXPECT_DECLS;
  37670. #if defined(OPENSSL_EXTRA)
  37671. byte seed[16];
  37672. XMEMSET(seed, 0, sizeof(seed));
  37673. /* No global methods set. */
  37674. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  37675. ExpectIntEQ(RAND_poll(), 1);
  37676. RAND_cleanup();
  37677. ExpectIntEQ(RAND_egd(NULL), -1);
  37678. #ifndef NO_FILESYSTEM
  37679. {
  37680. char fname[100];
  37681. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  37682. ExpectIntEQ(RAND_write_file(NULL), 0);
  37683. }
  37684. #endif
  37685. #endif
  37686. return EXPECT_RESULT();
  37687. }
  37688. static int test_wolfSSL_PKCS8_Compat(void)
  37689. {
  37690. EXPECT_DECLS;
  37691. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  37692. !defined(NO_BIO)
  37693. PKCS8_PRIV_KEY_INFO* pt = NULL;
  37694. BIO* bio = NULL;
  37695. XFILE f = XBADFILE;
  37696. int bytes;
  37697. char pkcs8_buffer[512];
  37698. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37699. EVP_PKEY *pkey = NULL;
  37700. #endif
  37701. /* file from wolfssl/certs/ directory */
  37702. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  37703. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  37704. 0);
  37705. if (f != XBADFILE)
  37706. XFCLOSE(f);
  37707. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37708. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  37709. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37710. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  37711. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37712. /* gets PKCS8 pointer to pkey */
  37713. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  37714. EVP_PKEY_free(pkey);
  37715. #endif
  37716. BIO_free(bio);
  37717. PKCS8_PRIV_KEY_INFO_free(pt);
  37718. #endif
  37719. return EXPECT_RESULT();
  37720. }
  37721. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  37722. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  37723. {
  37724. (void)passwd;
  37725. (void)sz;
  37726. (void)rw;
  37727. (void)userdata;
  37728. return -1;
  37729. }
  37730. #endif
  37731. static int test_wolfSSL_PKCS8_d2i(void)
  37732. {
  37733. EXPECT_DECLS;
  37734. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37735. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37736. * requires a 12 byte password in FIPS mode. This test ends up
  37737. * trying to use an 8 byte password. */
  37738. #ifndef NO_FILESYSTEM
  37739. unsigned char pkcs8_buffer[2048];
  37740. const unsigned char* p = NULL;
  37741. int bytes = 0;
  37742. XFILE file = XBADFILE;
  37743. WOLFSSL_EVP_PKEY* pkey = NULL;
  37744. #ifndef NO_BIO
  37745. BIO* bio = NULL;
  37746. #if defined(OPENSSL_ALL) && \
  37747. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37748. defined(HAVE_ECC)) && \
  37749. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37750. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37751. #endif
  37752. #endif
  37753. #ifndef NO_RSA
  37754. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37755. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37756. #ifndef NO_DES3
  37757. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37758. #endif
  37759. #endif /* NO_RSA */
  37760. #ifdef HAVE_ECC
  37761. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37762. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37763. #ifndef NO_DES3
  37764. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37765. #endif
  37766. #endif /* HAVE_ECC */
  37767. #endif /* !NO_FILESYSTEM */
  37768. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37769. #ifndef NO_RSA
  37770. #ifdef USE_CERT_BUFFERS_1024
  37771. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37772. int rsaSz = sizeof_server_key_der_1024;
  37773. #else
  37774. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37775. int rsaSz = sizeof_server_key_der_2048;
  37776. #endif
  37777. #endif
  37778. #ifdef HAVE_ECC
  37779. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37780. int ecSz = sizeof_ecc_key_der_256;
  37781. #endif
  37782. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37783. #ifndef NO_FILESYSTEM
  37784. (void)pkcs8_buffer;
  37785. (void)p;
  37786. (void)bytes;
  37787. (void)file;
  37788. #ifndef NO_BIO
  37789. (void)bio;
  37790. #endif
  37791. #endif
  37792. #ifdef OPENSSL_ALL
  37793. #ifndef NO_RSA
  37794. /* Try to auto-detect normal RSA private key */
  37795. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37796. EVP_PKEY_free(pkey);
  37797. pkey = NULL;
  37798. #endif
  37799. #ifdef HAVE_ECC
  37800. /* Try to auto-detect normal EC private key */
  37801. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37802. EVP_PKEY_free(pkey);
  37803. pkey = NULL;
  37804. #endif
  37805. #endif /* OPENSSL_ALL */
  37806. #ifndef NO_FILESYSTEM
  37807. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37808. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37809. NULL), 0);
  37810. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37811. NULL), 0);
  37812. #endif
  37813. #ifndef NO_RSA
  37814. /* Get DER encoded RSA PKCS#8 data. */
  37815. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37816. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37817. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37818. file)), 0);
  37819. if (file != XBADFILE) {
  37820. XFCLOSE(file);
  37821. file = XBADFILE;
  37822. }
  37823. p = pkcs8_buffer;
  37824. #ifdef OPENSSL_ALL
  37825. /* Try to decode - auto-detect key type. */
  37826. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37827. #else
  37828. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37829. #endif
  37830. /* Get PEM encoded RSA PKCS#8 data. */
  37831. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37832. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37833. file)), 0);
  37834. if (file != XBADFILE) {
  37835. XFCLOSE(file);
  37836. file = XBADFILE;
  37837. }
  37838. #if defined(OPENSSL_ALL) && \
  37839. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37840. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37841. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37842. NULL), 0);
  37843. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37844. NULL), 0);
  37845. /* Write PKCS#8 PEM to BIO. */
  37846. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37847. NULL), bytes);
  37848. /* Write PKCS#8 PEM to stderr. */
  37849. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37850. NULL), bytes);
  37851. /* Compare file and written data */
  37852. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37853. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37854. BIO_free(bio);
  37855. bio = NULL;
  37856. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37857. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37858. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37859. #endif
  37860. #if !defined(NO_DES3) && !defined(NO_SHA)
  37861. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37862. /* Write Encrypted PKCS#8 PEM to BIO. */
  37863. bytes = 1834;
  37864. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37865. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37866. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37867. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37868. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37869. (void*)"yassl123"));
  37870. EVP_PKEY_free(evpPkey);
  37871. evpPkey = NULL;
  37872. BIO_free(bio);
  37873. bio = NULL;
  37874. #endif /* !NO_DES3 && !NO_SHA */
  37875. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37876. EVP_PKEY_free(pkey);
  37877. pkey = NULL;
  37878. /* PKCS#8 encrypted RSA key */
  37879. #ifndef NO_DES3
  37880. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37881. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37882. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37883. file)), 0);
  37884. if (file != XBADFILE) {
  37885. XFCLOSE(file);
  37886. file = XBADFILE;
  37887. }
  37888. #if defined(OPENSSL_ALL) && \
  37889. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37890. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37891. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37892. (void*)"yassl123"));
  37893. EVP_PKEY_free(pkey);
  37894. pkey = NULL;
  37895. BIO_free(bio);
  37896. bio = NULL;
  37897. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37898. #endif /* !NO_DES3 */
  37899. #endif /* NO_RSA */
  37900. #ifdef HAVE_ECC
  37901. /* PKCS#8 encode EC key */
  37902. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37903. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37904. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37905. file)), 0);
  37906. if (file != XBADFILE) {
  37907. XFCLOSE(file);
  37908. file = XBADFILE;
  37909. }
  37910. p = pkcs8_buffer;
  37911. #ifdef OPENSSL_ALL
  37912. /* Try to decode - auto-detect key type. */
  37913. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37914. #else
  37915. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37916. #endif
  37917. /* Get PEM encoded RSA PKCS#8 data. */
  37918. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37919. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37920. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37921. file)), 0);
  37922. if (file != XBADFILE) {
  37923. XFCLOSE(file);
  37924. file = XBADFILE;
  37925. }
  37926. #if defined(OPENSSL_ALL) && \
  37927. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37928. defined(HAVE_AES_CBC)
  37929. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37930. /* Write PKCS#8 PEM to BIO. */
  37931. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37932. NULL), bytes);
  37933. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37934. NULL), bytes);
  37935. /* Compare file and written data */
  37936. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37937. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37938. BIO_free(bio);
  37939. bio = NULL;
  37940. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37941. /* Write Encrypted PKCS#8 PEM to BIO. */
  37942. bytes = 379;
  37943. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37944. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37945. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37946. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37947. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37948. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37949. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37950. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37951. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37952. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37953. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37954. (void*)"yassl123"));
  37955. EVP_PKEY_free(evpPkey);
  37956. evpPkey = NULL;
  37957. BIO_free(bio);
  37958. bio = NULL;
  37959. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37960. EVP_PKEY_free(pkey);
  37961. pkey = NULL;
  37962. /* PKCS#8 encrypted EC key */
  37963. #ifndef NO_DES3
  37964. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37965. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37966. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37967. file)), 0);
  37968. if (file != XBADFILE) {
  37969. XFCLOSE(file);
  37970. file = XBADFILE;
  37971. }
  37972. #if defined(OPENSSL_ALL) && \
  37973. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37974. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37975. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37976. (void*)"yassl123"));
  37977. EVP_PKEY_free(pkey);
  37978. pkey = NULL;
  37979. BIO_free(bio);
  37980. bio = NULL;
  37981. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37982. #endif /* !NO_DES3 */
  37983. #endif /* HAVE_ECC */
  37984. #endif /* !NO_FILESYSTEM */
  37985. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37986. return EXPECT_RESULT();
  37987. }
  37988. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37989. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37990. #define LOGGING_THREADS 5
  37991. #define ERROR_COUNT 10
  37992. /* copied from logging.c since this is not exposed otherwise */
  37993. #ifndef ERROR_QUEUE_MAX
  37994. #ifdef ERROR_QUEUE_PER_THREAD
  37995. #define ERROR_QUEUE_MAX 16
  37996. #else
  37997. /* this breaks from compat of unlimited error queue size */
  37998. #define ERROR_QUEUE_MAX 100
  37999. #endif
  38000. #endif
  38001. static volatile int loggingThreadsReady;
  38002. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  38003. {
  38004. const char* file;
  38005. int line;
  38006. unsigned long err;
  38007. int errorCount = 0;
  38008. int i;
  38009. (void)args;
  38010. while (!loggingThreadsReady);
  38011. for (i = 0; i < ERROR_COUNT; i++)
  38012. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  38013. while ((err = ERR_get_error_line(&file, &line))) {
  38014. AssertIntEQ(err, 990 + errorCount);
  38015. errorCount++;
  38016. }
  38017. AssertIntEQ(errorCount, ERROR_COUNT);
  38018. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  38019. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  38020. errorCount = 0;
  38021. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  38022. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  38023. while ((err = ERR_get_error_line(&file, &line))) {
  38024. AssertIntEQ(err, 990 + errorCount);
  38025. errorCount++;
  38026. }
  38027. /* test that the 3 errors over the max were dropped */
  38028. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  38029. WOLFSSL_RETURN_FROM_THREAD(0);
  38030. }
  38031. #endif
  38032. static int test_error_queue_per_thread(void)
  38033. {
  38034. int res = TEST_SKIPPED;
  38035. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  38036. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  38037. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  38038. int i;
  38039. ERR_clear_error(); /* clear out any error nodes */
  38040. loggingThreadsReady = 0;
  38041. for (i = 0; i < LOGGING_THREADS; i++)
  38042. start_thread(test_logging, NULL, &loggingThreads[i]);
  38043. loggingThreadsReady = 1;
  38044. for (i = 0; i < LOGGING_THREADS; i++)
  38045. join_thread(loggingThreads[i]);
  38046. res = TEST_SUCCESS;
  38047. #endif
  38048. return res;
  38049. }
  38050. static int test_wolfSSL_ERR_put_error(void)
  38051. {
  38052. EXPECT_DECLS;
  38053. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38054. defined(DEBUG_WOLFSSL)
  38055. const char* file;
  38056. int line;
  38057. ERR_clear_error(); /* clear out any error nodes */
  38058. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  38059. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38060. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  38061. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  38062. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  38063. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  38064. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  38065. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  38066. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  38067. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  38068. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  38069. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  38070. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  38071. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  38072. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  38073. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  38074. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  38075. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  38076. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  38077. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  38078. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  38079. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  38080. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  38081. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  38082. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  38083. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  38084. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  38085. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  38086. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  38087. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  38088. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  38089. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  38090. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  38091. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  38092. "this file", 100);
  38093. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  38094. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  38095. ExpectIntEQ(line, 100);
  38096. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  38097. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  38098. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  38099. #endif
  38100. /* try reading past end of error queue */
  38101. file = NULL;
  38102. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38103. ExpectNull(file);
  38104. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  38105. PEMerr(4,4);
  38106. ExpectIntEQ(ERR_get_error(), 4);
  38107. /* Empty and free up all error nodes */
  38108. ERR_clear_error();
  38109. /* Verify all nodes are cleared */
  38110. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  38111. ERR_clear_error();
  38112. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  38113. #endif
  38114. return EXPECT_RESULT();
  38115. }
  38116. /*
  38117. * This is a regression test for a bug where the peek/get error functions were
  38118. * drawing from the end of the queue rather than the front.
  38119. */
  38120. static int test_wolfSSL_ERR_get_error_order(void)
  38121. {
  38122. EXPECT_DECLS;
  38123. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  38124. /* Empty the queue. */
  38125. wolfSSL_ERR_clear_error();
  38126. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  38127. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  38128. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  38129. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  38130. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  38131. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  38132. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  38133. return EXPECT_RESULT();
  38134. }
  38135. #ifndef NO_BIO
  38136. static int test_wolfSSL_ERR_print_errors(void)
  38137. {
  38138. EXPECT_DECLS;
  38139. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38140. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  38141. BIO* bio = NULL;
  38142. char buf[1024];
  38143. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38144. ERR_clear_error(); /* clear out any error nodes */
  38145. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  38146. /* Choosing -600 as an unused errno. */
  38147. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  38148. ERR_print_errors(bio);
  38149. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  38150. ExpectIntEQ(XSTRNCMP(
  38151. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  38152. buf, 55), 0);
  38153. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  38154. ExpectIntEQ(XSTRNCMP(
  38155. "error:600:wolfSSL library:unknown error number:asn.c:100",
  38156. buf, 56), 0);
  38157. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  38158. ExpectIntEQ(buf[0], '\0');
  38159. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  38160. BIO_free(bio);
  38161. #endif
  38162. return EXPECT_RESULT();
  38163. }
  38164. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38165. defined(DEBUG_WOLFSSL)
  38166. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  38167. {
  38168. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  38169. return 0;
  38170. }
  38171. #endif
  38172. static int test_wolfSSL_ERR_print_errors_cb(void)
  38173. {
  38174. EXPECT_DECLS;
  38175. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  38176. defined(DEBUG_WOLFSSL)
  38177. BIO* bio = NULL;
  38178. char buf[1024];
  38179. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38180. ERR_clear_error(); /* clear out any error nodes */
  38181. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  38182. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  38183. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  38184. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  38185. ExpectIntEQ(XSTRNCMP(
  38186. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  38187. buf, 53), 0);
  38188. ExpectIntEQ(XSTRNCMP(
  38189. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  38190. buf + 53, 55), 0);
  38191. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  38192. BIO_free(bio);
  38193. #endif
  38194. return EXPECT_RESULT();
  38195. }
  38196. /*
  38197. * Testing WOLFSSL_ERROR_MSG
  38198. */
  38199. static int test_WOLFSSL_ERROR_MSG(void)
  38200. {
  38201. int res = TEST_SKIPPED;
  38202. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  38203. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  38204. const char* msg = TEST_STRING;
  38205. WOLFSSL_ERROR_MSG(msg);
  38206. res = TEST_SUCCESS;
  38207. #endif
  38208. return res;
  38209. } /* End test_WOLFSSL_ERROR_MSG */
  38210. /*
  38211. * Testing wc_ERR_remove_state
  38212. */
  38213. static int test_wc_ERR_remove_state(void)
  38214. {
  38215. int res = TEST_SKIPPED;
  38216. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  38217. wc_ERR_remove_state();
  38218. res = TEST_SUCCESS;
  38219. #endif
  38220. return res;
  38221. } /* End test_wc_ERR_remove_state */
  38222. /*
  38223. * Testing wc_ERR_print_errors_fp
  38224. */
  38225. static int test_wc_ERR_print_errors_fp(void)
  38226. {
  38227. EXPECT_DECLS;
  38228. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  38229. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  38230. long sz;
  38231. XFILE fp = XBADFILE;
  38232. WOLFSSL_ERROR(BAD_FUNC_ARG);
  38233. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  38234. XBADFILE);
  38235. wc_ERR_print_errors_fp(fp);
  38236. #if defined(DEBUG_WOLFSSL)
  38237. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  38238. #ifdef NO_ERROR_QUEUE
  38239. ExpectIntEQ(sz = XFTELL(fp), 0);
  38240. #else
  38241. ExpectIntNE(sz = XFTELL(fp), 0);
  38242. #endif
  38243. #endif
  38244. if (fp != XBADFILE)
  38245. XFCLOSE(fp);
  38246. (void)sz;
  38247. #endif
  38248. return EXPECT_RESULT();
  38249. } /* End test_wc_ERR_print_errors_fp */
  38250. #ifdef DEBUG_WOLFSSL
  38251. static void Logging_cb(const int logLevel, const char *const logMessage)
  38252. {
  38253. (void)logLevel;
  38254. (void)logMessage;
  38255. }
  38256. #endif
  38257. /*
  38258. * Testing wolfSSL_GetLoggingCb
  38259. */
  38260. static int test_wolfSSL_GetLoggingCb(void)
  38261. {
  38262. EXPECT_DECLS;
  38263. #ifdef DEBUG_WOLFSSL
  38264. /* Testing without wolfSSL_SetLoggingCb() */
  38265. ExpectNull(wolfSSL_GetLoggingCb());
  38266. /* Testing with wolfSSL_SetLoggingCb() */
  38267. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  38268. ExpectNotNull(wolfSSL_GetLoggingCb());
  38269. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  38270. #endif
  38271. ExpectNull(wolfSSL_GetLoggingCb());
  38272. return EXPECT_RESULT();
  38273. } /* End test_wolfSSL_GetLoggingCb */
  38274. #endif /* !NO_BIO */
  38275. static int test_wolfSSL_MD4(void)
  38276. {
  38277. EXPECT_DECLS;
  38278. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  38279. MD4_CTX md4;
  38280. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  38281. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  38282. "789012345678901234567890";
  38283. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  38284. "\xcc\x05\x36";
  38285. int msgSz = (int)XSTRLEN(msg);
  38286. XMEMSET(out, 0, sizeof(out));
  38287. MD4_Init(&md4);
  38288. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  38289. MD4_Final(out, &md4);
  38290. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  38291. #endif
  38292. return EXPECT_RESULT();
  38293. }
  38294. static int test_wolfSSL_MD5(void)
  38295. {
  38296. EXPECT_DECLS;
  38297. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  38298. byte input1[] = "";
  38299. byte input2[] = "message digest";
  38300. byte hash[WC_MD5_DIGEST_SIZE];
  38301. unsigned char output1[] =
  38302. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  38303. unsigned char output2[] =
  38304. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  38305. WOLFSSL_MD5_CTX md5;
  38306. XMEMSET(&md5, 0, sizeof(md5));
  38307. /* Test cases for illegal parameters */
  38308. ExpectIntEQ(MD5_Init(NULL), 0);
  38309. ExpectIntEQ(MD5_Init(&md5), 1);
  38310. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  38311. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  38312. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  38313. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  38314. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  38315. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  38316. /* Init MD5 CTX */
  38317. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  38318. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  38319. 1);
  38320. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  38321. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  38322. /* Init MD5 CTX */
  38323. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  38324. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  38325. (int)XSTRLEN((const char*)input2)), 1);
  38326. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  38327. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  38328. #if !defined(NO_OLD_NAMES) && \
  38329. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  38330. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  38331. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  38332. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  38333. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  38334. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  38335. &hash);
  38336. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  38337. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  38338. &hash);
  38339. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  38340. {
  38341. byte data[] = "Data to be hashed.";
  38342. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  38343. ExpectNotNull(MD5(data, sizeof(data), NULL));
  38344. ExpectNotNull(MD5(data, sizeof(data), hash));
  38345. ExpectNotNull(MD5(NULL, 0, hash));
  38346. ExpectNull(MD5(NULL, sizeof(data), hash));
  38347. }
  38348. #endif
  38349. #endif
  38350. return EXPECT_RESULT();
  38351. }
  38352. static int test_wolfSSL_MD5_Transform(void)
  38353. {
  38354. EXPECT_DECLS;
  38355. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  38356. byte input1[] = "";
  38357. byte input2[] = "abc";
  38358. byte local[WC_MD5_BLOCK_SIZE];
  38359. word32 sLen = 0;
  38360. #ifdef BIG_ENDIAN_ORDER
  38361. unsigned char output1[] =
  38362. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  38363. unsigned char output2[] =
  38364. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  38365. #else
  38366. unsigned char output1[] =
  38367. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  38368. unsigned char output2[] =
  38369. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  38370. #endif
  38371. union {
  38372. wc_Md5 native;
  38373. MD5_CTX compat;
  38374. } md5;
  38375. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  38376. XMEMSET(&local, 0, sizeof(local));
  38377. /* sanity check */
  38378. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  38379. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  38380. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  38381. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  38382. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38383. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  38384. /* Init MD5 CTX */
  38385. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  38386. /* Do Transform*/
  38387. sLen = (word32)XSTRLEN((char*)input1);
  38388. XMEMCPY(local, input1, sLen);
  38389. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  38390. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  38391. /* Init MD5 CTX */
  38392. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  38393. sLen = (word32)XSTRLEN((char*)input2);
  38394. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  38395. XMEMCPY(local, input2, sLen);
  38396. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  38397. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  38398. #endif
  38399. return EXPECT_RESULT();
  38400. }
  38401. static int test_wolfSSL_SHA(void)
  38402. {
  38403. EXPECT_DECLS;
  38404. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  38405. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  38406. (!defined(HAVE_FIPS) || \
  38407. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  38408. {
  38409. const unsigned char in[] = "abc";
  38410. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  38411. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  38412. unsigned char out[WC_SHA_DIGEST_SIZE];
  38413. unsigned char* p = NULL;
  38414. WOLFSSL_SHA_CTX sha;
  38415. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  38416. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  38417. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38418. /* SHA interface test */
  38419. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  38420. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  38421. ExpectNotNull(SHA(in, 0, out));
  38422. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  38423. ExpectNotNull(SHA(NULL, 0, out));
  38424. ExpectNotNull(SHA(NULL, 0, NULL));
  38425. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  38426. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38427. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  38428. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  38429. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  38430. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  38431. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  38432. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38433. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  38434. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  38435. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  38436. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38437. }
  38438. #endif
  38439. #if !defined(NO_SHA256)
  38440. {
  38441. const unsigned char in[] = "abc";
  38442. unsigned char expected[] =
  38443. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  38444. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  38445. "\x15\xAD";
  38446. unsigned char out[WC_SHA256_DIGEST_SIZE];
  38447. unsigned char* p = NULL;
  38448. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  38449. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38450. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  38451. #else
  38452. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  38453. #endif
  38454. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  38455. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38456. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  38457. #else
  38458. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  38459. #endif
  38460. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  38461. }
  38462. #endif
  38463. #if defined(WOLFSSL_SHA384)
  38464. {
  38465. const unsigned char in[] = "abc";
  38466. unsigned char expected[] =
  38467. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  38468. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  38469. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  38470. "\xc8\x25\xa7";
  38471. unsigned char out[WC_SHA384_DIGEST_SIZE];
  38472. unsigned char* p = NULL;
  38473. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  38474. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38475. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  38476. #else
  38477. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  38478. #endif
  38479. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  38480. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38481. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  38482. #else
  38483. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  38484. #endif
  38485. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  38486. }
  38487. #endif
  38488. #if defined(WOLFSSL_SHA512)
  38489. {
  38490. const unsigned char in[] = "abc";
  38491. unsigned char expected[] =
  38492. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  38493. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  38494. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  38495. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  38496. "\xa5\x4c\xa4\x9f";
  38497. unsigned char out[WC_SHA512_DIGEST_SIZE];
  38498. unsigned char* p = NULL;
  38499. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  38500. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38501. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  38502. #else
  38503. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  38504. #endif
  38505. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  38506. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38507. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  38508. #else
  38509. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  38510. #endif
  38511. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  38512. }
  38513. #endif
  38514. #endif
  38515. return EXPECT_RESULT();
  38516. }
  38517. static int test_wolfSSL_SHA_Transform(void)
  38518. {
  38519. EXPECT_DECLS;
  38520. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  38521. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38522. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  38523. byte input1[] = "";
  38524. byte input2[] = "abc";
  38525. byte local[WC_SHA_BLOCK_SIZE];
  38526. word32 sLen = 0;
  38527. #ifdef BIG_ENDIAN_ORDER
  38528. unsigned char output1[] =
  38529. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  38530. "\x09\xf7\x3a\x93";
  38531. unsigned char output2[] =
  38532. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  38533. "\x7c\x6e\x08\xb8";
  38534. #else
  38535. unsigned char output1[] =
  38536. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  38537. "\x93\x3a\xf7\x09";
  38538. unsigned char output2[] =
  38539. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  38540. "\xb8\x08\x6e\x7c";
  38541. #endif
  38542. union {
  38543. wc_Sha native;
  38544. SHA_CTX compat;
  38545. } sha;
  38546. union {
  38547. wc_Sha native;
  38548. SHA_CTX compat;
  38549. } sha1;
  38550. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  38551. XMEMSET(&local, 0, sizeof(local));
  38552. /* sanity check */
  38553. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  38554. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  38555. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  38556. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  38557. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  38558. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  38559. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  38560. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38561. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  38562. /* Init SHA CTX */
  38563. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  38564. /* Do Transform*/
  38565. sLen = (word32)XSTRLEN((char*)input1);
  38566. XMEMCPY(local, input1, sLen);
  38567. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  38568. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  38569. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  38570. /* Init SHA CTX */
  38571. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  38572. sLen = (word32)XSTRLEN((char*)input2);
  38573. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38574. XMEMCPY(local, input2, sLen);
  38575. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  38576. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  38577. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  38578. /* SHA1 */
  38579. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38580. /* Init SHA CTX */
  38581. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  38582. /* Do Transform*/
  38583. sLen = (word32)XSTRLEN((char*)input1);
  38584. XMEMCPY(local, input1, sLen);
  38585. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  38586. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  38587. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  38588. /* Init SHA CTX */
  38589. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  38590. sLen = (word32)XSTRLEN((char*)input2);
  38591. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38592. XMEMCPY(local, input2, sLen);
  38593. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  38594. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  38595. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  38596. #endif
  38597. #endif
  38598. return EXPECT_RESULT();
  38599. }
  38600. static int test_wolfSSL_SHA224(void)
  38601. {
  38602. EXPECT_DECLS;
  38603. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  38604. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38605. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  38606. unsigned char input[] =
  38607. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  38608. unsigned char output[] =
  38609. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  38610. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  38611. size_t inLen;
  38612. byte hash[WC_SHA224_DIGEST_SIZE];
  38613. unsigned char* p;
  38614. inLen = XSTRLEN((char*)input);
  38615. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  38616. ExpectNull(SHA224(NULL, inLen, hash));
  38617. ExpectNotNull(SHA224(input, 0, hash));
  38618. ExpectNotNull(SHA224(input, inLen, NULL));
  38619. ExpectNotNull(SHA224(NULL, 0, hash));
  38620. ExpectNotNull(SHA224(NULL, 0, NULL));
  38621. ExpectNotNull(SHA224(input, inLen, hash));
  38622. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  38623. ExpectNotNull(p = SHA224(input, inLen, NULL));
  38624. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  38625. #endif
  38626. return EXPECT_RESULT();
  38627. }
  38628. static int test_wolfSSL_SHA256(void)
  38629. {
  38630. EXPECT_DECLS;
  38631. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  38632. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38633. unsigned char input[] =
  38634. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  38635. unsigned char output[] =
  38636. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  38637. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  38638. "\x06\xC1";
  38639. size_t inLen;
  38640. byte hash[WC_SHA256_DIGEST_SIZE];
  38641. inLen = XSTRLEN((char*)input);
  38642. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  38643. ExpectNotNull(SHA256(input, inLen, hash));
  38644. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  38645. #endif
  38646. return EXPECT_RESULT();
  38647. }
  38648. static int test_wolfSSL_SHA256_Transform(void)
  38649. {
  38650. EXPECT_DECLS;
  38651. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38652. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38653. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38654. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  38655. !defined(WOLFSSL_KCAPI_HASH)
  38656. byte input1[] = "";
  38657. byte input2[] = "abc";
  38658. byte local[WC_SHA256_BLOCK_SIZE];
  38659. word32 sLen = 0;
  38660. #ifdef BIG_ENDIAN_ORDER
  38661. unsigned char output1[] =
  38662. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  38663. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  38664. unsigned char output2[] =
  38665. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  38666. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  38667. #else
  38668. unsigned char output1[] =
  38669. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  38670. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  38671. unsigned char output2[] =
  38672. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  38673. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  38674. #endif
  38675. union {
  38676. wc_Sha256 native;
  38677. SHA256_CTX compat;
  38678. } sha256;
  38679. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  38680. XMEMSET(&local, 0, sizeof(local));
  38681. /* sanity check */
  38682. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  38683. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  38684. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  38685. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  38686. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38687. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  38688. /* Init SHA256 CTX */
  38689. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  38690. /* Do Transform*/
  38691. sLen = (word32)XSTRLEN((char*)input1);
  38692. XMEMCPY(local, input1, sLen);
  38693. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  38694. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  38695. 0);
  38696. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  38697. /* Init SHA256 CTX */
  38698. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  38699. sLen = (word32)XSTRLEN((char*)input2);
  38700. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  38701. XMEMCPY(local, input2, sLen);
  38702. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  38703. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  38704. 0);
  38705. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  38706. #endif
  38707. #endif
  38708. return EXPECT_RESULT();
  38709. }
  38710. static int test_wolfSSL_SHA512_Transform(void)
  38711. {
  38712. EXPECT_DECLS;
  38713. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  38714. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38715. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38716. !defined(WOLFSSL_KCAPI_HASH)
  38717. byte input1[] = "";
  38718. byte input2[] = "abc";
  38719. byte local[WC_SHA512_BLOCK_SIZE];
  38720. word32 sLen = 0;
  38721. #ifdef BIG_ENDIAN_ORDER
  38722. unsigned char output1[] =
  38723. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  38724. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  38725. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  38726. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  38727. unsigned char output2[] =
  38728. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  38729. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  38730. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  38731. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  38732. #else
  38733. unsigned char output1[] =
  38734. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  38735. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  38736. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  38737. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  38738. unsigned char output2[] =
  38739. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  38740. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  38741. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  38742. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  38743. #endif
  38744. union {
  38745. wc_Sha512 native;
  38746. SHA512_CTX compat;
  38747. } sha512;
  38748. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38749. XMEMSET(&local, 0, sizeof(local));
  38750. /* sanity check */
  38751. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  38752. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  38753. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  38754. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  38755. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38756. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38757. /* Init SHA512 CTX */
  38758. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  38759. /* Do Transform*/
  38760. sLen = (word32)XSTRLEN((char*)input1);
  38761. XMEMCPY(local, input1, sLen);
  38762. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38763. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38764. WC_SHA512_DIGEST_SIZE), 0);
  38765. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38766. /* Init SHA512 CTX */
  38767. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  38768. sLen = (word32)XSTRLEN((char*)input2);
  38769. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38770. XMEMCPY(local, input2, sLen);
  38771. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38772. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38773. WC_SHA512_DIGEST_SIZE), 0);
  38774. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38775. (void)input1;
  38776. #endif
  38777. #endif
  38778. return EXPECT_RESULT();
  38779. }
  38780. static int test_wolfSSL_SHA512_224_Transform(void)
  38781. {
  38782. EXPECT_DECLS;
  38783. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38784. !defined(WOLFSSL_NOSHA512_224)
  38785. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38786. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38787. !defined(WOLFSSL_KCAPI_HASH)
  38788. byte input1[] = "";
  38789. byte input2[] = "abc";
  38790. byte local[WC_SHA512_BLOCK_SIZE];
  38791. word32 sLen = 0;
  38792. unsigned char output1[] =
  38793. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  38794. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  38795. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  38796. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  38797. unsigned char output2[] =
  38798. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  38799. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  38800. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  38801. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  38802. union {
  38803. wc_Sha512 native;
  38804. SHA512_CTX compat;
  38805. } sha512;
  38806. #ifdef BIG_ENDIAN_ORDER
  38807. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38808. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38809. #endif
  38810. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38811. XMEMSET(&local, 0, sizeof(local));
  38812. /* sanity check */
  38813. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38814. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38815. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38816. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38817. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38818. BAD_FUNC_ARG);
  38819. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38820. /* Init SHA512 CTX */
  38821. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38822. /* Do Transform*/
  38823. sLen = (word32)XSTRLEN((char*)input1);
  38824. XMEMCPY(local, input1, sLen);
  38825. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38826. 1);
  38827. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38828. WC_SHA512_DIGEST_SIZE), 0);
  38829. /* frees resources */
  38830. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38831. /* Init SHA512 CTX */
  38832. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38833. sLen = (word32)XSTRLEN((char*)input2);
  38834. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38835. XMEMCPY(local, input2, sLen);
  38836. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38837. 1);
  38838. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38839. WC_SHA512_DIGEST_SIZE), 0);
  38840. /* frees resources */
  38841. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38842. #endif
  38843. #endif
  38844. return EXPECT_RESULT();
  38845. }
  38846. static int test_wolfSSL_SHA512_256_Transform(void)
  38847. {
  38848. EXPECT_DECLS;
  38849. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38850. !defined(WOLFSSL_NOSHA512_256)
  38851. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38852. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38853. !defined(WOLFSSL_KCAPI_HASH)
  38854. byte input1[] = "";
  38855. byte input2[] = "abc";
  38856. byte local[WC_SHA512_BLOCK_SIZE];
  38857. word32 sLen = 0;
  38858. unsigned char output1[] =
  38859. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38860. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38861. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38862. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38863. unsigned char output2[] =
  38864. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38865. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38866. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38867. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38868. union {
  38869. wc_Sha512 native;
  38870. SHA512_CTX compat;
  38871. } sha512;
  38872. #ifdef BIG_ENDIAN_ORDER
  38873. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38874. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38875. #endif
  38876. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38877. XMEMSET(&local, 0, sizeof(local));
  38878. /* sanity check */
  38879. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38880. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38881. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38882. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38883. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38884. BAD_FUNC_ARG);
  38885. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38886. /* Init SHA512 CTX */
  38887. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38888. /* Do Transform*/
  38889. sLen = (word32)XSTRLEN((char*)input1);
  38890. XMEMCPY(local, input1, sLen);
  38891. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38892. 1);
  38893. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38894. WC_SHA512_DIGEST_SIZE), 0);
  38895. /* frees resources */
  38896. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38897. /* Init SHA512 CTX */
  38898. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38899. sLen = (word32)XSTRLEN((char*)input2);
  38900. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38901. XMEMCPY(local, input2, sLen);
  38902. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38903. 1);
  38904. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38905. WC_SHA512_DIGEST_SIZE), 0);
  38906. /* frees resources */
  38907. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38908. #endif
  38909. #endif
  38910. return EXPECT_RESULT();
  38911. }
  38912. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38913. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38914. * buffer of 64 bytes.
  38915. *
  38916. * returns the size of the digest buffer on success and a negative value on
  38917. * failure.
  38918. */
  38919. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38920. int* sz)
  38921. {
  38922. EXPECT_DECLS;
  38923. HMAC_CTX ctx1;
  38924. HMAC_CTX ctx2;
  38925. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38926. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38927. unsigned char long_key[] =
  38928. "0123456789012345678901234567890123456789"
  38929. "0123456789012345678901234567890123456789"
  38930. "0123456789012345678901234567890123456789"
  38931. "0123456789012345678901234567890123456789";
  38932. unsigned char msg[] = "message to hash";
  38933. unsigned int digestSz = 64;
  38934. int keySz = sizeof(key);
  38935. int long_keySz = sizeof(long_key);
  38936. int msgSz = sizeof(msg);
  38937. unsigned char digest2[64];
  38938. unsigned int digestSz2 = 64;
  38939. HMAC_CTX_init(&ctx1);
  38940. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38941. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38942. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38943. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38944. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38945. HMAC_CTX_cleanup(&ctx1);
  38946. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38947. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38948. HMAC_CTX_cleanup(&ctx2);
  38949. ExpectIntEQ(digestSz, digestSz2);
  38950. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38951. /* test HMAC_Init with NULL key */
  38952. /* init after copy */
  38953. HMAC_CTX_init(&ctx1);
  38954. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38955. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38956. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38957. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38958. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38959. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38960. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38961. HMAC_CTX_cleanup(&ctx1);
  38962. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38963. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38964. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38965. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38966. HMAC_CTX_cleanup(&ctx2);
  38967. ExpectIntEQ(digestSz, digestSz2);
  38968. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38969. /* long key */
  38970. HMAC_CTX_init(&ctx1);
  38971. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38972. SSL_SUCCESS);
  38973. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38974. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38975. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38976. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38977. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38978. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38979. HMAC_CTX_cleanup(&ctx1);
  38980. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38981. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38982. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38983. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38984. HMAC_CTX_cleanup(&ctx2);
  38985. ExpectIntEQ(digestSz, digestSz2);
  38986. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38987. /* init before copy */
  38988. HMAC_CTX_init(&ctx1);
  38989. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38990. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38991. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38992. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38993. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38994. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38995. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38996. HMAC_CTX_cleanup(&ctx1);
  38997. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38998. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38999. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  39000. HMAC_CTX_cleanup(&ctx2);
  39001. ExpectIntEQ(digestSz, digestSz2);
  39002. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  39003. *sz = (int)digestSz;
  39004. return EXPECT_RESULT();
  39005. }
  39006. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  39007. static int test_wolfSSL_HMAC_CTX(void)
  39008. {
  39009. EXPECT_DECLS;
  39010. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  39011. unsigned char digest[64];
  39012. int digestSz;
  39013. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  39014. WOLFSSL_HMAC_CTX ctx1;
  39015. WOLFSSL_HMAC_CTX ctx2;
  39016. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  39017. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  39018. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  39019. wolfSSL_HMAC_CTX_free(NULL);
  39020. wolfSSL_HMAC_CTX_free(hmac_ctx);
  39021. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  39022. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  39023. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  39024. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  39025. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  39026. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  39027. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  39028. ((! defined(HAVE_FIPS_VERSION)) || \
  39029. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  39030. /* Copy object that hasn't had a digest set - MD5. */
  39031. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  39032. #else
  39033. /* Copy object that hasn't had a digest set. */
  39034. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  39035. #endif
  39036. HMAC_CTX_cleanup(NULL);
  39037. HMAC_CTX_cleanup(&ctx2);
  39038. ExpectNull(HMAC_CTX_get_md(NULL));
  39039. #ifndef NO_SHA
  39040. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  39041. TEST_SUCCESS);
  39042. ExpectIntEQ(digestSz, 20);
  39043. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  39044. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  39045. #endif /* !NO_SHA */
  39046. #ifdef WOLFSSL_SHA224
  39047. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  39048. TEST_SUCCESS);
  39049. ExpectIntEQ(digestSz, 28);
  39050. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  39051. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  39052. "\x02\x0E", digest, digestSz), 0);
  39053. #endif /* WOLFSSL_SHA224 */
  39054. #ifndef NO_SHA256
  39055. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  39056. TEST_SUCCESS);
  39057. ExpectIntEQ(digestSz, 32);
  39058. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  39059. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  39060. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  39061. #endif /* !NO_SHA256 */
  39062. #ifdef WOLFSSL_SHA384
  39063. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  39064. TEST_SUCCESS);
  39065. ExpectIntEQ(digestSz, 48);
  39066. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  39067. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  39068. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  39069. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  39070. digest, digestSz), 0);
  39071. #endif /* WOLFSSL_SHA384 */
  39072. #ifdef WOLFSSL_SHA512
  39073. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  39074. TEST_SUCCESS);
  39075. ExpectIntEQ(digestSz, 64);
  39076. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  39077. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  39078. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  39079. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  39080. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  39081. digest, digestSz), 0);
  39082. #endif /* WOLFSSL_SHA512 */
  39083. #ifdef WOLFSSL_SHA3
  39084. #ifndef WOLFSSL_NOSHA3_224
  39085. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  39086. TEST_SUCCESS);
  39087. ExpectIntEQ(digestSz, 28);
  39088. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  39089. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  39090. "\x29\x0f", digest, digestSz), 0);
  39091. #endif
  39092. #ifndef WOLFSSL_NOSHA3_256
  39093. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  39094. TEST_SUCCESS);
  39095. ExpectIntEQ(digestSz, 32);
  39096. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  39097. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  39098. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  39099. #endif
  39100. #ifndef WOLFSSL_NOSHA3_384
  39101. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  39102. TEST_SUCCESS);
  39103. ExpectIntEQ(digestSz, 48);
  39104. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  39105. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  39106. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  39107. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  39108. digest, digestSz), 0);
  39109. #endif
  39110. #ifndef WOLFSSL_NOSHA3_512
  39111. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  39112. TEST_SUCCESS);
  39113. ExpectIntEQ(digestSz, 64);
  39114. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  39115. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  39116. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  39117. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  39118. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  39119. digest, digestSz), 0);
  39120. #endif
  39121. #endif
  39122. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  39123. HAVE_FIPS_VERSION <= 2)
  39124. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  39125. TEST_SUCCESS);
  39126. ExpectIntEQ(digestSz, 16);
  39127. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  39128. "\xE4\x98\xDD", digest, digestSz), 0);
  39129. #endif /* !NO_MD5 */
  39130. #endif
  39131. return EXPECT_RESULT();
  39132. }
  39133. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  39134. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  39135. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  39136. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  39137. {
  39138. EXPECT_DECLS;
  39139. static const unsigned char key[] = "simple test key";
  39140. HMAC_CTX* hmac = NULL;
  39141. ENGINE* e = NULL;
  39142. unsigned char hash[WC_MAX_DIGEST_SIZE];
  39143. unsigned int len;
  39144. ExpectNotNull(hmac = HMAC_CTX_new());
  39145. HMAC_CTX_init(hmac);
  39146. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  39147. ((! defined(HAVE_FIPS_VERSION)) || \
  39148. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  39149. /* Get size on object that hasn't had a digest set - MD5. */
  39150. ExpectIntEQ(HMAC_size(hmac), 16);
  39151. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  39152. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  39153. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  39154. #else
  39155. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  39156. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  39157. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  39158. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  39159. #endif
  39160. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  39161. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  39162. /* re-using test key as data to hash */
  39163. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  39164. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  39165. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  39166. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  39167. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  39168. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  39169. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  39170. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  39171. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  39172. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  39173. ExpectIntEQ(len, md_len);
  39174. ExpectIntEQ(HMAC_size(NULL), 0);
  39175. ExpectIntEQ(HMAC_size(hmac), md_len);
  39176. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  39177. HMAC_cleanup(NULL);
  39178. HMAC_cleanup(hmac);
  39179. HMAC_CTX_free(hmac);
  39180. len = 0;
  39181. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  39182. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  39183. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  39184. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  39185. ExpectIntEQ(len, md_len);
  39186. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  39187. /* With data. */
  39188. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  39189. &len));
  39190. /* With NULL data. */
  39191. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  39192. &len));
  39193. /* With zero length data. */
  39194. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  39195. return EXPECT_RESULT();
  39196. }
  39197. #endif
  39198. static int test_wolfSSL_HMAC(void)
  39199. {
  39200. EXPECT_DECLS;
  39201. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  39202. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  39203. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  39204. #ifndef NO_SHA256
  39205. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  39206. TEST_SUCCESS);
  39207. #endif
  39208. #ifdef WOLFSSL_SHA224
  39209. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  39210. TEST_SUCCESS);
  39211. #endif
  39212. #ifdef WOLFSSL_SHA384
  39213. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  39214. TEST_SUCCESS);
  39215. #endif
  39216. #ifdef WOLFSSL_SHA512
  39217. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  39218. TEST_SUCCESS);
  39219. #endif
  39220. #ifdef WOLFSSL_SHA3
  39221. #ifndef WOLFSSL_NOSHA3_224
  39222. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  39223. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  39224. #endif
  39225. #ifndef WOLFSSL_NOSHA3_256
  39226. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  39227. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  39228. #endif
  39229. #ifndef WOLFSSL_NOSHA3_384
  39230. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  39231. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  39232. #endif
  39233. #ifndef WOLFSSL_NOSHA3_512
  39234. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  39235. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  39236. #endif
  39237. #endif
  39238. #ifndef NO_SHA
  39239. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  39240. TEST_SUCCESS);
  39241. #endif
  39242. #endif
  39243. return EXPECT_RESULT();
  39244. }
  39245. static int test_wolfSSL_CMAC(void)
  39246. {
  39247. EXPECT_DECLS;
  39248. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  39249. defined(WOLFSSL_AES_DIRECT)
  39250. int i;
  39251. byte key[AES_256_KEY_SIZE];
  39252. CMAC_CTX* cmacCtx = NULL;
  39253. byte out[AES_BLOCK_SIZE];
  39254. size_t outLen = AES_BLOCK_SIZE;
  39255. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  39256. key[i] = i;
  39257. }
  39258. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39259. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  39260. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  39261. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  39262. NULL), 1);
  39263. /* re-using test key as data to hash */
  39264. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  39265. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  39266. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  39267. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  39268. /* No Update works. */
  39269. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  39270. NULL), 1);
  39271. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39272. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  39273. NULL), 1);
  39274. /* Test parameters with CMAC_Update. */
  39275. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  39276. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  39277. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  39278. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  39279. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  39280. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  39281. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  39282. /* Test parameters with CMAC_Final. */
  39283. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  39284. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  39285. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  39286. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  39287. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  39288. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  39289. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39290. CMAC_CTX_free(cmacCtx);
  39291. /* Test parameters with CMAC Init. */
  39292. cmacCtx = NULL;
  39293. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39294. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  39295. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  39296. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  39297. NULL), 0);
  39298. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  39299. NULL), 0);
  39300. /* give a key too small for the cipher, verify we get failure */
  39301. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  39302. NULL), 0);
  39303. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  39304. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  39305. /* Only AES-CBC supported. */
  39306. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  39307. NULL), 0);
  39308. #endif
  39309. CMAC_CTX_free(cmacCtx);
  39310. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  39311. cmacCtx = NULL;
  39312. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39313. /* No Init. */
  39314. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  39315. CMAC_CTX_free(cmacCtx);
  39316. /* Test AES-256-CBC */
  39317. cmacCtx = NULL;
  39318. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39319. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  39320. NULL), 1);
  39321. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  39322. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39323. CMAC_CTX_free(cmacCtx);
  39324. /* Test AES-192-CBC */
  39325. cmacCtx = NULL;
  39326. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39327. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  39328. NULL), 1);
  39329. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  39330. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  39331. CMAC_CTX_free(cmacCtx);
  39332. cmacCtx = NULL;
  39333. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  39334. CMAC_CTX_free(cmacCtx);
  39335. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  39336. return EXPECT_RESULT();
  39337. }
  39338. static int test_wolfSSL_DES(void)
  39339. {
  39340. EXPECT_DECLS;
  39341. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39342. const_DES_cblock myDes;
  39343. DES_cblock iv;
  39344. DES_key_schedule key;
  39345. word32 i = 0;
  39346. DES_LONG dl = 0;
  39347. unsigned char msg[] = "hello wolfssl";
  39348. unsigned char weakKey[][8] = {
  39349. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  39350. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  39351. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  39352. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  39353. };
  39354. unsigned char semiWeakKey[][8] = {
  39355. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  39356. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  39357. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  39358. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  39359. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  39360. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  39361. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  39362. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  39363. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  39364. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  39365. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  39366. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  39367. };
  39368. DES_check_key(1);
  39369. DES_set_key(&myDes, &key);
  39370. /* check, check of odd parity */
  39371. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  39372. myDes[0] = 6; /* set even parity */
  39373. XMEMSET(key, 5, sizeof(DES_key_schedule));
  39374. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  39375. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  39376. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  39377. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  39378. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  39379. /* set odd parity for success case */
  39380. DES_set_odd_parity(&myDes);
  39381. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  39382. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  39383. myDes[3]);
  39384. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  39385. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  39386. ExpectIntEQ(key[i], myDes[i]);
  39387. }
  39388. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  39389. /* check weak key */
  39390. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  39391. XMEMSET(key, 5, sizeof(DES_key_schedule));
  39392. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  39393. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  39394. DES_set_key_unchecked(NULL, NULL);
  39395. DES_set_key_unchecked(&myDes, NULL);
  39396. DES_set_key_unchecked(NULL, &key);
  39397. /* compare arrays, should be the same */
  39398. /* now do unchecked copy of a weak key over */
  39399. DES_set_key_unchecked(&myDes, &key);
  39400. /* compare arrays, should be the same */
  39401. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  39402. ExpectIntEQ(key[i], myDes[i]);
  39403. }
  39404. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  39405. myDes[7] = 2;
  39406. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  39407. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  39408. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  39409. /* Test all weak keys. */
  39410. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  39411. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  39412. }
  39413. /* Test all semi-weak keys. */
  39414. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  39415. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  39416. }
  39417. /* check DES_key_sched API */
  39418. XMEMSET(key, 1, sizeof(DES_key_schedule));
  39419. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  39420. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  39421. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  39422. /* compare arrays, should be the same */
  39423. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  39424. ExpectIntEQ(key[i], myDes[i]);
  39425. }
  39426. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  39427. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  39428. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  39429. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  39430. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  39431. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  39432. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  39433. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  39434. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  39435. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  39436. * DES_cbc_encrypt on the input */
  39437. XMEMSET(iv, 0, sizeof(DES_cblock));
  39438. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  39439. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  39440. ExpectIntEQ(dl, 480052723);
  39441. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  39442. return EXPECT_RESULT();
  39443. }
  39444. static int test_wolfSSL_DES_ncbc(void)
  39445. {
  39446. EXPECT_DECLS;
  39447. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39448. const_DES_cblock myDes;
  39449. DES_cblock iv = {1};
  39450. DES_key_schedule key = {0};
  39451. unsigned char msg[] = "hello wolfssl";
  39452. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  39453. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  39454. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  39455. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  39456. /* partial block test */
  39457. DES_set_key(&key, &myDes);
  39458. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  39459. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  39460. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  39461. DES_set_key(&key, &myDes);
  39462. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  39463. *((byte*)&iv) = 1;
  39464. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  39465. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  39466. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  39467. /* full block test */
  39468. DES_set_key(&key, &myDes);
  39469. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  39470. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  39471. *((byte*)&iv) = 1;
  39472. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  39473. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  39474. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  39475. DES_set_key(&key, &myDes);
  39476. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  39477. *((byte*)&iv) = 1;
  39478. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  39479. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  39480. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  39481. #endif
  39482. return EXPECT_RESULT();
  39483. }
  39484. static int test_wolfSSL_DES_ecb_encrypt(void)
  39485. {
  39486. EXPECT_DECLS;
  39487. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  39488. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  39489. WOLFSSL_DES_key_schedule key;
  39490. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  39491. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  39492. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  39493. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  39494. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  39495. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  39496. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  39497. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  39498. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  39499. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  39500. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  39501. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  39502. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  39503. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  39504. /* Encrypt messages */
  39505. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  39506. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  39507. {
  39508. /* Decrypt messages */
  39509. int ret1 = 0;
  39510. int ret2 = 0;
  39511. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  39512. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  39513. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  39514. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  39515. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  39516. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  39517. }
  39518. #endif
  39519. return EXPECT_RESULT();
  39520. }
  39521. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  39522. {
  39523. EXPECT_DECLS;
  39524. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39525. unsigned char input1[8], input2[8];
  39526. unsigned char output1[8], output2[8];
  39527. unsigned char back1[8], back2[8];
  39528. WOLFSSL_DES_cblock iv1, iv2;
  39529. WOLFSSL_DES_key_schedule key1, key2, key3;
  39530. int i;
  39531. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  39532. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  39533. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  39534. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  39535. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  39536. XMEMSET(output1, 0, sizeof(output1));
  39537. XMEMSET(output2, 0, sizeof(output2));
  39538. XMEMSET(back1, 0, sizeof(back1));
  39539. XMEMSET(back2, 0, sizeof(back2));
  39540. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39541. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39542. /* Encrypt messages */
  39543. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  39544. DES_ENCRYPT);
  39545. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  39546. DES_ENCRYPT);
  39547. {
  39548. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39549. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39550. /* Decrypt messages */
  39551. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  39552. &iv1, DES_DECRYPT);
  39553. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  39554. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  39555. &iv2, DES_DECRYPT);
  39556. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  39557. }
  39558. for (i = 0; i < 8; i++) {
  39559. XMEMSET(output1, 0, sizeof(output1));
  39560. XMEMSET(output2, 0, sizeof(output2));
  39561. XMEMSET(back1, 0, sizeof(back1));
  39562. XMEMSET(back2, 0, sizeof(back2));
  39563. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39564. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39565. /* Encrypt partial messages */
  39566. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  39567. &iv1, DES_ENCRYPT);
  39568. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  39569. &iv2, DES_ENCRYPT);
  39570. {
  39571. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39572. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39573. /* Decrypt messages */
  39574. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  39575. &key3, &iv1, DES_DECRYPT);
  39576. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  39577. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  39578. &key3, &iv2, DES_DECRYPT);
  39579. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  39580. }
  39581. }
  39582. #endif
  39583. return EXPECT_RESULT();
  39584. }
  39585. static int test_wolfSSL_AES_encrypt(void)
  39586. {
  39587. EXPECT_DECLS;
  39588. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  39589. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39590. AES_KEY enc;
  39591. AES_KEY dec;
  39592. const byte msg[] = {
  39593. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39594. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39595. };
  39596. const byte exp[] = {
  39597. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  39598. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  39599. };
  39600. const byte key[] = {
  39601. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39602. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39603. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39604. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39605. };
  39606. byte eout[sizeof(msg)];
  39607. byte dout[sizeof(msg)];
  39608. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  39609. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  39610. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  39611. wolfSSL_AES_encrypt(msg, NULL, NULL);
  39612. wolfSSL_AES_encrypt(NULL, eout, NULL);
  39613. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  39614. wolfSSL_AES_encrypt(msg, eout, NULL);
  39615. wolfSSL_AES_encrypt(msg, NULL, &enc);
  39616. wolfSSL_AES_encrypt(NULL, eout, &enc);
  39617. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  39618. wolfSSL_AES_decrypt(eout, NULL, NULL);
  39619. wolfSSL_AES_decrypt(NULL, dout, NULL);
  39620. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  39621. wolfSSL_AES_decrypt(eout, dout, NULL);
  39622. wolfSSL_AES_decrypt(eout, NULL, &dec);
  39623. wolfSSL_AES_decrypt(NULL, dout, &dec);
  39624. wolfSSL_AES_encrypt(msg, eout, &enc);
  39625. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  39626. wolfSSL_AES_decrypt(eout, dout, &dec);
  39627. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  39628. #endif
  39629. return EXPECT_RESULT();
  39630. }
  39631. static int test_wolfSSL_AES_ecb_encrypt(void)
  39632. {
  39633. EXPECT_DECLS;
  39634. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  39635. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39636. AES_KEY aes;
  39637. const byte msg[] =
  39638. {
  39639. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39640. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  39641. };
  39642. const byte verify[] =
  39643. {
  39644. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  39645. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  39646. };
  39647. const byte key[] =
  39648. {
  39649. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39650. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39651. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39652. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  39653. };
  39654. byte out[AES_BLOCK_SIZE];
  39655. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  39656. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39657. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  39658. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  39659. #ifdef HAVE_AES_DECRYPT
  39660. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  39661. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39662. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  39663. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  39664. #endif
  39665. /* test bad arguments */
  39666. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  39667. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  39668. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  39669. #endif
  39670. return EXPECT_RESULT();
  39671. }
  39672. static int test_wolfSSL_AES_cbc_encrypt(void)
  39673. {
  39674. EXPECT_DECLS;
  39675. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39676. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39677. AES_KEY aes;
  39678. AES_KEY* aesN = NULL;
  39679. size_t len = 0;
  39680. size_t lenB = 0;
  39681. int keySz0 = 0;
  39682. int keySzN = -1;
  39683. byte out[AES_BLOCK_SIZE] = {0};
  39684. byte* outN = NULL;
  39685. /* Test vectors retrieved from:
  39686. * <begin URL>
  39687. * https://csrc.nist.gov/
  39688. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  39689. * documents/aes/KAT_AES.zip
  39690. * </end URL>
  39691. */
  39692. const byte* pt128N = NULL;
  39693. byte* key128N = NULL;
  39694. byte* iv128N = NULL;
  39695. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  39696. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  39698. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  39699. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  39700. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39701. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  39702. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  39703. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  39704. len = sizeof(pt128);
  39705. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  39706. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  39707. ExpectIntNE(XMEMCMP(b, g, h), i)
  39708. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  39709. /* Stressing wolfSSL_AES_cbc_encrypt() */
  39710. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39711. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  39712. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  39713. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39714. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  39715. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39716. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39717. /* Stressing wolfSSL_AES_set_encrypt_key */
  39718. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39719. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  39720. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  39721. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  39722. /* Stressing wolfSSL_AES_set_decrypt_key */
  39723. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39724. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  39725. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  39726. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  39727. #ifdef WOLFSSL_AES_128
  39728. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  39729. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39730. RESET_IV(iv128tmp, iv128);
  39731. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39732. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  39733. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39734. wc_AesFree((Aes*)&aes);
  39735. #ifdef HAVE_AES_DECRYPT
  39736. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  39737. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39738. RESET_IV(iv128tmp, iv128);
  39739. len = sizeof(ct128);
  39740. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39741. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  39742. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  39743. wc_AesFree((Aes*)&aes);
  39744. #endif
  39745. #endif /* WOLFSSL_AES_128 */
  39746. #ifdef WOLFSSL_AES_192
  39747. {
  39748. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  39749. * Appendix F.2.3 */
  39750. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  39751. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39752. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39753. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  39754. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  39755. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39756. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39757. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  39758. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  39759. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  39760. len = sizeof(pt192);
  39761. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  39762. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39763. RESET_IV(iv192tmp, iv192);
  39764. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39765. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  39766. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  39767. wc_AesFree((Aes*)&aes);
  39768. #ifdef HAVE_AES_DECRYPT
  39769. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  39770. len = sizeof(ct192);
  39771. RESET_IV(iv192tmp, iv192);
  39772. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39773. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39774. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  39775. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  39776. wc_AesFree((Aes*)&aes);
  39777. #endif
  39778. }
  39779. #endif /* WOLFSSL_AES_192 */
  39780. #ifdef WOLFSSL_AES_256
  39781. {
  39782. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  39783. * Appendix F.2.5 */
  39784. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  39785. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39786. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39787. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  39788. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  39789. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39790. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39791. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39792. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39793. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39794. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  39795. len = sizeof(pt256);
  39796. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  39797. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39798. RESET_IV(iv256tmp, iv256);
  39799. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39800. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  39801. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  39802. wc_AesFree((Aes*)&aes);
  39803. #ifdef HAVE_AES_DECRYPT
  39804. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  39805. len = sizeof(ct256);
  39806. RESET_IV(iv256tmp, iv256);
  39807. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39808. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39809. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39810. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39811. wc_AesFree((Aes*)&aes);
  39812. #endif
  39813. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39814. !defined(HAVE_SELFTEST)
  39815. {
  39816. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39817. byte wrapPlain[sizeof(key256)] = { 0 };
  39818. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39819. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39820. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39821. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39822. 15), WOLFSSL_FAILURE);
  39823. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39824. sizeof(key256)), sizeof(wrapCipher));
  39825. wc_AesFree((Aes*)&aes);
  39826. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39827. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39828. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39829. 23), WOLFSSL_FAILURE);
  39830. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39831. sizeof(wrapCipher)), sizeof(wrapPlain));
  39832. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39833. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39834. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39835. wc_AesFree((Aes*)&aes);
  39836. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39837. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39838. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39839. sizeof(key256)), sizeof(wrapCipher));
  39840. wc_AesFree((Aes*)&aes);
  39841. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39842. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39843. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39844. sizeof(wrapCipher)), sizeof(wrapPlain));
  39845. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39846. wc_AesFree((Aes*)&aes);
  39847. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39848. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39849. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39850. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39851. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39852. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39853. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39854. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39855. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39856. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39857. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39858. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39859. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39860. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39861. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39862. 0);
  39863. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39864. 0);
  39865. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39866. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39867. }
  39868. #endif /* HAVE_AES_KEYWRAP */
  39869. }
  39870. #endif /* WOLFSSL_AES_256 */
  39871. #endif
  39872. return EXPECT_RESULT();
  39873. }
  39874. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39875. {
  39876. EXPECT_DECLS;
  39877. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39878. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39879. AES_KEY aesEnc;
  39880. AES_KEY aesDec;
  39881. const byte msg[] = {
  39882. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39883. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39884. };
  39885. const byte exp[] = {
  39886. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39887. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39888. };
  39889. const byte key[] = {
  39890. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39891. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39892. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39893. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39894. };
  39895. const byte ivData[] = {
  39896. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39897. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39898. };
  39899. byte out[AES_BLOCK_SIZE];
  39900. byte iv[AES_BLOCK_SIZE];
  39901. word32 i;
  39902. int num;
  39903. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39904. XMEMCPY(iv, ivData, sizeof(iv));
  39905. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39906. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39907. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39908. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39909. #ifdef HAVE_AES_DECRYPT
  39910. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39911. XMEMCPY(iv, ivData, sizeof(iv));
  39912. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39913. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39914. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39915. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39916. #endif
  39917. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39918. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39919. XMEMCPY(iv, ivData, sizeof(iv));
  39920. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39921. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39922. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39923. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39924. if (i == 0) {
  39925. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39926. }
  39927. else {
  39928. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39929. }
  39930. #ifdef HAVE_AES_DECRYPT
  39931. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39932. XMEMCPY(iv, ivData, sizeof(iv));
  39933. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39934. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39935. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39936. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39937. if (i == 0) {
  39938. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39939. }
  39940. else {
  39941. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39942. }
  39943. #endif
  39944. }
  39945. if (EXPECT_SUCCESS()) {
  39946. /* test bad arguments */
  39947. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39948. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39949. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39950. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39951. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39952. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39953. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39954. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39955. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39956. }
  39957. #endif
  39958. return EXPECT_RESULT();
  39959. }
  39960. static int test_wolfSSL_CRYPTO_cts128(void)
  39961. {
  39962. EXPECT_DECLS;
  39963. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39964. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39965. byte tmp[64]; /* Largest vector size */
  39966. /* Test vectors taken form RFC3962 Appendix B */
  39967. const testVector vects[] = {
  39968. {
  39969. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39970. "\x20",
  39971. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39972. "\x97",
  39973. 17, 17
  39974. },
  39975. {
  39976. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39977. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39978. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39979. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39980. 31, 31
  39981. },
  39982. {
  39983. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39984. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39985. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39986. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39987. 32, 32
  39988. },
  39989. {
  39990. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39991. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39992. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39993. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39994. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39995. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39996. 47, 47
  39997. },
  39998. {
  39999. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40000. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40001. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  40002. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40003. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  40004. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  40005. 48, 48
  40006. },
  40007. {
  40008. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40009. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40010. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  40011. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  40012. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40013. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  40014. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  40015. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  40016. 64, 64
  40017. }
  40018. };
  40019. byte keyBytes[AES_128_KEY_SIZE] = {
  40020. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  40021. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  40022. };
  40023. size_t i;
  40024. AES_KEY encKey;
  40025. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  40026. XMEMSET(tmp, 0, sizeof(tmp));
  40027. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  40028. AES_KEY decKey;
  40029. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  40030. &encKey), 0);
  40031. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  40032. &decKey), 0);
  40033. XMEMSET(iv, 0, sizeof(iv));
  40034. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  40035. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  40036. vects[i].outLen);
  40037. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  40038. XMEMSET(iv, 0, sizeof(iv));
  40039. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  40040. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  40041. vects[i].inLen);
  40042. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  40043. }
  40044. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  40045. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  40046. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  40047. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  40048. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  40049. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  40050. (cbc128_f)AES_cbc_encrypt), 0);
  40051. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  40052. (cbc128_f)AES_cbc_encrypt), 0);
  40053. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  40054. (cbc128_f)AES_cbc_encrypt), 0);
  40055. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  40056. (cbc128_f)AES_cbc_encrypt), 0);
  40057. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  40058. (cbc128_f)AES_cbc_encrypt), 0);
  40059. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  40060. /* Length too small. */
  40061. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  40062. (cbc128_f)AES_cbc_encrypt), 0);
  40063. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  40064. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  40065. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  40066. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  40067. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  40068. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  40069. (cbc128_f)AES_cbc_encrypt), 0);
  40070. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  40071. (cbc128_f)AES_cbc_encrypt), 0);
  40072. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  40073. (cbc128_f)AES_cbc_encrypt), 0);
  40074. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  40075. (cbc128_f)AES_cbc_encrypt), 0);
  40076. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  40077. (cbc128_f)AES_cbc_encrypt), 0);
  40078. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  40079. /* Length too small. */
  40080. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  40081. (cbc128_f)AES_cbc_encrypt), 0);
  40082. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  40083. return EXPECT_RESULT();
  40084. }
  40085. static int test_wolfSSL_RC4(void)
  40086. {
  40087. EXPECT_DECLS;
  40088. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  40089. WOLFSSL_RC4_KEY rc4Key;
  40090. unsigned char key[] = {
  40091. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40092. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40093. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40094. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  40095. };
  40096. unsigned char data[] = {
  40097. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40098. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40099. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  40101. };
  40102. unsigned char enc[sizeof(data)];
  40103. unsigned char dec[sizeof(data)];
  40104. word32 i;
  40105. word32 j;
  40106. wolfSSL_RC4_set_key(NULL, -1, NULL);
  40107. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  40108. wolfSSL_RC4_set_key(NULL, 0, NULL);
  40109. wolfSSL_RC4_set_key(NULL, -1, key);
  40110. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  40111. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  40112. wolfSSL_RC4_set_key(NULL, 0, key);
  40113. wolfSSL_RC4(NULL, 0, NULL, NULL);
  40114. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  40115. wolfSSL_RC4(NULL, 0, data, NULL);
  40116. wolfSSL_RC4(NULL, 0, NULL, enc);
  40117. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  40118. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  40119. wolfSSL_RC4(NULL, 0, data, enc);
  40120. ExpectIntEQ(1, 1);
  40121. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  40122. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  40123. XMEMSET(enc, 0, sizeof(enc));
  40124. XMEMSET(dec, 0, sizeof(dec));
  40125. /* Encrypt */
  40126. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  40127. wolfSSL_RC4(&rc4Key, j, data, enc);
  40128. /* Decrypt */
  40129. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  40130. wolfSSL_RC4(&rc4Key, j, enc, dec);
  40131. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  40132. }
  40133. }
  40134. #endif
  40135. return EXPECT_RESULT();
  40136. }
  40137. static int test_wolfSSL_OBJ(void)
  40138. {
  40139. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  40140. * mode
  40141. */
  40142. EXPECT_DECLS;
  40143. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  40144. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  40145. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  40146. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  40147. ASN1_OBJECT *obj = NULL;
  40148. ASN1_OBJECT *obj2 = NULL;
  40149. char buf[50];
  40150. XFILE fp = XBADFILE;
  40151. X509 *x509 = NULL;
  40152. X509_NAME *x509Name = NULL;
  40153. X509_NAME_ENTRY *x509NameEntry = NULL;
  40154. ASN1_OBJECT *asn1Name = NULL;
  40155. int numNames = 0;
  40156. BIO *bio = NULL;
  40157. int nid;
  40158. int i, j;
  40159. const char *f[] = {
  40160. #ifndef NO_RSA
  40161. "./certs/ca-cert.der",
  40162. #endif
  40163. #ifdef HAVE_ECC
  40164. "./certs/ca-ecc-cert.der",
  40165. "./certs/ca-ecc384-cert.der",
  40166. #endif
  40167. NULL};
  40168. ASN1_OBJECT *field_name_obj = NULL;
  40169. int lastpos = -1;
  40170. int tmp = -1;
  40171. ASN1_STRING *asn1 = NULL;
  40172. unsigned char *buf_dyn = NULL;
  40173. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  40174. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  40175. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  40176. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  40177. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  40178. ASN1_OBJECT_free(obj);
  40179. obj = NULL;
  40180. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  40181. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  40182. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  40183. #ifdef WOLFSSL_CERT_EXT
  40184. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  40185. #endif
  40186. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  40187. ExpectNotNull(obj2 = OBJ_dup(obj));
  40188. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  40189. ASN1_OBJECT_free(obj);
  40190. obj = NULL;
  40191. ASN1_OBJECT_free(obj2);
  40192. obj2 = NULL;
  40193. for (i = 0; f[i] != NULL; i++)
  40194. {
  40195. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  40196. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  40197. if (fp != XBADFILE) {
  40198. XFCLOSE(fp);
  40199. fp = XBADFILE;
  40200. }
  40201. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  40202. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  40203. /* Get the Common Name by using OBJ_txt2obj */
  40204. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  40205. do
  40206. {
  40207. lastpos = tmp;
  40208. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  40209. } while (tmp > -1);
  40210. ExpectIntNE(lastpos, -1);
  40211. ASN1_OBJECT_free(field_name_obj);
  40212. field_name_obj = NULL;
  40213. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  40214. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  40215. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  40216. /*
  40217. * All Common Names should be www.wolfssl.com
  40218. * This makes testing easier as we can test for the expected value.
  40219. */
  40220. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  40221. OPENSSL_free(buf_dyn);
  40222. buf_dyn = NULL;
  40223. bio = BIO_new(BIO_s_mem());
  40224. ExpectTrue(bio != NULL);
  40225. for (j = 0; j < numNames; j++)
  40226. {
  40227. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  40228. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  40229. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  40230. }
  40231. BIO_free(bio);
  40232. bio = NULL;
  40233. X509_free(x509);
  40234. x509 = NULL;
  40235. }
  40236. #ifdef HAVE_PKCS12
  40237. {
  40238. PKCS12 *p12 = NULL;
  40239. int boolRet;
  40240. EVP_PKEY *pkey = NULL;
  40241. const char *p12_f[] = {
  40242. #if !defined(NO_DES3) && !defined(NO_RSA)
  40243. "./certs/test-servercert.p12",
  40244. #endif
  40245. NULL};
  40246. for (i = 0; p12_f[i] != NULL; i++)
  40247. {
  40248. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  40249. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  40250. if (fp != XBADFILE) {
  40251. XFCLOSE(fp);
  40252. fp = XBADFILE;
  40253. }
  40254. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  40255. &pkey, &x509, NULL)) > 0);
  40256. wc_PKCS12_free(p12);
  40257. p12 = NULL;
  40258. EVP_PKEY_free(pkey);
  40259. x509Name = X509_get_issuer_name(x509);
  40260. ExpectNotNull(x509Name);
  40261. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  40262. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  40263. for (j = 0; j < numNames; j++)
  40264. {
  40265. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  40266. ExpectNotNull(asn1Name =
  40267. X509_NAME_ENTRY_get_object(x509NameEntry));
  40268. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  40269. }
  40270. BIO_free(bio);
  40271. bio = NULL;
  40272. X509_free(x509);
  40273. x509 = NULL;
  40274. }
  40275. }
  40276. #endif /* HAVE_PKCS12 */
  40277. #endif
  40278. return EXPECT_RESULT();
  40279. }
  40280. static int test_wolfSSL_OBJ_cmp(void)
  40281. {
  40282. EXPECT_DECLS;
  40283. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  40284. ASN1_OBJECT *obj = NULL;
  40285. ASN1_OBJECT *obj2 = NULL;
  40286. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  40287. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  40288. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  40289. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  40290. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  40291. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  40292. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  40293. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  40294. ASN1_OBJECT_free(obj);
  40295. ASN1_OBJECT_free(obj2);
  40296. #endif
  40297. return EXPECT_RESULT();
  40298. }
  40299. static int test_wolfSSL_OBJ_txt2nid(void)
  40300. {
  40301. EXPECT_DECLS;
  40302. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  40303. defined(WOLFSSL_APACHE_HTTPD)
  40304. int i;
  40305. static const struct {
  40306. const char* sn;
  40307. const char* ln;
  40308. const char* oid;
  40309. int nid;
  40310. } testVals[] = {
  40311. #ifdef WOLFSSL_APACHE_HTTPD
  40312. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  40313. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  40314. NID_id_on_dnsSRV },
  40315. { "msUPN", "Microsoft User Principal Name",
  40316. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  40317. #endif
  40318. { NULL, NULL, NULL, NID_undef }
  40319. };
  40320. /* Invalid cases */
  40321. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  40322. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  40323. /* Valid cases */
  40324. for (i = 0; testVals[i].sn != NULL; i++) {
  40325. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  40326. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  40327. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  40328. }
  40329. #endif
  40330. return EXPECT_RESULT();
  40331. }
  40332. static int test_wolfSSL_OBJ_txt2obj(void)
  40333. {
  40334. EXPECT_DECLS;
  40335. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  40336. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  40337. int i;
  40338. char buf[50];
  40339. ASN1_OBJECT* obj = NULL;
  40340. static const struct {
  40341. const char* oidStr;
  40342. const char* sn;
  40343. const char* ln;
  40344. } objs_list[] = {
  40345. #if defined(WOLFSSL_APACHE_HTTPD)
  40346. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  40347. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  40348. #endif
  40349. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  40350. { NULL, NULL, NULL }
  40351. };
  40352. static const struct {
  40353. const char* numeric;
  40354. const char* name;
  40355. } objs_named[] = {
  40356. /* In dictionary but not in normal list. */
  40357. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  40358. /* Made up OID. */
  40359. { "1.3.5.7", "1.3.5.7" },
  40360. { NULL, NULL }
  40361. };
  40362. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  40363. ASN1_OBJECT_free(obj);
  40364. obj = NULL;
  40365. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  40366. ASN1_OBJECT_free(obj);
  40367. obj = NULL;
  40368. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  40369. /* Test numerical value of oid (oidStr) */
  40370. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  40371. /* Convert object back to text to confirm oid is correct */
  40372. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40373. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  40374. ASN1_OBJECT_free(obj);
  40375. obj = NULL;
  40376. XMEMSET(buf, 0, sizeof(buf));
  40377. /* Test short name (sn) */
  40378. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  40379. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  40380. /* Convert object back to text to confirm oid is correct */
  40381. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40382. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  40383. ASN1_OBJECT_free(obj);
  40384. obj = NULL;
  40385. XMEMSET(buf, 0, sizeof(buf));
  40386. /* Test long name (ln) - should fail when no_name = 1 */
  40387. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  40388. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  40389. /* Convert object back to text to confirm oid is correct */
  40390. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40391. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  40392. ASN1_OBJECT_free(obj);
  40393. obj = NULL;
  40394. XMEMSET(buf, 0, sizeof(buf));
  40395. }
  40396. for (i = 0; objs_named[i].numeric != NULL; i++) {
  40397. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  40398. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  40399. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  40400. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  40401. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  40402. ASN1_OBJECT_free(obj);
  40403. obj = NULL;
  40404. }
  40405. #endif
  40406. return EXPECT_RESULT();
  40407. }
  40408. static int test_wolfSSL_PEM_write_bio_X509(void)
  40409. {
  40410. EXPECT_DECLS;
  40411. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  40412. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  40413. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  40414. !defined(NO_FILESYSTEM)
  40415. /* This test contains the hard coded expected
  40416. * lengths. Update if necessary */
  40417. XFILE fp = XBADFILE;
  40418. WOLFSSL_EVP_PKEY *priv = NULL;
  40419. BIO* input = NULL;
  40420. BIO* output = NULL;
  40421. X509* x509a = NULL;
  40422. X509* x509b = NULL;
  40423. ASN1_TIME* notBeforeA = NULL;
  40424. ASN1_TIME* notAfterA = NULL;
  40425. #ifndef NO_ASN_TIME
  40426. ASN1_TIME* notBeforeB = NULL;
  40427. ASN1_TIME* notAfterB = NULL;
  40428. #endif
  40429. int expectedLen;
  40430. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  40431. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  40432. if (fp != XBADFILE) {
  40433. XFCLOSE(fp);
  40434. fp = XBADFILE;
  40435. }
  40436. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  40437. "rb"));
  40438. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  40439. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  40440. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40441. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  40442. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  40443. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  40444. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40445. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40446. /* compare length against expected */
  40447. expectedLen = 2000;
  40448. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  40449. #ifndef NO_ASN_TIME
  40450. /* read exported X509 PEM back into struct, sanity check on export,
  40451. * make sure notBefore/notAfter are the same and certs are identical. */
  40452. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40453. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  40454. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  40455. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  40456. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  40457. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40458. X509_free(x509b);
  40459. x509b = NULL;
  40460. #endif
  40461. /* Reset output buffer */
  40462. BIO_free(output);
  40463. output = NULL;
  40464. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40465. /* Test forcing the AKID to be generated just from KeyIdentifier */
  40466. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  40467. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  40468. x509a->authKeyId = x509a->authKeyIdSrc;
  40469. x509a->authKeyIdSrc = NULL;
  40470. x509a->authKeyIdSrcSz = 0;
  40471. }
  40472. /* Resign to re-generate the der */
  40473. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  40474. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40475. /* Check that we generate a smaller output since the AKID will
  40476. * only contain the KeyIdentifier without any additional
  40477. * information */
  40478. /* Here we copy the validity struct from the original */
  40479. expectedLen = 1688;
  40480. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  40481. /* Reset buffers and x509 */
  40482. BIO_free(input);
  40483. input = NULL;
  40484. BIO_free(output);
  40485. output = NULL;
  40486. X509_free(x509a);
  40487. x509a = NULL;
  40488. /* test CA and basicConstSet values are encoded when
  40489. * the cert is a CA */
  40490. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  40491. /* read PEM into X509 struct */
  40492. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40493. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  40494. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40495. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40496. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  40497. * values are maintained and certs are identical.*/
  40498. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40499. ExpectIntEQ(x509b->isCa, 1);
  40500. ExpectIntEQ(x509b->basicConstSet, 1);
  40501. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40502. X509_free(x509a);
  40503. x509a = NULL;
  40504. X509_free(x509b);
  40505. x509b = NULL;
  40506. BIO_free(input);
  40507. input = NULL;
  40508. BIO_free(output);
  40509. output = NULL;
  40510. /* test CA and basicConstSet values are encoded when
  40511. * the cert is not CA */
  40512. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  40513. /* read PEM into X509 struct */
  40514. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40515. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  40516. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40517. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40518. /* read exported X509 PEM back into struct, ensure isCa and
  40519. * basicConstSet values are maintained and certs are identical */
  40520. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40521. ExpectIntEQ(x509b->isCa, 0);
  40522. ExpectIntEQ(x509b->basicConstSet, 1);
  40523. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40524. wolfSSL_EVP_PKEY_free(priv);
  40525. X509_free(x509a);
  40526. X509_free(x509b);
  40527. BIO_free(input);
  40528. BIO_free(output);
  40529. #endif
  40530. return EXPECT_RESULT();
  40531. }
  40532. static int test_wolfSSL_X509_NAME_ENTRY(void)
  40533. {
  40534. EXPECT_DECLS;
  40535. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40536. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  40537. X509* x509 = NULL;
  40538. #ifndef NO_BIO
  40539. BIO* bio = NULL;
  40540. #endif
  40541. X509_NAME* nm = NULL;
  40542. X509_NAME_ENTRY* entry = NULL;
  40543. unsigned char cn[] = "another name to add";
  40544. #ifdef OPENSSL_ALL
  40545. int i;
  40546. int names_len = 0;
  40547. #endif
  40548. ExpectNotNull(x509 =
  40549. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  40550. #ifndef NO_BIO
  40551. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40552. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  40553. #endif
  40554. #ifdef WOLFSSL_CERT_REQ
  40555. {
  40556. X509_REQ* req = NULL;
  40557. #ifndef NO_BIO
  40558. BIO* bReq = NULL;
  40559. #endif
  40560. ExpectNotNull(req =
  40561. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  40562. #ifndef NO_BIO
  40563. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  40564. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  40565. BIO_free(bReq);
  40566. #endif
  40567. X509_free(req);
  40568. }
  40569. #endif
  40570. ExpectNotNull(nm = X509_get_subject_name(x509));
  40571. /* Test add entry */
  40572. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  40573. 0x0c, cn, (int)sizeof(cn)));
  40574. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  40575. #ifdef WOLFSSL_CERT_EXT
  40576. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  40577. (byte*)"support@wolfssl.com", 19, -1,
  40578. 1), WOLFSSL_SUCCESS);
  40579. #endif
  40580. X509_NAME_ENTRY_free(entry);
  40581. entry = NULL;
  40582. #ifdef WOLFSSL_CERT_REQ
  40583. {
  40584. unsigned char srv_pkcs9p[] = "Server";
  40585. unsigned char fvrtDrnk[] = "tequila";
  40586. unsigned char* der = NULL;
  40587. char* subject = NULL;
  40588. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  40589. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  40590. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  40591. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  40592. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  40593. ExpectNotNull(der);
  40594. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  40595. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  40596. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  40597. #ifdef DEBUG_WOLFSSL
  40598. if (subject != NULL) {
  40599. fprintf(stderr, "\n\t%s\n", subject);
  40600. }
  40601. #endif
  40602. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  40603. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  40604. }
  40605. #endif
  40606. /* Test add entry by text */
  40607. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  40608. 0x0c, cn, (int)sizeof(cn)));
  40609. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  40610. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  40611. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  40612. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  40613. #endif
  40614. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  40615. X509_NAME_ENTRY_free(entry);
  40616. entry = NULL;
  40617. /* Test add entry by NID */
  40618. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  40619. cn, -1, -1, 0), SSL_SUCCESS);
  40620. #ifdef OPENSSL_ALL
  40621. /* stack of name entry */
  40622. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  40623. for (i = 0; i < names_len; i++) {
  40624. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  40625. }
  40626. #endif
  40627. #ifndef NO_BIO
  40628. BIO_free(bio);
  40629. #endif
  40630. X509_free(x509); /* free's nm */
  40631. #endif
  40632. return EXPECT_RESULT();
  40633. }
  40634. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40635. static int test_GENERAL_NAME_set0_othername(void) {
  40636. EXPECT_DECLS;
  40637. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40638. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40639. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40640. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40641. defined(WOLFSSL_FPKI)
  40642. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40643. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40644. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40645. const char * cert_fname = "./certs/server-cert.der";
  40646. const char * key_fname = "./certs/server-key.der";
  40647. X509* x509 = NULL;
  40648. GENERAL_NAME* gn = NULL;
  40649. GENERAL_NAMES* gns = NULL;
  40650. ASN1_OBJECT* upn_oid = NULL;
  40651. ASN1_UTF8STRING *utf8str = NULL;
  40652. ASN1_TYPE *value = NULL;
  40653. X509_EXTENSION * ext = NULL;
  40654. byte* pt = NULL;
  40655. byte der[4096];
  40656. int derSz = 0;
  40657. EVP_PKEY* priv = NULL;
  40658. XFILE f = XBADFILE;
  40659. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  40660. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  40661. if (f != XBADFILE) {
  40662. XFCLOSE(f);
  40663. f = XBADFILE;
  40664. }
  40665. ExpectNotNull(gn = GENERAL_NAME_new());
  40666. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40667. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40668. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40669. ExpectNotNull(value = ASN1_TYPE_new());
  40670. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40671. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  40672. wolfSSL_ASN1_STRING_free(utf8str);
  40673. }
  40674. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40675. if (EXPECT_FAIL()) {
  40676. ASN1_TYPE_free(value);
  40677. }
  40678. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40679. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40680. if (EXPECT_FAIL()) {
  40681. GENERAL_NAME_free(gn);
  40682. gn = NULL;
  40683. }
  40684. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40685. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  40686. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40687. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40688. if (f != XBADFILE) {
  40689. XFCLOSE(f);
  40690. f = XBADFILE;
  40691. }
  40692. pt = der;
  40693. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40694. (const unsigned char**)&pt, derSz));
  40695. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  40696. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40697. gns = NULL;
  40698. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40699. NULL));
  40700. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  40701. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  40702. ExpectIntEQ(gn->type, 0);
  40703. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40704. ASN1_OBJECT_free(upn_oid);
  40705. X509_EXTENSION_free(ext);
  40706. X509_free(x509);
  40707. EVP_PKEY_free(priv);
  40708. #endif
  40709. return EXPECT_RESULT();
  40710. }
  40711. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40712. static int test_othername_and_SID_ext(void) {
  40713. EXPECT_DECLS;
  40714. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40715. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40716. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40717. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40718. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  40719. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40720. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40721. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40722. const char* csr_fname = "./certs/csr.signed.der";
  40723. const char* key_fname = "./certs/server-key.der";
  40724. byte der[4096];
  40725. int derSz = 0;
  40726. X509_REQ* x509 = NULL;
  40727. STACK_OF(X509_EXTENSION) *exts = NULL;
  40728. X509_EXTENSION * san_ext = NULL;
  40729. X509_EXTENSION * ext = NULL;
  40730. GENERAL_NAME* gn = NULL;
  40731. GENERAL_NAMES* gns = NULL;
  40732. ASN1_OBJECT* upn_oid = NULL;
  40733. ASN1_UTF8STRING *utf8str = NULL;
  40734. ASN1_TYPE *value = NULL;
  40735. ASN1_STRING *extval = NULL;
  40736. /* SID extension. SID data format explained here:
  40737. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  40738. */
  40739. byte SidExtension[] = {
  40740. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  40741. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  40742. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  40743. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  40744. byte expectedAltName[] = {
  40745. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  40746. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  40747. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  40748. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  40749. X509_EXTENSION *sid_ext = NULL;
  40750. ASN1_OBJECT* sid_oid = NULL;
  40751. ASN1_OCTET_STRING *sid_data = NULL;
  40752. EVP_PKEY* priv = NULL;
  40753. XFILE f = XBADFILE;
  40754. byte* pt = NULL;
  40755. BIO* bio = NULL;
  40756. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  40757. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  40758. if (f != XBADFILE) {
  40759. XFCLOSE(f);
  40760. f = XBADFILE;
  40761. }
  40762. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  40763. ExpectNotNull(gn = GENERAL_NAME_new());
  40764. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40765. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40766. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40767. ExpectNotNull(value = ASN1_TYPE_new());
  40768. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40769. if (EXPECT_FAIL()) {
  40770. ASN1_UTF8STRING_free(utf8str);
  40771. }
  40772. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40773. if (EXPECT_FAIL()) {
  40774. ASN1_TYPE_free(value);
  40775. GENERAL_NAME_free(gn);
  40776. gn = NULL;
  40777. }
  40778. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40779. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40780. if (EXPECT_FAIL()) {
  40781. GENERAL_NAME_free(gn);
  40782. }
  40783. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40784. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  40785. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  40786. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  40787. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  40788. sid_data));
  40789. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  40790. /* Ensure an empty stack doesn't raise an error. */
  40791. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40792. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  40793. if (EXPECT_FAIL()) {
  40794. X509_EXTENSION_free(san_ext);
  40795. }
  40796. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  40797. if (EXPECT_FAIL()) {
  40798. X509_EXTENSION_free(sid_ext);
  40799. }
  40800. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40801. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40802. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40803. if (f != XBADFILE)
  40804. XFCLOSE(f);
  40805. pt = der;
  40806. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40807. (const unsigned char**)&pt, derSz));
  40808. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40809. pt = der;
  40810. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40811. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40812. gns = NULL;
  40813. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40814. exts = NULL;
  40815. ASN1_OBJECT_free(upn_oid);
  40816. ASN1_OBJECT_free(sid_oid);
  40817. ASN1_OCTET_STRING_free(sid_data);
  40818. X509_REQ_free(x509);
  40819. x509 = NULL;
  40820. EVP_PKEY_free(priv);
  40821. /* At this point everything used to generate what is in der is cleaned up.
  40822. * We now read back from der to confirm the extensions were inserted
  40823. * correctly. */
  40824. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40825. ExpectNotNull(bio);
  40826. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40827. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40828. ExpectNotNull(x509);
  40829. BIO_free(bio);
  40830. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40831. x509));
  40832. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40833. /* Check the SID extension. */
  40834. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40835. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40836. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40837. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40838. /* Check the AltNames extension. */
  40839. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40840. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40841. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40842. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40843. 0);
  40844. /* Cleanup */
  40845. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40846. NULL));
  40847. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40848. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40849. ExpectIntEQ(gn->type, 0);
  40850. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40851. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40852. X509_REQ_free(x509);
  40853. #endif
  40854. return EXPECT_RESULT();
  40855. }
  40856. static int test_wolfSSL_X509_set_name(void)
  40857. {
  40858. EXPECT_DECLS;
  40859. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40860. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40861. X509* x509 = NULL;
  40862. X509_NAME* name = NULL;
  40863. ExpectNotNull(name = X509_NAME_new());
  40864. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40865. (byte*)"wolfssl.com", 11, 0, 1),
  40866. WOLFSSL_SUCCESS);
  40867. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40868. (byte*)"support@wolfssl.com", 19, -1,
  40869. 1), WOLFSSL_SUCCESS);
  40870. ExpectNotNull(x509 = X509_new());
  40871. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40872. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40873. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40874. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40875. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40876. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40877. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40878. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40879. X509_free(x509);
  40880. X509_NAME_free(name);
  40881. #endif /* OPENSSL_ALL && !NO_CERTS */
  40882. return EXPECT_RESULT();
  40883. }
  40884. static int test_wolfSSL_X509_set_notAfter(void)
  40885. {
  40886. EXPECT_DECLS;
  40887. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40888. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40889. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40890. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40891. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40892. /* Generalized time will overflow time_t if not long */
  40893. X509* x = NULL;
  40894. BIO* bio = NULL;
  40895. ASN1_TIME *asn_time = NULL;
  40896. ASN1_TIME *time_check = NULL;
  40897. const int year = 365*24*60*60;
  40898. const int day = 24*60*60;
  40899. const int hour = 60*60;
  40900. const int mini = 60;
  40901. int offset_day;
  40902. unsigned char buf[25];
  40903. time_t t;
  40904. /*
  40905. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40906. */
  40907. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40908. offset_day = 7;
  40909. /*
  40910. * Free these.
  40911. */
  40912. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40913. ExpectNotNull(asn_time);
  40914. ExpectNotNull(x = X509_new());
  40915. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40916. /*
  40917. * Tests
  40918. */
  40919. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40920. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40921. ExpectNotNull(time_check = X509_get_notAfter(x));
  40922. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40923. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40924. /* Convert to human readable format and compare to intended date */
  40925. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40926. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40927. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40928. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40929. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40930. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40931. /*
  40932. * Cleanup
  40933. */
  40934. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40935. X509_free(x);
  40936. BIO_free(bio);
  40937. #endif
  40938. return EXPECT_RESULT();
  40939. }
  40940. static int test_wolfSSL_X509_set_notBefore(void)
  40941. {
  40942. EXPECT_DECLS;
  40943. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40944. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40945. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40946. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40947. X509* x = NULL;
  40948. BIO* bio = NULL;
  40949. ASN1_TIME *asn_time = NULL;
  40950. ASN1_TIME *time_check = NULL;
  40951. const int year = 365*24*60*60;
  40952. const int day = 24*60*60;
  40953. const int hour = 60*60;
  40954. const int mini = 60;
  40955. int offset_day;
  40956. unsigned char buf[25];
  40957. time_t t;
  40958. /*
  40959. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40960. */
  40961. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40962. offset_day = 7;
  40963. /*
  40964. * Free these.
  40965. */
  40966. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40967. ExpectNotNull(asn_time);
  40968. ExpectNotNull(x = X509_new());
  40969. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40970. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40971. /*
  40972. * Main Tests
  40973. */
  40974. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40975. /* time_check == (ANS1_TIME*)x->notBefore */
  40976. ExpectNotNull(time_check = X509_get_notBefore(x));
  40977. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40978. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40979. /* Convert to human readable format and compare to intended date */
  40980. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40981. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40982. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40983. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40984. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40985. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40986. /*
  40987. * Cleanup
  40988. */
  40989. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40990. X509_free(x);
  40991. BIO_free(bio);
  40992. #endif
  40993. return EXPECT_RESULT();
  40994. }
  40995. static int test_wolfSSL_X509_set_version(void)
  40996. {
  40997. EXPECT_DECLS;
  40998. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40999. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  41000. X509* x509 = NULL;
  41001. long v = 2L;
  41002. long maxInt = INT_MAX;
  41003. ExpectNotNull(x509 = X509_new());
  41004. /* These should pass. */
  41005. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  41006. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  41007. /* Fail Case: When v(long) is greater than x509->version(int). */
  41008. v = maxInt+1;
  41009. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  41010. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  41011. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  41012. /* Cleanup */
  41013. X509_free(x509);
  41014. #endif
  41015. return EXPECT_RESULT();
  41016. }
  41017. #ifndef NO_BIO
  41018. static int test_wolfSSL_BIO_gets(void)
  41019. {
  41020. EXPECT_DECLS;
  41021. #if defined(OPENSSL_EXTRA)
  41022. BIO* bio = NULL;
  41023. BIO* bio2 = NULL;
  41024. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  41025. char emp[] = "";
  41026. char bio_buffer[20];
  41027. int bufferSz = 20;
  41028. #ifdef OPENSSL_ALL
  41029. BUF_MEM* emp_bm = NULL;
  41030. BUF_MEM* msg_bm = NULL;
  41031. #endif
  41032. /* try with bad args */
  41033. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  41034. #ifdef OPENSSL_ALL
  41035. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  41036. #endif
  41037. /* try with real msg */
  41038. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  41039. XMEMSET(bio_buffer, 0, bufferSz);
  41040. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  41041. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  41042. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  41043. ExpectFalse(bio2 != BIO_next(bio));
  41044. /* make buffer filled with no terminating characters */
  41045. XMEMSET(bio_buffer, 1, bufferSz);
  41046. /* BIO_gets reads a line of data */
  41047. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  41048. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  41049. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  41050. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  41051. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  41052. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  41053. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  41054. #ifdef OPENSSL_ALL
  41055. /* test setting the mem_buf manually */
  41056. BIO_free(bio);
  41057. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  41058. ExpectNotNull(emp_bm = BUF_MEM_new());
  41059. ExpectNotNull(msg_bm = BUF_MEM_new());
  41060. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  41061. if (EXPECT_SUCCESS()) {
  41062. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  41063. msg_bm->data = NULL;
  41064. }
  41065. /* emp size is 1 for terminator */
  41066. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  41067. if (EXPECT_SUCCESS()) {
  41068. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  41069. emp_bm->data = emp;
  41070. msg_bm->data = msg;
  41071. }
  41072. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  41073. /* check reading an empty string */
  41074. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  41075. ExpectStrEQ(emp, bio_buffer);
  41076. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  41077. /* BIO_gets reads a line of data */
  41078. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  41079. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  41080. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  41081. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  41082. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  41083. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  41084. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  41085. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  41086. if (EXPECT_SUCCESS())
  41087. emp_bm->data = NULL;
  41088. BUF_MEM_free(emp_bm);
  41089. if (EXPECT_SUCCESS())
  41090. msg_bm->data = NULL;
  41091. BUF_MEM_free(msg_bm);
  41092. #endif
  41093. /* check not null terminated string */
  41094. BIO_free(bio);
  41095. bio = NULL;
  41096. msg[0] = 0x33;
  41097. msg[1] = 0x33;
  41098. msg[2] = 0x33;
  41099. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  41100. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  41101. ExpectIntEQ(bio_buffer[0], msg[0]);
  41102. ExpectIntEQ(bio_buffer[1], msg[1]);
  41103. ExpectIntNE(bio_buffer[2], msg[2]);
  41104. BIO_free(bio);
  41105. bio = NULL;
  41106. msg[3] = 0x33;
  41107. bio_buffer[3] = 0x33;
  41108. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  41109. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  41110. ExpectIntEQ(bio_buffer[0], msg[0]);
  41111. ExpectIntEQ(bio_buffer[1], msg[1]);
  41112. ExpectIntEQ(bio_buffer[2], msg[2]);
  41113. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  41114. /* check reading an empty string */
  41115. BIO_free(bio);
  41116. bio = NULL;
  41117. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  41118. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  41119. ExpectStrEQ(emp, bio_buffer);
  41120. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  41121. /* check error cases */
  41122. BIO_free(bio);
  41123. bio = NULL;
  41124. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  41125. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41126. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  41127. #if !defined(NO_FILESYSTEM)
  41128. {
  41129. BIO* f_bio = NULL;
  41130. XFILE f = XBADFILE;
  41131. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  41132. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  41133. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  41134. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  41135. if (EXPECT_FAIL() && (f != XBADFILE)) {
  41136. XFCLOSE(f);
  41137. }
  41138. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  41139. BIO_free(f_bio);
  41140. f_bio = NULL;
  41141. }
  41142. #endif /* NO_FILESYSTEM */
  41143. BIO_free(bio);
  41144. bio = NULL;
  41145. BIO_free(bio2);
  41146. bio2 = NULL;
  41147. /* try with type BIO */
  41148. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  41149. sizeof(msg));
  41150. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  41151. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  41152. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  41153. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  41154. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  41155. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  41156. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  41157. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  41158. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  41159. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  41160. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  41161. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  41162. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  41163. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  41164. BIO_free(bio);
  41165. bio = NULL;
  41166. BIO_free(bio2);
  41167. bio2 = NULL;
  41168. /* check reading an empty string */
  41169. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  41170. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  41171. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  41172. ExpectStrEQ(emp, bio_buffer);
  41173. BIO_free(bio);
  41174. bio = NULL;
  41175. #endif
  41176. return EXPECT_RESULT();
  41177. }
  41178. static int test_wolfSSL_BIO_puts(void)
  41179. {
  41180. EXPECT_DECLS;
  41181. #if defined(OPENSSL_EXTRA)
  41182. BIO* bio = NULL;
  41183. char input[] = "hello\0world\n.....ok\n\0";
  41184. char output[128];
  41185. XMEMSET(output, 0, sizeof(output));
  41186. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41187. ExpectIntEQ(BIO_puts(bio, input), 5);
  41188. ExpectIntEQ(BIO_pending(bio), 5);
  41189. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  41190. ExpectIntEQ(BIO_pending(bio), 19);
  41191. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  41192. ExpectStrEQ(output, "helloworld\n");
  41193. ExpectIntEQ(BIO_pending(bio), 8);
  41194. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  41195. ExpectStrEQ(output, ".....ok\n");
  41196. ExpectIntEQ(BIO_pending(bio), 0);
  41197. ExpectIntEQ(BIO_puts(bio, ""), -1);
  41198. BIO_free(bio);
  41199. #endif
  41200. return EXPECT_RESULT();
  41201. }
  41202. static int test_wolfSSL_BIO_dump(void)
  41203. {
  41204. EXPECT_DECLS;
  41205. #if defined(OPENSSL_EXTRA)
  41206. BIO* bio;
  41207. static const unsigned char data[] = {
  41208. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  41209. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  41210. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  41211. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  41212. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  41213. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  41214. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  41215. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  41216. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  41217. 0xB4
  41218. };
  41219. /* Generated with OpenSSL. */
  41220. static const char expected[] =
  41221. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  41222. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  41223. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  41224. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  41225. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  41226. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  41227. static const char expectedAll[] =
  41228. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  41229. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  41230. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  41231. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  41232. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  41233. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  41234. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  41235. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  41236. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  41237. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  41238. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  41239. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  41240. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  41241. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  41242. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  41243. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  41244. char output[16 * 80];
  41245. int i;
  41246. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41247. /* Example key dumped. */
  41248. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  41249. sizeof(expected) - 1);
  41250. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  41251. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  41252. /* Try every possible value for a character. */
  41253. for (i = 0; i < 256; i++)
  41254. output[i] = i;
  41255. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  41256. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  41257. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  41258. BIO_free(bio);
  41259. #endif
  41260. return EXPECT_RESULT();
  41261. }
  41262. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41263. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  41264. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  41265. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  41266. {
  41267. (void)ssl;
  41268. (void)buf;
  41269. (void)sz;
  41270. (void)ctx;
  41271. return WOLFSSL_CBIO_ERR_WANT_READ;
  41272. }
  41273. #endif
  41274. static int test_wolfSSL_BIO_should_retry(void)
  41275. {
  41276. EXPECT_DECLS;
  41277. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41278. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  41279. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  41280. tcp_ready ready;
  41281. func_args server_args;
  41282. THREAD_TYPE serverThread;
  41283. SOCKET_T sockfd = 0;
  41284. WOLFSSL_CTX* ctx = NULL;
  41285. WOLFSSL* ssl = NULL;
  41286. char msg[64] = "hello wolfssl!";
  41287. char reply[1024];
  41288. int msgSz = (int)XSTRLEN(msg);
  41289. int ret;
  41290. BIO* bio = NULL;
  41291. XMEMSET(&server_args, 0, sizeof(func_args));
  41292. #ifdef WOLFSSL_TIRTOS
  41293. fdOpenSession(Task_self());
  41294. #endif
  41295. StartTCP();
  41296. InitTcpReady(&ready);
  41297. #if defined(USE_WINDOWS_API)
  41298. /* use RNG to get random port if using windows */
  41299. ready.port = GetRandomPort();
  41300. #endif
  41301. server_args.signal = &ready;
  41302. start_thread(test_server_nofail, &server_args, &serverThread);
  41303. wait_tcp_ready(&server_args);
  41304. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41305. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  41306. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  41307. #endif
  41308. ExpectIntEQ(WOLFSSL_SUCCESS,
  41309. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  41310. ExpectIntEQ(WOLFSSL_SUCCESS,
  41311. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  41312. ExpectIntEQ(WOLFSSL_SUCCESS,
  41313. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41314. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  41315. /* force retry */
  41316. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  41317. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  41318. ExpectNotNull(ssl);
  41319. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41320. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  41321. if (EXPECT_FAIL()) {
  41322. wolfSSL_free(ssl);
  41323. ssl = NULL;
  41324. }
  41325. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  41326. ExpectIntNE(BIO_should_retry(bio), 0);
  41327. ExpectIntEQ(BIO_should_read(bio), 0);
  41328. ExpectIntEQ(BIO_should_write(bio), 0);
  41329. /* now perform successful connection */
  41330. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  41331. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  41332. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  41333. ret = wolfSSL_get_error(ssl, -1);
  41334. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  41335. ExpectIntNE(BIO_should_retry(bio), 0);
  41336. if (ret == WOLFSSL_ERROR_WANT_READ)
  41337. ExpectIntEQ(BIO_should_read(bio), 1);
  41338. else
  41339. ExpectIntEQ(BIO_should_read(bio), 0);
  41340. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  41341. ExpectIntEQ(BIO_should_write(bio), 1);
  41342. else
  41343. ExpectIntEQ(BIO_should_write(bio), 0);
  41344. }
  41345. else {
  41346. ExpectIntEQ(BIO_should_retry(bio), 0);
  41347. ExpectIntEQ(BIO_should_read(bio), 0);
  41348. ExpectIntEQ(BIO_should_write(bio), 0);
  41349. }
  41350. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  41351. XSTRLEN("I hear you fa shizzle!")), 0);
  41352. BIO_free(bio);
  41353. wolfSSL_CTX_free(ctx);
  41354. CloseSocket(sockfd);
  41355. join_thread(serverThread);
  41356. FreeTcpReady(&ready);
  41357. #ifdef WOLFSSL_TIRTOS
  41358. fdOpenSession(Task_self());
  41359. #endif
  41360. #endif
  41361. return EXPECT_RESULT();
  41362. }
  41363. static int test_wolfSSL_BIO_connect(void)
  41364. {
  41365. EXPECT_DECLS;
  41366. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41367. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  41368. tcp_ready ready;
  41369. func_args server_args;
  41370. THREAD_TYPE serverThread;
  41371. BIO *tcpBio = NULL;
  41372. BIO *sslBio = NULL;
  41373. SSL_CTX* ctx = NULL;
  41374. SSL *ssl = NULL;
  41375. SSL *sslPtr;
  41376. char msg[] = "hello wolfssl!";
  41377. char reply[30];
  41378. char buff[10] = {0};
  41379. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41380. ExpectIntEQ(WOLFSSL_SUCCESS,
  41381. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  41382. ExpectIntEQ(WOLFSSL_SUCCESS,
  41383. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  41384. ExpectIntEQ(WOLFSSL_SUCCESS,
  41385. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  41386. /* Setup server */
  41387. XMEMSET(&server_args, 0, sizeof(func_args));
  41388. StartTCP();
  41389. InitTcpReady(&ready);
  41390. #if defined(USE_WINDOWS_API)
  41391. /* use RNG to get random port if using windows */
  41392. ready.port = GetRandomPort();
  41393. #endif
  41394. server_args.signal = &ready;
  41395. start_thread(test_server_nofail, &server_args, &serverThread);
  41396. wait_tcp_ready(&server_args);
  41397. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  41398. /* Start the test proper */
  41399. /* Setup the TCP BIO */
  41400. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  41401. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  41402. /* Setup the SSL object */
  41403. ExpectNotNull(ssl = SSL_new(ctx));
  41404. SSL_set_connect_state(ssl);
  41405. /* Setup the SSL BIO */
  41406. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  41407. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  41408. if (EXPECT_FAIL()) {
  41409. wolfSSL_free(ssl);
  41410. }
  41411. /* Verify that BIO_get_ssl works. */
  41412. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  41413. ExpectPtrEq(ssl, sslPtr);
  41414. /* Link BIO's so that sslBio uses tcpBio for IO */
  41415. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  41416. /* Do TCP connect */
  41417. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  41418. /* Do TLS handshake */
  41419. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  41420. /* Test writing */
  41421. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  41422. /* Expect length of default wolfSSL reply */
  41423. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  41424. /* Clean it all up */
  41425. BIO_free_all(sslBio);
  41426. /* Server clean up */
  41427. join_thread(serverThread);
  41428. FreeTcpReady(&ready);
  41429. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  41430. * after. */
  41431. XMEMSET(&server_args, 0, sizeof(func_args));
  41432. StartTCP();
  41433. InitTcpReady(&ready);
  41434. #if defined(USE_WINDOWS_API)
  41435. /* use RNG to get random port if using windows */
  41436. ready.port = GetRandomPort();
  41437. #endif
  41438. server_args.signal = &ready;
  41439. start_thread(test_server_nofail, &server_args, &serverThread);
  41440. wait_tcp_ready(&server_args);
  41441. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  41442. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  41443. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  41444. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  41445. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  41446. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  41447. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  41448. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  41449. /* Attempt to close the TLS connection gracefully. */
  41450. BIO_ssl_shutdown(sslBio);
  41451. BIO_free_all(sslBio);
  41452. join_thread(serverThread);
  41453. FreeTcpReady(&ready);
  41454. SSL_CTX_free(ctx);
  41455. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41456. wc_ecc_fp_free(); /* free per thread cache */
  41457. #endif
  41458. #endif
  41459. return EXPECT_RESULT();
  41460. }
  41461. static int test_wolfSSL_BIO_tls(void)
  41462. {
  41463. EXPECT_DECLS;
  41464. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  41465. SSL_CTX* ctx = NULL;
  41466. SSL *ssl = NULL;
  41467. BIO *readBio = NULL;
  41468. BIO *writeBio = NULL;
  41469. int ret;
  41470. int err = 0;
  41471. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41472. ExpectNotNull(ssl = SSL_new(ctx));
  41473. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  41474. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  41475. /* Qt reads data from write-bio,
  41476. * then writes the read data into plain packet.
  41477. * Qt reads data from plain packet,
  41478. * then writes the read data into read-bio.
  41479. */
  41480. SSL_set_bio(ssl, readBio, writeBio);
  41481. do {
  41482. #ifdef WOLFSSL_ASYNC_CRYPT
  41483. if (err == WC_PENDING_E) {
  41484. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41485. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41486. }
  41487. #endif
  41488. ret = SSL_connect(ssl);
  41489. err = SSL_get_error(ssl, 0);
  41490. } while (err == WC_PENDING_E);
  41491. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  41492. /* in this use case, should return WANT READ
  41493. * so that Qt will read the data from plain packet for next state.
  41494. */
  41495. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  41496. SSL_free(ssl);
  41497. SSL_CTX_free(ctx);
  41498. #endif
  41499. return EXPECT_RESULT();
  41500. }
  41501. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41502. defined(HAVE_HTTP_CLIENT)
  41503. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  41504. {
  41505. BIO* clientBio;
  41506. SSL* sslClient;
  41507. SSL_CTX* ctx;
  41508. char connectAddr[20]; /* IP + port */;
  41509. (void)args;
  41510. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  41511. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  41512. AssertIntEQ(BIO_do_connect(clientBio), 1);
  41513. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41514. AssertNotNull(sslClient = SSL_new(ctx));
  41515. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  41516. SSL_set_bio(sslClient, clientBio, clientBio);
  41517. AssertIntEQ(SSL_connect(sslClient), 1);
  41518. SSL_free(sslClient);
  41519. SSL_CTX_free(ctx);
  41520. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41521. wc_ecc_fp_free(); /* free per thread cache */
  41522. #endif
  41523. WOLFSSL_RETURN_FROM_THREAD(0);
  41524. }
  41525. #endif
  41526. static int test_wolfSSL_BIO_accept(void)
  41527. {
  41528. EXPECT_DECLS;
  41529. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41530. defined(HAVE_HTTP_CLIENT)
  41531. BIO* serverBindBio = NULL;
  41532. BIO* serverAcceptBio = NULL;
  41533. SSL* sslServer = NULL;
  41534. SSL_CTX* ctx = NULL;
  41535. func_args args;
  41536. THREAD_TYPE thread;
  41537. char port[10]; /* 10 bytes should be enough to store the string
  41538. * representation of the port */
  41539. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  41540. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  41541. /* First BIO_do_accept binds the port */
  41542. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  41543. XMEMSET(&args, 0, sizeof(func_args));
  41544. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  41545. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  41546. /* Let's plug it into SSL to test */
  41547. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41548. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  41549. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  41550. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  41551. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  41552. ExpectNotNull(sslServer = SSL_new(ctx));
  41553. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  41554. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  41555. ExpectIntEQ(SSL_accept(sslServer), 1);
  41556. join_thread(thread);
  41557. BIO_free(serverBindBio);
  41558. SSL_free(sslServer);
  41559. SSL_CTX_free(ctx);
  41560. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41561. wc_ecc_fp_free(); /* free per thread cache */
  41562. #endif
  41563. #endif
  41564. return EXPECT_RESULT();
  41565. }
  41566. static int test_wolfSSL_BIO_write(void)
  41567. {
  41568. EXPECT_DECLS;
  41569. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  41570. BIO* bio = NULL;
  41571. BIO* bio64 = NULL;
  41572. BIO* bio_mem = NULL;
  41573. BIO* ptr = NULL;
  41574. int sz;
  41575. char msg[] = "conversion test";
  41576. char out[40];
  41577. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  41578. void* bufPtr = NULL;
  41579. BUF_MEM* buf = NULL;
  41580. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  41581. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  41582. if (EXPECT_FAIL()) {
  41583. BIO_free(bio64);
  41584. }
  41585. /* now should convert to base64 then write to memory */
  41586. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41587. BIO_flush(bio);
  41588. /* test BIO chain */
  41589. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  41590. ExpectNotNull(buf);
  41591. ExpectIntEQ(buf->length, 25);
  41592. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  41593. ExpectPtrEq(buf->data, bufPtr);
  41594. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  41595. sz = sizeof(out);
  41596. XMEMSET(out, 0, sz);
  41597. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  41598. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  41599. /* write then read should return the same message */
  41600. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41601. sz = sizeof(out);
  41602. XMEMSET(out, 0, sz);
  41603. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  41604. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41605. /* now try encoding with no line ending */
  41606. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  41607. #ifdef HAVE_EX_DATA
  41608. BIO_set_ex_data(bio64, 0, (void*) "data");
  41609. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  41610. #endif
  41611. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41612. BIO_flush(bio);
  41613. sz = sizeof(out);
  41614. XMEMSET(out, 0, sz);
  41615. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  41616. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  41617. BIO_free_all(bio); /* frees bio64 also */
  41618. bio = NULL;
  41619. /* test with more than one bio64 in list */
  41620. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  41621. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  41622. if (EXPECT_FAIL()) {
  41623. BIO_free(bio64);
  41624. bio64 = NULL;
  41625. }
  41626. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  41627. ExpectNotNull(BIO_push(bio64, bio_mem));
  41628. if (EXPECT_FAIL()) {
  41629. BIO_free(bio_mem);
  41630. }
  41631. /* now should convert to base64 when stored and then decode with read */
  41632. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  41633. BIO_flush(bio);
  41634. sz = sizeof(out);
  41635. XMEMSET(out, 0, sz);
  41636. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  41637. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  41638. BIO_clear_flags(bio64, ~0);
  41639. BIO_set_retry_read(bio);
  41640. BIO_free_all(bio); /* frees bio64s also */
  41641. bio = NULL;
  41642. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  41643. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41644. BIO_free(bio);
  41645. #endif
  41646. return EXPECT_RESULT();
  41647. }
  41648. static int test_wolfSSL_BIO_printf(void)
  41649. {
  41650. EXPECT_DECLS;
  41651. #if defined(OPENSSL_ALL)
  41652. BIO* bio = NULL;
  41653. int sz = 7;
  41654. char msg[] = "TLS 1.3 for the world";
  41655. char out[60];
  41656. char expected[] = "TLS 1.3 for the world : sz = 7";
  41657. XMEMSET(out, 0, sizeof(out));
  41658. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41659. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  41660. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  41661. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  41662. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  41663. BIO_free(bio);
  41664. #endif
  41665. return EXPECT_RESULT();
  41666. }
  41667. static int test_wolfSSL_BIO_f_md(void)
  41668. {
  41669. EXPECT_DECLS;
  41670. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  41671. BIO* bio = NULL;
  41672. BIO* mem = NULL;
  41673. char msg[] = "message to hash";
  41674. char out[60];
  41675. EVP_MD_CTX* ctx = NULL;
  41676. const unsigned char testKey[] =
  41677. {
  41678. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  41679. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  41680. 0x0b, 0x0b, 0x0b, 0x0b
  41681. };
  41682. const char testData[] = "Hi There";
  41683. const unsigned char testResult[] =
  41684. {
  41685. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  41686. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  41687. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  41688. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  41689. };
  41690. const unsigned char expectedHash[] =
  41691. {
  41692. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  41693. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  41694. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  41695. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  41696. };
  41697. const unsigned char emptyHash[] =
  41698. {
  41699. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  41700. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  41701. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  41702. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  41703. };
  41704. unsigned char check[sizeof(testResult) + 1];
  41705. size_t checkSz = -1;
  41706. EVP_PKEY* key = NULL;
  41707. XMEMSET(out, 0, sizeof(out));
  41708. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41709. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41710. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  41711. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  41712. /* should not be able to write/read yet since just digest wrapper and no
  41713. * data is passing through the bio */
  41714. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  41715. ExpectIntEQ(BIO_pending(bio), 0);
  41716. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  41717. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  41718. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41719. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  41720. BIO_reset(bio);
  41721. /* append BIO mem to bio in order to read/write */
  41722. ExpectNotNull(bio = BIO_push(bio, mem));
  41723. XMEMSET(out, 0, sizeof(out));
  41724. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  41725. ExpectIntEQ(BIO_pending(bio), 16);
  41726. /* this just reads the message and does not hash it (gets calls final) */
  41727. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  41728. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41729. /* create a message digest using BIO */
  41730. XMEMSET(out, 0, sizeof(out));
  41731. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  41732. ExpectIntEQ(BIO_pending(mem), 16);
  41733. ExpectIntEQ(BIO_pending(bio), 16);
  41734. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41735. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  41736. BIO_free(bio);
  41737. bio = NULL;
  41738. BIO_free(mem);
  41739. mem = NULL;
  41740. /* test with HMAC */
  41741. XMEMSET(out, 0, sizeof(out));
  41742. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41743. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41744. BIO_get_md_ctx(bio, &ctx);
  41745. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  41746. (int)sizeof(testKey)));
  41747. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  41748. ExpectNotNull(bio = BIO_push(bio, mem));
  41749. BIO_write(bio, testData, (int)strlen(testData));
  41750. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  41751. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  41752. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  41753. EVP_PKEY_free(key);
  41754. BIO_free(bio);
  41755. BIO_free(mem);
  41756. #endif
  41757. return EXPECT_RESULT();
  41758. }
  41759. static int test_wolfSSL_BIO_up_ref(void)
  41760. {
  41761. EXPECT_DECLS;
  41762. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41763. BIO* bio = NULL;
  41764. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41765. ExpectIntEQ(BIO_up_ref(NULL), 0);
  41766. ExpectIntEQ(BIO_up_ref(bio), 1);
  41767. BIO_free(bio);
  41768. ExpectIntEQ(BIO_up_ref(bio), 1);
  41769. BIO_free(bio);
  41770. BIO_free(bio);
  41771. #endif
  41772. return EXPECT_RESULT();
  41773. }
  41774. static int test_wolfSSL_BIO_reset(void)
  41775. {
  41776. EXPECT_DECLS;
  41777. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41778. BIO* bio = NULL;
  41779. byte buf[16];
  41780. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  41781. (word32)XSTRLEN("secure your data")));
  41782. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  41783. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  41784. XMEMSET(buf, 0, 16);
  41785. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  41786. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  41787. /* You cannot write to MEM BIO with read-only mode. */
  41788. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  41789. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  41790. XMEMSET(buf, 0, 16);
  41791. ExpectIntEQ(BIO_reset(bio), 0);
  41792. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  41793. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  41794. BIO_free(bio);
  41795. #endif
  41796. return EXPECT_RESULT();
  41797. }
  41798. #endif /* !NO_BIO */
  41799. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41800. /* test that the callback arg is correct */
  41801. static int certCbArg = 0;
  41802. static int certCb(WOLFSSL* ssl, void* arg)
  41803. {
  41804. if (ssl == NULL || arg != &certCbArg)
  41805. return 0;
  41806. if (wolfSSL_is_server(ssl)) {
  41807. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41808. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41809. return 0;
  41810. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41811. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41812. return 0;
  41813. }
  41814. else {
  41815. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41816. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41817. return 0;
  41818. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41819. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41820. return 0;
  41821. }
  41822. return 1;
  41823. }
  41824. static int certSetupCb(WOLFSSL_CTX* ctx)
  41825. {
  41826. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41827. return TEST_SUCCESS;
  41828. }
  41829. /**
  41830. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41831. * to stop certificate and key loading
  41832. */
  41833. static int certClearCb(WOLFSSL* ssl)
  41834. {
  41835. /* Clear the loaded certs to force the callbacks to set them up */
  41836. SSL_certs_clear(ssl);
  41837. return TEST_SUCCESS;
  41838. }
  41839. #endif
  41840. static int test_wolfSSL_cert_cb(void)
  41841. {
  41842. EXPECT_DECLS;
  41843. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41844. test_ssl_cbf func_cb_client;
  41845. test_ssl_cbf func_cb_server;
  41846. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41847. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41848. func_cb_client.ctx_ready = certSetupCb;
  41849. func_cb_client.ssl_ready = certClearCb;
  41850. func_cb_server.ctx_ready = certSetupCb;
  41851. func_cb_server.ssl_ready = certClearCb;
  41852. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41853. &func_cb_server, NULL), TEST_SUCCESS);
  41854. #endif
  41855. return EXPECT_RESULT();
  41856. }
  41857. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41858. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41859. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41860. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41861. {
  41862. EXPECT_DECLS;
  41863. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41864. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41865. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41866. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41867. return EXPECT_RESULT();
  41868. }
  41869. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41870. {
  41871. const byte* suites = NULL;
  41872. word16 suiteSz = 0;
  41873. const byte* hashSigAlgo = NULL;
  41874. word16 hashSigAlgoSz = 0;
  41875. word16 idx = 0;
  41876. int haveRSA = 0;
  41877. int haveECC = 0;
  41878. (void)arg;
  41879. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41880. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41881. return 0;
  41882. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41883. hashSigAlgoSz == 0)
  41884. return 0;
  41885. for (idx = 0; idx < suiteSz; idx += 2) {
  41886. WOLFSSL_CIPHERSUITE_INFO info =
  41887. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41888. if (info.rsaAuth)
  41889. haveRSA = 1;
  41890. else if (info.eccAuth)
  41891. haveECC = 1;
  41892. }
  41893. if (hashSigAlgoSz > 0) {
  41894. /* sigalgs extension takes precedence over ciphersuites */
  41895. haveRSA = 0;
  41896. haveECC = 0;
  41897. }
  41898. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41899. int hashAlgo = 0;
  41900. int sigAlgo = 0;
  41901. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41902. &hashAlgo, &sigAlgo) != 0)
  41903. return 0;
  41904. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41905. haveRSA = 1;
  41906. else if (sigAlgo == ECDSAk)
  41907. haveECC = 1;
  41908. }
  41909. if (haveRSA) {
  41910. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41911. != WOLFSSL_SUCCESS)
  41912. return 0;
  41913. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41914. != WOLFSSL_SUCCESS)
  41915. return 0;
  41916. }
  41917. else if (haveECC) {
  41918. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41919. != WOLFSSL_SUCCESS)
  41920. return 0;
  41921. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41922. != WOLFSSL_SUCCESS)
  41923. return 0;
  41924. }
  41925. return 1;
  41926. }
  41927. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41928. {
  41929. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41930. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41931. return TEST_SUCCESS;
  41932. }
  41933. #endif
  41934. /* Testing dynamic ciphers offered by client */
  41935. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41936. {
  41937. EXPECT_DECLS;
  41938. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41939. test_ssl_cbf func_cb_client;
  41940. test_ssl_cbf func_cb_server;
  41941. struct {
  41942. method_provider client_meth;
  41943. const char* client_ciphers;
  41944. const char* client_sigalgs;
  41945. const char* client_ca;
  41946. method_provider server_meth;
  41947. } test_params[] = {
  41948. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41949. #ifdef WOLFSSL_TLS13
  41950. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41951. {wolfTLSv1_3_client_method,
  41952. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41953. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41954. #endif
  41955. #ifdef HAVE_ECC
  41956. {wolfTLSv1_3_client_method,
  41957. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41958. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41959. #endif
  41960. #endif
  41961. #ifndef WOLFSSL_NO_TLS12
  41962. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41963. {wolfTLSv1_2_client_method,
  41964. "DHE-RSA-AES128-GCM-SHA256",
  41965. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41966. #endif
  41967. #ifdef HAVE_ECC
  41968. {wolfTLSv1_2_client_method,
  41969. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41970. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41971. #endif
  41972. #endif
  41973. #endif
  41974. };
  41975. size_t i;
  41976. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41977. if (testCount > 0) {
  41978. for (i = 0; i < testCount; i++) {
  41979. printf("\tTesting %s ciphers with %s sigalgs\n",
  41980. test_params[i].client_ciphers,
  41981. test_params[i].client_sigalgs);
  41982. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41983. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41984. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41985. test_params[i].client_ciphers;
  41986. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41987. test_params[i].client_sigalgs;
  41988. func_cb_client.method = test_params[i].client_meth;
  41989. func_cb_client.caPemFile = test_params[i].client_ca;
  41990. func_cb_client.ctx_ready =
  41991. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41992. func_cb_server.ctx_ready =
  41993. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41994. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41995. func_cb_server.method = test_params[i].server_meth;
  41996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41997. &func_cb_server, NULL), TEST_SUCCESS);
  41998. }
  41999. }
  42000. #endif
  42001. return EXPECT_RESULT();
  42002. }
  42003. static int test_wolfSSL_ciphersuite_auth(void)
  42004. {
  42005. EXPECT_DECLS;
  42006. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  42007. WOLFSSL_CIPHERSUITE_INFO info;
  42008. (void)info;
  42009. #ifndef WOLFSSL_NO_TLS12
  42010. #ifdef HAVE_CHACHA
  42011. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  42012. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  42013. ExpectIntEQ(info.rsaAuth, 1);
  42014. ExpectIntEQ(info.eccAuth, 0);
  42015. ExpectIntEQ(info.eccStatic, 0);
  42016. ExpectIntEQ(info.psk, 0);
  42017. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  42018. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  42019. ExpectIntEQ(info.rsaAuth, 0);
  42020. ExpectIntEQ(info.eccAuth, 1);
  42021. ExpectIntEQ(info.eccStatic, 0);
  42022. ExpectIntEQ(info.psk, 0);
  42023. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  42024. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  42025. ExpectIntEQ(info.rsaAuth, 0);
  42026. ExpectIntEQ(info.eccAuth, 0);
  42027. ExpectIntEQ(info.eccStatic, 0);
  42028. ExpectIntEQ(info.psk, 1);
  42029. #endif
  42030. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  42031. #ifndef NO_RSA
  42032. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42033. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  42034. ExpectIntEQ(info.rsaAuth, 1);
  42035. ExpectIntEQ(info.eccAuth, 0);
  42036. ExpectIntEQ(info.eccStatic, 0);
  42037. ExpectIntEQ(info.psk, 0);
  42038. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42039. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  42040. ExpectIntEQ(info.rsaAuth, 1);
  42041. ExpectIntEQ(info.eccAuth, 0);
  42042. ExpectIntEQ(info.eccStatic, 1);
  42043. ExpectIntEQ(info.psk, 0);
  42044. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42045. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  42046. ExpectIntEQ(info.rsaAuth, 1);
  42047. ExpectIntEQ(info.eccAuth, 0);
  42048. ExpectIntEQ(info.eccStatic, 1);
  42049. ExpectIntEQ(info.psk, 0);
  42050. #endif
  42051. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42052. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  42053. ExpectIntEQ(info.rsaAuth, 0);
  42054. ExpectIntEQ(info.eccAuth, 1);
  42055. ExpectIntEQ(info.eccStatic, 0);
  42056. ExpectIntEQ(info.psk, 0);
  42057. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  42058. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  42059. ExpectIntEQ(info.rsaAuth, 0);
  42060. ExpectIntEQ(info.eccAuth, 1);
  42061. ExpectIntEQ(info.eccStatic, 1);
  42062. ExpectIntEQ(info.psk, 0);
  42063. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  42064. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  42065. ExpectIntEQ(info.rsaAuth, 0);
  42066. ExpectIntEQ(info.eccAuth, 0);
  42067. ExpectIntEQ(info.eccStatic, 0);
  42068. ExpectIntEQ(info.psk, 1);
  42069. #endif
  42070. #endif
  42071. #ifdef WOLFSSL_TLS13
  42072. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  42073. TLS_AES_128_GCM_SHA256);
  42074. ExpectIntEQ(info.rsaAuth, 0);
  42075. ExpectIntEQ(info.eccAuth, 0);
  42076. ExpectIntEQ(info.eccStatic, 0);
  42077. ExpectIntEQ(info.psk, 0);
  42078. #endif
  42079. #endif
  42080. return EXPECT_RESULT();
  42081. }
  42082. static int test_wolfSSL_sigalg_info(void)
  42083. {
  42084. EXPECT_DECLS;
  42085. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  42086. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  42087. word16 len = 0;
  42088. word16 idx = 0;
  42089. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  42090. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  42091. for (idx = 0; idx < len; idx += 2) {
  42092. int hashAlgo = 0;
  42093. int sigAlgo = 0;
  42094. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  42095. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  42096. ExpectIntNE(hashAlgo, 0);
  42097. ExpectIntNE(sigAlgo, 0);
  42098. }
  42099. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  42100. 0xFFFFFFFF, &len);
  42101. for (idx = 0; idx < len; idx += 2) {
  42102. int hashAlgo = 0;
  42103. int sigAlgo = 0;
  42104. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  42105. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  42106. ExpectIntNE(hashAlgo, 0);
  42107. }
  42108. #endif
  42109. return EXPECT_RESULT();
  42110. }
  42111. static int test_wolfSSL_SESSION(void)
  42112. {
  42113. EXPECT_DECLS;
  42114. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42115. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42116. !defined(NO_SESSION_CACHE)
  42117. WOLFSSL* ssl = NULL;
  42118. WOLFSSL_CTX* ctx = NULL;
  42119. WOLFSSL_SESSION* sess = NULL;
  42120. WOLFSSL_SESSION* sess_copy = NULL;
  42121. #ifdef OPENSSL_EXTRA
  42122. #ifdef HAVE_EXT_CACHE
  42123. unsigned char* sessDer = NULL;
  42124. unsigned char* ptr = NULL;
  42125. int sz = 0;
  42126. #endif
  42127. const unsigned char context[] = "user app context";
  42128. unsigned int contextSz = (unsigned int)sizeof(context);
  42129. #endif
  42130. int ret = 0, err = 0;
  42131. SOCKET_T sockfd;
  42132. tcp_ready ready;
  42133. func_args server_args;
  42134. THREAD_TYPE serverThread;
  42135. char msg[80];
  42136. const char* sendGET = "GET";
  42137. /* TLS v1.3 requires session tickets */
  42138. /* CHACHA and POLY1305 required for myTicketEncCb */
  42139. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  42140. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  42141. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  42142. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  42143. #else
  42144. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42145. #endif
  42146. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  42147. WOLFSSL_FILETYPE_PEM));
  42148. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  42149. WOLFSSL_FILETYPE_PEM));
  42150. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  42151. WOLFSSL_SUCCESS);
  42152. #ifdef WOLFSSL_ENCRYPTED_KEYS
  42153. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  42154. #endif
  42155. #ifdef HAVE_SESSION_TICKET
  42156. /* Use session tickets, for ticket tests below */
  42157. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  42158. #endif
  42159. XMEMSET(&server_args, 0, sizeof(func_args));
  42160. #ifdef WOLFSSL_TIRTOS
  42161. fdOpenSession(Task_self());
  42162. #endif
  42163. StartTCP();
  42164. InitTcpReady(&ready);
  42165. #if defined(USE_WINDOWS_API)
  42166. /* use RNG to get random port if using windows */
  42167. ready.port = GetRandomPort();
  42168. #endif
  42169. server_args.signal = &ready;
  42170. start_thread(test_server_nofail, &server_args, &serverThread);
  42171. wait_tcp_ready(&server_args);
  42172. /* client connection */
  42173. ExpectNotNull(ssl = wolfSSL_new(ctx));
  42174. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  42175. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  42176. #ifdef WOLFSSL_ASYNC_CRYPT
  42177. err = 0; /* Reset error */
  42178. #endif
  42179. do {
  42180. #ifdef WOLFSSL_ASYNC_CRYPT
  42181. if (err == WC_PENDING_E) {
  42182. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42183. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42184. }
  42185. #endif
  42186. ret = wolfSSL_connect(ssl);
  42187. err = wolfSSL_get_error(ssl, 0);
  42188. } while (err == WC_PENDING_E);
  42189. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  42190. #ifdef WOLFSSL_ASYNC_CRYPT
  42191. err = 0; /* Reset error */
  42192. #endif
  42193. do {
  42194. #ifdef WOLFSSL_ASYNC_CRYPT
  42195. if (err == WC_PENDING_E) {
  42196. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42197. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42198. }
  42199. #endif
  42200. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  42201. err = wolfSSL_get_error(ssl, 0);
  42202. } while (err == WC_PENDING_E);
  42203. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  42204. #ifdef WOLFSSL_ASYNC_CRYPT
  42205. err = 0; /* Reset error */
  42206. #endif
  42207. do {
  42208. #ifdef WOLFSSL_ASYNC_CRYPT
  42209. if (err == WC_PENDING_E) {
  42210. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42211. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42212. }
  42213. #endif
  42214. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  42215. err = wolfSSL_get_error(ssl, 0);
  42216. } while (err == WC_PENDING_E);
  42217. ExpectIntEQ(ret, 23);
  42218. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  42219. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  42220. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  42221. #ifdef HAVE_EXT_CACHE
  42222. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  42223. * HAVE_EXT_CACHE we get new objects each time */
  42224. #endif
  42225. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  42226. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  42227. sess = wolfSSL_get_session(ssl);
  42228. #ifdef OPENSSL_EXTRA
  42229. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  42230. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  42231. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  42232. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  42233. #ifdef HAVE_SESSION_TICKET
  42234. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  42235. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  42236. SESSION_TICKET_HINT_DEFAULT);
  42237. #else
  42238. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  42239. #endif
  42240. #else
  42241. (void)sess;
  42242. #endif /* OPENSSL_EXTRA */
  42243. /* Retain copy of the session for later testing */
  42244. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  42245. wolfSSL_shutdown(ssl);
  42246. wolfSSL_free(ssl); ssl = NULL;
  42247. CloseSocket(sockfd);
  42248. join_thread(serverThread);
  42249. FreeTcpReady(&ready);
  42250. #ifdef WOLFSSL_TIRTOS
  42251. fdOpenSession(Task_self());
  42252. #endif
  42253. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  42254. {
  42255. X509 *x509 = NULL;
  42256. char buf[30];
  42257. int bufSz = 0;
  42258. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  42259. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  42260. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  42261. sizeof(buf))), 0);
  42262. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  42263. if (bufSz == 7) {
  42264. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  42265. }
  42266. if (bufSz == 16) {
  42267. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  42268. }
  42269. }
  42270. #endif
  42271. #ifdef HAVE_EXT_CACHE
  42272. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  42273. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  42274. sess_copy = NULL;
  42275. #endif
  42276. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  42277. /* get session from DER and update the timeout */
  42278. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  42279. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  42280. wolfSSL_SESSION_free(sess); sess = NULL;
  42281. sess = NULL;
  42282. ptr = sessDer;
  42283. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  42284. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  42285. (const unsigned char**)&ptr, sz));
  42286. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  42287. sessDer = NULL;
  42288. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  42289. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  42290. #endif
  42291. /* successful set session test */
  42292. ExpectNotNull(ssl = wolfSSL_new(ctx));
  42293. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  42294. #ifdef HAVE_SESSION_TICKET
  42295. /* Test set/get session ticket */
  42296. {
  42297. const char* ticket = "This is a session ticket";
  42298. char buf[64] = {0};
  42299. word32 bufSz = (word32)sizeof(buf);
  42300. ExpectIntEQ(SSL_SUCCESS,
  42301. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  42302. (word32)XSTRLEN(ticket)));
  42303. ExpectIntEQ(SSL_SUCCESS,
  42304. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  42305. ExpectStrEQ(ticket, buf);
  42306. }
  42307. #endif
  42308. #ifdef OPENSSL_EXTRA
  42309. /* session timeout case */
  42310. /* make the session to be expired */
  42311. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  42312. XSLEEP_MS(1200);
  42313. /* SSL_set_session should reject specified session but return success
  42314. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  42315. */
  42316. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42317. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  42318. #else
  42319. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  42320. #endif
  42321. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  42322. #ifdef WOLFSSL_SESSION_ID_CTX
  42323. /* fail case with miss match session context IDs (use compatibility API) */
  42324. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  42325. SSL_SUCCESS);
  42326. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  42327. wolfSSL_free(ssl); ssl = NULL;
  42328. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  42329. SSL_FAILURE);
  42330. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  42331. SSL_SUCCESS);
  42332. ExpectNotNull(ssl = wolfSSL_new(ctx));
  42333. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  42334. #endif
  42335. #endif /* OPENSSL_EXTRA */
  42336. wolfSSL_free(ssl);
  42337. wolfSSL_SESSION_free(sess);
  42338. wolfSSL_CTX_free(ctx);
  42339. #endif
  42340. return EXPECT_RESULT();
  42341. }
  42342. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42343. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42344. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  42345. !defined(WOLFSSL_NO_TLS12)
  42346. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  42347. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  42348. {
  42349. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  42350. /* returns previous timeout value */
  42351. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  42352. #else
  42353. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  42354. #endif
  42355. }
  42356. /* set the session to timeout in a second */
  42357. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  42358. {
  42359. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  42360. }
  42361. /* store the client side session from the first successful connection */
  42362. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  42363. {
  42364. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  42365. NULL); /* ref count 1 */
  42366. }
  42367. /* wait till session is expired then set it in the WOLFSSL struct for use */
  42368. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  42369. {
  42370. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  42371. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  42372. WOLFSSL_SUCCESS);
  42373. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  42374. }
  42375. /* set expired session in the WOLFSSL struct for use */
  42376. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  42377. {
  42378. XSLEEP_MS(1200); /* wait a second for session to expire */
  42379. /* set the expired session, call to set session fails but continuing on
  42380. after failure should be handled here */
  42381. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42382. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  42383. WOLFSSL_SUCCESS);
  42384. #else
  42385. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  42386. WOLFSSL_SUCCESS);
  42387. #endif
  42388. }
  42389. /* check that the expired session was not reused */
  42390. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  42391. {
  42392. /* since the session has expired it should not have been reused */
  42393. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  42394. }
  42395. #endif
  42396. static int test_wolfSSL_SESSION_expire_downgrade(void)
  42397. {
  42398. EXPECT_DECLS;
  42399. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42400. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42401. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  42402. !defined(WOLFSSL_NO_TLS12)
  42403. WOLFSSL_CTX* ctx = NULL;
  42404. callback_functions server_cbf, client_cbf;
  42405. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  42406. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  42407. /* force server side to use TLS 1.2 */
  42408. server_cbf.ctx = ctx;
  42409. server_cbf.method = wolfTLSv1_2_server_method;
  42410. client_cbf.method = wolfSSLv23_client_method;
  42411. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  42412. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  42413. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  42414. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  42415. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  42416. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  42417. /* set the previously created session and wait till expired */
  42418. server_cbf.ctx = ctx;
  42419. client_cbf.method = wolfSSLv23_client_method;
  42420. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  42421. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  42422. client_cbf.on_result =
  42423. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  42424. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  42425. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  42426. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  42427. /* set the previously created expired session */
  42428. server_cbf.ctx = ctx;
  42429. client_cbf.method = wolfSSLv23_client_method;
  42430. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  42431. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  42432. client_cbf.on_result =
  42433. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  42434. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  42435. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  42436. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  42437. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  42438. wolfSSL_CTX_free(ctx);
  42439. #endif
  42440. return EXPECT_RESULT();
  42441. }
  42442. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  42443. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  42444. static int clientSessRemCountMalloc = 0;
  42445. static int serverSessRemCountMalloc = 0;
  42446. static int clientSessRemCountFree = 0;
  42447. static int serverSessRemCountFree = 0;
  42448. static WOLFSSL_CTX* serverSessCtx = NULL;
  42449. static WOLFSSL_SESSION* serverSess = NULL;
  42450. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42451. !defined(NO_SESSION_CACHE_REF)
  42452. static WOLFSSL_CTX* clientSessCtx = NULL;
  42453. static WOLFSSL_SESSION* clientSess = NULL;
  42454. #endif
  42455. static int serverSessRemIdx = 3;
  42456. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  42457. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  42458. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  42459. {
  42460. int* side;
  42461. (void)ctx;
  42462. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  42463. if (side != NULL) {
  42464. if (*side == WOLFSSL_CLIENT_END)
  42465. clientSessRemCountFree++;
  42466. else
  42467. serverSessRemCountFree++;
  42468. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  42469. }
  42470. }
  42471. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  42472. {
  42473. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  42474. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  42475. !defined(NO_SESSION_CACHE_REF)
  42476. {
  42477. EXPECT_DECLS;
  42478. /* Allow downgrade, set min version, and disable TLS 1.3.
  42479. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  42480. * reference to the session cache. But with WOLFSSL_TLS13 and without
  42481. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  42482. * the session in the cache. In this case we need to downgrade to
  42483. * previous versions to just use the legacy session ID field. */
  42484. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  42485. SSL_SUCCESS);
  42486. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  42487. SSL_SUCCESS);
  42488. return EXPECT_RESULT();
  42489. }
  42490. #else
  42491. return TEST_SUCCESS;
  42492. #endif
  42493. }
  42494. static int SessRemSslSetupCb(WOLFSSL* ssl)
  42495. {
  42496. EXPECT_DECLS;
  42497. int* side;
  42498. if (SSL_is_server(ssl)) {
  42499. side = &sessRemCtx_Server;
  42500. serverSessRemCountMalloc++;
  42501. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  42502. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  42503. SSL_SUCCESS);
  42504. }
  42505. else {
  42506. side = &sessRemCtx_Client;
  42507. clientSessRemCountMalloc++;
  42508. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42509. !defined(NO_SESSION_CACHE_REF)
  42510. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  42511. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  42512. SSL_SUCCESS);
  42513. #endif
  42514. }
  42515. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  42516. serverSessRemIdx, side), SSL_SUCCESS);
  42517. return EXPECT_RESULT();
  42518. }
  42519. #endif
  42520. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  42521. {
  42522. EXPECT_DECLS;
  42523. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  42524. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  42525. /* Check that the remove callback gets called for external data in a
  42526. * session object */
  42527. test_ssl_cbf func_cb;
  42528. XMEMSET(&func_cb, 0, sizeof(func_cb));
  42529. func_cb.ctx_ready = SessRemCtxSetupCb;
  42530. func_cb.on_result = SessRemSslSetupCb;
  42531. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  42532. NULL), TEST_SUCCESS);
  42533. /* Both should have been allocated */
  42534. ExpectIntEQ(clientSessRemCountMalloc, 1);
  42535. ExpectIntEQ(serverSessRemCountMalloc, 1);
  42536. /* This should not be called yet. Session wasn't evicted from cache yet. */
  42537. ExpectIntEQ(clientSessRemCountFree, 0);
  42538. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42539. !defined(NO_SESSION_CACHE_REF)
  42540. /* Force a cache lookup */
  42541. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  42542. /* Force a cache update */
  42543. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  42544. /* This should set the timeout to 0 and call the remove callback from within
  42545. * the session cache. */
  42546. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  42547. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  42548. ExpectIntEQ(clientSessRemCountFree, 1);
  42549. #endif
  42550. /* Server session is in the cache so ex_data isn't free'd with the SSL
  42551. * object */
  42552. ExpectIntEQ(serverSessRemCountFree, 0);
  42553. /* Force a cache lookup */
  42554. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  42555. /* Force a cache update */
  42556. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  42557. /* This should set the timeout to 0 and call the remove callback from within
  42558. * the session cache. */
  42559. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  42560. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  42561. ExpectIntEQ(serverSessRemCountFree, 1);
  42562. /* Need to free the references that we kept */
  42563. SSL_CTX_free(serverSessCtx);
  42564. SSL_SESSION_free(serverSess);
  42565. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42566. !defined(NO_SESSION_CACHE_REF)
  42567. SSL_CTX_free(clientSessCtx);
  42568. SSL_SESSION_free(clientSess);
  42569. #endif
  42570. #endif
  42571. return EXPECT_RESULT();
  42572. }
  42573. static int test_wolfSSL_ticket_keys(void)
  42574. {
  42575. EXPECT_DECLS;
  42576. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  42577. !defined(NO_WOLFSSL_SERVER)
  42578. WOLFSSL_CTX* ctx = NULL;
  42579. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  42580. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42581. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  42582. WOLFSSL_FAILURE);
  42583. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  42584. WOLFSSL_FAILURE);
  42585. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  42586. WOLFSSL_FAILURE);
  42587. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  42588. WOLFSSL_FAILURE);
  42589. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  42590. WOLFSSL_FAILURE);
  42591. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  42592. WOLFSSL_FAILURE);
  42593. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  42594. WOLFSSL_FAILURE);
  42595. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  42596. WOLFSSL_FAILURE);
  42597. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  42598. WOLFSSL_FAILURE);
  42599. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  42600. WOLFSSL_FAILURE);
  42601. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  42602. WOLFSSL_FAILURE);
  42603. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  42604. WOLFSSL_FAILURE);
  42605. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  42606. WOLFSSL_FAILURE);
  42607. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  42608. WOLFSSL_FAILURE);
  42609. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  42610. WOLFSSL_SUCCESS);
  42611. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  42612. WOLFSSL_SUCCESS);
  42613. wolfSSL_CTX_free(ctx);
  42614. #endif
  42615. return EXPECT_RESULT();
  42616. }
  42617. #ifndef NO_BIO
  42618. static int test_wolfSSL_d2i_PUBKEY(void)
  42619. {
  42620. EXPECT_DECLS;
  42621. #if defined(OPENSSL_EXTRA)
  42622. BIO* bio = NULL;
  42623. EVP_PKEY* pkey = NULL;
  42624. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42625. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  42626. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  42627. /* RSA PUBKEY test */
  42628. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  42629. sizeof_client_keypub_der_2048), 0);
  42630. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42631. EVP_PKEY_free(pkey);
  42632. pkey = NULL;
  42633. #endif
  42634. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  42635. /* ECC PUBKEY test */
  42636. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  42637. sizeof_ecc_clikeypub_der_256), 0);
  42638. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42639. EVP_PKEY_free(pkey);
  42640. pkey = NULL;
  42641. #endif
  42642. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  42643. /* DSA PUBKEY test */
  42644. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  42645. sizeof_dsa_pub_key_der_2048), 0);
  42646. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42647. EVP_PKEY_free(pkey);
  42648. pkey = NULL;
  42649. #endif
  42650. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  42651. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  42652. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  42653. (HAVE_FIPS_VERSION > 2))
  42654. /* DH PUBKEY test */
  42655. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  42656. sizeof_dh_pub_key_der_2048), 0);
  42657. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42658. EVP_PKEY_free(pkey);
  42659. pkey = NULL;
  42660. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42661. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  42662. BIO_free(bio);
  42663. (void)pkey;
  42664. #endif
  42665. return EXPECT_RESULT();
  42666. }
  42667. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  42668. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  42669. {
  42670. EXPECT_DECLS;
  42671. BIO* bio = NULL;
  42672. EVP_PKEY* pkey = NULL;
  42673. WOLFSSL_CTX* ctx = NULL;
  42674. #if defined(WOLFSSL_KEY_GEN)
  42675. unsigned char buff[4096];
  42676. unsigned char* bufPtr = buff;
  42677. #endif
  42678. /* test creating new EVP_PKEY with bad arg */
  42679. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  42680. /* test loading RSA key using BIO */
  42681. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42682. {
  42683. XFILE file = XBADFILE;
  42684. const char* fname = "./certs/server-key.der";
  42685. size_t sz = 0;
  42686. byte* buf = NULL;
  42687. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42688. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42689. ExpectTrue((sz = XFTELL(file)) != 0);
  42690. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42691. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42692. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42693. if (file != XBADFILE) {
  42694. XFCLOSE(file);
  42695. }
  42696. /* Test using BIO new mem and loading DER private key */
  42697. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42698. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42699. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42700. BIO_free(bio);
  42701. bio = NULL;
  42702. EVP_PKEY_free(pkey);
  42703. pkey = NULL;
  42704. }
  42705. #endif
  42706. /* test loading ECC key using BIO */
  42707. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  42708. {
  42709. XFILE file = XBADFILE;
  42710. const char* fname = "./certs/ecc-key.der";
  42711. size_t sz = 0;
  42712. byte* buf = NULL;
  42713. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42714. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42715. ExpectTrue((sz = XFTELL(file)) != 0);
  42716. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42717. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42718. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42719. if (file != XBADFILE)
  42720. XFCLOSE(file);
  42721. /* Test using BIO new mem and loading DER private key */
  42722. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42723. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42724. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42725. BIO_free(bio);
  42726. bio = NULL;
  42727. EVP_PKEY_free(pkey);
  42728. pkey = NULL;
  42729. }
  42730. #endif
  42731. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42732. #ifndef NO_WOLFSSL_SERVER
  42733. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42734. #else
  42735. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  42736. #endif
  42737. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42738. {
  42739. RSA* rsa = NULL;
  42740. /* Tests bad parameters */
  42741. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  42742. /* RSA not set yet, expecting to fail*/
  42743. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  42744. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  42745. /* set RSA using bio*/
  42746. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  42747. sizeof_client_key_der_2048), 0);
  42748. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  42749. ExpectNotNull(rsa);
  42750. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  42751. /* i2d RSAprivate key tests */
  42752. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  42753. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  42754. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42755. sizeof_client_key_der_2048);
  42756. bufPtr -= sizeof_client_key_der_2048;
  42757. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42758. sizeof_client_key_der_2048), 0);
  42759. bufPtr = NULL;
  42760. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42761. sizeof_client_key_der_2048);
  42762. ExpectNotNull(bufPtr);
  42763. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42764. sizeof_client_key_der_2048), 0);
  42765. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42766. RSA_free(rsa);
  42767. rsa = RSA_new();
  42768. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  42769. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  42770. RSA_free(rsa);
  42771. }
  42772. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  42773. SSL_CTX_free(ctx);
  42774. ctx = NULL;
  42775. BIO_free(bio);
  42776. bio = NULL;
  42777. return EXPECT_RESULT();
  42778. }
  42779. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  42780. #endif /* !NO_BIO */
  42781. static int test_wolfSSL_sk_GENERAL_NAME(void)
  42782. {
  42783. EXPECT_DECLS;
  42784. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42785. !defined(NO_RSA)
  42786. X509* x509 = NULL;
  42787. GENERAL_NAME* gn = NULL;
  42788. unsigned char buf[4096];
  42789. const unsigned char* bufPt = NULL;
  42790. int bytes = 0;
  42791. int i;
  42792. int j;
  42793. XFILE f = XBADFILE;
  42794. STACK_OF(GENERAL_NAME)* sk = NULL;
  42795. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42796. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42797. if (f != XBADFILE)
  42798. XFCLOSE(f);
  42799. for (j = 0; j < 2; ++j) {
  42800. bufPt = buf;
  42801. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42802. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42803. NID_subject_alt_name, NULL, NULL));
  42804. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  42805. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  42806. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  42807. if (gn != NULL) {
  42808. switch (gn->type) {
  42809. case GEN_DNS:
  42810. fprintf(stderr, "found type GEN_DNS\n");
  42811. break;
  42812. case GEN_EMAIL:
  42813. fprintf(stderr, "found type GEN_EMAIL\n");
  42814. break;
  42815. case GEN_URI:
  42816. fprintf(stderr, "found type GEN_URI\n");
  42817. break;
  42818. }
  42819. }
  42820. }
  42821. X509_free(x509);
  42822. x509 = NULL;
  42823. if (j == 0) {
  42824. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42825. }
  42826. else {
  42827. /*
  42828. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42829. * it was supposed to. This is a regression test for that bug.
  42830. */
  42831. GENERAL_NAMES_free(sk);
  42832. }
  42833. sk = NULL;
  42834. }
  42835. #endif
  42836. return EXPECT_RESULT();
  42837. }
  42838. static int test_wolfSSL_GENERAL_NAME_print(void)
  42839. {
  42840. EXPECT_DECLS;
  42841. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42842. X509* x509 = NULL;
  42843. GENERAL_NAME* gn = NULL;
  42844. unsigned char buf[4096];
  42845. const unsigned char* bufPt = NULL;
  42846. int bytes;
  42847. XFILE f = XBADFILE;
  42848. STACK_OF(GENERAL_NAME)* sk = NULL;
  42849. BIO* out = NULL;
  42850. unsigned char outbuf[128];
  42851. X509_EXTENSION* ext = NULL;
  42852. AUTHORITY_INFO_ACCESS* aia = NULL;
  42853. ACCESS_DESCRIPTION* ad = NULL;
  42854. ASN1_IA5STRING *dnsname = NULL;
  42855. const unsigned char v4Addr[] = {192,168,53,1};
  42856. const unsigned char v6Addr[] =
  42857. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42858. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42859. const unsigned char email[] =
  42860. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42861. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42862. const char* dnsStr = "DNS:example.com";
  42863. const char* uriStr = "URI:http://127.0.0.1:22220";
  42864. const char* v4addStr = "IP Address:192.168.53.1";
  42865. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42866. const char* emailStr = "email:info@wolfssl.com";
  42867. const char* othrStr = "othername:<unsupported>";
  42868. const char* x400Str = "X400Name:<unsupported>";
  42869. const char* ediStr = "EdiPartyName:<unsupported>";
  42870. /* BIO to output */
  42871. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42872. /* test for NULL param */
  42873. gn = NULL;
  42874. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42875. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42876. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42877. /* test for GEN_DNS */
  42878. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42879. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42880. if (f != XBADFILE) {
  42881. XFCLOSE(f);
  42882. f = XBADFILE;
  42883. }
  42884. bufPt = buf;
  42885. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42886. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42887. NID_subject_alt_name, NULL, NULL));
  42888. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42889. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42890. XMEMSET(outbuf, 0, sizeof(outbuf));
  42891. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42892. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42893. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42894. gn = NULL;
  42895. sk = NULL;
  42896. X509_free(x509);
  42897. x509 = NULL;
  42898. /* Lets test for setting as well. */
  42899. ExpectNotNull(gn = GENERAL_NAME_new());
  42900. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42901. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42902. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42903. dnsname = NULL;
  42904. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42905. XMEMSET(outbuf, 0, sizeof(outbuf));
  42906. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42907. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42908. GENERAL_NAME_free(gn);
  42909. /* test for GEN_URI */
  42910. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42911. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42912. if (f != XBADFILE) {
  42913. XFCLOSE(f);
  42914. f = XBADFILE;
  42915. }
  42916. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42917. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42918. ext));
  42919. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42920. if (ad != NULL) {
  42921. gn = ad->location;
  42922. }
  42923. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42924. gn = NULL;
  42925. XMEMSET(outbuf,0,sizeof(outbuf));
  42926. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42927. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42928. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42929. aia = NULL;
  42930. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42931. ExpectNotNull(aia);
  42932. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42933. aia = NULL;
  42934. X509_free(x509);
  42935. x509 = NULL;
  42936. /* test for GEN_IPADD */
  42937. /* ip v4 address */
  42938. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42939. if (gn != NULL) {
  42940. gn->type = GEN_IPADD;
  42941. if (gn->d.iPAddress != NULL) {
  42942. gn->d.iPAddress->length = sizeof(v4Addr);
  42943. }
  42944. }
  42945. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42946. sizeof(v4Addr)), 1);
  42947. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42948. XMEMSET(outbuf,0,sizeof(outbuf));
  42949. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42950. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42951. GENERAL_NAME_free(gn);
  42952. gn = NULL;
  42953. /* ip v6 address */
  42954. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42955. if (gn != NULL) {
  42956. gn->type = GEN_IPADD;
  42957. if (gn->d.iPAddress != NULL) {
  42958. gn->d.iPAddress->length = sizeof(v6Addr);
  42959. }
  42960. }
  42961. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42962. sizeof(v6Addr)), 1);
  42963. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42964. XMEMSET(outbuf,0,sizeof(outbuf));
  42965. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42966. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42967. GENERAL_NAME_free(gn);
  42968. gn = NULL;
  42969. /* test for GEN_EMAIL */
  42970. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42971. if (gn != NULL) {
  42972. gn->type = GEN_EMAIL;
  42973. if (gn->d.rfc822Name != NULL) {
  42974. gn->d.rfc822Name->length = sizeof(email);
  42975. }
  42976. }
  42977. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42978. 1);
  42979. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42980. XMEMSET(outbuf,0,sizeof(outbuf));
  42981. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42982. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42983. GENERAL_NAME_free(gn);
  42984. gn = NULL;
  42985. /* test for GEN_OTHERNAME */
  42986. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42987. if (gn != NULL) {
  42988. gn->type = GEN_OTHERNAME;
  42989. }
  42990. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42991. XMEMSET(outbuf,0,sizeof(outbuf));
  42992. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42993. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42994. GENERAL_NAME_free(gn);
  42995. gn = NULL;
  42996. /* test for GEN_X400 */
  42997. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42998. if (gn != NULL) {
  42999. gn->type = GEN_X400;
  43000. }
  43001. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  43002. XMEMSET(outbuf,0,sizeof(outbuf));
  43003. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  43004. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  43005. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  43006. if (gn != NULL) {
  43007. gn->type = GEN_IA5;
  43008. }
  43009. GENERAL_NAME_free(gn);
  43010. gn = NULL;
  43011. /* test for GEN_EDIPARTY */
  43012. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  43013. if (gn != NULL) {
  43014. gn->type = GEN_EDIPARTY;
  43015. }
  43016. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  43017. XMEMSET(outbuf,0,sizeof(outbuf));
  43018. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  43019. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  43020. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  43021. if (gn != NULL) {
  43022. gn->type = GEN_IA5;
  43023. }
  43024. GENERAL_NAME_free(gn);
  43025. gn = NULL;
  43026. BIO_free(out);
  43027. #endif /* OPENSSL_ALL */
  43028. return EXPECT_RESULT();
  43029. }
  43030. static int test_wolfSSL_sk_DIST_POINT(void)
  43031. {
  43032. EXPECT_DECLS;
  43033. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43034. !defined(NO_RSA)
  43035. X509* x509 = NULL;
  43036. unsigned char buf[4096];
  43037. const unsigned char* bufPt;
  43038. int bytes = 0;
  43039. int i = 0;
  43040. int j = 0;
  43041. XFILE f = XBADFILE;
  43042. DIST_POINT* dp = NULL;
  43043. DIST_POINT_NAME* dpn = NULL;
  43044. GENERAL_NAME* gn = NULL;
  43045. ASN1_IA5STRING* uri = NULL;
  43046. STACK_OF(DIST_POINT)* dps = NULL;
  43047. STACK_OF(GENERAL_NAME)* gns = NULL;
  43048. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  43049. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  43050. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  43051. if (f != XBADFILE)
  43052. XFCLOSE(f);
  43053. bufPt = buf;
  43054. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  43055. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  43056. NID_crl_distribution_points, NULL, NULL));
  43057. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  43058. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  43059. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  43060. ExpectNotNull(dpn = dp->distpoint);
  43061. /* this should be type 0, fullname */
  43062. ExpectIntEQ(dpn->type, 0);
  43063. ExpectNotNull(gns = dp->distpoint->name.fullname);
  43064. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  43065. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  43066. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  43067. ExpectIntEQ(gn->type, GEN_URI);
  43068. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  43069. ExpectNotNull(uri->data);
  43070. ExpectIntGT(uri->length, 0);
  43071. }
  43072. }
  43073. X509_free(x509);
  43074. CRL_DIST_POINTS_free(dps);
  43075. #endif
  43076. return EXPECT_RESULT();
  43077. }
  43078. static int test_wolfSSL_verify_mode(void)
  43079. {
  43080. EXPECT_DECLS;
  43081. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  43082. WOLFSSL* ssl = NULL;
  43083. WOLFSSL_CTX* ctx = NULL;
  43084. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43085. ExpectNotNull(ssl = SSL_new(ctx));
  43086. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  43087. SSL_free(ssl);
  43088. ssl = NULL;
  43089. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43090. ExpectNotNull(ssl = SSL_new(ctx));
  43091. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  43092. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  43093. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  43094. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  43095. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  43096. SSL_free(ssl);
  43097. ssl = NULL;
  43098. wolfSSL_CTX_set_verify(ctx,
  43099. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  43100. ExpectNotNull(ssl = SSL_new(ctx));
  43101. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  43102. ExpectIntEQ(SSL_get_verify_mode(ssl),
  43103. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43104. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  43105. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  43106. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43107. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  43108. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  43109. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  43110. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  43111. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43112. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  43113. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  43114. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43115. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  43116. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  43117. #endif
  43118. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  43119. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  43120. SSL_free(ssl);
  43121. SSL_CTX_free(ctx);
  43122. #endif
  43123. return EXPECT_RESULT();
  43124. }
  43125. static int test_wolfSSL_verify_depth(void)
  43126. {
  43127. EXPECT_DECLS;
  43128. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  43129. WOLFSSL* ssl = NULL;
  43130. WOLFSSL_CTX* ctx = NULL;
  43131. long depth;
  43132. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43133. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  43134. ExpectNotNull(ssl = SSL_new(ctx));
  43135. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  43136. SSL_free(ssl);
  43137. ssl = NULL;
  43138. SSL_CTX_set_verify_depth(ctx, -1);
  43139. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  43140. SSL_CTX_set_verify_depth(ctx, 2);
  43141. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  43142. ExpectNotNull(ssl = SSL_new(ctx));
  43143. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  43144. SSL_free(ssl);
  43145. SSL_CTX_free(ctx);
  43146. #endif
  43147. return EXPECT_RESULT();
  43148. }
  43149. static int test_wolfSSL_verify_result(void)
  43150. {
  43151. EXPECT_DECLS;
  43152. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  43153. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  43154. WOLFSSL* ssl = NULL;
  43155. WOLFSSL_CTX* ctx = NULL;
  43156. long result = 0xDEADBEEF;
  43157. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  43158. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43159. ExpectNotNull(ssl = SSL_new(ctx));
  43160. wolfSSL_set_verify_result(ssl, result);
  43161. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  43162. SSL_free(ssl);
  43163. SSL_CTX_free(ctx);
  43164. #endif
  43165. return EXPECT_RESULT();
  43166. }
  43167. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  43168. static void sslMsgCb(int w, int version, int type, const void* buf,
  43169. size_t sz, SSL* ssl, void* arg)
  43170. {
  43171. int i;
  43172. unsigned char* pt = (unsigned char*)buf;
  43173. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  43174. (w)?"Writing":"Reading", (int)sz, version, type);
  43175. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  43176. fprintf(stderr, "\n");
  43177. (void)ssl;
  43178. (void)arg;
  43179. }
  43180. #endif /* OPENSSL_EXTRA */
  43181. static int test_wolfSSL_msg_callback(void)
  43182. {
  43183. EXPECT_DECLS;
  43184. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  43185. WOLFSSL* ssl = NULL;
  43186. WOLFSSL_CTX* ctx = NULL;
  43187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43188. ExpectNotNull(ssl = SSL_new(ctx));
  43189. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  43190. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  43191. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  43192. SSL_free(ssl);
  43193. SSL_CTX_free(ctx);
  43194. #endif
  43195. return EXPECT_RESULT();
  43196. }
  43197. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  43198. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  43199. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  43200. static void binary_dump(void *ptr, int size)
  43201. {
  43202. #ifdef WOLFSSL_EVP_PRINT
  43203. int i = 0;
  43204. unsigned char *p = (unsigned char *) ptr;
  43205. fprintf(stderr, "{");
  43206. while ((p != NULL) && (i < size)) {
  43207. if ((i % 8) == 0) {
  43208. fprintf(stderr, "\n");
  43209. fprintf(stderr, " ");
  43210. }
  43211. fprintf(stderr, "0x%02x, ", p[i]);
  43212. i++;
  43213. }
  43214. fprintf(stderr, "\n};\n");
  43215. #else
  43216. (void) ptr;
  43217. (void) size;
  43218. #endif
  43219. }
  43220. static int last_val = 0x0f;
  43221. static int check_result(unsigned char *data, int len)
  43222. {
  43223. int i;
  43224. for ( ; len; ) {
  43225. last_val = (last_val + 1) % 16;
  43226. for (i = 0; i < 16; len--, i++, data++)
  43227. if (*data != last_val) {
  43228. return -1;
  43229. }
  43230. }
  43231. return 0;
  43232. }
  43233. static int r_offset;
  43234. static int w_offset;
  43235. static void init_offset(void)
  43236. {
  43237. r_offset = 0;
  43238. w_offset = 0;
  43239. }
  43240. static void get_record(unsigned char *data, unsigned char *buf, int len)
  43241. {
  43242. XMEMCPY(buf, data+r_offset, len);
  43243. r_offset += len;
  43244. }
  43245. static void set_record(unsigned char *data, unsigned char *buf, int len)
  43246. {
  43247. XMEMCPY(data+w_offset, buf, len);
  43248. w_offset += len;
  43249. }
  43250. static void set_plain(unsigned char *plain, int rec)
  43251. {
  43252. int i, j;
  43253. unsigned char *p = plain;
  43254. #define BLOCKSZ 16
  43255. for (i=0; i<(rec/BLOCKSZ); i++) {
  43256. for (j=0; j<BLOCKSZ; j++)
  43257. *p++ = (i % 16);
  43258. }
  43259. }
  43260. #endif
  43261. static int test_wolfSSL_EVP_Cipher_extra(void)
  43262. {
  43263. EXPECT_DECLS;
  43264. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  43265. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  43266. /* aes128-cbc, keylen=16, ivlen=16 */
  43267. byte aes128_cbc_key[] = {
  43268. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  43269. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  43270. };
  43271. byte aes128_cbc_iv[] = {
  43272. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  43273. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  43274. };
  43275. /* teset data size table */
  43276. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  43277. int test_drive2[] = {8, 3, 8, 512, 0};
  43278. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  43279. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  43280. int test_drive_len[100];
  43281. int ret = 0;
  43282. EVP_CIPHER_CTX *evp = NULL;
  43283. int ilen = 0;
  43284. int klen = 0;
  43285. int i, j;
  43286. const EVP_CIPHER *type;
  43287. byte *iv;
  43288. byte *key;
  43289. int ivlen;
  43290. int keylen;
  43291. #define RECORDS 16
  43292. #define BUFFSZ 512
  43293. byte plain [BUFFSZ * RECORDS];
  43294. byte cipher[BUFFSZ * RECORDS];
  43295. byte inb[BUFFSZ];
  43296. byte outb[BUFFSZ+16];
  43297. int outl = 0;
  43298. int inl;
  43299. iv = aes128_cbc_iv;
  43300. ivlen = sizeof(aes128_cbc_iv);
  43301. key = aes128_cbc_key;
  43302. keylen = sizeof(aes128_cbc_key);
  43303. type = EVP_aes_128_cbc();
  43304. set_plain(plain, BUFFSZ * RECORDS);
  43305. SSL_library_init();
  43306. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  43307. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  43308. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  43309. klen = EVP_CIPHER_CTX_key_length(evp);
  43310. if (klen > 0 && keylen != klen) {
  43311. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  43312. }
  43313. ilen = EVP_CIPHER_CTX_iv_length(evp);
  43314. if (ilen > 0 && ivlen != ilen) {
  43315. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  43316. }
  43317. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  43318. for (j = 0; j<RECORDS; j++)
  43319. {
  43320. inl = BUFFSZ;
  43321. get_record(plain, inb, inl);
  43322. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  43323. set_record(cipher, outb, outl);
  43324. }
  43325. for (i = 0; test_drive[i]; i++) {
  43326. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  43327. init_offset();
  43328. test_drive_len[i] = 0;
  43329. for (j = 0; test_drive[i][j]; j++)
  43330. {
  43331. inl = test_drive[i][j];
  43332. test_drive_len[i] += inl;
  43333. get_record(plain, inb, inl);
  43334. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  43335. 0);
  43336. /* output to cipher buffer, so that following Dec test can detect
  43337. if any error */
  43338. set_record(cipher, outb, outl);
  43339. }
  43340. EVP_CipherFinal(evp, outb, &outl);
  43341. if (outl > 0)
  43342. set_record(cipher, outb, outl);
  43343. }
  43344. for (i = 0; test_drive[i]; i++) {
  43345. last_val = 0x0f;
  43346. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  43347. init_offset();
  43348. for (j = 0; test_drive[i][j]; j++) {
  43349. inl = test_drive[i][j];
  43350. get_record(cipher, inb, inl);
  43351. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  43352. 0);
  43353. binary_dump(outb, outl);
  43354. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  43355. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  43356. }
  43357. ret = EVP_CipherFinal(evp, outb, &outl);
  43358. binary_dump(outb, outl);
  43359. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  43360. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  43361. ExpectTrue(ret);
  43362. }
  43363. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  43364. EVP_CIPHER_CTX_free(evp);
  43365. evp = NULL;
  43366. /* Do an extra test to verify correct behavior with empty input. */
  43367. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  43368. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  43369. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  43370. klen = EVP_CIPHER_CTX_key_length(evp);
  43371. if (klen > 0 && keylen != klen) {
  43372. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  43373. }
  43374. ilen = EVP_CIPHER_CTX_iv_length(evp);
  43375. if (ilen > 0 && ivlen != ilen) {
  43376. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  43377. }
  43378. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  43379. /* outl should be set to 0 after passing NULL, 0 for input args. */
  43380. outl = -1;
  43381. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  43382. ExpectIntEQ(outl, 0);
  43383. EVP_CIPHER_CTX_free(evp);
  43384. #endif /* test_EVP_Cipher */
  43385. return EXPECT_RESULT();
  43386. }
  43387. static int test_wolfSSL_PEM_read_DHparams(void)
  43388. {
  43389. EXPECT_DECLS;
  43390. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  43391. !defined(NO_FILESYSTEM)
  43392. DH* dh = NULL;
  43393. XFILE fp = XBADFILE;
  43394. unsigned char derOut[300];
  43395. unsigned char* derOutBuf = derOut;
  43396. int derOutSz = 0;
  43397. unsigned char derExpected[300];
  43398. int derExpectedSz = 0;
  43399. XMEMSET(derOut, 0, sizeof(derOut));
  43400. XMEMSET(derExpected, 0, sizeof(derExpected));
  43401. /* open DH param file, read into DH struct */
  43402. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  43403. /* bad args */
  43404. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  43405. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  43406. /* good args */
  43407. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  43408. if (fp != XBADFILE) {
  43409. XFCLOSE(fp);
  43410. fp = XBADFILE;
  43411. }
  43412. /* read in certs/dh2048.der for comparison against exported params */
  43413. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  43414. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  43415. fp), 0);
  43416. if (fp != XBADFILE) {
  43417. XFCLOSE(fp);
  43418. fp = XBADFILE;
  43419. }
  43420. /* export DH back to DER and compare */
  43421. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  43422. ExpectIntEQ(derOutSz, derExpectedSz);
  43423. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  43424. DH_free(dh);
  43425. dh = NULL;
  43426. /* Test parsing with X9.42 header */
  43427. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  43428. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  43429. if (fp != XBADFILE)
  43430. XFCLOSE(fp);
  43431. DH_free(dh);
  43432. dh = NULL;
  43433. #endif
  43434. return EXPECT_RESULT();
  43435. }
  43436. static int test_wolfSSL_X509_get_serialNumber(void)
  43437. {
  43438. EXPECT_DECLS;
  43439. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  43440. ASN1_INTEGER* a = NULL;
  43441. BIGNUM* bn = NULL;
  43442. X509* x509 = NULL;
  43443. char *serialHex = NULL;
  43444. byte serial[3];
  43445. int serialSz;
  43446. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43447. SSL_FILETYPE_PEM));
  43448. ExpectNotNull(a = X509_get_serialNumber(x509));
  43449. /* check on value of ASN1 Integer */
  43450. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  43451. a = NULL;
  43452. /* test setting serial number and then retrieving it */
  43453. ExpectNotNull(a = ASN1_INTEGER_new());
  43454. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  43455. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  43456. serialSz = sizeof(serial);
  43457. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  43458. WOLFSSL_SUCCESS);
  43459. ExpectIntEQ(serialSz, 1);
  43460. ExpectIntEQ(serial[0], 3);
  43461. ASN1_INTEGER_free(a);
  43462. a = NULL;
  43463. /* test setting serial number with 0's in it */
  43464. serial[0] = 0x01;
  43465. serial[1] = 0x00;
  43466. serial[2] = 0x02;
  43467. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  43468. if (a != NULL) {
  43469. a->data[0] = ASN_INTEGER;
  43470. a->data[1] = sizeof(serial);
  43471. XMEMCPY(&a->data[2], serial, sizeof(serial));
  43472. a->length = sizeof(serial) + 2;
  43473. }
  43474. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  43475. XMEMSET(serial, 0, sizeof(serial));
  43476. serialSz = sizeof(serial);
  43477. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  43478. WOLFSSL_SUCCESS);
  43479. ExpectIntEQ(serialSz, 3);
  43480. ExpectIntEQ(serial[0], 0x01);
  43481. ExpectIntEQ(serial[1], 0x00);
  43482. ExpectIntEQ(serial[2], 0x02);
  43483. ASN1_INTEGER_free(a);
  43484. a = NULL;
  43485. X509_free(x509); /* free's a */
  43486. ExpectNotNull(serialHex = BN_bn2hex(bn));
  43487. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  43488. ExpectStrEQ(serialHex, "01");
  43489. #else
  43490. ExpectStrEQ(serialHex, "1");
  43491. #endif
  43492. OPENSSL_free(serialHex);
  43493. ExpectIntEQ(BN_get_word(bn), 1);
  43494. BN_free(bn);
  43495. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  43496. ExpectNotNull(a = ASN1_INTEGER_new());
  43497. if (a != NULL) {
  43498. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  43499. DYNAMIC_TYPE_OPENSSL));
  43500. a->isDynamic = 1;
  43501. ASN1_INTEGER_free(a);
  43502. }
  43503. #endif
  43504. return EXPECT_RESULT();
  43505. }
  43506. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  43507. {
  43508. EXPECT_DECLS;
  43509. #if defined(OPENSSL_EXTRA)
  43510. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  43511. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  43512. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  43513. #endif
  43514. return EXPECT_RESULT();
  43515. }
  43516. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  43517. {
  43518. EXPECT_DECLS;
  43519. #if defined(OPENSSL_EXTRA)
  43520. #define MAX_HEXSTR_BUFSZ 9
  43521. #define NUM_CASES 5
  43522. struct Output {
  43523. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  43524. long ret;
  43525. };
  43526. int i;
  43527. int j;
  43528. const char* inputs[NUM_CASES] = {
  43529. "aabcd1357e",
  43530. "01:12:23:34:a5:b6:c7:d8:e9",
  43531. ":01:02",
  43532. "012",
  43533. ":ab:ac:d"
  43534. };
  43535. struct Output expectedOutputs[NUM_CASES] = {
  43536. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  43537. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  43538. {{0x01, 0x02}, 2},
  43539. {{0x00}, 0},
  43540. {{0x00}, 0}
  43541. };
  43542. long len = 0;
  43543. unsigned char* returnedBuf = NULL;
  43544. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  43545. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  43546. if (returnedBuf == NULL) {
  43547. ExpectIntEQ(expectedOutputs[i].ret, 0);
  43548. continue;
  43549. }
  43550. ExpectIntEQ(expectedOutputs[i].ret, len);
  43551. for (j = 0; j < len; ++j) {
  43552. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  43553. }
  43554. OPENSSL_free(returnedBuf);
  43555. returnedBuf = NULL;
  43556. }
  43557. #endif
  43558. return EXPECT_RESULT();
  43559. }
  43560. static int test_wolfSSL_X509_CA_num(void)
  43561. {
  43562. EXPECT_DECLS;
  43563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  43564. defined(HAVE_ECC) && !defined(NO_RSA)
  43565. WOLFSSL_X509_STORE *store = NULL;
  43566. WOLFSSL_X509 *x509_1 = NULL;
  43567. WOLFSSL_X509 *x509_2 = NULL;
  43568. int ca_num = 0;
  43569. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  43570. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43571. WOLFSSL_FILETYPE_PEM));
  43572. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  43573. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  43574. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  43575. WOLFSSL_FILETYPE_PEM));
  43576. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  43577. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  43578. wolfSSL_X509_free(x509_1);
  43579. wolfSSL_X509_free(x509_2);
  43580. wolfSSL_X509_STORE_free(store);
  43581. #endif
  43582. return EXPECT_RESULT();
  43583. }
  43584. static int test_wolfSSL_X509_check_ca(void)
  43585. {
  43586. EXPECT_DECLS;
  43587. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  43588. WOLFSSL_X509 *x509 = NULL;
  43589. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43590. WOLFSSL_FILETYPE_PEM));
  43591. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  43592. wolfSSL_X509_free(x509);
  43593. #endif
  43594. return EXPECT_RESULT();
  43595. }
  43596. static int test_wolfSSL_X509_check_ip_asc(void)
  43597. {
  43598. EXPECT_DECLS;
  43599. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  43600. WOLFSSL_X509 *x509 = NULL;
  43601. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  43602. WOLFSSL_FILETYPE_PEM));
  43603. #if 0
  43604. /* TODO: add cert gen for testing positive case */
  43605. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  43606. #endif
  43607. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  43608. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  43609. wolfSSL_X509_free(x509);
  43610. #endif
  43611. return EXPECT_RESULT();
  43612. }
  43613. static int test_wolfSSL_make_cert(void)
  43614. {
  43615. EXPECT_DECLS;
  43616. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  43617. defined(WOLFSSL_CERT_EXT)
  43618. int ret = 0;
  43619. Cert cert;
  43620. CertName name;
  43621. RsaKey key;
  43622. WC_RNG rng;
  43623. byte der[FOURK_BUF];
  43624. word32 idx = 0;
  43625. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  43626. #ifdef OPENSSL_EXTRA
  43627. const unsigned char* pt = NULL;
  43628. int certSz = 0;
  43629. X509* x509 = NULL;
  43630. X509_NAME* x509name = NULL;
  43631. X509_NAME_ENTRY* entry = NULL;
  43632. ASN1_STRING* entryValue = NULL;
  43633. #endif
  43634. XMEMSET(&name, 0, sizeof(CertName));
  43635. /* set up cert name */
  43636. XMEMCPY(name.country, "US", sizeof("US"));
  43637. name.countryEnc = CTC_PRINTABLE;
  43638. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  43639. name.stateEnc = CTC_UTF8;
  43640. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  43641. name.localityEnc = CTC_UTF8;
  43642. XMEMCPY(name.sur, "Test", sizeof("Test"));
  43643. name.surEnc = CTC_UTF8;
  43644. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  43645. name.orgEnc = CTC_UTF8;
  43646. XMEMCPY(name.unit, "Development", sizeof("Development"));
  43647. name.unitEnc = CTC_UTF8;
  43648. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  43649. name.commonNameEnc = CTC_UTF8;
  43650. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  43651. name.serialDevEnc = CTC_PRINTABLE;
  43652. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  43653. name.userIdEnc = CTC_PRINTABLE;
  43654. #ifdef WOLFSSL_MULTI_ATTRIB
  43655. #if CTC_MAX_ATTRIB > 2
  43656. {
  43657. NameAttrib* n;
  43658. n = &name.name[0];
  43659. n->id = ASN_DOMAIN_COMPONENT;
  43660. n->type = CTC_UTF8;
  43661. n->sz = sizeof("com");
  43662. XMEMCPY(n->value, "com", sizeof("com"));
  43663. n = &name.name[1];
  43664. n->id = ASN_DOMAIN_COMPONENT;
  43665. n->type = CTC_UTF8;
  43666. n->sz = sizeof("wolfssl");
  43667. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  43668. }
  43669. #endif
  43670. #endif /* WOLFSSL_MULTI_ATTRIB */
  43671. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  43672. #ifndef HAVE_FIPS
  43673. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  43674. #else
  43675. ExpectIntEQ(wc_InitRng(&rng), 0);
  43676. #endif
  43677. /* load test RSA key */
  43678. idx = 0;
  43679. #if defined(USE_CERT_BUFFERS_1024)
  43680. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  43681. sizeof_server_key_der_1024), 0);
  43682. #elif defined(USE_CERT_BUFFERS_2048)
  43683. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  43684. sizeof_server_key_der_2048), 0);
  43685. #else
  43686. /* error case, no RSA key loaded, happens later */
  43687. (void)idx;
  43688. #endif
  43689. XMEMSET(&cert, 0 , sizeof(Cert));
  43690. ExpectIntEQ(wc_InitCert(&cert), 0);
  43691. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  43692. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  43693. cert.serialSz = (int)sizeof(mySerial);
  43694. cert.isCA = 1;
  43695. #ifndef NO_SHA256
  43696. cert.sigType = CTC_SHA256wRSA;
  43697. #else
  43698. cert.sigType = CTC_SHAwRSA;
  43699. #endif
  43700. /* add SKID from the Public Key */
  43701. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  43702. /* add AKID from the Public Key */
  43703. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  43704. ret = 0;
  43705. do {
  43706. #if defined(WOLFSSL_ASYNC_CRYPT)
  43707. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  43708. #endif
  43709. if (ret >= 0) {
  43710. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  43711. }
  43712. } while (ret == WC_PENDING_E);
  43713. ExpectIntGT(ret, 0);
  43714. #ifdef OPENSSL_EXTRA
  43715. /* der holds a certificate with DC's now check X509 parsing of it */
  43716. certSz = ret;
  43717. pt = der;
  43718. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  43719. ExpectNotNull(x509name = X509_get_subject_name(x509));
  43720. #ifdef WOLFSSL_MULTI_ATTRIB
  43721. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43722. -1)), 5);
  43723. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43724. (int)idx)), 6);
  43725. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43726. (int)idx)), -1);
  43727. #endif /* WOLFSSL_MULTI_ATTRIB */
  43728. /* compare DN at index 0 */
  43729. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  43730. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43731. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  43732. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  43733. #ifndef WOLFSSL_MULTI_ATTRIB
  43734. /* compare Serial Number */
  43735. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  43736. -1)), 7);
  43737. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43738. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43739. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  43740. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  43741. #endif
  43742. #ifdef WOLFSSL_MULTI_ATTRIB
  43743. /* get first and second DC and compare result */
  43744. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43745. -1)), 5);
  43746. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  43747. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43748. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  43749. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43750. (int)idx)), 6);
  43751. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  43752. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43753. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  43754. #endif /* WOLFSSL_MULTI_ATTRIB */
  43755. /* try invalid index locations for regression test and sanity check */
  43756. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  43757. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  43758. X509_free(x509);
  43759. #endif /* OPENSSL_EXTRA */
  43760. wc_FreeRsaKey(&key);
  43761. wc_FreeRng(&rng);
  43762. #endif
  43763. return EXPECT_RESULT();
  43764. }
  43765. static int test_wolfSSL_X509_get_version(void)
  43766. {
  43767. EXPECT_DECLS;
  43768. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43769. WOLFSSL_X509 *x509 = NULL;
  43770. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43771. WOLFSSL_FILETYPE_PEM));
  43772. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  43773. wolfSSL_X509_free(x509);
  43774. #endif
  43775. return EXPECT_RESULT();
  43776. }
  43777. #if defined(OPENSSL_ALL)
  43778. static int test_wolfSSL_sk_CIPHER_description(void)
  43779. {
  43780. EXPECT_DECLS;
  43781. #if !defined(NO_RSA)
  43782. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43783. int i;
  43784. int numCiphers = 0;
  43785. const SSL_METHOD *method = NULL;
  43786. const SSL_CIPHER *cipher = NULL;
  43787. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43788. SSL_CTX *ctx = NULL;
  43789. SSL *ssl = NULL;
  43790. char buf[256];
  43791. char test_str[9] = "0000000";
  43792. const char badStr[] = "unknown";
  43793. const char certPath[] = "./certs/client-cert.pem";
  43794. XMEMSET(buf, 0, sizeof(buf));
  43795. ExpectNotNull(method = TLSv1_2_client_method());
  43796. ExpectNotNull(ctx = SSL_CTX_new(method));
  43797. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43798. SSL_CTX_set_verify_depth(ctx, 4);
  43799. SSL_CTX_set_options(ctx, flags);
  43800. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43801. WOLFSSL_SUCCESS);
  43802. ExpectNotNull(ssl = SSL_new(ctx));
  43803. /* SSL_get_ciphers returns a stack of all configured ciphers
  43804. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  43805. */
  43806. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43807. /* loop through the amount of supportedCiphers */
  43808. numCiphers = sk_num(supportedCiphers);
  43809. for (i = 0; i < numCiphers; ++i) {
  43810. int j;
  43811. /* sk_value increments "sk->data.cipher->cipherOffset".
  43812. * wolfSSL_sk_CIPHER_description sets the description for
  43813. * the cipher based on the provided offset.
  43814. */
  43815. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43816. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43817. }
  43818. /* Search cipher description string for "unknown" descriptor */
  43819. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43820. int k = 0;
  43821. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43822. test_str[k] = badStr[k];
  43823. j++;
  43824. k++;
  43825. }
  43826. }
  43827. /* Fail if test_str == badStr == "unknown" */
  43828. ExpectStrNE(test_str,badStr);
  43829. }
  43830. SSL_free(ssl);
  43831. SSL_CTX_free(ctx);
  43832. #endif
  43833. return EXPECT_RESULT();
  43834. }
  43835. static int test_wolfSSL_get_ciphers_compat(void)
  43836. {
  43837. EXPECT_DECLS;
  43838. #if !defined(NO_RSA)
  43839. const SSL_METHOD *method = NULL;
  43840. const char certPath[] = "./certs/client-cert.pem";
  43841. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43842. SSL_CTX *ctx = NULL;
  43843. WOLFSSL *ssl = NULL;
  43844. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43845. ExpectNotNull(method = SSLv23_client_method());
  43846. ExpectNotNull(ctx = SSL_CTX_new(method));
  43847. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43848. SSL_CTX_set_verify_depth(ctx, 4);
  43849. SSL_CTX_set_options(ctx, flags);
  43850. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43851. WOLFSSL_SUCCESS);
  43852. ExpectNotNull(ssl = SSL_new(ctx));
  43853. /* Test Bad NULL input */
  43854. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43855. /* Test for Good input */
  43856. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43857. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43858. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43859. SSL_free(ssl);
  43860. SSL_CTX_free(ctx);
  43861. #endif
  43862. return EXPECT_RESULT();
  43863. }
  43864. static int test_wolfSSL_X509_PUBKEY_get(void)
  43865. {
  43866. EXPECT_DECLS;
  43867. WOLFSSL_X509_PUBKEY pubkey;
  43868. WOLFSSL_X509_PUBKEY* key;
  43869. WOLFSSL_EVP_PKEY evpkey ;
  43870. WOLFSSL_EVP_PKEY* evpPkey;
  43871. WOLFSSL_EVP_PKEY* retEvpPkey;
  43872. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43873. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43874. key = &pubkey;
  43875. evpPkey = &evpkey;
  43876. evpPkey->type = WOLFSSL_SUCCESS;
  43877. key->pkey = evpPkey;
  43878. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43879. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43880. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43881. key->pkey = NULL;
  43882. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43883. return EXPECT_RESULT();
  43884. }
  43885. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43886. {
  43887. EXPECT_DECLS;
  43888. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43889. DSA *dsa = NULL;
  43890. DSA *setDsa = NULL;
  43891. EVP_PKEY *pkey = NULL;
  43892. EVP_PKEY *set1Pkey = NULL;
  43893. SHA_CTX sha;
  43894. byte signature[DSA_SIG_SIZE];
  43895. byte hash[WC_SHA_DIGEST_SIZE];
  43896. word32 bytes;
  43897. int answer;
  43898. #ifdef USE_CERT_BUFFERS_1024
  43899. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43900. int dsaKeySz = sizeof_dsa_key_der_1024;
  43901. byte tmp[ONEK_BUF];
  43902. XMEMSET(tmp, 0, sizeof(tmp));
  43903. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43904. bytes = dsaKeySz;
  43905. #elif defined(USE_CERT_BUFFERS_2048)
  43906. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43907. int dsaKeySz = sizeof_dsa_key_der_2048;
  43908. byte tmp[TWOK_BUF];
  43909. XMEMSET(tmp, 0, sizeof(tmp));
  43910. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43911. bytes = (word32)dsaKeySz;
  43912. #else
  43913. byte tmp[TWOK_BUF];
  43914. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43915. int dsaKeySz;
  43916. XFILE fp = XBADFILE;
  43917. XMEMSET(tmp, 0, sizeof(tmp));
  43918. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43919. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43920. if (fp != XBADFILE)
  43921. XFCLOSE(fp);
  43922. #endif /* END USE_CERT_BUFFERS_1024 */
  43923. /* Create hash to later Sign and Verify */
  43924. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43925. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43926. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43927. /* Initialize pkey with der format dsa key */
  43928. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43929. (long)dsaKeySz));
  43930. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43931. /* Should Fail: NULL argument */
  43932. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43933. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43934. /* Should Pass: Initialized pkey argument */
  43935. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43936. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43937. #ifdef USE_CERT_BUFFERS_1024
  43938. ExpectIntEQ(DSA_bits(dsa), 1024);
  43939. #else
  43940. ExpectIntEQ(DSA_bits(dsa), 2048);
  43941. #endif
  43942. /* Sign */
  43943. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43944. /* Verify. */
  43945. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43946. WOLFSSL_SUCCESS);
  43947. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43948. /* Should Fail: set1Pkey not initialized */
  43949. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43950. /* Initialize set1Pkey */
  43951. set1Pkey = EVP_PKEY_new();
  43952. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43953. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43954. WOLFSSL_SUCCESS);
  43955. /* Should Pass: set dsa into set1Pkey */
  43956. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43957. DSA_free(dsa);
  43958. DSA_free(setDsa);
  43959. EVP_PKEY_free(pkey);
  43960. EVP_PKEY_free(set1Pkey);
  43961. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43962. return EXPECT_RESULT();
  43963. } /* END test_EVP_PKEY_set1_get1_DSA */
  43964. static int test_wolfSSL_DSA_generate_parameters(void)
  43965. {
  43966. EXPECT_DECLS;
  43967. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43968. !defined(HAVE_FIPS)
  43969. DSA *dsa = NULL;
  43970. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43971. NULL));
  43972. DSA_free(dsa);
  43973. #endif
  43974. return EXPECT_RESULT();
  43975. }
  43976. static int test_wolfSSL_DSA_SIG(void)
  43977. {
  43978. EXPECT_DECLS;
  43979. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43980. !defined(HAVE_FIPS)
  43981. DSA *dsa = NULL;
  43982. DSA *dsa2 = NULL;
  43983. DSA_SIG *sig = NULL;
  43984. const BIGNUM *p = NULL;
  43985. const BIGNUM *q = NULL;
  43986. const BIGNUM *g = NULL;
  43987. const BIGNUM *pub = NULL;
  43988. const BIGNUM *priv = NULL;
  43989. BIGNUM *dup_p = NULL;
  43990. BIGNUM *dup_q = NULL;
  43991. BIGNUM *dup_g = NULL;
  43992. BIGNUM *dup_pub = NULL;
  43993. BIGNUM *dup_priv = NULL;
  43994. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43995. ExpectNotNull(dsa = DSA_new());
  43996. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43997. NULL), 1);
  43998. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43999. DSA_get0_pqg(dsa, &p, &q, &g);
  44000. DSA_get0_key(dsa, &pub, &priv);
  44001. ExpectNotNull(dup_p = BN_dup(p));
  44002. ExpectNotNull(dup_q = BN_dup(q));
  44003. ExpectNotNull(dup_g = BN_dup(g));
  44004. ExpectNotNull(dup_pub = BN_dup(pub));
  44005. ExpectNotNull(dup_priv = BN_dup(priv));
  44006. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  44007. ExpectNotNull(dsa2 = DSA_new());
  44008. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  44009. if (EXPECT_FAIL()) {
  44010. BN_free(dup_p);
  44011. BN_free(dup_q);
  44012. BN_free(dup_g);
  44013. }
  44014. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  44015. if (EXPECT_FAIL()) {
  44016. BN_free(dup_pub);
  44017. BN_free(dup_priv);
  44018. }
  44019. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  44020. DSA_free(dsa);
  44021. DSA_free(dsa2);
  44022. DSA_SIG_free(sig);
  44023. #endif
  44024. return EXPECT_RESULT();
  44025. }
  44026. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  44027. {
  44028. EXPECT_DECLS;
  44029. #ifdef HAVE_ECC
  44030. WOLFSSL_EC_KEY* ecKey = NULL;
  44031. WOLFSSL_EC_KEY* ecGet1 = NULL;
  44032. EVP_PKEY* pkey = NULL;
  44033. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44034. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44035. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  44036. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  44037. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  44038. /* Should fail since ecKey is empty */
  44039. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  44040. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44041. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44042. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  44043. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  44044. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  44045. wolfSSL_EC_KEY_free(ecKey);
  44046. wolfSSL_EC_KEY_free(ecGet1);
  44047. EVP_PKEY_free(pkey);
  44048. #endif /* HAVE_ECC */
  44049. return EXPECT_RESULT();
  44050. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  44051. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  44052. {
  44053. EXPECT_DECLS;
  44054. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  44055. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44056. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44057. DH *dh = NULL;
  44058. DH *setDh = NULL;
  44059. EVP_PKEY *pkey = NULL;
  44060. XFILE f = XBADFILE;
  44061. unsigned char buf[4096];
  44062. const unsigned char* pt = buf;
  44063. const char* dh2048 = "./certs/dh2048.der";
  44064. long len = 0;
  44065. int code = -1;
  44066. XMEMSET(buf, 0, sizeof(buf));
  44067. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44068. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44069. if (f != XBADFILE)
  44070. XFCLOSE(f);
  44071. /* Load dh2048.der into DH with internal format */
  44072. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  44073. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44074. ExpectIntEQ(code, 0);
  44075. code = -1;
  44076. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44077. /* Set DH into PKEY */
  44078. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44079. /* Get DH from PKEY */
  44080. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  44081. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  44082. ExpectIntEQ(code, 0);
  44083. EVP_PKEY_free(pkey);
  44084. DH_free(setDh);
  44085. setDh = NULL;
  44086. DH_free(dh);
  44087. dh = NULL;
  44088. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  44089. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44090. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  44091. return EXPECT_RESULT();
  44092. } /* END test_EVP_PKEY_set1_get1_DH */
  44093. static int test_wolfSSL_CTX_ctrl(void)
  44094. {
  44095. EXPECT_DECLS;
  44096. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44097. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44098. char caFile[] = "./certs/client-ca.pem";
  44099. char clientFile[] = "./certs/client-cert.pem";
  44100. SSL_CTX* ctx = NULL;
  44101. X509* x509 = NULL;
  44102. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  44103. byte buf[6000];
  44104. char file[] = "./certs/dsaparams.pem";
  44105. XFILE f = XBADFILE;
  44106. int bytes = 0;
  44107. BIO* bio = NULL;
  44108. DSA* dsa = NULL;
  44109. DH* dh = NULL;
  44110. #endif
  44111. #ifdef HAVE_ECC
  44112. WOLFSSL_EC_KEY* ecKey = NULL;
  44113. #endif
  44114. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44115. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  44116. WOLFSSL_FILETYPE_PEM));
  44117. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  44118. if (EXPECT_FAIL()) {
  44119. wolfSSL_X509_free(x509);
  44120. }
  44121. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  44122. WOLFSSL_FILETYPE_PEM));
  44123. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  44124. /* Initialize DH */
  44125. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  44126. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  44127. if (f != XBADFILE)
  44128. XFCLOSE(f);
  44129. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  44130. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  44131. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  44132. #endif
  44133. #ifdef HAVE_ECC
  44134. /* Initialize WOLFSSL_EC_KEY */
  44135. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44136. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44137. #endif
  44138. /* additional test of getting EVP_PKEY key size from X509
  44139. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  44140. * allowed with user RSA */
  44141. {
  44142. EVP_PKEY* pkey = NULL;
  44143. #if defined(HAVE_ECC)
  44144. X509* ecX509 = NULL;
  44145. #endif /* HAVE_ECC */
  44146. ExpectNotNull(pkey = X509_get_pubkey(x509));
  44147. /* current RSA key is 2048 bit (256 bytes) */
  44148. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  44149. EVP_PKEY_free(pkey);
  44150. pkey = NULL;
  44151. #if defined(HAVE_ECC)
  44152. #if defined(USE_CERT_BUFFERS_256)
  44153. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  44154. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  44155. SSL_FILETYPE_ASN1));
  44156. #else
  44157. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  44158. cliEccCertFile, SSL_FILETYPE_PEM));
  44159. #endif
  44160. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  44161. /* current ECC key is 256 bit (32 bytes) */
  44162. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  44163. X509_free(ecX509);
  44164. EVP_PKEY_free(pkey);
  44165. #endif /* HAVE_ECC */
  44166. }
  44167. /* Tests should fail with passed in NULL pointer */
  44168. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  44169. SSL_FAILURE);
  44170. #if !defined(NO_DH) && !defined(NO_DSA)
  44171. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  44172. SSL_FAILURE);
  44173. #endif
  44174. #ifdef HAVE_ECC
  44175. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  44176. SSL_FAILURE);
  44177. #endif
  44178. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  44179. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  44180. */
  44181. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  44182. SSL_SUCCESS);
  44183. if (EXPECT_FAIL()) {
  44184. wolfSSL_X509_free(x509);
  44185. }
  44186. /* Test with SSL_CTRL_OPTIONS
  44187. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  44188. */
  44189. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  44190. NULL) == SSL_OP_NO_TLSv1);
  44191. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  44192. /* Test with SSL_CTRL_SET_TMP_DH
  44193. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  44194. */
  44195. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  44196. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  44197. SSL_SUCCESS);
  44198. #endif
  44199. /* Test with SSL_CTRL_SET_TMP_ECDH
  44200. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  44201. */
  44202. #ifdef HAVE_ECC
  44203. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  44204. SSL_SUCCESS);
  44205. #endif
  44206. #ifdef WOLFSSL_ENCRYPTED_KEYS
  44207. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  44208. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  44209. #endif
  44210. /* Test for min/max proto */
  44211. #ifndef WOLFSSL_NO_TLS12
  44212. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  44213. 0, NULL), SSL_SUCCESS);
  44214. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  44215. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  44216. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  44217. #endif
  44218. #ifdef WOLFSSL_TLS13
  44219. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  44220. 0, NULL), SSL_SUCCESS);
  44221. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  44222. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  44223. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  44224. #ifndef WOLFSSL_NO_TLS12
  44225. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  44226. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  44227. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  44228. #endif
  44229. #endif
  44230. /* Cleanup and Pass */
  44231. #if !defined(NO_DH) && !defined(NO_DSA)
  44232. #ifndef NO_BIO
  44233. BIO_free(bio);
  44234. DSA_free(dsa);
  44235. DH_free(dh);
  44236. dh = NULL;
  44237. #endif
  44238. #endif
  44239. #ifdef HAVE_ECC
  44240. wolfSSL_EC_KEY_free(ecKey);
  44241. #endif
  44242. SSL_CTX_free(ctx);
  44243. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  44244. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  44245. return EXPECT_RESULT();
  44246. }
  44247. static int test_wolfSSL_EVP_PKEY_assign(void)
  44248. {
  44249. EXPECT_DECLS;
  44250. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  44251. int type;
  44252. WOLFSSL_EVP_PKEY* pkey = NULL;
  44253. #ifndef NO_RSA
  44254. WOLFSSL_RSA* rsa = NULL;
  44255. #endif
  44256. #ifndef NO_DSA
  44257. WOLFSSL_DSA* dsa = NULL;
  44258. #endif
  44259. #ifdef HAVE_ECC
  44260. WOLFSSL_EC_KEY* ecKey = NULL;
  44261. #endif
  44262. #ifndef NO_RSA
  44263. type = EVP_PKEY_RSA;
  44264. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44265. ExpectNotNull(rsa = wolfSSL_RSA_new());
  44266. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  44267. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  44268. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  44269. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  44270. if (EXPECT_FAIL()) {
  44271. wolfSSL_RSA_free(rsa);
  44272. }
  44273. wolfSSL_EVP_PKEY_free(pkey);
  44274. pkey = NULL;
  44275. #endif /* NO_RSA */
  44276. #ifndef NO_DSA
  44277. type = EVP_PKEY_DSA;
  44278. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44279. ExpectNotNull(dsa = wolfSSL_DSA_new());
  44280. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  44281. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  44282. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  44283. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  44284. if (EXPECT_FAIL()) {
  44285. wolfSSL_DSA_free(dsa);
  44286. }
  44287. wolfSSL_EVP_PKEY_free(pkey);
  44288. pkey = NULL;
  44289. #endif /* NO_DSA */
  44290. #ifdef HAVE_ECC
  44291. type = EVP_PKEY_EC;
  44292. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44293. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44294. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  44295. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  44296. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  44297. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  44298. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44299. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  44300. if (EXPECT_FAIL()) {
  44301. wolfSSL_EC_KEY_free(ecKey);
  44302. }
  44303. wolfSSL_EVP_PKEY_free(pkey);
  44304. pkey = NULL;
  44305. #endif /* HAVE_ECC */
  44306. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  44307. return EXPECT_RESULT();
  44308. }
  44309. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  44310. {
  44311. EXPECT_DECLS;
  44312. #if !defined(NO_DH) && \
  44313. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44314. XFILE f = XBADFILE;
  44315. unsigned char buf[4096];
  44316. const unsigned char* pt = buf;
  44317. const char* params1 = "./certs/dh2048.der";
  44318. long len = 0;
  44319. WOLFSSL_DH* dh = NULL;
  44320. WOLFSSL_EVP_PKEY* pkey = NULL;
  44321. XMEMSET(buf, 0, sizeof(buf));
  44322. /* Load DH parameters DER. */
  44323. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  44324. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44325. if (f != XBADFILE)
  44326. XFCLOSE(f);
  44327. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  44328. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  44329. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44330. /* Bad cases */
  44331. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  44332. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  44333. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  44334. /* Good case */
  44335. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  44336. if (EXPECT_FAIL()) {
  44337. wolfSSL_DH_free(dh);
  44338. }
  44339. EVP_PKEY_free(pkey);
  44340. #endif
  44341. return EXPECT_RESULT();
  44342. }
  44343. static int test_wolfSSL_EVP_PKEY_base_id(void)
  44344. {
  44345. EXPECT_DECLS;
  44346. WOLFSSL_EVP_PKEY* pkey = NULL;
  44347. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44348. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  44349. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  44350. EVP_PKEY_free(pkey);
  44351. return EXPECT_RESULT();
  44352. }
  44353. static int test_wolfSSL_EVP_PKEY_id(void)
  44354. {
  44355. EXPECT_DECLS;
  44356. WOLFSSL_EVP_PKEY* pkey = NULL;
  44357. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44358. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  44359. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  44360. EVP_PKEY_free(pkey);
  44361. return EXPECT_RESULT();
  44362. }
  44363. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  44364. {
  44365. EXPECT_DECLS;
  44366. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  44367. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  44368. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  44369. ECC_MIN_KEY_SZ <= 256)
  44370. EVP_PKEY_CTX* ctx = NULL;
  44371. EVP_PKEY* pkey = NULL;
  44372. /* Test error conditions. */
  44373. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  44374. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  44375. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  44376. #ifndef NO_RSA
  44377. EVP_PKEY_CTX_free(ctx);
  44378. /* Parameter generation for RSA not supported yet. */
  44379. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  44380. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  44381. #endif
  44382. #ifdef HAVE_ECC
  44383. EVP_PKEY_CTX_free(ctx);
  44384. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  44385. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  44386. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  44387. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  44388. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  44389. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  44390. WOLFSSL_SUCCESS);
  44391. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  44392. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  44393. #endif
  44394. EVP_PKEY_CTX_free(ctx);
  44395. EVP_PKEY_free(pkey);
  44396. #endif
  44397. return EXPECT_RESULT();
  44398. }
  44399. static int test_wolfSSL_EVP_PKEY_keygen(void)
  44400. {
  44401. EXPECT_DECLS;
  44402. WOLFSSL_EVP_PKEY* pkey = NULL;
  44403. EVP_PKEY_CTX* ctx = NULL;
  44404. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  44405. WOLFSSL_EVP_PKEY* params = NULL;
  44406. DH* dh = NULL;
  44407. const BIGNUM* pubkey = NULL;
  44408. const BIGNUM* privkey = NULL;
  44409. ASN1_INTEGER* asn1int = NULL;
  44410. unsigned int length = 0;
  44411. byte* derBuffer = NULL;
  44412. #endif
  44413. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44414. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44415. /* Bad cases */
  44416. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  44417. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  44418. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  44419. /* Good case */
  44420. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  44421. EVP_PKEY_CTX_free(ctx);
  44422. ctx = NULL;
  44423. EVP_PKEY_free(pkey);
  44424. pkey = NULL;
  44425. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  44426. /* Test DH keygen */
  44427. {
  44428. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  44429. ExpectNotNull(dh = DH_get_2048_256());
  44430. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  44431. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  44432. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  44433. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  44434. DH_free(dh);
  44435. dh = NULL;
  44436. EVP_PKEY_CTX_free(ctx);
  44437. EVP_PKEY_free(params);
  44438. /* try exporting generated key to DER, to verify */
  44439. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  44440. DH_get0_key(dh, &pubkey, &privkey);
  44441. ExpectNotNull(pubkey);
  44442. ExpectNotNull(privkey);
  44443. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  44444. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  44445. ASN1_INTEGER_free(asn1int);
  44446. DH_free(dh);
  44447. dh = NULL;
  44448. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44449. EVP_PKEY_free(pkey);
  44450. }
  44451. #endif
  44452. return EXPECT_RESULT();
  44453. }
  44454. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  44455. {
  44456. EXPECT_DECLS;
  44457. WOLFSSL_EVP_PKEY* pkey = NULL;
  44458. EVP_PKEY_CTX *ctx = NULL;
  44459. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44460. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44461. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  44462. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  44463. EVP_PKEY_CTX_free(ctx);
  44464. EVP_PKEY_free(pkey);
  44465. return EXPECT_RESULT();
  44466. }
  44467. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  44468. {
  44469. EXPECT_DECLS;
  44470. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  44471. WOLFSSL_EVP_PKEY* pkey = NULL;
  44472. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44473. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  44474. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  44475. EVP_PKEY_free(pkey);
  44476. #endif
  44477. return EXPECT_RESULT();
  44478. }
  44479. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  44480. {
  44481. EXPECT_DECLS;
  44482. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  44483. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  44484. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  44485. !defined(NO_FILESYSTEM)
  44486. WOLFSSL_EVP_PKEY* params = NULL;
  44487. WOLFSSL_EVP_PKEY* copy = NULL;
  44488. DH* dh = NULL;
  44489. BIGNUM* p1;
  44490. BIGNUM* g1;
  44491. BIGNUM* q1;
  44492. BIGNUM* p2;
  44493. BIGNUM* g2;
  44494. BIGNUM* q2;
  44495. /* create DH with DH_get_2048_256 params */
  44496. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  44497. ExpectNotNull(dh = DH_get_2048_256());
  44498. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  44499. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  44500. (const BIGNUM**)&q1,
  44501. (const BIGNUM**)&g1);
  44502. DH_free(dh);
  44503. dh = NULL;
  44504. /* create DH with random generated DH params */
  44505. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  44506. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  44507. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  44508. DH_free(dh);
  44509. dh = NULL;
  44510. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  44511. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  44512. ExpectNotNull(dh->p);
  44513. ExpectNotNull(dh->g);
  44514. ExpectNotNull(dh->q);
  44515. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  44516. (const BIGNUM**)&q2,
  44517. (const BIGNUM**)&g2);
  44518. ExpectIntEQ(BN_cmp(p1, p2), 0);
  44519. ExpectIntEQ(BN_cmp(q1, q2), 0);
  44520. ExpectIntEQ(BN_cmp(g1, g2), 0);
  44521. DH_free(dh);
  44522. dh = NULL;
  44523. EVP_PKEY_free(copy);
  44524. EVP_PKEY_free(params);
  44525. #endif
  44526. return EXPECT_RESULT();
  44527. }
  44528. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  44529. {
  44530. EXPECT_DECLS;
  44531. WOLFSSL_EVP_PKEY* pkey = NULL;
  44532. EVP_PKEY_CTX* ctx = NULL;
  44533. int bits = 2048;
  44534. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44535. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44536. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  44537. WOLFSSL_SUCCESS);
  44538. EVP_PKEY_CTX_free(ctx);
  44539. EVP_PKEY_free(pkey);
  44540. return EXPECT_RESULT();
  44541. }
  44542. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  44543. {
  44544. EXPECT_DECLS;
  44545. /* This is large enough to be used for all key sizes */
  44546. byte key[AES_256_KEY_SIZE] = {0};
  44547. byte iv[AES_BLOCK_SIZE] = {0};
  44548. int i;
  44549. int nids[] = {
  44550. #ifdef HAVE_AES_CBC
  44551. NID_aes_128_cbc,
  44552. #endif
  44553. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44554. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44555. #ifdef HAVE_AESGCM
  44556. NID_aes_128_gcm,
  44557. #endif
  44558. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44559. #ifdef WOLFSSL_AES_COUNTER
  44560. NID_aes_128_ctr,
  44561. #endif
  44562. #ifndef NO_DES3
  44563. NID_des_cbc,
  44564. NID_des_ede3_cbc,
  44565. #endif
  44566. };
  44567. int iv_lengths[] = {
  44568. #ifdef HAVE_AES_CBC
  44569. AES_BLOCK_SIZE,
  44570. #endif
  44571. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44572. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44573. #ifdef HAVE_AESGCM
  44574. GCM_NONCE_MID_SZ,
  44575. #endif
  44576. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44577. #ifdef WOLFSSL_AES_COUNTER
  44578. AES_BLOCK_SIZE,
  44579. #endif
  44580. #ifndef NO_DES3
  44581. DES_BLOCK_SIZE,
  44582. DES_BLOCK_SIZE,
  44583. #endif
  44584. };
  44585. int nidsLen = (sizeof(nids)/sizeof(int));
  44586. for (i = 0; i < nidsLen; i++) {
  44587. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  44588. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  44589. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44590. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44591. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  44592. EVP_CIPHER_CTX_free(ctx);
  44593. }
  44594. return EXPECT_RESULT();
  44595. }
  44596. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  44597. {
  44598. EXPECT_DECLS;
  44599. byte key[AES_256_KEY_SIZE] = {0};
  44600. byte iv[AES_BLOCK_SIZE] = {0};
  44601. int i;
  44602. int nids[] = {
  44603. #ifdef HAVE_AES_CBC
  44604. NID_aes_128_cbc,
  44605. NID_aes_256_cbc,
  44606. #endif
  44607. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44608. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44609. #ifdef HAVE_AESGCM
  44610. NID_aes_128_gcm,
  44611. NID_aes_256_gcm,
  44612. #endif
  44613. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44614. #ifdef WOLFSSL_AES_COUNTER
  44615. NID_aes_128_ctr,
  44616. NID_aes_256_ctr,
  44617. #endif
  44618. #ifndef NO_DES3
  44619. NID_des_cbc,
  44620. NID_des_ede3_cbc,
  44621. #endif
  44622. };
  44623. int key_lengths[] = {
  44624. #ifdef HAVE_AES_CBC
  44625. AES_128_KEY_SIZE,
  44626. AES_256_KEY_SIZE,
  44627. #endif
  44628. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44629. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44630. #ifdef HAVE_AESGCM
  44631. AES_128_KEY_SIZE,
  44632. AES_256_KEY_SIZE,
  44633. #endif
  44634. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44635. #ifdef WOLFSSL_AES_COUNTER
  44636. AES_128_KEY_SIZE,
  44637. AES_256_KEY_SIZE,
  44638. #endif
  44639. #ifndef NO_DES3
  44640. DES_KEY_SIZE,
  44641. DES3_KEY_SIZE,
  44642. #endif
  44643. };
  44644. int nidsLen = (sizeof(nids)/sizeof(int));
  44645. for (i = 0; i < nidsLen; i++) {
  44646. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  44647. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  44648. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44649. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44650. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  44651. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  44652. WOLFSSL_SUCCESS);
  44653. EVP_CIPHER_CTX_free(ctx);
  44654. }
  44655. return EXPECT_RESULT();
  44656. }
  44657. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  44658. {
  44659. EXPECT_DECLS;
  44660. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  44661. int ivLen, keyLen;
  44662. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  44663. #ifdef HAVE_AESGCM
  44664. byte key[AES_128_KEY_SIZE] = {0};
  44665. byte iv[AES_BLOCK_SIZE] = {0};
  44666. const EVP_CIPHER *init = EVP_aes_128_gcm();
  44667. #else
  44668. byte key[DES3_KEY_SIZE] = {0};
  44669. byte iv[DES_BLOCK_SIZE] = {0};
  44670. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  44671. #endif
  44672. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44673. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44674. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  44675. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  44676. /* Bad cases */
  44677. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  44678. WOLFSSL_FAILURE);
  44679. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  44680. WOLFSSL_FAILURE);
  44681. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  44682. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  44683. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  44684. WOLFSSL_FAILURE);
  44685. /* Good case */
  44686. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  44687. EVP_CIPHER_CTX_free(ctx);
  44688. #endif
  44689. return EXPECT_RESULT();
  44690. }
  44691. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  44692. {
  44693. EXPECT_DECLS;
  44694. WOLFSSL_ENGINE* e = NULL;
  44695. int id = 0;
  44696. EVP_PKEY_CTX *ctx = NULL;
  44697. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  44698. EVP_PKEY_CTX_free(ctx);
  44699. return EXPECT_RESULT();
  44700. }
  44701. static int test_wolfSSL_EVP_rc4(void)
  44702. {
  44703. EXPECT_DECLS;
  44704. #if !defined(NO_RC4)
  44705. ExpectNotNull(wolfSSL_EVP_rc4());
  44706. #endif
  44707. return EXPECT_RESULT();
  44708. }
  44709. static int test_wolfSSL_EVP_enc_null(void)
  44710. {
  44711. EXPECT_DECLS;
  44712. ExpectNotNull(wolfSSL_EVP_enc_null());
  44713. return EXPECT_RESULT();
  44714. }
  44715. static int test_wolfSSL_EVP_rc2_cbc(void)
  44716. {
  44717. EXPECT_DECLS;
  44718. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  44719. ExpectNull(wolfSSL_EVP_rc2_cbc());
  44720. #endif
  44721. return EXPECT_RESULT();
  44722. }
  44723. static int test_wolfSSL_EVP_mdc2(void)
  44724. {
  44725. EXPECT_DECLS;
  44726. #if !defined(NO_WOLFSSL_STUB)
  44727. ExpectNull(wolfSSL_EVP_mdc2());
  44728. #endif
  44729. return EXPECT_RESULT();
  44730. }
  44731. static int test_wolfSSL_EVP_md4(void)
  44732. {
  44733. EXPECT_DECLS;
  44734. #if !defined(NO_MD4)
  44735. ExpectNotNull(wolfSSL_EVP_md4());
  44736. #endif
  44737. return EXPECT_RESULT();
  44738. }
  44739. static int test_wolfSSL_EVP_aes_256_gcm(void)
  44740. {
  44741. EXPECT_DECLS;
  44742. #ifdef HAVE_AESGCM
  44743. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  44744. #endif
  44745. return EXPECT_RESULT();
  44746. }
  44747. static int test_wolfSSL_EVP_aes_192_gcm(void)
  44748. {
  44749. EXPECT_DECLS;
  44750. #ifdef HAVE_AESGCM
  44751. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  44752. #endif
  44753. return EXPECT_RESULT();
  44754. }
  44755. static int test_wolfSSL_EVP_aes_256_ccm(void)
  44756. {
  44757. EXPECT_DECLS;
  44758. #ifdef HAVE_AESCCM
  44759. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  44760. #endif
  44761. return EXPECT_RESULT();
  44762. }
  44763. static int test_wolfSSL_EVP_aes_192_ccm(void)
  44764. {
  44765. EXPECT_DECLS;
  44766. #ifdef HAVE_AESCCM
  44767. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  44768. #endif
  44769. return EXPECT_RESULT();
  44770. }
  44771. static int test_wolfSSL_EVP_aes_128_ccm(void)
  44772. {
  44773. EXPECT_DECLS;
  44774. #ifdef HAVE_AESCCM
  44775. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  44776. #endif
  44777. return EXPECT_RESULT();
  44778. }
  44779. static int test_wolfSSL_EVP_ripemd160(void)
  44780. {
  44781. EXPECT_DECLS;
  44782. #if !defined(NO_WOLFSSL_STUB)
  44783. ExpectNull(wolfSSL_EVP_ripemd160());
  44784. #endif
  44785. return EXPECT_RESULT();
  44786. }
  44787. static int test_wolfSSL_EVP_get_digestbynid(void)
  44788. {
  44789. EXPECT_DECLS;
  44790. #ifndef NO_MD5
  44791. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  44792. #endif
  44793. #ifndef NO_SHA
  44794. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  44795. #endif
  44796. #ifndef NO_SHA256
  44797. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  44798. #endif
  44799. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  44800. return EXPECT_RESULT();
  44801. }
  44802. static int test_wolfSSL_EVP_MD_nid(void)
  44803. {
  44804. EXPECT_DECLS;
  44805. #ifndef NO_MD5
  44806. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  44807. #endif
  44808. #ifndef NO_SHA
  44809. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44810. #endif
  44811. #ifndef NO_SHA256
  44812. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44813. #endif
  44814. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44815. return EXPECT_RESULT();
  44816. }
  44817. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44818. {
  44819. EXPECT_DECLS;
  44820. #if defined(HAVE_ECC)
  44821. WOLFSSL_EVP_PKEY* pkey = NULL;
  44822. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44823. ExpectNotNull(pkey = EVP_PKEY_new());
  44824. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44825. EVP_PKEY_free(pkey);
  44826. #endif
  44827. return EXPECT_RESULT();
  44828. }
  44829. static int test_wolfSSL_EVP_X_STATE(void)
  44830. {
  44831. EXPECT_DECLS;
  44832. #if !defined(NO_DES3) && !defined(NO_RC4)
  44833. byte key[DES3_KEY_SIZE] = {0};
  44834. byte iv[DES_IV_SIZE] = {0};
  44835. EVP_CIPHER_CTX *ctx = NULL;
  44836. const EVP_CIPHER *init = NULL;
  44837. /* Bad test cases */
  44838. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44839. ExpectNotNull(init = EVP_des_ede3_cbc());
  44840. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44841. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44842. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44843. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44844. EVP_CIPHER_CTX_free(ctx);
  44845. ctx = NULL;
  44846. /* Good test case */
  44847. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44848. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44849. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44850. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44851. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44852. EVP_CIPHER_CTX_free(ctx);
  44853. #endif
  44854. return EXPECT_RESULT();
  44855. }
  44856. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44857. {
  44858. EXPECT_DECLS;
  44859. #if !defined(NO_DES3) && !defined(NO_RC4)
  44860. byte key[DES3_KEY_SIZE] = {0};
  44861. byte iv[DES_IV_SIZE] = {0};
  44862. EVP_CIPHER_CTX *ctx = NULL;
  44863. const EVP_CIPHER *init = NULL;
  44864. /* Bad test cases */
  44865. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44866. ExpectNotNull(init = EVP_des_ede3_cbc());
  44867. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44868. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44869. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44870. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44871. EVP_CIPHER_CTX_free(ctx);
  44872. ctx = NULL;
  44873. /* Good test case */
  44874. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44875. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44876. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44877. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44878. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44879. EVP_CIPHER_CTX_free(ctx);
  44880. #endif
  44881. return EXPECT_RESULT();
  44882. }
  44883. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44884. {
  44885. EXPECT_DECLS;
  44886. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44887. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44888. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44889. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44890. #ifdef HAVE_AES_CBC
  44891. #ifdef WOLFSSL_AES_128
  44892. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44893. #endif
  44894. #ifdef WOLFSSL_AES_192
  44895. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44896. #endif
  44897. #ifdef WOLFSSL_AES_256
  44898. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44899. #endif
  44900. #endif
  44901. #ifdef HAVE_AESGCM
  44902. #ifdef WOLFSSL_AES_128
  44903. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44904. #endif
  44905. #ifdef WOLFSSL_AES_192
  44906. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44907. #endif
  44908. #ifdef WOLFSSL_AES_256
  44909. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44910. #endif
  44911. #endif
  44912. #ifdef HAVE_AESCCM
  44913. #ifdef WOLFSSL_AES_128
  44914. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44915. #endif
  44916. #ifdef WOLFSSL_AES_192
  44917. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44918. #endif
  44919. #ifdef WOLFSSL_AES_256
  44920. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44921. #endif
  44922. #endif
  44923. #ifdef WOLFSSL_AES_COUNTER
  44924. #ifdef WOLFSSL_AES_128
  44925. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44926. #endif
  44927. #ifdef WOLFSSL_AES_192
  44928. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44929. #endif
  44930. #ifdef WOLFSSL_AES_256
  44931. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44932. #endif
  44933. #endif
  44934. #ifdef HAVE_AES_ECB
  44935. #ifdef WOLFSSL_AES_128
  44936. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44937. #endif
  44938. #ifdef WOLFSSL_AES_192
  44939. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44940. #endif
  44941. #ifdef WOLFSSL_AES_256
  44942. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44943. #endif
  44944. #endif
  44945. #ifdef WOLFSSL_AES_OFB
  44946. #ifdef WOLFSSL_AES_128
  44947. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44948. #endif
  44949. #ifdef WOLFSSL_AES_192
  44950. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44951. #endif
  44952. #ifdef WOLFSSL_AES_256
  44953. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44954. #endif
  44955. #endif
  44956. #ifndef NO_RC4
  44957. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44958. #endif
  44959. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44960. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44961. #endif
  44962. #endif
  44963. #ifdef WOLFSSL_SM4_ECB
  44964. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44965. #endif
  44966. #ifdef WOLFSSL_SM4_CBC
  44967. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44968. #endif
  44969. #ifdef WOLFSSL_SM4_CTR
  44970. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44971. #endif
  44972. #ifdef WOLFSSL_SM4_GCM
  44973. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44974. #endif
  44975. #ifdef WOLFSSL_SM4_CCM
  44976. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44977. #endif
  44978. return EXPECT_RESULT();
  44979. }
  44980. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44981. {
  44982. EXPECT_DECLS;
  44983. int nids[] = {
  44984. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44985. #ifdef WOLFSSL_AES_128
  44986. NID_aes_128_cbc,
  44987. #endif
  44988. #ifdef WOLFSSL_AES_192
  44989. NID_aes_192_cbc,
  44990. #endif
  44991. #ifdef WOLFSSL_AES_256
  44992. NID_aes_256_cbc,
  44993. #endif
  44994. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44995. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44996. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44997. #ifdef HAVE_AESGCM
  44998. #ifdef WOLFSSL_AES_128
  44999. NID_aes_128_gcm,
  45000. #endif
  45001. #ifdef WOLFSSL_AES_192
  45002. NID_aes_192_gcm,
  45003. #endif
  45004. #ifdef WOLFSSL_AES_256
  45005. NID_aes_256_gcm,
  45006. #endif
  45007. #endif /* HAVE_AESGCM */
  45008. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  45009. #ifdef WOLFSSL_AES_COUNTER
  45010. #ifdef WOLFSSL_AES_128
  45011. NID_aes_128_ctr,
  45012. #endif
  45013. #ifdef WOLFSSL_AES_192
  45014. NID_aes_192_ctr,
  45015. #endif
  45016. #ifdef WOLFSSL_AES_256
  45017. NID_aes_256_ctr,
  45018. #endif
  45019. #endif
  45020. #ifndef NO_DES3
  45021. NID_des_cbc,
  45022. NID_des_ede3_cbc,
  45023. #endif
  45024. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45025. NID_chacha20_poly1305,
  45026. #endif
  45027. };
  45028. int iv_lengths[] = {
  45029. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  45030. #ifdef WOLFSSL_AES_128
  45031. AES_BLOCK_SIZE,
  45032. #endif
  45033. #ifdef WOLFSSL_AES_192
  45034. AES_BLOCK_SIZE,
  45035. #endif
  45036. #ifdef WOLFSSL_AES_256
  45037. AES_BLOCK_SIZE,
  45038. #endif
  45039. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  45040. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  45041. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45042. #ifdef HAVE_AESGCM
  45043. #ifdef WOLFSSL_AES_128
  45044. GCM_NONCE_MID_SZ,
  45045. #endif
  45046. #ifdef WOLFSSL_AES_192
  45047. GCM_NONCE_MID_SZ,
  45048. #endif
  45049. #ifdef WOLFSSL_AES_256
  45050. GCM_NONCE_MID_SZ,
  45051. #endif
  45052. #endif /* HAVE_AESGCM */
  45053. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  45054. #ifdef WOLFSSL_AES_COUNTER
  45055. #ifdef WOLFSSL_AES_128
  45056. AES_BLOCK_SIZE,
  45057. #endif
  45058. #ifdef WOLFSSL_AES_192
  45059. AES_BLOCK_SIZE,
  45060. #endif
  45061. #ifdef WOLFSSL_AES_256
  45062. AES_BLOCK_SIZE,
  45063. #endif
  45064. #endif
  45065. #ifndef NO_DES3
  45066. DES_BLOCK_SIZE,
  45067. DES_BLOCK_SIZE,
  45068. #endif
  45069. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45070. CHACHA20_POLY1305_AEAD_IV_SIZE,
  45071. #endif
  45072. };
  45073. int i;
  45074. int nidsLen = (sizeof(nids)/sizeof(int));
  45075. for (i = 0; i < nidsLen; i++) {
  45076. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  45077. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  45078. }
  45079. return EXPECT_RESULT();
  45080. }
  45081. static int test_wolfSSL_EVP_SignInit_ex(void)
  45082. {
  45083. EXPECT_DECLS;
  45084. WOLFSSL_EVP_MD_CTX mdCtx;
  45085. WOLFSSL_ENGINE* e = 0;
  45086. const EVP_MD* md = EVP_sha256();
  45087. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45088. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  45089. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  45090. return EXPECT_RESULT();
  45091. }
  45092. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  45093. {
  45094. EXPECT_DECLS;
  45095. #if !defined(NO_SHA256)
  45096. WOLFSSL_EVP_MD_CTX mdCtx;
  45097. unsigned int s = 0;
  45098. unsigned char md[WC_SHA256_DIGEST_SIZE];
  45099. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  45100. /* Bad Case */
  45101. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  45102. (HAVE_FIPS_VERSION > 2))
  45103. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45104. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  45105. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  45106. #else
  45107. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45108. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  45109. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  45110. #endif
  45111. /* Good Case */
  45112. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  45113. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  45114. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  45115. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  45116. #endif
  45117. return EXPECT_RESULT();
  45118. }
  45119. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  45120. {
  45121. EXPECT_DECLS;
  45122. #if defined(OPENSSL_EXTRA)
  45123. EVP_PKEY* pkey = NULL;
  45124. EVP_PKEY_CTX* ctx = NULL;
  45125. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45126. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45127. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  45128. /* void */
  45129. EVP_PKEY_CTX_free(ctx);
  45130. #else
  45131. /* int */
  45132. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  45133. #endif
  45134. EVP_PKEY_free(pkey);
  45135. #endif
  45136. return EXPECT_RESULT();
  45137. }
  45138. static int test_wolfSSL_EVP_PKEY_param_check(void)
  45139. {
  45140. EXPECT_DECLS;
  45141. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  45142. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  45143. DH *dh = NULL;
  45144. DH *setDh = NULL;
  45145. EVP_PKEY *pkey = NULL;
  45146. EVP_PKEY_CTX* ctx = NULL;
  45147. FILE* f = NULL;
  45148. unsigned char buf[512];
  45149. const unsigned char* pt = buf;
  45150. const char* dh2048 = "./certs/dh2048.der";
  45151. long len = 0;
  45152. int code = -1;
  45153. XMEMSET(buf, 0, sizeof(buf));
  45154. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  45155. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  45156. if (f != XBADFILE)
  45157. XFCLOSE(f);
  45158. /* Load dh2048.der into DH with internal format */
  45159. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  45160. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  45161. ExpectIntEQ(code, 0);
  45162. code = -1;
  45163. pkey = wolfSSL_EVP_PKEY_new();
  45164. /* Set DH into PKEY */
  45165. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  45166. /* create ctx from pkey */
  45167. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45168. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  45169. /* TODO: more invalid cases */
  45170. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  45171. EVP_PKEY_CTX_free(ctx);
  45172. EVP_PKEY_free(pkey);
  45173. DH_free(setDh);
  45174. setDh = NULL;
  45175. DH_free(dh);
  45176. dh = NULL;
  45177. #endif
  45178. #endif
  45179. return EXPECT_RESULT();
  45180. }
  45181. static int test_wolfSSL_EVP_BytesToKey(void)
  45182. {
  45183. EXPECT_DECLS;
  45184. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  45185. byte key[AES_BLOCK_SIZE] = {0};
  45186. byte iv[AES_BLOCK_SIZE] = {0};
  45187. int count = 0;
  45188. const EVP_MD* md = EVP_sha256();
  45189. const EVP_CIPHER *type;
  45190. const unsigned char *salt = (unsigned char *)"salt1234";
  45191. int sz = 5;
  45192. const byte data[] = {
  45193. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  45194. 0x72,0x6c,0x64
  45195. };
  45196. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  45197. /* Bad cases */
  45198. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  45199. 0);
  45200. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  45201. 16);
  45202. md = "2";
  45203. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  45204. WOLFSSL_FAILURE);
  45205. /* Good case */
  45206. md = EVP_sha256();
  45207. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  45208. 16);
  45209. #endif
  45210. return EXPECT_RESULT();
  45211. }
  45212. static int test_evp_cipher_aes_gcm(void)
  45213. {
  45214. EXPECT_DECLS;
  45215. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  45216. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  45217. (HAVE_FIPS_VERSION >= 2)))
  45218. /*
  45219. * This test checks data at various points in the encrypt/decrypt process
  45220. * against known values produced using the same test with OpenSSL. This
  45221. * interop testing is critical for verifying the correctness of our
  45222. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  45223. * a flow supported by OpenSSL that uses the control command
  45224. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  45225. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  45226. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  45227. * wolfSSL OpenSSH clients because there was a bug in this flow that
  45228. * happened to "cancel out" if both sides of the connection had the bug.
  45229. */
  45230. enum {
  45231. NUM_ENCRYPTIONS = 3,
  45232. AAD_SIZE = 4
  45233. };
  45234. byte plainText1[] = {
  45235. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  45236. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45237. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  45238. };
  45239. byte plainText2[] = {
  45240. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  45241. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  45242. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  45243. };
  45244. byte plainText3[] = {
  45245. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  45246. 0x5b, 0x57, 0x10
  45247. };
  45248. byte* plainTexts[NUM_ENCRYPTIONS] = {
  45249. plainText1,
  45250. plainText2,
  45251. plainText3
  45252. };
  45253. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  45254. sizeof(plainText1),
  45255. sizeof(plainText2),
  45256. sizeof(plainText3)
  45257. };
  45258. byte aad1[AAD_SIZE] = {
  45259. 0x00, 0x00, 0x00, 0x01
  45260. };
  45261. byte aad2[AAD_SIZE] = {
  45262. 0x00, 0x00, 0x00, 0x10
  45263. };
  45264. byte aad3[AAD_SIZE] = {
  45265. 0x00, 0x00, 0x01, 0x00
  45266. };
  45267. byte* aads[NUM_ENCRYPTIONS] = {
  45268. aad1,
  45269. aad2,
  45270. aad3
  45271. };
  45272. const byte iv[GCM_NONCE_MID_SZ] = {
  45273. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  45274. };
  45275. byte currentIv[GCM_NONCE_MID_SZ];
  45276. const byte key[] = {
  45277. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  45278. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  45279. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  45280. };
  45281. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  45282. {
  45283. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  45284. 0xEF
  45285. },
  45286. {
  45287. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  45288. 0xF0
  45289. },
  45290. {
  45291. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  45292. 0xF1
  45293. }
  45294. };
  45295. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  45296. {
  45297. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  45298. 0x3D, 0x32, 0x18, 0x34, 0xA9
  45299. },
  45300. {
  45301. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  45302. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  45303. },
  45304. {
  45305. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  45306. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  45307. }
  45308. };
  45309. const byte expCipherText1[] = {
  45310. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  45311. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  45312. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  45313. };
  45314. const byte expCipherText2[] = {
  45315. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  45316. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  45317. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  45318. };
  45319. const byte expCipherText3[] = {
  45320. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  45321. 0x80, 0x5E, 0x6B,
  45322. };
  45323. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  45324. expCipherText1,
  45325. expCipherText2,
  45326. expCipherText3
  45327. };
  45328. byte* cipherText = NULL;
  45329. byte* calcPlainText = NULL;
  45330. byte tag[AES_BLOCK_SIZE];
  45331. EVP_CIPHER_CTX* encCtx = NULL;
  45332. EVP_CIPHER_CTX* decCtx = NULL;
  45333. int i, j, outl;
  45334. /****************************************************/
  45335. for (i = 0; i < 3; ++i) {
  45336. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  45337. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  45338. /* First iteration, set key before IV. */
  45339. if (i == 0) {
  45340. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  45341. SSL_SUCCESS);
  45342. /*
  45343. * The call to EVP_CipherInit below (with NULL key) should clear the
  45344. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  45345. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  45346. * behavior.
  45347. */
  45348. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  45349. (void*)iv), SSL_SUCCESS);
  45350. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  45351. SSL_SUCCESS);
  45352. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45353. currentIv), SSL_FAILURE);
  45354. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  45355. SSL_SUCCESS);
  45356. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  45357. SSL_SUCCESS);
  45358. }
  45359. /* Second iteration, IV before key. */
  45360. else {
  45361. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  45362. SSL_SUCCESS);
  45363. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  45364. SSL_SUCCESS);
  45365. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  45366. SSL_SUCCESS);
  45367. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  45368. SSL_SUCCESS);
  45369. }
  45370. /*
  45371. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  45372. * been issued first.
  45373. */
  45374. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45375. currentIv), SSL_FAILURE);
  45376. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  45377. (void*)iv), SSL_SUCCESS);
  45378. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  45379. (void*)iv), SSL_SUCCESS);
  45380. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  45381. /*************** Encrypt ***************/
  45382. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45383. currentIv), SSL_SUCCESS);
  45384. /* Check current IV against expected. */
  45385. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  45386. /* Add AAD. */
  45387. if (i == 2) {
  45388. /* Test streaming API. */
  45389. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  45390. AAD_SIZE), SSL_SUCCESS);
  45391. }
  45392. else {
  45393. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  45394. AAD_SIZE);
  45395. }
  45396. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  45397. DYNAMIC_TYPE_TMP_BUFFER));
  45398. /* Encrypt plaintext. */
  45399. if (i == 2) {
  45400. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  45401. plainTexts[j], plainTextSzs[j]),
  45402. SSL_SUCCESS);
  45403. }
  45404. else {
  45405. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  45406. plainTextSzs[j]), plainTextSzs[j]);
  45407. }
  45408. if (i == 2) {
  45409. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  45410. SSL_SUCCESS);
  45411. }
  45412. else {
  45413. /*
  45414. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  45415. * akin to calling EVP_CipherFinal.
  45416. */
  45417. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  45418. }
  45419. /* Check ciphertext against expected. */
  45420. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  45421. 0);
  45422. /* Get and check tag against expected. */
  45423. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  45424. sizeof(tag), tag), SSL_SUCCESS);
  45425. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  45426. /*************** Decrypt ***************/
  45427. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  45428. currentIv), SSL_SUCCESS);
  45429. /* Check current IV against expected. */
  45430. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  45431. /* Add AAD. */
  45432. if (i == 2) {
  45433. /* Test streaming API. */
  45434. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  45435. AAD_SIZE), SSL_SUCCESS);
  45436. }
  45437. else {
  45438. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  45439. AAD_SIZE);
  45440. }
  45441. /* Set expected tag. */
  45442. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  45443. sizeof(tag), tag), SSL_SUCCESS);
  45444. /* Decrypt ciphertext. */
  45445. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  45446. DYNAMIC_TYPE_TMP_BUFFER));
  45447. if (i == 2) {
  45448. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  45449. cipherText, plainTextSzs[j]),
  45450. SSL_SUCCESS);
  45451. }
  45452. else {
  45453. /* This first EVP_Cipher call will check the tag, too. */
  45454. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  45455. plainTextSzs[j]), plainTextSzs[j]);
  45456. }
  45457. if (i == 2) {
  45458. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  45459. SSL_SUCCESS);
  45460. }
  45461. else {
  45462. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  45463. }
  45464. /* Check plaintext against expected. */
  45465. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  45466. 0);
  45467. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45468. cipherText = NULL;
  45469. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45470. calcPlainText = NULL;
  45471. }
  45472. EVP_CIPHER_CTX_free(encCtx);
  45473. encCtx = NULL;
  45474. EVP_CIPHER_CTX_free(decCtx);
  45475. decCtx = NULL;
  45476. }
  45477. #endif
  45478. return EXPECT_RESULT();
  45479. }
  45480. static int test_wolfSSL_OBJ_ln(void)
  45481. {
  45482. EXPECT_DECLS;
  45483. const int nid_set[] = {
  45484. NID_commonName,
  45485. NID_serialNumber,
  45486. NID_countryName,
  45487. NID_localityName,
  45488. NID_stateOrProvinceName,
  45489. NID_organizationName,
  45490. NID_organizationalUnitName,
  45491. NID_domainComponent,
  45492. NID_businessCategory,
  45493. NID_jurisdictionCountryName,
  45494. NID_jurisdictionStateOrProvinceName,
  45495. NID_emailAddress
  45496. };
  45497. const char* ln_set[] = {
  45498. "commonName",
  45499. "serialNumber",
  45500. "countryName",
  45501. "localityName",
  45502. "stateOrProvinceName",
  45503. "organizationName",
  45504. "organizationalUnitName",
  45505. "domainComponent",
  45506. "businessCategory",
  45507. "jurisdictionCountryName",
  45508. "jurisdictionStateOrProvinceName",
  45509. "emailAddress",
  45510. };
  45511. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  45512. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  45513. #ifdef HAVE_ECC
  45514. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45515. {
  45516. EC_builtin_curve r[27];
  45517. size_t nCurves = sizeof(r) / sizeof(r[0]);
  45518. nCurves = EC_get_builtin_curves(r, nCurves);
  45519. for (i = 0; i < nCurves; i++) {
  45520. /* skip ECC_CURVE_INVALID */
  45521. if (r[i].nid != ECC_CURVE_INVALID) {
  45522. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  45523. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  45524. }
  45525. }
  45526. }
  45527. #endif
  45528. #endif
  45529. for (i = 0; i < maxIdx; i++) {
  45530. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  45531. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  45532. }
  45533. return EXPECT_RESULT();
  45534. }
  45535. static int test_wolfSSL_OBJ_sn(void)
  45536. {
  45537. EXPECT_DECLS;
  45538. int i = 0, maxIdx = 7;
  45539. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  45540. NID_stateOrProvinceName,NID_organizationName,
  45541. NID_organizationalUnitName,NID_emailAddress};
  45542. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  45543. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  45544. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  45545. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  45546. WOLFSSL_EMAIL_ADDR};
  45547. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  45548. for (i = 0; i < maxIdx; i++) {
  45549. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  45550. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  45551. }
  45552. return EXPECT_RESULT();
  45553. }
  45554. #if !defined(NO_BIO)
  45555. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  45556. {
  45557. return lh_strhash(s[3]);
  45558. }
  45559. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  45560. {
  45561. return XSTRCMP(a[3], b[3]);
  45562. }
  45563. #endif
  45564. static int test_wolfSSL_TXT_DB(void)
  45565. {
  45566. EXPECT_DECLS;
  45567. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45568. BIO *bio = NULL;
  45569. TXT_DB *db = NULL;
  45570. const int columns = 6;
  45571. const char *fields[6] = {
  45572. "V",
  45573. "320926161116Z",
  45574. "",
  45575. "12BD",
  45576. "unknown",
  45577. "/CN=rsa doe",
  45578. };
  45579. char** fields_copy = NULL;
  45580. /* Test read */
  45581. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  45582. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  45583. ExpectNotNull(db = TXT_DB_read(bio, columns));
  45584. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  45585. DYNAMIC_TYPE_OPENSSL));
  45586. if (fields_copy != NULL) {
  45587. XMEMCPY(fields_copy, fields, sizeof(fields));
  45588. }
  45589. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  45590. if (EXPECT_FAIL()) {
  45591. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  45592. }
  45593. BIO_free(bio);
  45594. bio = NULL;
  45595. /* Test write */
  45596. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45597. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  45598. BIO_free(bio);
  45599. /* Test index */
  45600. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  45601. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  45602. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45603. fields[3] = "12DA";
  45604. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45605. fields[3] = "FFFF";
  45606. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45607. fields[3] = "";
  45608. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45609. TXT_DB_free(db);
  45610. #endif
  45611. return EXPECT_RESULT();
  45612. }
  45613. static int test_wolfSSL_NCONF(void)
  45614. {
  45615. EXPECT_DECLS;
  45616. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45617. const char* confFile = "./tests/NCONF_test.cnf";
  45618. CONF* conf = NULL;
  45619. long eline = 0;
  45620. long num = 0;
  45621. ExpectNotNull(conf = NCONF_new(NULL));
  45622. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  45623. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  45624. ExpectIntEQ(num, 1234);
  45625. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  45626. ExpectIntEQ(num, 4321);
  45627. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  45628. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  45629. "./test-dir/file1");
  45630. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  45631. "./test-dir/file1:./test-dir/file2:./section1:file2");
  45632. NCONF_free(conf);
  45633. #endif
  45634. return EXPECT_RESULT();
  45635. }
  45636. #endif /* OPENSSL_ALL */
  45637. static int test_wolfSSL_X509V3_EXT_get(void) {
  45638. EXPECT_DECLS;
  45639. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45640. XFILE f = XBADFILE;
  45641. int numOfExt =0;
  45642. int extNid = 0;
  45643. int i = 0;
  45644. WOLFSSL_X509* x509 = NULL;
  45645. WOLFSSL_X509_EXTENSION* ext = NULL;
  45646. const WOLFSSL_v3_ext_method* method = NULL;
  45647. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45648. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45649. if (f != XBADFILE)
  45650. XFCLOSE(f);
  45651. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  45652. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45653. for (i = 0; i < numOfExt; i++) {
  45654. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45655. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  45656. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45657. ExpectIntEQ(method->ext_nid, extNid);
  45658. }
  45659. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  45660. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  45661. wolfSSL_X509_free(x509);
  45662. #endif
  45663. return EXPECT_RESULT();
  45664. }
  45665. static int test_wolfSSL_X509V3_EXT_nconf(void)
  45666. {
  45667. EXPECT_DECLS;
  45668. #ifdef OPENSSL_ALL
  45669. const char *ext_names[] = {
  45670. "subjectKeyIdentifier",
  45671. "authorityKeyIdentifier",
  45672. "subjectAltName",
  45673. "keyUsage",
  45674. "extendedKeyUsage",
  45675. };
  45676. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  45677. int ext_nids[] = {
  45678. NID_subject_key_identifier,
  45679. NID_authority_key_identifier,
  45680. NID_subject_alt_name,
  45681. NID_key_usage,
  45682. NID_ext_key_usage,
  45683. };
  45684. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  45685. const char *ext_values[] = {
  45686. "hash",
  45687. "hash",
  45688. "DNS:example.com, IP:127.0.0.1",
  45689. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  45690. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  45691. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  45692. "OCSPSigning",
  45693. };
  45694. size_t i;
  45695. X509_EXTENSION* ext = NULL;
  45696. X509* x509 = NULL;
  45697. unsigned int keyUsageFlags;
  45698. unsigned int extKeyUsageFlags;
  45699. ExpectNotNull(x509 = X509_new());
  45700. /* keyUsage / extKeyUsage should match string above */
  45701. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45702. | KU_NON_REPUDIATION
  45703. | KU_KEY_ENCIPHERMENT
  45704. | KU_DATA_ENCIPHERMENT
  45705. | KU_KEY_AGREEMENT
  45706. | KU_KEY_CERT_SIGN
  45707. | KU_CRL_SIGN
  45708. | KU_ENCIPHER_ONLY
  45709. | KU_DECIPHER_ONLY;
  45710. extKeyUsageFlags = XKU_SSL_CLIENT
  45711. | XKU_SSL_SERVER
  45712. | XKU_CODE_SIGN
  45713. | XKU_SMIME
  45714. | XKU_TIMESTAMP
  45715. | XKU_OCSP_SIGN;
  45716. for (i = 0; i < ext_names_count; i++) {
  45717. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45718. ext_values[i]));
  45719. X509_EXTENSION_free(ext);
  45720. ext = NULL;
  45721. }
  45722. for (i = 0; i < ext_nids_count; i++) {
  45723. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  45724. ext_values[i]));
  45725. X509_EXTENSION_free(ext);
  45726. ext = NULL;
  45727. }
  45728. /* Test adding extension to X509 */
  45729. for (i = 0; i < ext_nids_count; i++) {
  45730. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45731. ext_values[i]));
  45732. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  45733. if (ext_nids[i] == NID_key_usage) {
  45734. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45735. }
  45736. else if (ext_nids[i] == NID_ext_key_usage) {
  45737. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45738. }
  45739. X509_EXTENSION_free(ext);
  45740. ext = NULL;
  45741. }
  45742. X509_free(x509);
  45743. #endif
  45744. return EXPECT_RESULT();
  45745. }
  45746. static int test_wolfSSL_X509V3_EXT(void) {
  45747. EXPECT_DECLS;
  45748. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45749. XFILE f = XBADFILE;
  45750. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  45751. char* str = NULL;
  45752. unsigned char* data = NULL;
  45753. const WOLFSSL_v3_ext_method* method = NULL;
  45754. WOLFSSL_X509* x509 = NULL;
  45755. WOLFSSL_X509_EXTENSION* ext = NULL;
  45756. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  45757. WOLFSSL_ASN1_OBJECT *obj = NULL;
  45758. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  45759. WOLFSSL_ASN1_STRING* asn1str = NULL;
  45760. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  45761. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  45762. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  45763. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  45764. WOLFSSL_GENERAL_NAME* gn = NULL;
  45765. /* Check NULL argument */
  45766. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  45767. /* Using OCSP cert with X509V3 extensions */
  45768. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  45769. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45770. if (f != XBADFILE)
  45771. XFCLOSE(f);
  45772. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45773. /* Basic Constraints */
  45774. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45775. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45776. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  45777. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  45778. ExpectIntEQ(bc->ca, 1);
  45779. ExpectNull(bc->pathlen);
  45780. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  45781. bc = NULL;
  45782. i++;
  45783. /* Subject Key Identifier */
  45784. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45785. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45786. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  45787. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45788. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  45789. asn1str));
  45790. X509_EXTENSION_free(ext2);
  45791. ext2 = NULL;
  45792. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45793. ExpectNotNull(method->i2s);
  45794. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  45795. wolfSSL_ASN1_STRING_free(asn1str);
  45796. asn1str = NULL;
  45797. if (str != NULL) {
  45798. actual = strcmp(str,
  45799. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45800. }
  45801. ExpectIntEQ(actual, 0);
  45802. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45803. str = NULL;
  45804. i++;
  45805. /* Authority Key Identifier */
  45806. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45807. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45808. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45809. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45810. ext));
  45811. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45812. ExpectNotNull(asn1str = aKeyId->keyid);
  45813. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45814. asn1str));
  45815. asn1str = NULL;
  45816. if (str != NULL) {
  45817. actual = strcmp(str,
  45818. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45819. }
  45820. ExpectIntEQ(actual, 0);
  45821. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45822. str = NULL;
  45823. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45824. aKeyId = NULL;
  45825. i++;
  45826. /* Key Usage */
  45827. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45828. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45829. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45830. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45831. #if defined(WOLFSSL_QT)
  45832. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45833. #else
  45834. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45835. #endif
  45836. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45837. if (data != NULL) {
  45838. #ifdef BIG_ENDIAN_ORDER
  45839. actual = data[1];
  45840. #else
  45841. actual = data[0];
  45842. #endif
  45843. }
  45844. ExpectIntEQ(actual, expected);
  45845. wolfSSL_ASN1_STRING_free(asn1str);
  45846. asn1str = NULL;
  45847. #if 1
  45848. i++;
  45849. /* Authority Info Access */
  45850. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45851. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45852. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45853. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45854. ext));
  45855. #if defined(WOLFSSL_QT)
  45856. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45857. #else
  45858. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45859. #endif
  45860. /* URI entry is an ACCESS_DESCRIPTION type */
  45861. #if defined(WOLFSSL_QT)
  45862. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45863. #else
  45864. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45865. #endif
  45866. ExpectNotNull(adObj = ad->method);
  45867. /* Make sure nid is OCSP */
  45868. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45869. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45870. ExpectNotNull(gn = ad->location);
  45871. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45872. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45873. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45874. #if defined(WOLFSSL_QT)
  45875. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45876. #else
  45877. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45878. #endif
  45879. if (str != NULL) {
  45880. actual = strcmp(str, "http://127.0.0.1:22220");
  45881. }
  45882. ExpectIntEQ(actual, 0);
  45883. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45884. aia = NULL;
  45885. #else
  45886. (void) aia; (void) ad; (void) adObj; (void) gn;
  45887. #endif
  45888. wolfSSL_X509_free(x509);
  45889. #endif
  45890. return EXPECT_RESULT();
  45891. }
  45892. static int test_wolfSSL_X509_get_extension_flags(void)
  45893. {
  45894. EXPECT_DECLS;
  45895. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45896. XFILE f = XBADFILE;
  45897. X509* x509 = NULL;
  45898. unsigned int extFlags;
  45899. unsigned int keyUsageFlags;
  45900. unsigned int extKeyUsageFlags;
  45901. /* client-int-cert.pem has the following extension flags. */
  45902. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45903. /* and the following key usage flags. */
  45904. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45905. | KU_NON_REPUDIATION
  45906. | KU_KEY_ENCIPHERMENT;
  45907. /* and the following extended key usage flags. */
  45908. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45909. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45910. XBADFILE);
  45911. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45912. if (f != XBADFILE) {
  45913. XFCLOSE(f);
  45914. f = XBADFILE;
  45915. }
  45916. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45917. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45918. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45919. X509_free(x509);
  45920. x509 = NULL;
  45921. /* client-cert-ext.pem has the following extension flags. */
  45922. extFlags = EXFLAG_KUSAGE;
  45923. /* and the following key usage flags. */
  45924. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45925. | KU_KEY_CERT_SIGN
  45926. | KU_CRL_SIGN;
  45927. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45928. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45929. if (f != XBADFILE)
  45930. XFCLOSE(f);
  45931. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45932. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45933. X509_free(x509);
  45934. #endif /* OPENSSL_ALL */
  45935. return EXPECT_RESULT();
  45936. }
  45937. static int test_wolfSSL_X509_get_ext(void)
  45938. {
  45939. EXPECT_DECLS;
  45940. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45941. int ret = 0;
  45942. XFILE f = XBADFILE;
  45943. WOLFSSL_X509* x509 = NULL;
  45944. WOLFSSL_X509_EXTENSION* foundExtension;
  45945. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45946. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45947. if (f != XBADFILE)
  45948. XFCLOSE(f);
  45949. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45950. /* wolfSSL_X509_get_ext() valid input */
  45951. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45952. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45953. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45954. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45955. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45956. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45957. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45958. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45959. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45960. wolfSSL_X509_free(x509);
  45961. #endif
  45962. return EXPECT_RESULT();
  45963. }
  45964. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45965. {
  45966. EXPECT_DECLS;
  45967. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45968. int rc = 0;
  45969. XFILE f = XBADFILE;
  45970. WOLFSSL_X509* x509 = NULL;
  45971. ASN1_OBJECT* obj = NULL;
  45972. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45973. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45974. if (f != XBADFILE)
  45975. XFCLOSE(f);
  45976. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45977. -1), 0);
  45978. /* Start search from last location (should fail) */
  45979. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45980. rc), -1);
  45981. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45982. -2), -1);
  45983. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45984. -1), -1);
  45985. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45986. /* NID_ext_key_usage, check also its nid and oid */
  45987. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45988. -1);
  45989. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45990. x509, rc)));
  45991. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45992. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45993. wolfSSL_X509_free(x509);
  45994. #endif
  45995. return EXPECT_RESULT();
  45996. }
  45997. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45998. {
  45999. EXPECT_DECLS;
  46000. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  46001. int rc = 0;
  46002. XFILE f = XBADFILE;
  46003. WOLFSSL_X509* x509 = NULL;
  46004. WOLFSSL_X509_EXTENSION* ext = NULL;
  46005. WOLFSSL_ASN1_STRING* sanString = NULL;
  46006. byte* sanDer = NULL;
  46007. const byte expectedDer[] = {
  46008. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  46009. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  46010. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46011. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  46012. if (f != XBADFILE)
  46013. XFCLOSE(f);
  46014. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  46015. ExpectNotNull(ext = X509_get_ext(x509, rc));
  46016. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  46017. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  46018. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  46019. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  46020. X509_free(x509);
  46021. #endif
  46022. return EXPECT_RESULT();
  46023. }
  46024. static int test_wolfSSL_X509_EXTENSION_new(void)
  46025. {
  46026. EXPECT_DECLS;
  46027. #if defined (OPENSSL_ALL)
  46028. WOLFSSL_X509_EXTENSION* ext = NULL;
  46029. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  46030. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  46031. wolfSSL_X509_EXTENSION_free(ext);
  46032. #endif
  46033. return EXPECT_RESULT();
  46034. }
  46035. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  46036. {
  46037. EXPECT_DECLS;
  46038. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  46039. WOLFSSL_X509* x509 = NULL;
  46040. WOLFSSL_X509_EXTENSION* ext = NULL;
  46041. WOLFSSL_ASN1_OBJECT* o = NULL;
  46042. XFILE file = XBADFILE;
  46043. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46044. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  46045. if (file != XBADFILE)
  46046. XFCLOSE(file);
  46047. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  46048. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  46049. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  46050. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  46051. ExpectIntEQ(o->nid, 128);
  46052. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  46053. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  46054. wolfSSL_X509_free(x509);
  46055. #endif
  46056. return EXPECT_RESULT();
  46057. }
  46058. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  46059. {
  46060. EXPECT_DECLS;
  46061. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  46062. WOLFSSL_X509* x509 = NULL;
  46063. WOLFSSL_X509_EXTENSION* ext = NULL;
  46064. WOLFSSL_ASN1_STRING* str = NULL;
  46065. XFILE file = XBADFILE;
  46066. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46067. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  46068. if (file != XBADFILE)
  46069. XFCLOSE(file);
  46070. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  46071. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  46072. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  46073. wolfSSL_X509_free(x509);
  46074. #endif
  46075. return EXPECT_RESULT();
  46076. }
  46077. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  46078. {
  46079. EXPECT_DECLS;
  46080. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  46081. WOLFSSL_X509* x509 = NULL;
  46082. WOLFSSL_X509_EXTENSION* ext = NULL;
  46083. XFILE file = XBADFILE;
  46084. int crit = 0;
  46085. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46086. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  46087. if (file != XBADFILE)
  46088. XFCLOSE(file);
  46089. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  46090. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  46091. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  46092. wolfSSL_X509_free(x509);
  46093. #endif
  46094. return EXPECT_RESULT();
  46095. }
  46096. static int test_wolfSSL_X509V3_EXT_print(void)
  46097. {
  46098. EXPECT_DECLS;
  46099. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  46100. !defined(NO_RSA)
  46101. {
  46102. XFILE f = XBADFILE;
  46103. WOLFSSL_X509* x509 = NULL;
  46104. X509_EXTENSION * ext = NULL;
  46105. int loc = 0;
  46106. BIO *bio = NULL;
  46107. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  46108. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  46109. if (f != XBADFILE)
  46110. fclose(f);
  46111. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  46112. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  46113. NID_basic_constraints, -1), -1);
  46114. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  46115. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  46116. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  46117. NID_subject_key_identifier, -1), -1);
  46118. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  46119. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  46120. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  46121. NID_authority_key_identifier, -1), -1);
  46122. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  46123. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  46124. wolfSSL_BIO_free(bio);
  46125. wolfSSL_X509_free(x509);
  46126. }
  46127. {
  46128. X509 *x509 = NULL;
  46129. BIO *bio = NULL;
  46130. X509_EXTENSION *ext = NULL;
  46131. unsigned int i = 0;
  46132. unsigned int idx = 0;
  46133. /* Some NIDs to test with */
  46134. int nids[] = {
  46135. /* NID_key_usage, currently X509_get_ext returns this as a bit
  46136. * string, which messes up X509V3_EXT_print */
  46137. /* NID_ext_key_usage, */
  46138. NID_subject_alt_name,
  46139. };
  46140. int* n = NULL;
  46141. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  46142. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  46143. WOLFSSL_FILETYPE_PEM));
  46144. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  46145. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  46146. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  46147. * update the index */
  46148. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  46149. ExpectNotNull(ext = X509_get_ext(x509, (int)idx));
  46150. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  46151. ExpectIntGT(fprintf(stderr, "\n"), 0);
  46152. }
  46153. BIO_free(bio);
  46154. X509_free(x509);
  46155. }
  46156. #endif
  46157. return EXPECT_RESULT();
  46158. }
  46159. static int test_wolfSSL_X509_cmp(void)
  46160. {
  46161. EXPECT_DECLS;
  46162. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  46163. XFILE file1 = XBADFILE;
  46164. XFILE file2 = XBADFILE;
  46165. WOLFSSL_X509* cert1 = NULL;
  46166. WOLFSSL_X509* cert2 = NULL;
  46167. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  46168. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  46169. XBADFILE);
  46170. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  46171. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  46172. if (file1 != XBADFILE)
  46173. fclose(file1);
  46174. if (file2 != XBADFILE)
  46175. fclose(file2);
  46176. /* wolfSSL_X509_cmp() testing matching certs */
  46177. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  46178. /* wolfSSL_X509_cmp() testing mismatched certs */
  46179. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  46180. /* wolfSSL_X509_cmp() testing NULL, valid args */
  46181. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  46182. /* wolfSSL_X509_cmp() testing valid, NULL args */
  46183. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  46184. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  46185. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  46186. wolfSSL_X509_free(cert1);
  46187. wolfSSL_X509_free(cert2);
  46188. #endif
  46189. return EXPECT_RESULT();
  46190. }
  46191. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  46192. {
  46193. EXPECT_DECLS;
  46194. #if defined(OPENSSL_ALL)
  46195. EVP_PKEY* pkey;
  46196. pkey = EVP_PKEY_new();
  46197. ExpectNotNull(pkey);
  46198. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  46199. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  46200. EVP_PKEY_free(pkey);
  46201. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  46202. EVP_PKEY_free(pkey);
  46203. EVP_PKEY_free(pkey);
  46204. #endif
  46205. return EXPECT_RESULT();
  46206. }
  46207. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  46208. {
  46209. EXPECT_DECLS;
  46210. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46211. EVP_PKEY* pkey = NULL;
  46212. const unsigned char* p;
  46213. unsigned char *der = NULL;
  46214. unsigned char *tmp = NULL;
  46215. int derLen;
  46216. p = client_keypub_der_2048;
  46217. /* Check that key can be successfully decoded. */
  46218. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  46219. sizeof_client_keypub_der_2048));
  46220. /* Check that key can be successfully encoded. */
  46221. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  46222. /* Ensure that the encoded version matches the original. */
  46223. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  46224. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  46225. /* Do same test except with pre-allocated buffer to ensure the der pointer
  46226. * is advanced. */
  46227. tmp = der;
  46228. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  46229. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  46230. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  46231. ExpectTrue(der + derLen == tmp);
  46232. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  46233. EVP_PKEY_free(pkey);
  46234. #endif
  46235. return EXPECT_RESULT();
  46236. }
  46237. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  46238. {
  46239. EXPECT_DECLS;
  46240. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  46241. !defined(NO_ASN) && !defined(NO_PWDBASED)
  46242. EVP_PKEY* pkey = NULL;
  46243. const unsigned char* p;
  46244. unsigned char *der = NULL;
  46245. unsigned char *tmp = NULL;
  46246. int derLen;
  46247. unsigned char pub_buf[65];
  46248. const int pub_len = 65;
  46249. BN_CTX* ctx;
  46250. EC_GROUP* curve = NULL;
  46251. EC_KEY* ephemeral_key = NULL;
  46252. const EC_POINT* h;
  46253. /* Generate an x963 key pair and get public part into pub_buf */
  46254. ExpectNotNull(ctx = BN_CTX_new());
  46255. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  46256. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  46257. NID_X9_62_prime256v1));
  46258. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  46259. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  46260. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  46261. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  46262. /* Prepare the EVP_PKEY */
  46263. ExpectNotNull(pkey = EVP_PKEY_new());
  46264. p = pub_buf;
  46265. /* Check that key can be successfully decoded. */
  46266. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  46267. pub_len));
  46268. /* Check that key can be successfully encoded. */
  46269. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  46270. /* Ensure that the encoded version matches the original. */
  46271. ExpectIntEQ(derLen, pub_len);
  46272. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  46273. /* Do same test except with pre-allocated buffer to ensure the der pointer
  46274. * is advanced. */
  46275. tmp = der;
  46276. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  46277. ExpectIntEQ(derLen, pub_len);
  46278. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  46279. ExpectTrue(der + derLen == tmp);
  46280. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  46281. EVP_PKEY_free(pkey);
  46282. EC_KEY_free(ephemeral_key);
  46283. EC_GROUP_free(curve);
  46284. #endif
  46285. return EXPECT_RESULT();
  46286. }
  46287. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  46288. {
  46289. EXPECT_DECLS;
  46290. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  46291. DSA* dsa = NULL;
  46292. byte derIn[] = {
  46293. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  46294. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  46295. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  46296. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  46297. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  46298. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  46299. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  46300. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  46301. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  46302. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  46303. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  46304. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  46305. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  46306. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  46307. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  46308. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  46309. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  46310. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  46311. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  46312. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  46313. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  46314. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  46315. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  46316. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  46317. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  46318. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  46319. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  46320. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  46321. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  46322. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  46323. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  46324. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  46325. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  46326. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  46327. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  46328. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  46329. 0x1e, 0xde, 0xc4
  46330. };
  46331. int derInLen = sizeof(derIn);
  46332. byte* derOut = NULL;
  46333. int derOutLen;
  46334. byte* p = derIn;
  46335. /* Check that params can be successfully decoded. */
  46336. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  46337. /* Check that params can be successfully encoded. */
  46338. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  46339. /* Ensure that the encoded version matches the original. */
  46340. ExpectIntEQ(derInLen, derOutLen);
  46341. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  46342. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  46343. DSA_free(dsa);
  46344. #endif
  46345. return EXPECT_RESULT();
  46346. }
  46347. static int test_wolfSSL_i2d_PrivateKey(void)
  46348. {
  46349. EXPECT_DECLS;
  46350. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  46351. !defined(NO_ASN) && !defined(NO_PWDBASED)
  46352. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46353. {
  46354. EVP_PKEY* pkey = NULL;
  46355. const unsigned char* server_key =
  46356. (const unsigned char*)server_key_der_2048;
  46357. unsigned char buf[FOURK_BUF];
  46358. unsigned char* pt = NULL;
  46359. int bufSz = 0;
  46360. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  46361. (long)sizeof_server_key_der_2048));
  46362. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  46363. pt = buf;
  46364. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  46365. ExpectIntNE((pt - buf), 0);
  46366. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  46367. EVP_PKEY_free(pkey);
  46368. }
  46369. #endif
  46370. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46371. {
  46372. EVP_PKEY* pkey = NULL;
  46373. const unsigned char* client_key =
  46374. (const unsigned char*)ecc_clikey_der_256;
  46375. unsigned char buf[FOURK_BUF];
  46376. unsigned char* pt = NULL;
  46377. int bufSz = 0;
  46378. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  46379. (long)sizeof_ecc_clikey_der_256)));
  46380. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  46381. pt = buf;
  46382. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  46383. ExpectIntNE((pt - buf), 0);
  46384. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  46385. EVP_PKEY_free(pkey);
  46386. }
  46387. #endif
  46388. #endif
  46389. return EXPECT_RESULT();
  46390. }
  46391. static int test_wolfSSL_OCSP_id_get0_info(void)
  46392. {
  46393. EXPECT_DECLS;
  46394. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  46395. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46396. X509* cert = NULL;
  46397. X509* issuer = NULL;
  46398. OCSP_CERTID* id = NULL;
  46399. OCSP_CERTID* id2 = NULL;
  46400. ASN1_STRING* name = NULL;
  46401. ASN1_OBJECT* pmd = NULL;
  46402. ASN1_STRING* keyHash = NULL;
  46403. ASN1_INTEGER* serial = NULL;
  46404. ASN1_INTEGER* x509Int = NULL;
  46405. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  46406. SSL_FILETYPE_PEM));
  46407. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  46408. SSL_FILETYPE_PEM));
  46409. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  46410. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  46411. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  46412. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  46413. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  46414. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  46415. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  46416. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  46417. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  46418. ExpectNotNull(serial);
  46419. /* compare serial number to one in cert, should be equal */
  46420. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  46421. ExpectIntEQ(x509Int->length, serial->length);
  46422. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  46423. /* test OCSP_id_cmp */
  46424. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  46425. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  46426. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  46427. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  46428. if (id != NULL) {
  46429. id->issuerHash[0] = ~id->issuerHash[0];
  46430. }
  46431. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  46432. OCSP_CERTID_free(id);
  46433. OCSP_CERTID_free(id2);
  46434. X509_free(cert); /* free's x509Int */
  46435. X509_free(issuer);
  46436. #endif
  46437. return EXPECT_RESULT();
  46438. }
  46439. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  46440. {
  46441. EXPECT_DECLS;
  46442. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  46443. WOLFSSL_OCSP_CERTID certId;
  46444. byte* targetBuffer = NULL;
  46445. byte* p;
  46446. /* OCSP CertID bytes taken from PCAP */
  46447. byte rawCertId[] = {
  46448. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  46449. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  46450. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  46451. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  46452. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  46453. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  46454. 0xcf, 0xbc, 0x91
  46455. };
  46456. int ret = 0;
  46457. int i;
  46458. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  46459. certId.rawCertId = rawCertId;
  46460. certId.rawCertIdSize = sizeof(rawCertId);
  46461. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  46462. DYNAMIC_TYPE_TMP_BUFFER));
  46463. p = targetBuffer;
  46464. /* Function returns the size of the encoded data. */
  46465. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  46466. /* If target buffer is not null, function increments targetBuffer to point
  46467. * just past the end of the encoded data. */
  46468. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  46469. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  46470. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  46471. }
  46472. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46473. targetBuffer = NULL;
  46474. /* If target buffer is null, function allocates memory for a buffer and
  46475. * copies the encoded data into it. targetBuffer then points to the start of
  46476. * this newly allocate buffer. */
  46477. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  46478. sizeof(rawCertId));
  46479. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  46480. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  46481. }
  46482. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  46483. #endif
  46484. return EXPECT_RESULT();
  46485. }
  46486. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  46487. {
  46488. EXPECT_DECLS;
  46489. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  46490. WOLFSSL_OCSP_CERTID* certId;
  46491. WOLFSSL_OCSP_CERTID* certIdGood;
  46492. WOLFSSL_OCSP_CERTID* certIdBad;
  46493. const unsigned char* rawCertIdPtr;
  46494. const unsigned char rawCertId[] = {
  46495. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  46496. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  46497. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  46498. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  46499. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  46500. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  46501. 0xcf, 0xbc, 0x91
  46502. };
  46503. rawCertIdPtr = &rawCertId[0];
  46504. /* If the cert ID is NULL the function should allocate it and copy the
  46505. * data to it. */
  46506. certId = NULL;
  46507. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  46508. sizeof(rawCertId)));
  46509. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  46510. if (certId != NULL) {
  46511. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  46512. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  46513. }
  46514. /* If the cert ID is not NULL the function will just copy the data to it. */
  46515. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  46516. DYNAMIC_TYPE_TMP_BUFFER));
  46517. ExpectNotNull(certId);
  46518. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  46519. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  46520. rawCertIdPtr = &rawCertId[0];
  46521. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  46522. sizeof(rawCertId)));
  46523. ExpectPtrEq(certIdGood, certId);
  46524. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  46525. if (certId != NULL) {
  46526. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  46527. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46528. certId = NULL;
  46529. }
  46530. /* The below tests should fail when passed bad parameters. NULL should
  46531. * always be returned. */
  46532. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  46533. sizeof(rawCertId)));
  46534. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  46535. sizeof(rawCertId)));
  46536. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  46537. #endif
  46538. return EXPECT_RESULT();
  46539. }
  46540. static int test_wolfSSL_OCSP_id_cmp(void)
  46541. {
  46542. EXPECT_DECLS;
  46543. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46544. OCSP_CERTID id1;
  46545. OCSP_CERTID id2;
  46546. XMEMSET(&id1, 0, sizeof(id1));
  46547. XMEMSET(&id2, 0, sizeof(id2));
  46548. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  46549. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  46550. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  46551. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  46552. #endif
  46553. return EXPECT_RESULT();
  46554. }
  46555. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  46556. {
  46557. EXPECT_DECLS;
  46558. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46559. WOLFSSL_OCSP_SINGLERESP single;
  46560. const WOLFSSL_OCSP_CERTID* certId;
  46561. XMEMSET(&single, 0, sizeof(single));
  46562. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  46563. ExpectPtrEq(&single, certId);
  46564. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  46565. #endif
  46566. return EXPECT_RESULT();
  46567. }
  46568. static int test_wolfSSL_OCSP_single_get0_status(void)
  46569. {
  46570. EXPECT_DECLS;
  46571. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46572. WOLFSSL_OCSP_SINGLERESP single;
  46573. CertStatus certStatus;
  46574. WOLFSSL_ASN1_TIME* thisDate;
  46575. WOLFSSL_ASN1_TIME* nextDate;
  46576. int ret, i;
  46577. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46578. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  46579. /* Fill the date fields with some dummy data. */
  46580. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  46581. certStatus.thisDateParsed.data[i] = i;
  46582. certStatus.nextDateParsed.data[i] = i;
  46583. }
  46584. certStatus.status = CERT_GOOD;
  46585. single.status = &certStatus;
  46586. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  46587. &nextDate);
  46588. ExpectIntEQ(ret, CERT_GOOD);
  46589. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  46590. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  46591. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  46592. CERT_GOOD);
  46593. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  46594. NULL), CERT_GOOD);
  46595. #endif
  46596. return EXPECT_RESULT();
  46597. }
  46598. static int test_wolfSSL_OCSP_resp_count(void)
  46599. {
  46600. EXPECT_DECLS;
  46601. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46602. WOLFSSL_OCSP_BASICRESP basicResp;
  46603. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  46604. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  46605. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  46606. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46607. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46608. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  46609. basicResp.single = &singleRespOne;
  46610. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  46611. singleRespOne.next = &singleRespTwo;
  46612. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  46613. #endif
  46614. return EXPECT_RESULT();
  46615. }
  46616. static int test_wolfSSL_OCSP_resp_get0(void)
  46617. {
  46618. EXPECT_DECLS;
  46619. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46620. WOLFSSL_OCSP_BASICRESP basicResp;
  46621. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  46622. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  46623. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  46624. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46625. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46626. basicResp.single = &singleRespOne;
  46627. singleRespOne.next = &singleRespTwo;
  46628. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  46629. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  46630. #endif
  46631. return EXPECT_RESULT();
  46632. }
  46633. static int test_wolfSSL_EVP_PKEY_derive(void)
  46634. {
  46635. EXPECT_DECLS;
  46636. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  46637. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  46638. EVP_PKEY_CTX *ctx = NULL;
  46639. unsigned char *skey = NULL;
  46640. size_t skeylen;
  46641. EVP_PKEY *pkey = NULL;
  46642. EVP_PKEY *peerkey = NULL;
  46643. const unsigned char* key;
  46644. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  46645. /* DH */
  46646. key = dh_key_der_2048;
  46647. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  46648. sizeof_dh_key_der_2048)));
  46649. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  46650. key = dh_key_der_2048;
  46651. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  46652. sizeof_dh_key_der_2048)));
  46653. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  46654. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46655. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  46656. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  46657. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  46658. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  46659. DYNAMIC_TYPE_OPENSSL));
  46660. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  46661. EVP_PKEY_CTX_free(ctx);
  46662. ctx = NULL;
  46663. EVP_PKEY_free(peerkey);
  46664. peerkey = NULL;
  46665. EVP_PKEY_free(pkey);
  46666. pkey = NULL;
  46667. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  46668. skey = NULL;
  46669. #endif
  46670. #ifdef HAVE_ECC
  46671. /* ECDH */
  46672. key = ecc_clikey_der_256;
  46673. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  46674. sizeof_ecc_clikey_der_256)));
  46675. key = ecc_clikeypub_der_256;
  46676. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  46677. sizeof_ecc_clikeypub_der_256)));
  46678. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46679. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  46680. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  46681. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  46682. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  46683. DYNAMIC_TYPE_OPENSSL));
  46684. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  46685. EVP_PKEY_CTX_free(ctx);
  46686. EVP_PKEY_free(peerkey);
  46687. EVP_PKEY_free(pkey);
  46688. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  46689. #endif /* HAVE_ECC */
  46690. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  46691. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  46692. return EXPECT_RESULT();
  46693. }
  46694. static int test_wolfSSL_EVP_PBE_scrypt(void)
  46695. {
  46696. EXPECT_DECLS;
  46697. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  46698. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  46699. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  46700. int ret;
  46701. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  46702. int pwdlen = sizeof(pwd);
  46703. const byte salt[] = {'N','a','C','l'};
  46704. int saltlen = sizeof(salt);
  46705. byte key[80];
  46706. word64 numOvr32 = (word64)INT32_MAX + 1;
  46707. /* expected derived key for N:16, r:1, p:1 */
  46708. const byte expectedKey[] = {
  46709. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  46710. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  46711. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  46712. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  46713. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  46714. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  46715. 0xE7, 0xE9, 0xC0, 0x9A};
  46716. /* N r p mx key keylen */
  46717. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  46718. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  46719. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  46720. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  46721. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  46722. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  46723. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  46724. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  46725. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  46726. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  46727. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  46728. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  46729. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  46730. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  46731. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  46732. ExpectIntEQ(ret, 1); /* should succeed */
  46733. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  46734. key, 64);
  46735. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  46736. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  46737. key, 64);
  46738. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  46739. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  46740. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  46741. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  46742. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  46743. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  46744. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  46745. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  46746. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  46747. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  46748. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  46749. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  46750. ExpectIntEQ(ret, 1);
  46751. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  46752. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  46753. #endif /* !NO_PWDBASED && !NO_SHA256 */
  46754. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  46755. return EXPECT_RESULT();
  46756. }
  46757. static int test_no_op_functions(void)
  46758. {
  46759. EXPECT_DECLS;
  46760. #if defined(OPENSSL_EXTRA)
  46761. /* this makes sure wolfSSL can compile and run these no-op functions */
  46762. SSL_load_error_strings();
  46763. ENGINE_load_builtin_engines();
  46764. OpenSSL_add_all_ciphers();
  46765. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  46766. #endif
  46767. return EXPECT_RESULT();
  46768. }
  46769. static int test_wolfSSL_CRYPTO_memcmp(void)
  46770. {
  46771. EXPECT_DECLS;
  46772. #ifdef OPENSSL_EXTRA
  46773. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46774. "implementation of TLS/SSL for embedded devices to the cloud.";
  46775. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46776. "implementation of TLS/SSL for embedded devices to the cloud.";
  46777. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46778. "implementation of TLS/SSL for embedded devices to the cloud!";
  46779. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  46780. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  46781. #endif
  46782. return EXPECT_RESULT();
  46783. }
  46784. /*----------------------------------------------------------------------------*
  46785. | wolfCrypt ASN
  46786. *----------------------------------------------------------------------------*/
  46787. static int test_wc_CreateEncryptedPKCS8Key(void)
  46788. {
  46789. EXPECT_DECLS;
  46790. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  46791. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  46792. WC_RNG rng;
  46793. byte* encKey = NULL;
  46794. word32 encKeySz = 0;
  46795. word32 decKeySz = 0;
  46796. const char password[] = "Lorem ipsum dolor sit amet";
  46797. word32 passwordSz = (word32)XSTRLEN(password);
  46798. word32 tradIdx = 0;
  46799. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46800. ExpectIntEQ(wc_InitRng(&rng), 0);
  46801. PRIVATE_KEY_UNLOCK();
  46802. /* Call with NULL for out buffer to get necessary length. */
  46803. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46804. sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
  46805. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46806. LENGTH_ONLY_E);
  46807. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46808. DYNAMIC_TYPE_TMP_BUFFER));
  46809. /* Call with the allocated out buffer. */
  46810. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46811. sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
  46812. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46813. 0);
  46814. /* Decrypt the encrypted PKCS8 key we just made. */
  46815. ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46816. (int)passwordSz)), 0);
  46817. /* encKey now holds the decrypted key (decrypted in place). */
  46818. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46819. /* Check that the decrypted key matches the key prior to encryption. */
  46820. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46821. sizeof_server_key_der_2048), 0);
  46822. PRIVATE_KEY_LOCK();
  46823. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46824. wc_FreeRng(&rng);
  46825. #endif
  46826. return EXPECT_RESULT();
  46827. }
  46828. static int test_wc_GetPkcs8TraditionalOffset(void)
  46829. {
  46830. EXPECT_DECLS;
  46831. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46832. int length;
  46833. int derSz = 0;
  46834. word32 inOutIdx;
  46835. const char* path = "./certs/server-keyPkcs8.der";
  46836. XFILE file = XBADFILE;
  46837. byte der[2048];
  46838. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46839. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46840. if (file != XBADFILE)
  46841. XFCLOSE(file);
  46842. /* valid case */
  46843. inOutIdx = 0;
  46844. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46845. 0);
  46846. /* inOutIdx > sz */
  46847. inOutIdx = 4000;
  46848. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46849. BAD_FUNC_ARG);
  46850. /* null input */
  46851. inOutIdx = 0;
  46852. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46853. BAD_FUNC_ARG);
  46854. /* invalid input, fill buffer with 1's */
  46855. XMEMSET(der, 1, sizeof(der));
  46856. inOutIdx = 0;
  46857. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46858. ASN_PARSE_E);
  46859. #endif /* NO_ASN */
  46860. return EXPECT_RESULT();
  46861. }
  46862. static int test_wc_SetSubjectRaw(void)
  46863. {
  46864. EXPECT_DECLS;
  46865. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46866. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46867. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46868. WOLFSSL_X509* x509 = NULL;
  46869. int peerCertSz;
  46870. const byte* peerCertBuf = NULL;
  46871. Cert forgedCert;
  46872. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46873. WOLFSSL_FILETYPE_ASN1));
  46874. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46875. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46876. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46877. wolfSSL_FreeX509(x509);
  46878. #endif
  46879. return EXPECT_RESULT();
  46880. }
  46881. static int test_wc_GetSubjectRaw(void)
  46882. {
  46883. EXPECT_DECLS;
  46884. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46885. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46886. Cert cert;
  46887. byte *subjectRaw;
  46888. ExpectIntEQ(0, wc_InitCert(&cert));
  46889. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46890. #endif
  46891. return EXPECT_RESULT();
  46892. }
  46893. static int test_wc_SetIssuerRaw(void)
  46894. {
  46895. EXPECT_DECLS;
  46896. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46897. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46898. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46899. WOLFSSL_X509* x509 = NULL;
  46900. int peerCertSz;
  46901. const byte* peerCertBuf;
  46902. Cert forgedCert;
  46903. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46904. WOLFSSL_FILETYPE_ASN1));
  46905. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46906. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46907. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46908. wolfSSL_FreeX509(x509);
  46909. #endif
  46910. return EXPECT_RESULT();
  46911. }
  46912. static int test_wc_SetIssueBuffer(void)
  46913. {
  46914. EXPECT_DECLS;
  46915. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46916. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46917. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46918. WOLFSSL_X509* x509 = NULL;
  46919. int peerCertSz;
  46920. const byte* peerCertBuf;
  46921. Cert forgedCert;
  46922. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46923. WOLFSSL_FILETYPE_ASN1));
  46924. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46925. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46926. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46927. wolfSSL_FreeX509(x509);
  46928. #endif
  46929. return EXPECT_RESULT();
  46930. }
  46931. /*
  46932. * Testing wc_SetSubjectKeyId
  46933. */
  46934. static int test_wc_SetSubjectKeyId(void)
  46935. {
  46936. EXPECT_DECLS;
  46937. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46938. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46939. Cert cert;
  46940. const char* file = "certs/ecc-client-keyPub.pem";
  46941. ExpectIntEQ(0, wc_InitCert(&cert));
  46942. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46943. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46944. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46945. #endif
  46946. return EXPECT_RESULT();
  46947. } /* END test_wc_SetSubjectKeyId */
  46948. /*
  46949. * Testing wc_SetSubject
  46950. */
  46951. static int test_wc_SetSubject(void)
  46952. {
  46953. EXPECT_DECLS;
  46954. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46955. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46956. Cert cert;
  46957. const char* file = "./certs/ca-ecc-cert.pem";
  46958. ExpectIntEQ(0, wc_InitCert(&cert));
  46959. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46960. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46961. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46962. #endif
  46963. return EXPECT_RESULT();
  46964. } /* END test_wc_SetSubject */
  46965. static int test_CheckCertSignature(void)
  46966. {
  46967. EXPECT_DECLS;
  46968. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46969. WOLFSSL_CERT_MANAGER* cm = NULL;
  46970. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46971. XFILE fp = XBADFILE;
  46972. byte cert[4096];
  46973. int certSz;
  46974. #endif
  46975. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, NULL));
  46976. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46977. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, cm));
  46978. #ifndef NO_RSA
  46979. #ifdef USE_CERT_BUFFERS_1024
  46980. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_1024,
  46981. sizeof_server_cert_der_1024, NULL, cm));
  46982. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46983. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46984. WOLFSSL_FILETYPE_ASN1));
  46985. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_1024,
  46986. sizeof_server_cert_der_1024, NULL, cm));
  46987. #elif defined(USE_CERT_BUFFERS_2048)
  46988. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_2048,
  46989. sizeof_server_cert_der_2048, NULL, cm));
  46990. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46991. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46992. WOLFSSL_FILETYPE_ASN1));
  46993. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_2048,
  46994. sizeof_server_cert_der_2048, NULL, cm));
  46995. #endif
  46996. #endif
  46997. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46998. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(serv_ecc_der_256,
  46999. sizeof_serv_ecc_der_256, NULL, cm));
  47000. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  47001. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  47002. WOLFSSL_FILETYPE_ASN1));
  47003. ExpectIntEQ(0, wc_CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  47004. NULL, cm));
  47005. #endif
  47006. #if !defined(NO_FILESYSTEM)
  47007. wolfSSL_CertManagerFree(cm);
  47008. cm = NULL;
  47009. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  47010. #ifndef NO_RSA
  47011. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  47012. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  47013. if (fp != XBADFILE) {
  47014. XFCLOSE(fp);
  47015. fp = XBADFILE;
  47016. }
  47017. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47018. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  47019. "./certs/ca-cert.pem", NULL));
  47020. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47021. #endif
  47022. #ifdef HAVE_ECC
  47023. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  47024. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  47025. if (fp != XBADFILE) {
  47026. XFCLOSE(fp);
  47027. fp = XBADFILE;
  47028. }
  47029. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47030. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  47031. "./certs/ca-ecc-cert.pem", NULL));
  47032. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  47033. #endif
  47034. #endif
  47035. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  47036. (void)fp;
  47037. (void)cert;
  47038. (void)certSz;
  47039. #endif
  47040. wolfSSL_CertManagerFree(cm);
  47041. #endif
  47042. return EXPECT_RESULT();
  47043. }
  47044. static int test_wc_ParseCert(void)
  47045. {
  47046. EXPECT_DECLS;
  47047. #if !defined(NO_CERTS) && !defined(NO_RSA)
  47048. DecodedCert decodedCert;
  47049. const byte* rawCert = client_cert_der_2048;
  47050. const int rawCertSize = sizeof_client_cert_der_2048;
  47051. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  47052. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47053. #ifndef IGNORE_NAME_CONSTRAINTS
  47054. /* check that the subjects emailAddress was not put in the alt name list */
  47055. ExpectNotNull(decodedCert.subjectEmail);
  47056. ExpectNull(decodedCert.altEmailNames);
  47057. #endif
  47058. wc_FreeDecodedCert(&decodedCert);
  47059. #endif
  47060. return EXPECT_RESULT();
  47061. }
  47062. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  47063. * the API safely errors out on badly-formed ASN input.
  47064. * NOTE: Test not compatible with released FIPS implementations!
  47065. */
  47066. static int test_wc_ParseCert_Error(void)
  47067. {
  47068. EXPECT_DECLS;
  47069. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  47070. (!defined(HAVE_FIPS) || \
  47071. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  47072. DecodedCert decodedCert;
  47073. int i;
  47074. /* Certificate data */
  47075. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  47076. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  47077. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  47078. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  47079. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  47080. /* Test data */
  47081. const struct testStruct {
  47082. const byte* c;
  47083. const int cSz;
  47084. const int expRet;
  47085. } t[] = {
  47086. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  47087. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  47088. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  47089. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  47090. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  47091. };
  47092. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  47093. for (i = 0; i < tSz; i++) {
  47094. WOLFSSL_MSG_EX("i == %d", i);
  47095. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  47096. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  47097. wc_FreeDecodedCert(&decodedCert);
  47098. }
  47099. #endif
  47100. return EXPECT_RESULT();
  47101. }
  47102. static int test_MakeCertWithPathLen(void)
  47103. {
  47104. EXPECT_DECLS;
  47105. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  47106. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  47107. const byte expectedPathLen = 7;
  47108. Cert cert;
  47109. DecodedCert decodedCert;
  47110. byte der[FOURK_BUF];
  47111. int derSize = 0;
  47112. WC_RNG rng;
  47113. ecc_key key;
  47114. int ret;
  47115. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47116. XMEMSET(&key, 0, sizeof(ecc_key));
  47117. XMEMSET(&cert, 0, sizeof(Cert));
  47118. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  47119. ExpectIntEQ(wc_InitRng(&rng), 0);
  47120. ExpectIntEQ(wc_ecc_init(&key), 0);
  47121. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  47122. ExpectIntEQ(wc_InitCert(&cert), 0);
  47123. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  47124. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  47125. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  47126. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  47127. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  47128. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  47129. CTC_NAME_SIZE);
  47130. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  47131. CTC_NAME_SIZE);
  47132. cert.selfSigned = 1;
  47133. cert.isCA = 1;
  47134. cert.pathLen = expectedPathLen;
  47135. cert.pathLenSet = 1;
  47136. cert.sigType = CTC_SHA256wECDSA;
  47137. #ifdef WOLFSSL_CERT_EXT
  47138. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  47139. #endif
  47140. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  47141. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  47142. FOURK_BUF, NULL, &key, &rng), 0);
  47143. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  47144. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47145. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  47146. wc_FreeDecodedCert(&decodedCert);
  47147. ret = wc_ecc_free(&key);
  47148. ExpectIntEQ(ret, 0);
  47149. ret = wc_FreeRng(&rng);
  47150. ExpectIntEQ(ret, 0);
  47151. #endif
  47152. return EXPECT_RESULT();
  47153. }
  47154. static int test_MakeCertWith0Ser(void)
  47155. {
  47156. EXPECT_DECLS;
  47157. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  47158. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && \
  47159. defined(WOLFSSL_ASN_TEMPLATE)
  47160. Cert cert;
  47161. DecodedCert decodedCert;
  47162. byte der[FOURK_BUF];
  47163. int derSize = 0;
  47164. WC_RNG rng;
  47165. ecc_key key;
  47166. int ret;
  47167. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47168. XMEMSET(&key, 0, sizeof(ecc_key));
  47169. XMEMSET(&cert, 0, sizeof(Cert));
  47170. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  47171. ExpectIntEQ(wc_InitRng(&rng), 0);
  47172. ExpectIntEQ(wc_ecc_init(&key), 0);
  47173. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  47174. ExpectIntEQ(wc_InitCert(&cert), 0);
  47175. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  47176. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  47177. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  47178. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  47179. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  47180. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  47181. CTC_NAME_SIZE);
  47182. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  47183. CTC_NAME_SIZE);
  47184. cert.selfSigned = 1;
  47185. cert.isCA = 1;
  47186. cert.sigType = CTC_SHA256wECDSA;
  47187. #ifdef WOLFSSL_CERT_EXT
  47188. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  47189. #endif
  47190. /* set serial number to 0 */
  47191. cert.serialSz = 1;
  47192. cert.serial[0] = 0;
  47193. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  47194. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  47195. FOURK_BUF, NULL, &key, &rng), 0);
  47196. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  47197. #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
  47198. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL),
  47199. ASN_PARSE_E);
  47200. #else
  47201. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47202. #endif
  47203. wc_FreeDecodedCert(&decodedCert);
  47204. ret = wc_ecc_free(&key);
  47205. ExpectIntEQ(ret, 0);
  47206. ret = wc_FreeRng(&rng);
  47207. ExpectIntEQ(ret, 0);
  47208. #endif
  47209. return EXPECT_RESULT();
  47210. }
  47211. static int test_MakeCertWithCaFalse(void)
  47212. {
  47213. EXPECT_DECLS;
  47214. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  47215. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  47216. const byte expectedIsCa = 0;
  47217. Cert cert;
  47218. DecodedCert decodedCert;
  47219. byte der[FOURK_BUF];
  47220. int derSize = 0;
  47221. WC_RNG rng;
  47222. ecc_key key;
  47223. int ret;
  47224. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47225. XMEMSET(&key, 0, sizeof(ecc_key));
  47226. XMEMSET(&cert, 0, sizeof(Cert));
  47227. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  47228. ExpectIntEQ(wc_InitRng(&rng), 0);
  47229. ExpectIntEQ(wc_ecc_init(&key), 0);
  47230. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  47231. ExpectIntEQ(wc_InitCert(&cert), 0);
  47232. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  47233. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  47234. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  47235. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  47236. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  47237. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  47238. CTC_NAME_SIZE);
  47239. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  47240. CTC_NAME_SIZE);
  47241. cert.selfSigned = 1;
  47242. cert.isCA = expectedIsCa;
  47243. cert.isCaSet = 1;
  47244. cert.sigType = CTC_SHA256wECDSA;
  47245. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  47246. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  47247. FOURK_BUF, NULL, &key, &rng), 0);
  47248. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  47249. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  47250. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  47251. wc_FreeDecodedCert(&decodedCert);
  47252. ret = wc_ecc_free(&key);
  47253. ExpectIntEQ(ret, 0);
  47254. ret = wc_FreeRng(&rng);
  47255. ExpectIntEQ(ret, 0);
  47256. #endif
  47257. return EXPECT_RESULT();
  47258. }
  47259. /*----------------------------------------------------------------------------*
  47260. | wolfCrypt ECC
  47261. *----------------------------------------------------------------------------*/
  47262. static int test_wc_ecc_get_curve_size_from_name(void)
  47263. {
  47264. EXPECT_DECLS;
  47265. #ifdef HAVE_ECC
  47266. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47267. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  47268. #endif
  47269. /* invalid case */
  47270. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  47271. /* NULL input */
  47272. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  47273. #endif /* HAVE_ECC */
  47274. return EXPECT_RESULT();
  47275. }
  47276. static int test_wc_ecc_get_curve_id_from_name(void)
  47277. {
  47278. EXPECT_DECLS;
  47279. #ifdef HAVE_ECC
  47280. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47281. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  47282. ECC_SECP256R1);
  47283. #endif
  47284. /* invalid case */
  47285. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  47286. /* NULL input */
  47287. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  47288. #endif /* HAVE_ECC */
  47289. return EXPECT_RESULT();
  47290. }
  47291. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  47292. !defined(HAVE_SELFTEST) && \
  47293. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  47294. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  47295. {
  47296. EXPECT_DECLS;
  47297. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47298. ecc_key* key;
  47299. const ecc_set_type* params = NULL;
  47300. int ret;
  47301. #endif
  47302. WOLFSSL_EC_KEY *ecKey = NULL;
  47303. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47304. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  47305. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  47306. if (EXPECT_SUCCESS()) {
  47307. ret = EC_KEY_generate_key(ecKey);
  47308. } else
  47309. ret = 0;
  47310. if (ret == 1) {
  47311. /* normal test */
  47312. key = (ecc_key*)ecKey->internal;
  47313. if (key != NULL) {
  47314. params = key->dp;
  47315. }
  47316. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  47317. ECC_SECP256R1);
  47318. }
  47319. #endif
  47320. /* invalid case, NULL input*/
  47321. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  47322. wolfSSL_EC_KEY_free(ecKey);
  47323. return EXPECT_RESULT();
  47324. }
  47325. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  47326. static int test_wc_ecc_get_curve_id_from_params(void)
  47327. {
  47328. EXPECT_DECLS;
  47329. #ifdef HAVE_ECC
  47330. const byte prime[] =
  47331. {
  47332. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  47333. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47334. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  47335. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  47336. };
  47337. const byte primeInvalid[] =
  47338. {
  47339. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  47340. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47341. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  47342. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  47343. };
  47344. const byte Af[] =
  47345. {
  47346. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  47347. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47348. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  47349. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  47350. };
  47351. const byte Bf[] =
  47352. {
  47353. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  47354. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  47355. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  47356. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  47357. };
  47358. const byte order[] =
  47359. {
  47360. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  47361. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47362. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  47363. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  47364. };
  47365. const byte Gx[] =
  47366. {
  47367. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  47368. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  47369. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  47370. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  47371. };
  47372. const byte Gy[] =
  47373. {
  47374. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  47375. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  47376. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  47377. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  47378. };
  47379. int cofactor = 1;
  47380. int fieldSize = 256;
  47381. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  47382. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  47383. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  47384. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  47385. ECC_SECP256R1);
  47386. #endif
  47387. /* invalid case, fieldSize = 0 */
  47388. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  47389. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  47390. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  47391. /* invalid case, NULL prime */
  47392. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  47393. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  47394. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  47395. /* invalid case, invalid prime */
  47396. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  47397. primeInvalid, sizeof(primeInvalid),
  47398. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  47399. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  47400. #endif
  47401. return EXPECT_RESULT();
  47402. }
  47403. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  47404. {
  47405. EXPECT_DECLS;
  47406. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  47407. WOLFSSL_RSA* rsa = NULL;
  47408. WOLFSSL_EVP_PKEY* pkey = NULL;
  47409. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  47410. const char* in = "What is easy to do is easy not to do.";
  47411. size_t inlen = XSTRLEN(in);
  47412. size_t outEncLen = 0;
  47413. byte* outEnc = NULL;
  47414. byte* outDec = NULL;
  47415. size_t outDecLen = 0;
  47416. size_t rsaKeySz = 2048/8; /* Bytes */
  47417. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  47418. byte* inTmp = NULL;
  47419. byte* outEncTmp = NULL;
  47420. byte* outDecTmp = NULL;
  47421. #endif
  47422. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47423. DYNAMIC_TYPE_TMP_BUFFER));
  47424. if (outEnc != NULL) {
  47425. XMEMSET(outEnc, 0, rsaKeySz);
  47426. }
  47427. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47428. DYNAMIC_TYPE_TMP_BUFFER));
  47429. if (outDec != NULL) {
  47430. XMEMSET(outDec, 0, rsaKeySz);
  47431. }
  47432. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47433. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47434. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47435. if (EXPECT_FAIL()) {
  47436. RSA_free(rsa);
  47437. }
  47438. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  47439. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  47440. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47441. WOLFSSL_SUCCESS);
  47442. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  47443. since ctx uses it.*/
  47444. ExpectIntEQ(pkey->ref.count, 2);
  47445. EVP_PKEY_free(pkey);
  47446. ExpectIntEQ(pkey->ref.count, 1);
  47447. /* Encrypt data */
  47448. /* Check that we can get the required output buffer length by passing in a
  47449. * NULL output buffer. */
  47450. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  47451. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  47452. ExpectIntEQ(rsaKeySz, outEncLen);
  47453. /* Now do the actual encryption. */
  47454. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  47455. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  47456. /* Decrypt data */
  47457. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  47458. /* Check that we can get the required output buffer length by passing in a
  47459. * NULL output buffer. */
  47460. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  47461. WOLFSSL_SUCCESS);
  47462. ExpectIntEQ(rsaKeySz, outDecLen);
  47463. /* Now do the actual decryption. */
  47464. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  47465. WOLFSSL_SUCCESS);
  47466. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  47467. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  47468. /* The input length must be the same size as the RSA key.*/
  47469. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47470. DYNAMIC_TYPE_TMP_BUFFER));
  47471. if (inTmp != NULL) {
  47472. XMEMSET(inTmp, 9, rsaKeySz);
  47473. }
  47474. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47475. DYNAMIC_TYPE_TMP_BUFFER));
  47476. if (outEncTmp != NULL) {
  47477. XMEMSET(outEncTmp, 0, rsaKeySz);
  47478. }
  47479. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  47480. DYNAMIC_TYPE_TMP_BUFFER));
  47481. if (outDecTmp != NULL) {
  47482. XMEMSET(outDecTmp, 0, rsaKeySz);
  47483. }
  47484. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  47485. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  47486. WOLFSSL_SUCCESS);
  47487. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  47488. WOLFSSL_SUCCESS);
  47489. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  47490. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  47491. outEncLen), WOLFSSL_SUCCESS);
  47492. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  47493. #endif
  47494. EVP_PKEY_CTX_free(ctx);
  47495. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47496. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47497. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  47498. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47499. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47500. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47501. #endif
  47502. #endif
  47503. return EXPECT_RESULT();
  47504. }
  47505. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47506. !defined(HAVE_SELFTEST)
  47507. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47508. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47509. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47510. #endif
  47511. #endif
  47512. #endif
  47513. #if defined(OPENSSL_EXTRA)
  47514. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47515. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47516. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47517. #endif
  47518. #endif
  47519. #endif
  47520. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47521. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47522. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47523. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47524. #endif
  47525. #endif
  47526. #endif
  47527. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  47528. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  47529. {
  47530. EXPECT_DECLS;
  47531. #if defined(OPENSSL_EXTRA)
  47532. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47533. !defined(HAVE_SELFTEST)
  47534. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47535. WOLFSSL_RSA* rsa = NULL;
  47536. #endif
  47537. #endif
  47538. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47539. WOLFSSL_DSA* dsa = NULL;
  47540. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47541. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47542. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47543. WOLFSSL_EC_KEY* ecKey = NULL;
  47544. #endif
  47545. #endif
  47546. WOLFSSL_EVP_PKEY* pkey = NULL;
  47547. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  47548. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  47549. const char* in = "What is easy to do is easy not to do.";
  47550. size_t inlen = XSTRLEN(in);
  47551. byte hash[SHA256_DIGEST_LENGTH] = {0};
  47552. byte zero[SHA256_DIGEST_LENGTH] = {0};
  47553. SHA256_CTX c;
  47554. byte* sig = NULL;
  47555. byte* sigVerify = NULL;
  47556. size_t siglen;
  47557. size_t siglenOnlyLen;
  47558. size_t keySz = 2048/8; /* Bytes */
  47559. ExpectNotNull(sig =
  47560. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  47561. ExpectNotNull(sigVerify =
  47562. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  47563. siglen = keySz;
  47564. ExpectNotNull(XMEMSET(sig, 0, keySz));
  47565. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  47566. /* Generate hash */
  47567. SHA256_Init(&c);
  47568. SHA256_Update(&c, in, inlen);
  47569. SHA256_Final(hash, &c);
  47570. #ifdef WOLFSSL_SMALL_STACK_CACHE
  47571. /* workaround for small stack cache case */
  47572. wc_Sha256Free((wc_Sha256*)&c);
  47573. #endif
  47574. /* Generate key */
  47575. ExpectNotNull(pkey = EVP_PKEY_new());
  47576. switch (keyType) {
  47577. case EVP_PKEY_RSA:
  47578. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47579. !defined(HAVE_SELFTEST)
  47580. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47581. {
  47582. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47583. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47584. }
  47585. #endif
  47586. #endif
  47587. break;
  47588. case EVP_PKEY_DSA:
  47589. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47590. ExpectNotNull(dsa = DSA_new());
  47591. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  47592. NULL, 0, NULL, NULL, NULL), 1);
  47593. ExpectIntEQ(DSA_generate_key(dsa), 1);
  47594. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  47595. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47596. break;
  47597. case EVP_PKEY_EC:
  47598. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47599. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47600. {
  47601. ExpectNotNull(ecKey = EC_KEY_new());
  47602. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  47603. ExpectIntEQ(
  47604. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47605. if (EXPECT_FAIL()) {
  47606. EC_KEY_free(ecKey);
  47607. }
  47608. }
  47609. #endif
  47610. #endif
  47611. break;
  47612. }
  47613. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  47614. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47615. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47616. !defined(HAVE_SELFTEST)
  47617. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47618. if (keyType == EVP_PKEY_RSA)
  47619. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47620. WOLFSSL_SUCCESS);
  47621. #endif
  47622. #endif
  47623. /* Check returning only length */
  47624. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  47625. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  47626. ExpectIntGT(siglenOnlyLen, 0);
  47627. /* Sign data */
  47628. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  47629. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  47630. ExpectIntGE(siglenOnlyLen, siglen);
  47631. /* Verify signature */
  47632. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  47633. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47634. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47635. !defined(HAVE_SELFTEST)
  47636. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47637. if (keyType == EVP_PKEY_RSA)
  47638. ExpectIntEQ(
  47639. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  47640. WOLFSSL_SUCCESS);
  47641. #endif
  47642. #endif
  47643. ExpectIntEQ(EVP_PKEY_verify(
  47644. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  47645. WOLFSSL_SUCCESS);
  47646. ExpectIntEQ(EVP_PKEY_verify(
  47647. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  47648. WOLFSSL_FAILURE);
  47649. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47650. !defined(HAVE_SELFTEST)
  47651. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47652. if (keyType == EVP_PKEY_RSA) {
  47653. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  47654. /* Try RSA sign/verify with no padding. */
  47655. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47656. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  47657. WOLFSSL_SUCCESS);
  47658. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  47659. siglen), WOLFSSL_SUCCESS);
  47660. ExpectIntGE(siglenOnlyLen, siglen);
  47661. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47662. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47663. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  47664. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  47665. siglen), WOLFSSL_SUCCESS);
  47666. #endif
  47667. /* Wrong padding schemes. */
  47668. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47669. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  47670. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  47671. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  47672. siglen), WOLFSSL_SUCCESS);
  47673. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47674. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47675. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  47676. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  47677. siglen), WOLFSSL_SUCCESS);
  47678. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47679. WOLFSSL_SUCCESS);
  47680. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47681. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  47682. }
  47683. #endif
  47684. #endif
  47685. /* error cases */
  47686. siglen = keySz; /* Reset because sig size may vary slightly */
  47687. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  47688. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47689. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  47690. WOLFSSL_SUCCESS);
  47691. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  47692. WOLFSSL_SUCCESS);
  47693. EVP_PKEY_free(pkey);
  47694. pkey = NULL;
  47695. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47696. DSA_free(dsa);
  47697. dsa = NULL;
  47698. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47699. EVP_PKEY_CTX_free(ctx_verify);
  47700. ctx_verify = NULL;
  47701. EVP_PKEY_CTX_free(ctx);
  47702. ctx = NULL;
  47703. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47704. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47705. #endif /* OPENSSL_EXTRA */
  47706. return EXPECT_RESULT();
  47707. }
  47708. #endif
  47709. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  47710. {
  47711. EXPECT_DECLS;
  47712. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47713. !defined(HAVE_SELFTEST)
  47714. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47715. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  47716. #endif
  47717. #endif
  47718. return EXPECT_RESULT();
  47719. }
  47720. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  47721. {
  47722. EXPECT_DECLS;
  47723. #if defined(OPENSSL_EXTRA)
  47724. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47725. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  47726. #endif
  47727. #endif
  47728. return EXPECT_RESULT();
  47729. }
  47730. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  47731. {
  47732. EXPECT_DECLS;
  47733. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47734. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47735. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  47736. #endif
  47737. #endif
  47738. return EXPECT_RESULT();
  47739. }
  47740. static int test_EVP_PKEY_rsa(void)
  47741. {
  47742. EXPECT_DECLS;
  47743. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47744. WOLFSSL_RSA* rsa = NULL;
  47745. WOLFSSL_EVP_PKEY* pkey = NULL;
  47746. ExpectNotNull(rsa = wolfSSL_RSA_new());
  47747. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47748. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  47749. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  47750. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47751. if (EXPECT_FAIL()) {
  47752. wolfSSL_RSA_free(rsa);
  47753. }
  47754. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  47755. wolfSSL_EVP_PKEY_free(pkey);
  47756. #endif
  47757. return EXPECT_RESULT();
  47758. }
  47759. static int test_EVP_PKEY_ec(void)
  47760. {
  47761. EXPECT_DECLS;
  47762. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47763. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47764. WOLFSSL_EC_KEY* ecKey = NULL;
  47765. WOLFSSL_EVP_PKEY* pkey = NULL;
  47766. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  47767. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47768. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  47769. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  47770. /* Should fail since ecKey is empty */
  47771. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  47772. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  47773. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47774. if (EXPECT_FAIL()) {
  47775. wolfSSL_EC_KEY_free(ecKey);
  47776. }
  47777. wolfSSL_EVP_PKEY_free(pkey);
  47778. #endif
  47779. #endif
  47780. return EXPECT_RESULT();
  47781. }
  47782. static int test_EVP_PKEY_cmp(void)
  47783. {
  47784. EXPECT_DECLS;
  47785. #if defined(OPENSSL_EXTRA)
  47786. EVP_PKEY *a = NULL;
  47787. EVP_PKEY *b = NULL;
  47788. const unsigned char *in;
  47789. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47790. in = client_key_der_2048;
  47791. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47792. &in, (long)sizeof_client_key_der_2048));
  47793. in = client_key_der_2048;
  47794. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47795. &in, (long)sizeof_client_key_der_2048));
  47796. /* Test success case RSA */
  47797. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47798. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47799. #else
  47800. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47801. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47802. EVP_PKEY_free(b);
  47803. b = NULL;
  47804. EVP_PKEY_free(a);
  47805. a = NULL;
  47806. #endif
  47807. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47808. in = ecc_clikey_der_256;
  47809. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47810. &in, (long)sizeof_ecc_clikey_der_256));
  47811. in = ecc_clikey_der_256;
  47812. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47813. &in, (long)sizeof_ecc_clikey_der_256));
  47814. /* Test success case ECC */
  47815. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47816. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47817. #else
  47818. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47819. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47820. EVP_PKEY_free(b);
  47821. b = NULL;
  47822. EVP_PKEY_free(a);
  47823. a = NULL;
  47824. #endif
  47825. /* Test failure cases */
  47826. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  47827. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47828. in = client_key_der_2048;
  47829. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47830. &in, (long)sizeof_client_key_der_2048));
  47831. in = ecc_clikey_der_256;
  47832. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47833. &in, (long)sizeof_ecc_clikey_der_256));
  47834. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47835. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  47836. #else
  47837. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47838. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47839. EVP_PKEY_free(b);
  47840. b = NULL;
  47841. EVP_PKEY_free(a);
  47842. a = NULL;
  47843. #endif
  47844. /* invalid or empty failure cases */
  47845. a = EVP_PKEY_new();
  47846. b = EVP_PKEY_new();
  47847. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47848. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  47849. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  47850. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  47851. #ifdef NO_RSA
  47852. /* Type check will fail since RSA is the default EVP key type */
  47853. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  47854. #else
  47855. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47856. #endif
  47857. #else
  47858. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  47859. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  47860. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  47861. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47862. #endif
  47863. EVP_PKEY_free(b);
  47864. EVP_PKEY_free(a);
  47865. (void)in;
  47866. #endif
  47867. return EXPECT_RESULT();
  47868. }
  47869. static int test_ERR_load_crypto_strings(void)
  47870. {
  47871. #if defined(OPENSSL_ALL)
  47872. ERR_load_crypto_strings();
  47873. return TEST_SUCCESS;
  47874. #else
  47875. return TEST_SKIPPED;
  47876. #endif
  47877. }
  47878. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47879. static void free_x509(X509* x)
  47880. {
  47881. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47882. }
  47883. #endif
  47884. static int test_sk_X509(void)
  47885. {
  47886. EXPECT_DECLS;
  47887. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47888. {
  47889. STACK_OF(X509)* s = NULL;
  47890. ExpectNotNull(s = sk_X509_new_null());
  47891. ExpectIntEQ(sk_X509_num(s), 0);
  47892. sk_X509_pop_free(s, NULL);
  47893. ExpectNotNull(s = sk_X509_new_null());
  47894. ExpectIntEQ(sk_X509_num(s), 0);
  47895. sk_X509_pop_free(s, NULL);
  47896. ExpectNotNull(s = sk_X509_new_null());
  47897. sk_X509_push(s, (X509*)1);
  47898. ExpectIntEQ(sk_X509_num(s), 1);
  47899. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47900. sk_X509_push(s, (X509*)2);
  47901. ExpectIntEQ(sk_X509_num(s), 2);
  47902. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47903. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47904. sk_X509_push(s, (X509*)2);
  47905. sk_X509_pop_free(s, free_x509);
  47906. }
  47907. {
  47908. /* Push a list of 10 X509s onto stack, then verify that
  47909. * value(), push(), shift(), and pop() behave as expected. */
  47910. STACK_OF(X509)* s = NULL;
  47911. X509* xList[10];
  47912. int i = 0;
  47913. const int len = (sizeof(xList) / sizeof(xList[0]));
  47914. for (i = 0; i < len; ++i) {
  47915. xList[i] = NULL;
  47916. ExpectNotNull(xList[i] = X509_new());
  47917. }
  47918. /* test push, pop, and free */
  47919. ExpectNotNull(s = sk_X509_new_null());
  47920. for (i = 0; i < len; ++i) {
  47921. sk_X509_push(s, xList[i]);
  47922. ExpectIntEQ(sk_X509_num(s), i + 1);
  47923. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47924. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47925. }
  47926. /* pop returns and removes last pushed on stack, which is index 0
  47927. * in sk_x509_value */
  47928. for (i = 0; i < len; ++i) {
  47929. X509 * x = sk_X509_value(s, 0);
  47930. X509 * y = sk_X509_pop(s);
  47931. X509 * z = xList[len - 1 - i];
  47932. ExpectIntEQ((x == y), 1);
  47933. ExpectIntEQ((x == z), 1);
  47934. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47935. }
  47936. sk_free(s);
  47937. s = NULL;
  47938. /* test push, shift, and free */
  47939. ExpectNotNull(s = sk_X509_new_null());
  47940. for (i = 0; i < len; ++i) {
  47941. sk_X509_push(s, xList[i]);
  47942. ExpectIntEQ(sk_X509_num(s), i + 1);
  47943. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47944. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47945. }
  47946. /* shift returns and removes first pushed on stack, which is index i
  47947. * in sk_x509_value() */
  47948. for (i = 0; i < len; ++i) {
  47949. X509 * x = sk_X509_value(s, len - 1 - i);
  47950. X509 * y = sk_X509_shift(s);
  47951. X509 * z = xList[i];
  47952. ExpectIntEQ((x == y), 1);
  47953. ExpectIntEQ((x == z), 1);
  47954. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47955. }
  47956. sk_free(s);
  47957. for (i = 0; i < len; ++i)
  47958. X509_free(xList[i]);
  47959. }
  47960. #endif
  47961. return EXPECT_RESULT();
  47962. }
  47963. static int test_sk_X509_CRL(void)
  47964. {
  47965. EXPECT_DECLS;
  47966. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47967. X509_CRL* crl = NULL;
  47968. XFILE fp = XBADFILE;
  47969. STACK_OF(X509_CRL)* s = NULL;
  47970. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47971. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47972. NULL, NULL));
  47973. if (fp != XBADFILE)
  47974. XFCLOSE(fp);
  47975. ExpectNotNull(s = sk_X509_CRL_new());
  47976. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47977. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47978. if (EXPECT_FAIL()) {
  47979. X509_CRL_free(crl);
  47980. }
  47981. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47982. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47983. sk_X509_CRL_free(s);
  47984. #endif
  47985. return EXPECT_RESULT();
  47986. }
  47987. static int test_X509_get_signature_nid(void)
  47988. {
  47989. EXPECT_DECLS;
  47990. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47991. X509* x509 = NULL;
  47992. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47993. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47994. SSL_FILETYPE_PEM));
  47995. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47996. X509_free(x509);
  47997. #endif
  47998. return EXPECT_RESULT();
  47999. }
  48000. static int test_X509_REQ(void)
  48001. {
  48002. EXPECT_DECLS;
  48003. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  48004. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  48005. X509_NAME* name = NULL;
  48006. #ifndef NO_RSA
  48007. X509_NAME* subject = NULL;
  48008. #endif
  48009. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48010. X509_REQ* req = NULL;
  48011. EVP_PKEY* priv = NULL;
  48012. EVP_PKEY* pub = NULL;
  48013. unsigned char* der = NULL;
  48014. int len;
  48015. #endif
  48016. #ifndef NO_RSA
  48017. EVP_MD_CTX *mctx = NULL;
  48018. EVP_PKEY_CTX *pkctx = NULL;
  48019. #ifdef USE_CERT_BUFFERS_1024
  48020. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  48021. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  48022. #elif defined(USE_CERT_BUFFERS_2048)
  48023. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  48024. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  48025. #endif
  48026. #endif
  48027. #ifdef HAVE_ECC
  48028. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  48029. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  48030. #endif
  48031. ExpectNotNull(name = X509_NAME_new());
  48032. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  48033. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  48034. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  48035. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  48036. #ifndef NO_RSA
  48037. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  48038. (long)sizeof_client_key_der_2048));
  48039. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  48040. (long)sizeof_client_keypub_der_2048));
  48041. ExpectNotNull(req = X509_REQ_new());
  48042. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  48043. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  48044. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  48045. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  48046. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  48047. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  48048. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  48049. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48050. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  48051. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  48052. len = i2d_X509_REQ(req, &der);
  48053. DEBUG_WRITE_DER(der, len, "req.der");
  48054. #ifdef USE_CERT_BUFFERS_1024
  48055. ExpectIntEQ(len, 381);
  48056. #else
  48057. ExpectIntEQ(len, 643);
  48058. #endif
  48059. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  48060. der = NULL;
  48061. mctx = EVP_MD_CTX_new();
  48062. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  48063. WOLFSSL_SUCCESS);
  48064. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  48065. EVP_MD_CTX_free(mctx);
  48066. mctx = NULL;
  48067. X509_REQ_free(NULL);
  48068. X509_REQ_free(req);
  48069. req = NULL;
  48070. /* Test getting the subject from a newly created X509_REQ */
  48071. ExpectNotNull(req = X509_REQ_new());
  48072. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  48073. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  48074. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  48075. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  48076. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  48077. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  48078. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  48079. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  48080. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  48081. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  48082. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  48083. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  48084. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  48085. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  48086. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  48087. len = i2d_X509_REQ(req, &der);
  48088. DEBUG_WRITE_DER(der, len, "req2.der");
  48089. #ifdef USE_CERT_BUFFERS_1024
  48090. ExpectIntEQ(len, 435);
  48091. #else
  48092. ExpectIntEQ(len, 696);
  48093. #endif
  48094. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  48095. der = NULL;
  48096. EVP_PKEY_free(pub);
  48097. pub = NULL;
  48098. EVP_PKEY_free(priv);
  48099. priv = NULL;
  48100. X509_REQ_free(req);
  48101. req = NULL;
  48102. #endif
  48103. #ifdef HAVE_ECC
  48104. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  48105. sizeof_ecc_clikey_der_256));
  48106. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  48107. sizeof_ecc_clikeypub_der_256));
  48108. ExpectNotNull(req = X509_REQ_new());
  48109. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  48110. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  48111. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  48112. /* Signature is random and may be shorter or longer. */
  48113. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  48114. ExpectIntLE(len, 253);
  48115. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  48116. X509_REQ_free(req);
  48117. EVP_PKEY_free(pub);
  48118. EVP_PKEY_free(priv);
  48119. #ifdef FP_ECC
  48120. wc_ecc_fp_free();
  48121. #endif
  48122. #endif /* HAVE_ECC */
  48123. X509_NAME_free(name);
  48124. #endif
  48125. return EXPECT_RESULT();
  48126. }
  48127. static int test_wolfssl_PKCS7(void)
  48128. {
  48129. EXPECT_DECLS;
  48130. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  48131. !defined(NO_RSA)
  48132. PKCS7* pkcs7 = NULL;
  48133. byte data[FOURK_BUF];
  48134. word32 len = sizeof(data);
  48135. const byte* p = data;
  48136. byte content[] = "Test data to encode.";
  48137. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  48138. BIO* bio = NULL;
  48139. byte key[sizeof(client_key_der_2048)];
  48140. word32 keySz = (word32)sizeof(key);
  48141. byte* out = NULL;
  48142. #endif
  48143. ExpectIntGT((len = (word32)CreatePKCS7SignedData(data, (int)len, content,
  48144. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  48145. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, (int)len));
  48146. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  48147. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  48148. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  48149. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  48150. PKCS7_free(pkcs7);
  48151. pkcs7 = NULL;
  48152. /* fail case, without PKCS7_NOVERIFY */
  48153. p = data;
  48154. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  48155. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  48156. 0), WOLFSSL_FAILURE);
  48157. PKCS7_free(pkcs7);
  48158. pkcs7 = NULL;
  48159. /* success case, with PKCS7_NOVERIFY */
  48160. p = data;
  48161. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  48162. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  48163. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  48164. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  48165. /* test i2d */
  48166. XMEMCPY(key, client_key_der_2048, keySz);
  48167. if (pkcs7 != NULL) {
  48168. pkcs7->privateKey = key;
  48169. pkcs7->privateKeySz = (word32)sizeof(key);
  48170. pkcs7->encryptOID = RSAk;
  48171. #ifdef NO_SHA
  48172. pkcs7->hashOID = SHA256h;
  48173. #else
  48174. pkcs7->hashOID = SHAh;
  48175. #endif
  48176. }
  48177. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48178. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  48179. #ifndef NO_ASN_TIME
  48180. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  48181. #else
  48182. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  48183. #endif
  48184. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48185. BIO_free(bio);
  48186. #endif
  48187. PKCS7_free(NULL);
  48188. PKCS7_free(pkcs7);
  48189. #endif
  48190. return EXPECT_RESULT();
  48191. }
  48192. static int test_wolfSSL_PKCS7_sign(void)
  48193. {
  48194. EXPECT_DECLS;
  48195. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  48196. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48197. PKCS7* p7 = NULL;
  48198. PKCS7* p7Ver = NULL;
  48199. byte* out = NULL;
  48200. byte* tmpPtr = NULL;
  48201. int outLen = 0;
  48202. int flags = 0;
  48203. byte data[] = "Test data to encode.";
  48204. const char* cert = "./certs/server-cert.pem";
  48205. const char* key = "./certs/server-key.pem";
  48206. const char* ca = "./certs/ca-cert.pem";
  48207. WOLFSSL_BIO* certBio = NULL;
  48208. WOLFSSL_BIO* keyBio = NULL;
  48209. WOLFSSL_BIO* caBio = NULL;
  48210. WOLFSSL_BIO* inBio = NULL;
  48211. X509* signCert = NULL;
  48212. EVP_PKEY* signKey = NULL;
  48213. X509* caCert = NULL;
  48214. X509_STORE* store = NULL;
  48215. #ifndef NO_PKCS7_STREAM
  48216. int z;
  48217. int ret;
  48218. #endif /* !NO_PKCS7_STREAM */
  48219. /* read signer cert/key into BIO */
  48220. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  48221. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  48222. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  48223. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  48224. /* read CA cert into store (for verify) */
  48225. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  48226. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  48227. ExpectNotNull(store = X509_STORE_new());
  48228. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  48229. /* data to be signed into BIO */
  48230. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48231. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48232. /* PKCS7_sign, bad args: signer NULL */
  48233. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  48234. /* PKCS7_sign, bad args: signer key NULL */
  48235. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  48236. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  48237. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  48238. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  48239. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  48240. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  48241. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  48242. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  48243. {
  48244. flags = PKCS7_BINARY;
  48245. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48246. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48247. /* verify with d2i_PKCS7 */
  48248. tmpPtr = out;
  48249. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  48250. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48251. PKCS7_free(p7Ver);
  48252. p7Ver = NULL;
  48253. /* verify with wc_PKCS7_VerifySignedData */
  48254. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48255. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  48256. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  48257. #ifndef NO_PKCS7_STREAM
  48258. /* verify with wc_PKCS7_VerifySignedData streaming */
  48259. wc_PKCS7_Free(p7Ver);
  48260. p7Ver = NULL;
  48261. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48262. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  48263. /* test for streaming */
  48264. ret = -1;
  48265. for (z = 0; z < outLen && ret != 0; z++) {
  48266. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  48267. if (ret < 0){
  48268. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  48269. }
  48270. }
  48271. ExpectIntEQ(ret, 0);
  48272. #endif /* !NO_PKCS7_STREAM */
  48273. /* compare the signer found to expected signer */
  48274. ExpectIntNE(p7Ver->verifyCertSz, 0);
  48275. tmpPtr = NULL;
  48276. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  48277. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  48278. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  48279. tmpPtr = NULL;
  48280. wc_PKCS7_Free(p7Ver);
  48281. p7Ver = NULL;
  48282. ExpectNotNull(out);
  48283. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48284. out = NULL;
  48285. PKCS7_free(p7);
  48286. p7 = NULL;
  48287. }
  48288. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  48289. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  48290. {
  48291. /* re-populate input BIO, may have been consumed */
  48292. BIO_free(inBio);
  48293. inBio = NULL;
  48294. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48295. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48296. flags = PKCS7_BINARY | PKCS7_STREAM;
  48297. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48298. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  48299. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48300. /* PKCS7_final, bad args: PKCS7 null */
  48301. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  48302. /* PKCS7_final, bad args: PKCS7 null */
  48303. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  48304. tmpPtr = out;
  48305. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  48306. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48307. PKCS7_free(p7Ver);
  48308. p7Ver = NULL;
  48309. ExpectNotNull(out);
  48310. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48311. out = NULL;
  48312. PKCS7_free(p7);
  48313. p7 = NULL;
  48314. }
  48315. /* TEST SUCCESS: Detached, not streaming, not MIME */
  48316. {
  48317. /* re-populate input BIO, may have been consumed */
  48318. BIO_free(inBio);
  48319. inBio = NULL;
  48320. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48321. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48322. flags = PKCS7_BINARY | PKCS7_DETACHED;
  48323. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48324. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48325. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  48326. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48327. if (p7Ver != NULL) {
  48328. p7Ver->content = data;
  48329. p7Ver->contentSz = sizeof(data);
  48330. }
  48331. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  48332. wc_PKCS7_Free(p7Ver);
  48333. p7Ver = NULL;
  48334. #ifndef NO_PKCS7_STREAM
  48335. /* verify with wc_PKCS7_VerifySignedData streaming */
  48336. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48337. if (p7Ver != NULL) {
  48338. p7Ver->content = data;
  48339. p7Ver->contentSz = sizeof(data);
  48340. }
  48341. /* test for streaming */
  48342. ret = -1;
  48343. for (z = 0; z < outLen && ret != 0; z++) {
  48344. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  48345. if (ret < 0){
  48346. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  48347. }
  48348. }
  48349. ExpectIntEQ(ret, 0);
  48350. wc_PKCS7_Free(p7Ver);
  48351. p7Ver = NULL;
  48352. #endif /* !NO_PKCS7_STREAM */
  48353. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  48354. * yet support detached content */
  48355. tmpPtr = out;
  48356. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  48357. PKCS7_free(p7Ver);
  48358. p7Ver = NULL;
  48359. ExpectNotNull(out);
  48360. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48361. out = NULL;
  48362. PKCS7_free(p7);
  48363. p7 = NULL;
  48364. }
  48365. /* TEST SUCCESS: Detached, streaming, not MIME */
  48366. {
  48367. /* re-populate input BIO, may have been consumed */
  48368. BIO_free(inBio);
  48369. inBio = NULL;
  48370. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48371. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48372. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  48373. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48374. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  48375. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  48376. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  48377. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48378. if (p7Ver != NULL) {
  48379. p7Ver->content = data;
  48380. p7Ver->contentSz = sizeof(data);
  48381. }
  48382. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  48383. wc_PKCS7_Free(p7Ver);
  48384. p7Ver = NULL;
  48385. ExpectNotNull(out);
  48386. #ifndef NO_PKCS7_STREAM
  48387. /* verify with wc_PKCS7_VerifySignedData streaming */
  48388. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  48389. if (p7Ver != NULL) {
  48390. p7Ver->content = data;
  48391. p7Ver->contentSz = sizeof(data);
  48392. }
  48393. /* test for streaming */
  48394. ret = -1;
  48395. for (z = 0; z < outLen && ret != 0; z++) {
  48396. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  48397. if (ret < 0){
  48398. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  48399. }
  48400. }
  48401. ExpectIntEQ(ret, 0);
  48402. ExpectNotNull(out);
  48403. wc_PKCS7_Free(p7Ver);
  48404. p7Ver = NULL;
  48405. #endif /* !NO_PKCS7_STREAM */
  48406. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48407. PKCS7_free(p7);
  48408. p7 = NULL;
  48409. }
  48410. X509_STORE_free(store);
  48411. X509_free(caCert);
  48412. X509_free(signCert);
  48413. EVP_PKEY_free(signKey);
  48414. BIO_free(inBio);
  48415. BIO_free(keyBio);
  48416. BIO_free(certBio);
  48417. BIO_free(caBio);
  48418. #endif
  48419. return EXPECT_RESULT();
  48420. }
  48421. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  48422. {
  48423. EXPECT_DECLS;
  48424. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  48425. PKCS7_SIGNED* pkcs7 = NULL;
  48426. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  48427. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  48428. PKCS7_SIGNED_free(pkcs7);
  48429. #endif
  48430. return EXPECT_RESULT();
  48431. }
  48432. #ifndef NO_BIO
  48433. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  48434. {
  48435. EXPECT_DECLS;
  48436. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  48437. PKCS7* pkcs7 = NULL;
  48438. BIO* bio = NULL;
  48439. const byte* cert_buf = NULL;
  48440. int ret = 0;
  48441. WC_RNG rng;
  48442. const byte data[] = { /* Hello World */
  48443. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  48444. 0x72,0x6c,0x64
  48445. };
  48446. #ifndef NO_RSA
  48447. #if defined(USE_CERT_BUFFERS_2048)
  48448. byte key[sizeof(client_key_der_2048)];
  48449. byte cert[sizeof(client_cert_der_2048)];
  48450. word32 keySz = (word32)sizeof(key);
  48451. word32 certSz = (word32)sizeof(cert);
  48452. XMEMSET(key, 0, keySz);
  48453. XMEMSET(cert, 0, certSz);
  48454. XMEMCPY(key, client_key_der_2048, keySz);
  48455. XMEMCPY(cert, client_cert_der_2048, certSz);
  48456. #elif defined(USE_CERT_BUFFERS_1024)
  48457. byte key[sizeof_client_key_der_1024];
  48458. byte cert[sizeof(sizeof_client_cert_der_1024)];
  48459. word32 keySz = (word32)sizeof(key);
  48460. word32 certSz = (word32)sizeof(cert);
  48461. XMEMSET(key, 0, keySz);
  48462. XMEMSET(cert, 0, certSz);
  48463. XMEMCPY(key, client_key_der_1024, keySz);
  48464. XMEMCPY(cert, client_cert_der_1024, certSz);
  48465. #else
  48466. unsigned char cert[ONEK_BUF];
  48467. unsigned char key[ONEK_BUF];
  48468. XFILE fp = XBADFILE;
  48469. int certSz;
  48470. int keySz;
  48471. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  48472. XBADFILE);
  48473. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  48474. fp), 0);
  48475. if (fp != XBADFILE) {
  48476. XFCLOSE(fp);
  48477. fp = XBADFILE;
  48478. }
  48479. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  48480. XBADFILE);
  48481. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  48482. 0);
  48483. if (fp != XBADFILE) {
  48484. XFCLOSE(fp);
  48485. fp = XBADFILE;
  48486. }
  48487. #endif
  48488. #elif defined(HAVE_ECC)
  48489. #if defined(USE_CERT_BUFFERS_256)
  48490. unsigned char cert[sizeof(cliecc_cert_der_256)];
  48491. unsigned char key[sizeof(ecc_clikey_der_256)];
  48492. int certSz = (int)sizeof(cert);
  48493. int keySz = (int)sizeof(key);
  48494. XMEMSET(cert, 0, certSz);
  48495. XMEMSET(key, 0, keySz);
  48496. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  48497. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  48498. #else
  48499. unsigned char cert[ONEK_BUF];
  48500. unsigned char key[ONEK_BUF];
  48501. XFILE fp = XBADFILE;
  48502. int certSz, keySz;
  48503. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  48504. XBADFILE);
  48505. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  48506. fp), 0);
  48507. if (fp != XBADFILE) {
  48508. XFCLOSE(fp);
  48509. fp = XBADFILE;
  48510. }
  48511. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  48512. XBADFILE);
  48513. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  48514. 0);
  48515. if (fp != XBADFILE) {
  48516. XFCLOSE(fp);
  48517. fp = XBADFILE;
  48518. }
  48519. #endif
  48520. #else
  48521. #error PKCS7 requires ECC or RSA
  48522. #endif
  48523. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  48524. /* initialize with DER encoded cert */
  48525. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  48526. /* init rng */
  48527. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48528. ExpectIntEQ(wc_InitRng(&rng), 0);
  48529. if (pkcs7 != NULL) {
  48530. pkcs7->rng = &rng;
  48531. pkcs7->content = (byte*)data; /* not used for ex */
  48532. pkcs7->contentSz = (word32)sizeof(data);
  48533. pkcs7->contentOID = SIGNED_DATA;
  48534. pkcs7->privateKey = key;
  48535. pkcs7->privateKeySz = (word32)sizeof(key);
  48536. pkcs7->encryptOID = RSAk;
  48537. #ifdef NO_SHA
  48538. pkcs7->hashOID = SHA256h;
  48539. #else
  48540. pkcs7->hashOID = SHAh;
  48541. #endif
  48542. pkcs7->signedAttribs = NULL;
  48543. pkcs7->signedAttribsSz = 0;
  48544. }
  48545. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48546. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  48547. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  48548. /* Read PKCS#7 PEM from BIO */
  48549. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  48550. ExpectIntGE(ret, 0);
  48551. BIO_free(bio);
  48552. wc_PKCS7_Free(pkcs7);
  48553. wc_FreeRng(&rng);
  48554. #endif
  48555. return EXPECT_RESULT();
  48556. }
  48557. #ifdef HAVE_SMIME
  48558. /* // NOLINTBEGIN(clang-analyzer-unix.Stream) */
  48559. static int test_wolfSSL_SMIME_read_PKCS7(void)
  48560. {
  48561. EXPECT_DECLS;
  48562. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  48563. !defined(NO_RSA)
  48564. PKCS7* pkcs7 = NULL;
  48565. BIO* bio = NULL;
  48566. BIO* bcont = NULL;
  48567. BIO* out = NULL;
  48568. const byte* outBuf = NULL;
  48569. int outBufLen = 0;
  48570. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  48571. XFILE smimeTestFile = XBADFILE;
  48572. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  48573. XBADFILE);
  48574. /* smime-test.p7s */
  48575. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  48576. ExpectNotNull(bio);
  48577. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48578. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48579. ExpectNotNull(pkcs7);
  48580. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48581. PKCS7_NOVERIFY), SSL_SUCCESS);
  48582. if (smimeTestFile != XBADFILE) {
  48583. XFCLOSE(smimeTestFile);
  48584. smimeTestFile = XBADFILE;
  48585. }
  48586. if (bcont) BIO_free(bcont);
  48587. bcont = NULL;
  48588. wolfSSL_PKCS7_free(pkcs7);
  48589. pkcs7 = NULL;
  48590. /* smime-test-multipart.p7s */
  48591. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  48592. ExpectFalse(smimeTestFile == XBADFILE);
  48593. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48594. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48595. ExpectNotNull(pkcs7);
  48596. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48597. PKCS7_NOVERIFY), SSL_SUCCESS);
  48598. if (smimeTestFile != XBADFILE) {
  48599. XFCLOSE(smimeTestFile);
  48600. smimeTestFile = XBADFILE;
  48601. }
  48602. if (bcont) BIO_free(bcont);
  48603. bcont = NULL;
  48604. wolfSSL_PKCS7_free(pkcs7);
  48605. pkcs7 = NULL;
  48606. /* smime-test-multipart-badsig.p7s */
  48607. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  48608. ExpectFalse(smimeTestFile == XBADFILE);
  48609. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48610. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48611. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  48612. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48613. PKCS7_NOVERIFY), SSL_FAILURE);
  48614. if (smimeTestFile != XBADFILE) {
  48615. XFCLOSE(smimeTestFile);
  48616. smimeTestFile = XBADFILE;
  48617. }
  48618. if (bcont) BIO_free(bcont);
  48619. bcont = NULL;
  48620. wolfSSL_PKCS7_free(pkcs7);
  48621. pkcs7 = NULL;
  48622. /* smime-test-canon.p7s */
  48623. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  48624. ExpectFalse(smimeTestFile == XBADFILE);
  48625. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48626. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48627. ExpectNotNull(pkcs7);
  48628. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48629. PKCS7_NOVERIFY), SSL_SUCCESS);
  48630. if (smimeTestFile != XBADFILE) {
  48631. XFCLOSE(smimeTestFile);
  48632. smimeTestFile = XBADFILE;
  48633. }
  48634. if (bcont) BIO_free(bcont);
  48635. bcont = NULL;
  48636. wolfSSL_PKCS7_free(pkcs7);
  48637. pkcs7 = NULL;
  48638. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  48639. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  48640. ExpectFalse(smimeTestFile == XBADFILE);
  48641. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48642. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48643. ExpectNotNull(pkcs7);
  48644. out = wolfSSL_BIO_new(BIO_s_mem());
  48645. ExpectNotNull(out);
  48646. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  48647. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  48648. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  48649. /* Content-Type should not show up at beginning of output buffer */
  48650. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  48651. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  48652. BIO_free(out);
  48653. BIO_free(bio);
  48654. if (bcont) BIO_free(bcont);
  48655. wolfSSL_PKCS7_free(pkcs7);
  48656. #endif
  48657. return EXPECT_RESULT();
  48658. }
  48659. /* // NOLINTEND(clang-analyzer-unix.Stream) */
  48660. static int test_wolfSSL_SMIME_write_PKCS7(void)
  48661. {
  48662. EXPECT_DECLS;
  48663. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  48664. PKCS7* p7 = NULL;
  48665. PKCS7* p7Ver = NULL;
  48666. int flags = 0;
  48667. byte data[] = "Test data to encode.";
  48668. const char* cert = "./certs/server-cert.pem";
  48669. const char* key = "./certs/server-key.pem";
  48670. const char* ca = "./certs/ca-cert.pem";
  48671. WOLFSSL_BIO* certBio = NULL;
  48672. WOLFSSL_BIO* keyBio = NULL;
  48673. WOLFSSL_BIO* caBio = NULL;
  48674. WOLFSSL_BIO* inBio = NULL;
  48675. WOLFSSL_BIO* outBio = NULL;
  48676. WOLFSSL_BIO* content = NULL;
  48677. X509* signCert = NULL;
  48678. EVP_PKEY* signKey = NULL;
  48679. X509* caCert = NULL;
  48680. X509_STORE* store = NULL;
  48681. /* read signer cert/key into BIO */
  48682. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  48683. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  48684. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  48685. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  48686. /* read CA cert into store (for verify) */
  48687. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  48688. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  48689. ExpectNotNull(store = X509_STORE_new());
  48690. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  48691. /* generate and verify SMIME: not detached */
  48692. {
  48693. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48694. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48695. flags = PKCS7_STREAM;
  48696. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48697. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48698. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48699. /* bad arg: out NULL */
  48700. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  48701. /* bad arg: pkcs7 NULL */
  48702. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  48703. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48704. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48705. BIO_free(content);
  48706. content = NULL;
  48707. BIO_free(inBio);
  48708. inBio = NULL;
  48709. BIO_free(outBio);
  48710. outBio = NULL;
  48711. PKCS7_free(p7Ver);
  48712. p7Ver = NULL;
  48713. PKCS7_free(p7);
  48714. p7 = NULL;
  48715. }
  48716. /* generate and verify SMIME: not detached, add Content-Type */
  48717. {
  48718. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48719. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48720. flags = PKCS7_STREAM | PKCS7_TEXT;
  48721. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48722. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48723. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48724. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48725. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48726. BIO_free(content);
  48727. content = NULL;
  48728. BIO_free(inBio);
  48729. inBio = NULL;
  48730. BIO_free(outBio);
  48731. outBio = NULL;
  48732. PKCS7_free(p7Ver);
  48733. p7Ver = NULL;
  48734. PKCS7_free(p7);
  48735. p7 = NULL;
  48736. }
  48737. /* generate and verify SMIME: detached */
  48738. {
  48739. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48740. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48741. flags = PKCS7_DETACHED | PKCS7_STREAM;
  48742. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48743. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48744. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48745. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48746. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  48747. BIO_free(content);
  48748. content = NULL;
  48749. BIO_free(inBio);
  48750. inBio = NULL;
  48751. BIO_free(outBio);
  48752. outBio = NULL;
  48753. PKCS7_free(p7Ver);
  48754. p7Ver = NULL;
  48755. PKCS7_free(p7);
  48756. p7 = NULL;
  48757. }
  48758. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  48759. {
  48760. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48761. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48762. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  48763. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48764. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48765. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48766. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48767. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  48768. BIO_free(content);
  48769. content = NULL;
  48770. BIO_free(inBio);
  48771. inBio = NULL;
  48772. BIO_free(outBio);
  48773. outBio = NULL;
  48774. PKCS7_free(p7Ver);
  48775. p7Ver = NULL;
  48776. PKCS7_free(p7);
  48777. p7 = NULL;
  48778. }
  48779. X509_STORE_free(store);
  48780. X509_free(caCert);
  48781. X509_free(signCert);
  48782. EVP_PKEY_free(signKey);
  48783. BIO_free(keyBio);
  48784. BIO_free(certBio);
  48785. BIO_free(caBio);
  48786. #endif
  48787. return EXPECT_RESULT();
  48788. }
  48789. #endif /* HAVE_SMIME */
  48790. #endif /* !NO_BIO */
  48791. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  48792. * returns 0) */
  48793. static int test_X509_STORE_No_SSL_CTX(void)
  48794. {
  48795. EXPECT_DECLS;
  48796. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48797. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48798. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48799. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48800. defined(HAVE_CRL) && !defined(NO_RSA)
  48801. X509_STORE * store = NULL;
  48802. X509_STORE_CTX * storeCtx = NULL;
  48803. X509_CRL * crl = NULL;
  48804. X509 * ca = NULL;
  48805. X509 * cert = NULL;
  48806. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48807. const char srvCert[] = "./certs/server-cert.pem";
  48808. const char caCert[] = "./certs/ca-cert.pem";
  48809. const char caDir[] = "./certs/crl/hash_pem";
  48810. XFILE fp = XBADFILE;
  48811. X509_LOOKUP * lookup = NULL;
  48812. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48813. /* Set up store with CA */
  48814. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48815. SSL_FILETYPE_PEM)));
  48816. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48817. /* Add CRL lookup directory to store
  48818. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  48819. * of crl.pem */
  48820. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48821. X509_LOOKUP_hash_dir())));
  48822. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  48823. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  48824. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48825. SSL_SUCCESS);
  48826. /* Add CRL to store NOT containing the verified certificate, which
  48827. * forces use of the CRL lookup directory */
  48828. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48829. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48830. NULL, NULL));
  48831. if (fp != XBADFILE)
  48832. XFCLOSE(fp);
  48833. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48834. /* Create verification context outside of an SSL session */
  48835. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48836. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48837. SSL_FILETYPE_PEM)));
  48838. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48839. /* Perform verification, which should NOT indicate CRL missing due to the
  48840. * store CM's X509 store pointer being NULL */
  48841. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48842. X509_CRL_free(crl);
  48843. X509_STORE_free(store);
  48844. X509_STORE_CTX_free(storeCtx);
  48845. X509_free(cert);
  48846. X509_free(ca);
  48847. #endif
  48848. return EXPECT_RESULT();
  48849. }
  48850. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  48851. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  48852. static int test_X509_LOOKUP_add_dir(void)
  48853. {
  48854. EXPECT_DECLS;
  48855. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48856. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48857. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48858. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48859. defined(HAVE_CRL) && !defined(NO_RSA)
  48860. X509_STORE * store = NULL;
  48861. X509_STORE_CTX * storeCtx = NULL;
  48862. X509_CRL * crl = NULL;
  48863. X509 * ca = NULL;
  48864. X509 * cert = NULL;
  48865. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48866. const char srvCert[] = "./certs/server-cert.pem";
  48867. const char caCert[] = "./certs/ca-cert.pem";
  48868. const char caDir[] = "./certs/crl/hash_der";
  48869. XFILE fp = XBADFILE;
  48870. X509_LOOKUP * lookup = NULL;
  48871. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48872. /* Set up store with CA */
  48873. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48874. SSL_FILETYPE_PEM)));
  48875. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48876. /* Add CRL lookup directory to store.
  48877. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  48878. * of crl.der */
  48879. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48880. X509_LOOKUP_hash_dir())));
  48881. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  48882. SSL_SUCCESS);
  48883. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48884. SSL_SUCCESS);
  48885. /* Add CRL to store NOT containing the verified certificate, which
  48886. * forces use of the CRL lookup directory */
  48887. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48888. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48889. NULL, NULL));
  48890. if (fp != XBADFILE) {
  48891. XFCLOSE(fp);
  48892. fp = XBADFILE;
  48893. }
  48894. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48895. /* Create verification context outside of an SSL session */
  48896. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48897. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48898. SSL_FILETYPE_PEM)));
  48899. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48900. /* Perform verification, which should NOT return CRL missing */
  48901. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48902. X509_CRL_free(crl);
  48903. crl = NULL;
  48904. X509_STORE_free(store);
  48905. store = NULL;
  48906. X509_STORE_CTX_free(storeCtx);
  48907. storeCtx = NULL;
  48908. X509_free(cert);
  48909. cert = NULL;
  48910. X509_free(ca);
  48911. ca = NULL;
  48912. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48913. * We should get CRL_MISSING at the end, because the lookup
  48914. * dir has only ASN1 CRLs. */
  48915. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48916. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48917. SSL_FILETYPE_PEM)));
  48918. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48919. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48920. X509_LOOKUP_hash_dir())));
  48921. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48922. SSL_SUCCESS);
  48923. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48924. SSL_SUCCESS);
  48925. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48926. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48927. NULL, NULL));
  48928. if (fp != XBADFILE) {
  48929. XFCLOSE(fp);
  48930. fp = XBADFILE;
  48931. }
  48932. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48933. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48934. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48935. SSL_FILETYPE_PEM)));
  48936. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48937. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48938. * in dir containing only ASN1/DER. */
  48939. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48940. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  48941. X509_V_ERR_UNABLE_TO_GET_CRL);
  48942. X509_CRL_free(crl);
  48943. X509_STORE_free(store);
  48944. X509_STORE_CTX_free(storeCtx);
  48945. X509_free(cert);
  48946. X509_free(ca);
  48947. #endif
  48948. return EXPECT_RESULT();
  48949. }
  48950. /*----------------------------------------------------------------------------*
  48951. | Certificate Failure Checks
  48952. *----------------------------------------------------------------------------*/
  48953. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48954. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48955. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48956. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48957. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48958. int type)
  48959. {
  48960. int ret;
  48961. WOLFSSL_CERT_MANAGER* cm = NULL;
  48962. switch (type) {
  48963. case TESTING_RSA:
  48964. #ifdef NO_RSA
  48965. fprintf(stderr, "RSA disabled, skipping test\n");
  48966. return ASN_SIG_CONFIRM_E;
  48967. #else
  48968. break;
  48969. #endif
  48970. case TESTING_ECC:
  48971. #ifndef HAVE_ECC
  48972. fprintf(stderr, "ECC disabled, skipping test\n");
  48973. return ASN_SIG_CONFIRM_E;
  48974. #else
  48975. break;
  48976. #endif
  48977. default:
  48978. fprintf(stderr, "Bad function argument\n");
  48979. return BAD_FUNC_ARG;
  48980. }
  48981. cm = wolfSSL_CertManagerNew();
  48982. if (cm == NULL) {
  48983. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48984. return -1;
  48985. }
  48986. #ifndef NO_FILESYSTEM
  48987. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48988. if (ret != WOLFSSL_SUCCESS) {
  48989. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48990. wolfSSL_CertManagerFree(cm);
  48991. return ret;
  48992. }
  48993. #else
  48994. (void)ca;
  48995. #endif
  48996. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, (long int)cert_sz,
  48997. WOLFSSL_FILETYPE_ASN1);
  48998. /* Let ExpectIntEQ handle return code */
  48999. wolfSSL_CertManagerFree(cm);
  49000. return ret;
  49001. }
  49002. #endif
  49003. #if !defined(NO_FILESYSTEM)
  49004. static int test_RsaSigFailure_cm(void)
  49005. {
  49006. EXPECT_DECLS;
  49007. #ifndef NO_RSA
  49008. const char* ca_cert = "./certs/ca-cert.pem";
  49009. const char* server_cert = "./certs/server-cert.der";
  49010. byte* cert_buf = NULL;
  49011. size_t cert_sz = 0;
  49012. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  49013. if (cert_buf != NULL) {
  49014. /* corrupt DER - invert last byte, which is signature */
  49015. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  49016. /* test bad cert */
  49017. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49018. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  49019. WOLFSSL_FATAL_ERROR);
  49020. #else
  49021. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  49022. ASN_SIG_CONFIRM_E);
  49023. #endif
  49024. }
  49025. /* load_file() uses malloc. */
  49026. if (cert_buf != NULL) {
  49027. free(cert_buf);
  49028. }
  49029. #endif /* !NO_RSA */
  49030. return EXPECT_RESULT();
  49031. }
  49032. static int test_EccSigFailure_cm(void)
  49033. {
  49034. EXPECT_DECLS;
  49035. #ifdef HAVE_ECC
  49036. /* self-signed ECC cert, so use server cert as CA */
  49037. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  49038. const char* server_cert = "./certs/server-ecc.der";
  49039. byte* cert_buf = NULL;
  49040. size_t cert_sz = 0;
  49041. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  49042. if (cert_buf != NULL) {
  49043. /* corrupt DER - invert last byte, which is signature */
  49044. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  49045. /* test bad cert */
  49046. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49047. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  49048. WOLFSSL_FATAL_ERROR);
  49049. #else
  49050. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  49051. ASN_SIG_CONFIRM_E);
  49052. #endif
  49053. }
  49054. /* load_file() uses malloc. */
  49055. if (cert_buf != NULL) {
  49056. free(cert_buf);
  49057. }
  49058. #ifdef FP_ECC
  49059. wc_ecc_fp_free();
  49060. #endif
  49061. #endif /* HAVE_ECC */
  49062. return EXPECT_RESULT();
  49063. }
  49064. #endif /* !NO_FILESYSTEM */
  49065. #endif /* NO_CERTS */
  49066. #ifdef WOLFSSL_TLS13
  49067. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  49068. #ifdef WC_SHA384_DIGEST_SIZE
  49069. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  49070. #else
  49071. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  49072. #endif
  49073. #endif
  49074. #ifdef WOLFSSL_EARLY_DATA
  49075. static const char earlyData[] = "Early Data";
  49076. static char earlyDataBuffer[1];
  49077. #endif
  49078. static int test_tls13_apis(void)
  49079. {
  49080. EXPECT_DECLS;
  49081. int ret;
  49082. #ifndef WOLFSSL_NO_TLS12
  49083. #ifndef NO_WOLFSSL_CLIENT
  49084. WOLFSSL_CTX* clientTls12Ctx = NULL;
  49085. WOLFSSL* clientTls12Ssl = NULL;
  49086. #endif
  49087. #ifndef NO_WOLFSSL_SERVER
  49088. WOLFSSL_CTX* serverTls12Ctx = NULL;
  49089. WOLFSSL* serverTls12Ssl = NULL;
  49090. #endif
  49091. #endif
  49092. #ifndef NO_WOLFSSL_CLIENT
  49093. WOLFSSL_CTX* clientCtx = NULL;
  49094. WOLFSSL* clientSsl = NULL;
  49095. #endif
  49096. #ifndef NO_WOLFSSL_SERVER
  49097. WOLFSSL_CTX* serverCtx = NULL;
  49098. WOLFSSL* serverSsl = NULL;
  49099. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  49100. const char* ourCert = svrCertFile;
  49101. const char* ourKey = svrKeyFile;
  49102. #endif
  49103. #endif
  49104. int required;
  49105. #ifdef WOLFSSL_EARLY_DATA
  49106. int outSz;
  49107. #endif
  49108. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  49109. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  49110. #ifdef HAVE_PQC
  49111. WOLFSSL_KYBER_LEVEL1
  49112. #else
  49113. WOLFSSL_ECC_SECP256R1
  49114. #endif
  49115. };
  49116. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  49117. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  49118. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  49119. int numGroups = 2;
  49120. #endif
  49121. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49122. char groupList[] =
  49123. #ifdef HAVE_CURVE25519
  49124. "X25519:"
  49125. #endif
  49126. #ifdef HAVE_CURVE448
  49127. "X448:"
  49128. #endif
  49129. #ifndef NO_ECC_SECP
  49130. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  49131. "P-521:secp521r1:"
  49132. #endif
  49133. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  49134. "P-384:secp384r1:"
  49135. #endif
  49136. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  49137. "P-256:secp256r1"
  49138. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  49139. ":P256_KYBER_LEVEL1"
  49140. #endif
  49141. #endif
  49142. #endif /* !defined(NO_ECC_SECP) */
  49143. #ifdef HAVE_PQC
  49144. ":KYBER_LEVEL1"
  49145. #endif
  49146. "";
  49147. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  49148. (void)ret;
  49149. #ifndef WOLFSSL_NO_TLS12
  49150. #ifndef NO_WOLFSSL_CLIENT
  49151. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  49152. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  49153. #endif
  49154. #ifndef NO_WOLFSSL_SERVER
  49155. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  49156. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  49157. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  49158. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  49159. WOLFSSL_FILETYPE_PEM);
  49160. #endif
  49161. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  49162. #endif
  49163. #endif
  49164. #ifndef NO_WOLFSSL_CLIENT
  49165. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  49166. clientSsl = wolfSSL_new(clientCtx);
  49167. #endif
  49168. #ifndef NO_WOLFSSL_SERVER
  49169. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  49170. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  49171. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  49172. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  49173. #endif
  49174. serverSsl = wolfSSL_new(serverCtx);
  49175. ExpectNotNull(serverSsl);
  49176. #endif
  49177. #ifdef WOLFSSL_SEND_HRR_COOKIE
  49178. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  49179. #ifndef NO_WOLFSSL_CLIENT
  49180. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  49181. #endif
  49182. #ifndef NO_WOLFSSL_SERVER
  49183. #ifndef WOLFSSL_NO_TLS12
  49184. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  49185. BAD_FUNC_ARG);
  49186. #endif
  49187. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  49188. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  49189. WOLFSSL_SUCCESS);
  49190. #endif
  49191. #endif
  49192. #ifdef HAVE_SUPPORTED_CURVES
  49193. #ifdef HAVE_ECC
  49194. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  49195. BAD_FUNC_ARG);
  49196. #ifndef NO_WOLFSSL_SERVER
  49197. do {
  49198. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  49199. #ifdef WOLFSSL_ASYNC_CRYPT
  49200. if (ret == WC_PENDING_E)
  49201. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  49202. #endif
  49203. }
  49204. while (ret == WC_PENDING_E);
  49205. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49206. #endif
  49207. #ifndef NO_WOLFSSL_CLIENT
  49208. #ifndef WOLFSSL_NO_TLS12
  49209. do {
  49210. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  49211. #ifdef WOLFSSL_ASYNC_CRYPT
  49212. if (ret == WC_PENDING_E)
  49213. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  49214. #endif
  49215. }
  49216. while (ret == WC_PENDING_E);
  49217. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49218. #endif
  49219. do {
  49220. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  49221. #ifdef WOLFSSL_ASYNC_CRYPT
  49222. if (ret == WC_PENDING_E)
  49223. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  49224. #endif
  49225. }
  49226. while (ret == WC_PENDING_E);
  49227. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49228. #endif
  49229. #elif defined(HAVE_CURVE25519)
  49230. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  49231. #ifndef NO_WOLFSSL_SERVER
  49232. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  49233. WOLFSSL_SUCCESS);
  49234. #endif
  49235. #ifndef NO_WOLFSSL_CLIENT
  49236. #ifndef WOLFSSL_NO_TLS12
  49237. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  49238. WOLFSSL_SUCCESS);
  49239. #endif
  49240. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  49241. WOLFSSL_SUCCESS);
  49242. #endif
  49243. #elif defined(HAVE_CURVE448)
  49244. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  49245. #ifndef NO_WOLFSSL_SERVER
  49246. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  49247. WOLFSSL_SUCCESS);
  49248. #endif
  49249. #ifndef NO_WOLFSSL_CLIENT
  49250. #ifndef WOLFSSL_NO_TLS12
  49251. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  49252. WOLFSSL_SUCCESS);
  49253. #endif
  49254. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  49255. WOLFSSL_SUCCESS);
  49256. #endif
  49257. #else
  49258. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  49259. BAD_FUNC_ARG);
  49260. #ifndef NO_WOLFSSL_CLIENT
  49261. #ifndef WOLFSSL_NO_TLS12
  49262. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  49263. NOT_COMPILED_IN);
  49264. #endif
  49265. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  49266. NOT_COMPILED_IN);
  49267. #endif
  49268. #endif
  49269. #if defined(HAVE_PQC)
  49270. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  49271. #ifndef NO_WOLFSSL_SERVER
  49272. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  49273. WOLFSSL_SUCCESS);
  49274. #endif
  49275. #ifndef NO_WOLFSSL_CLIENT
  49276. #ifndef WOLFSSL_NO_TLS12
  49277. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  49278. BAD_FUNC_ARG);
  49279. #endif
  49280. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  49281. WOLFSSL_SUCCESS);
  49282. #endif
  49283. #endif
  49284. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  49285. #ifndef NO_WOLFSSL_SERVER
  49286. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  49287. #endif
  49288. #ifndef NO_WOLFSSL_CLIENT
  49289. #ifndef WOLFSSL_NO_TLS12
  49290. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  49291. #endif
  49292. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  49293. #endif
  49294. #endif /* HAVE_SUPPORTED_CURVES */
  49295. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  49296. #ifndef NO_WOLFSSL_CLIENT
  49297. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  49298. #endif
  49299. #ifndef NO_WOLFSSL_SERVER
  49300. #ifndef WOLFSSL_NO_TLS12
  49301. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  49302. #endif
  49303. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  49304. #endif
  49305. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  49306. #ifndef NO_WOLFSSL_CLIENT
  49307. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  49308. #endif
  49309. #ifndef NO_WOLFSSL_SERVER
  49310. #ifndef WOLFSSL_NO_TLS12
  49311. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  49312. #endif
  49313. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  49314. #endif
  49315. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  49316. #ifndef NO_WOLFSSL_CLIENT
  49317. #ifndef WOLFSSL_NO_TLS12
  49318. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  49319. #endif
  49320. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  49321. #endif
  49322. #ifndef NO_WOLFSSL_SERVER
  49323. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  49324. #endif
  49325. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  49326. #ifndef NO_WOLFSSL_CLIENT
  49327. #ifndef WOLFSSL_NO_TLS12
  49328. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  49329. #endif
  49330. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  49331. #endif
  49332. #ifndef NO_WOLFSSL_SERVER
  49333. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  49334. #endif
  49335. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  49336. #ifndef NO_WOLFSSL_CLIENT
  49337. #ifndef WOLFSSL_NO_TLS12
  49338. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  49339. #endif
  49340. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  49341. #endif
  49342. #ifndef NO_WOLFSSL_SERVER
  49343. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  49344. #endif
  49345. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  49346. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  49347. #ifndef NO_WOLFSSL_CLIENT
  49348. #ifndef WOLFSSL_NO_TLS12
  49349. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  49350. BAD_FUNC_ARG);
  49351. #endif
  49352. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  49353. #endif
  49354. #ifndef NO_WOLFSSL_SERVER
  49355. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  49356. #endif
  49357. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  49358. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  49359. #ifndef NO_WOLFSSL_SERVER
  49360. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  49361. #endif
  49362. #ifndef NO_WOLFSSL_CLIENT
  49363. #ifndef WOLFSSL_NO_TLS12
  49364. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  49365. BAD_FUNC_ARG);
  49366. #endif
  49367. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  49368. #endif
  49369. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  49370. #ifndef NO_WOLFSSL_SERVER
  49371. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  49372. #endif
  49373. #ifndef NO_WOLFSSL_CLIENT
  49374. #ifndef WOLFSSL_NO_TLS12
  49375. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  49376. BAD_FUNC_ARG);
  49377. #endif
  49378. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  49379. #endif
  49380. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  49381. #ifndef NO_WOLFSSL_CLIENT
  49382. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  49383. #endif
  49384. #ifndef NO_WOLFSSL_SERVER
  49385. #ifndef WOLFSSL_NO_TLS12
  49386. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  49387. BAD_FUNC_ARG);
  49388. #endif
  49389. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  49390. #endif
  49391. #endif
  49392. #ifdef HAVE_ECC
  49393. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  49394. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  49395. #ifndef NO_WOLFSSL_SERVER
  49396. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  49397. #endif
  49398. #ifndef NO_WOLFSSL_CLIENT
  49399. #ifndef WOLFSSL_NO_TLS12
  49400. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  49401. #endif
  49402. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  49403. #endif
  49404. #endif
  49405. #ifdef HAVE_SUPPORTED_CURVES
  49406. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  49407. #ifndef NO_WOLFSSL_CLIENT
  49408. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  49409. #endif
  49410. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  49411. #ifndef NO_WOLFSSL_CLIENT
  49412. #ifndef WOLFSSL_NO_TLS12
  49413. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  49414. WOLFSSL_SUCCESS);
  49415. #endif
  49416. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  49417. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  49418. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  49419. WOLFSSL_SUCCESS);
  49420. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  49421. BAD_FUNC_ARG);
  49422. #endif
  49423. #ifndef NO_WOLFSSL_SERVER
  49424. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  49425. WOLFSSL_SUCCESS);
  49426. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  49427. BAD_FUNC_ARG);
  49428. #endif
  49429. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  49430. #ifndef NO_WOLFSSL_CLIENT
  49431. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  49432. #endif
  49433. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  49434. #ifndef NO_WOLFSSL_CLIENT
  49435. #ifndef WOLFSSL_NO_TLS12
  49436. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  49437. WOLFSSL_SUCCESS);
  49438. #endif
  49439. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  49440. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  49441. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  49442. WOLFSSL_SUCCESS);
  49443. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  49444. BAD_FUNC_ARG);
  49445. #endif
  49446. #ifndef NO_WOLFSSL_SERVER
  49447. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  49448. WOLFSSL_SUCCESS);
  49449. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  49450. BAD_FUNC_ARG);
  49451. #endif
  49452. #ifdef OPENSSL_EXTRA
  49453. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  49454. #ifndef NO_WOLFSSL_CLIENT
  49455. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  49456. WOLFSSL_FAILURE);
  49457. #endif
  49458. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  49459. WOLFSSL_FAILURE);
  49460. #ifndef NO_WOLFSSL_CLIENT
  49461. #ifndef WOLFSSL_NO_TLS12
  49462. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  49463. WOLFSSL_SUCCESS);
  49464. #endif
  49465. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  49466. WOLFSSL_SUCCESS);
  49467. #endif
  49468. #ifndef NO_WOLFSSL_SERVER
  49469. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  49470. WOLFSSL_SUCCESS);
  49471. #endif
  49472. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  49473. #ifndef NO_WOLFSSL_CLIENT
  49474. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  49475. #endif
  49476. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  49477. #ifndef NO_WOLFSSL_CLIENT
  49478. #ifndef WOLFSSL_NO_TLS12
  49479. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  49480. WOLFSSL_SUCCESS);
  49481. #endif
  49482. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  49483. WOLFSSL_SUCCESS);
  49484. #endif
  49485. #ifndef NO_WOLFSSL_SERVER
  49486. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  49487. WOLFSSL_SUCCESS);
  49488. #endif
  49489. #endif /* OPENSSL_EXTRA */
  49490. #endif /* HAVE_SUPPORTED_CURVES */
  49491. #endif /* HAVE_ECC */
  49492. #ifdef WOLFSSL_EARLY_DATA
  49493. #ifndef OPENSSL_EXTRA
  49494. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49495. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  49496. #else
  49497. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49498. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  49499. #endif
  49500. #ifndef NO_WOLFSSL_CLIENT
  49501. #ifndef OPENSSL_EXTRA
  49502. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  49503. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  49504. #else
  49505. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  49506. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  49507. #endif
  49508. #endif
  49509. #ifndef NO_WOLFSSL_SERVER
  49510. #ifndef WOLFSSL_NO_TLS12
  49511. #ifndef OPENSSL_EXTRA
  49512. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  49513. BAD_FUNC_ARG);
  49514. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  49515. #else
  49516. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  49517. BAD_FUNC_ARG);
  49518. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  49519. #endif
  49520. #endif
  49521. #ifndef OPENSSL_EXTRA
  49522. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49523. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  49524. WOLFSSL_SUCCESS);
  49525. #else
  49526. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  49527. #endif
  49528. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  49529. #else
  49530. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  49531. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  49532. #endif
  49533. #endif
  49534. #ifndef OPENSSL_EXTRA
  49535. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49536. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  49537. #else
  49538. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  49539. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  49540. #endif
  49541. #ifndef NO_WOLFSSL_CLIENT
  49542. #ifndef OPENSSL_EXTRA
  49543. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49544. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  49545. #else
  49546. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  49547. #endif
  49548. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  49549. #else
  49550. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  49551. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  49552. #endif
  49553. #endif
  49554. #ifndef NO_WOLFSSL_SERVER
  49555. #ifndef WOLFSSL_NO_TLS12
  49556. #ifndef OPENSSL_EXTRA
  49557. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  49558. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  49559. #else
  49560. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  49561. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  49562. #endif
  49563. #endif
  49564. #ifndef OPENSSL_EXTRA
  49565. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49566. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  49567. #else
  49568. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  49569. #endif
  49570. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  49571. #else
  49572. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  49573. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  49574. #endif
  49575. #endif
  49576. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  49577. &outSz), BAD_FUNC_ARG);
  49578. #ifndef NO_WOLFSSL_CLIENT
  49579. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  49580. &outSz), BAD_FUNC_ARG);
  49581. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  49582. BAD_FUNC_ARG);
  49583. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  49584. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  49585. #endif
  49586. #ifndef NO_WOLFSSL_SERVER
  49587. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  49588. sizeof(earlyData), &outSz), SIDE_ERROR);
  49589. #endif
  49590. #ifndef NO_WOLFSSL_CLIENT
  49591. #ifndef WOLFSSL_NO_TLS12
  49592. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  49593. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  49594. #endif
  49595. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  49596. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  49597. #endif
  49598. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  49599. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49600. #ifndef NO_WOLFSSL_SERVER
  49601. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  49602. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49603. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  49604. &outSz), BAD_FUNC_ARG);
  49605. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  49606. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  49607. #endif
  49608. #ifndef NO_WOLFSSL_CLIENT
  49609. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  49610. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  49611. #endif
  49612. #ifndef NO_WOLFSSL_SERVER
  49613. #ifndef WOLFSSL_NO_TLS12
  49614. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  49615. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49616. #endif
  49617. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  49618. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  49619. #endif
  49620. #endif
  49621. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  49622. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  49623. #endif
  49624. #ifndef NO_WOLFSSL_SERVER
  49625. wolfSSL_free(serverSsl);
  49626. wolfSSL_CTX_free(serverCtx);
  49627. #endif
  49628. #ifndef NO_WOLFSSL_CLIENT
  49629. wolfSSL_free(clientSsl);
  49630. wolfSSL_CTX_free(clientCtx);
  49631. #endif
  49632. #ifndef WOLFSSL_NO_TLS12
  49633. #ifndef NO_WOLFSSL_SERVER
  49634. wolfSSL_free(serverTls12Ssl);
  49635. wolfSSL_CTX_free(serverTls12Ctx);
  49636. #endif
  49637. #ifndef NO_WOLFSSL_CLIENT
  49638. wolfSSL_free(clientTls12Ssl);
  49639. wolfSSL_CTX_free(clientTls12Ctx);
  49640. #endif
  49641. #endif
  49642. return EXPECT_RESULT();
  49643. }
  49644. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  49645. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  49646. defined(BUILD_TLS_AES_256_GCM_SHA384)
  49647. /* Called when writing. */
  49648. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  49649. {
  49650. (void)ssl;
  49651. (void)buf;
  49652. (void)sz;
  49653. (void)ctx;
  49654. /* Force error return from wolfSSL_accept_TLSv13(). */
  49655. return WANT_WRITE;
  49656. }
  49657. /* Called when reading. */
  49658. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  49659. {
  49660. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  49661. int len = (int)msg->length;
  49662. (void)ssl;
  49663. (void)sz;
  49664. /* Pass back as much of message as will fit in buffer. */
  49665. if (len > sz)
  49666. len = sz;
  49667. XMEMCPY(buf, msg->buffer, len);
  49668. /* Move over returned data. */
  49669. msg->buffer += len;
  49670. msg->length -= len;
  49671. /* Amount actually copied. */
  49672. return len;
  49673. }
  49674. #endif
  49675. static int test_tls13_cipher_suites(void)
  49676. {
  49677. EXPECT_DECLS;
  49678. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  49679. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  49680. defined(BUILD_TLS_AES_256_GCM_SHA384)
  49681. WOLFSSL_CTX* ctx = NULL;
  49682. WOLFSSL *ssl = NULL;
  49683. int i;
  49684. byte clientHello[] = {
  49685. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  49686. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  49687. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  49688. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  49689. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  49690. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  49691. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  49692. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  49693. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  49695. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  49696. 0x13, 0x01,
  49697. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  49698. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  49699. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  49700. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  49701. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  49702. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  49703. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  49704. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  49705. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  49706. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  49707. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  49708. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  49709. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  49710. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  49711. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  49712. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  49713. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  49714. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  49715. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  49716. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  49717. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  49718. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  49719. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  49720. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  49721. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  49722. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  49723. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  49724. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  49725. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  49726. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  49727. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  49728. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  49729. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  49730. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  49731. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  49732. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49733. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49734. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  49735. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  49736. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  49737. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  49738. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  49739. };
  49740. WOLFSSL_BUFFER_INFO msg;
  49741. /* Offset into ClientHello message data of first cipher suite. */
  49742. const int csOff = 78;
  49743. /* Server cipher list. */
  49744. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  49745. /* Suite list with duplicates. */
  49746. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  49747. "TLS13-AES128-GCM-SHA256:"
  49748. "TLS13-AES256-GCM-SHA384:"
  49749. "TLS13-AES256-GCM-SHA384:"
  49750. "TLS13-AES128-GCM-SHA256";
  49751. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49752. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  49753. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  49754. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  49755. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  49756. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  49757. #endif
  49758. /* Set up wolfSSL context. */
  49759. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49760. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  49761. WOLFSSL_FILETYPE_PEM));
  49762. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  49763. WOLFSSL_FILETYPE_PEM));
  49764. /* Read from 'msg'. */
  49765. wolfSSL_SetIORecv(ctx, CsRecv);
  49766. /* No where to send to - dummy sender. */
  49767. wolfSSL_SetIOSend(ctx, CsSend);
  49768. /* Test cipher suite list with many copies of a cipher suite. */
  49769. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49770. msg.buffer = clientHello;
  49771. msg.length = (unsigned int)sizeof(clientHello);
  49772. wolfSSL_SetIOReadCtx(ssl, &msg);
  49773. /* Force server to have as many occurrences of same cipher suite as
  49774. * possible. */
  49775. if (ssl != NULL) {
  49776. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  49777. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  49778. for (i = 0; i < suites->suiteSz; i += 2) {
  49779. suites->suites[i + 0] = TLS13_BYTE;
  49780. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  49781. }
  49782. }
  49783. /* Test multiple occurrences of same cipher suite. */
  49784. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49785. wolfSSL_free(ssl);
  49786. ssl = NULL;
  49787. /* Set client order opposite to server order:
  49788. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  49789. clientHello[csOff + 0] = TLS13_BYTE;
  49790. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  49791. clientHello[csOff + 2] = TLS13_BYTE;
  49792. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  49793. /* Test server order negotiation. */
  49794. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49795. msg.buffer = clientHello;
  49796. msg.length = (unsigned int)sizeof(clientHello);
  49797. wolfSSL_SetIOReadCtx(ssl, &msg);
  49798. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49799. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49800. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  49801. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49802. /* Check refined order - server order. */
  49803. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49804. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49805. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49806. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49807. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49808. wolfSSL_free(ssl);
  49809. ssl = NULL;
  49810. /* Test client order negotiation. */
  49811. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49812. msg.buffer = clientHello;
  49813. msg.length = (unsigned int)sizeof(clientHello);
  49814. wolfSSL_SetIOReadCtx(ssl, &msg);
  49815. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49816. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49817. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  49818. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  49819. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49820. /* Check refined order - client order. */
  49821. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49822. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49823. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49824. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49825. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49826. wolfSSL_free(ssl);
  49827. ssl = NULL;
  49828. /* Check duplicate detection is working. */
  49829. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  49830. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49831. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49832. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49833. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49834. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49835. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49836. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  49837. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  49838. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49839. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49840. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49841. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49842. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49843. #endif
  49844. wolfSSL_CTX_free(ctx);
  49845. #endif
  49846. return EXPECT_RESULT();
  49847. }
  49848. #endif
  49849. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  49850. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49851. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49852. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49853. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  49854. const unsigned char* priv, unsigned int privSz,
  49855. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  49856. unsigned char* out, unsigned int* outlen,
  49857. void* ctx)
  49858. {
  49859. int result;
  49860. /* Test fail when context associated with WOLFSSL is NULL */
  49861. if (ctx == NULL) {
  49862. return -1;
  49863. }
  49864. (void)ssl;
  49865. /* return 0 on success */
  49866. PRIVATE_KEY_UNLOCK();
  49867. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  49868. PRIVATE_KEY_LOCK();
  49869. return result;
  49870. }
  49871. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  49872. EXPECT_DECLS;
  49873. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  49874. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  49875. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  49876. WOLFSSL_SUCCESS);
  49877. #endif
  49878. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  49879. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  49880. WOLFSSL_SUCCESS);
  49881. #endif
  49882. return EXPECT_RESULT();
  49883. }
  49884. static int test_dh_ssl_setup(WOLFSSL* ssl)
  49885. {
  49886. EXPECT_DECLS;
  49887. static int dh_test_ctx = 1;
  49888. int ret;
  49889. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49890. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49891. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49892. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49893. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49894. }
  49895. return EXPECT_RESULT();
  49896. }
  49897. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49898. {
  49899. EXPECT_DECLS;
  49900. int ret;
  49901. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49902. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49903. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49904. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49905. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49906. }
  49907. return EXPECT_RESULT();
  49908. }
  49909. #endif
  49910. static int test_DhCallbacks(void)
  49911. {
  49912. EXPECT_DECLS;
  49913. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49914. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49915. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49916. WOLFSSL_CTX *ctx = NULL;
  49917. WOLFSSL *ssl = NULL;
  49918. int test;
  49919. test_ssl_cbf func_cb_client;
  49920. test_ssl_cbf func_cb_server;
  49921. /* Test that DH callback APIs work. */
  49922. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49923. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49924. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49925. /* load client ca cert */
  49926. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49927. WOLFSSL_SUCCESS);
  49928. /* test with NULL arguments */
  49929. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49930. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49931. /* test success case */
  49932. test = 1;
  49933. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49934. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49935. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49936. wolfSSL_free(ssl);
  49937. wolfSSL_CTX_free(ctx);
  49938. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49939. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49940. /* set callbacks to use DH functions */
  49941. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49942. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49943. func_cb_client.method = wolfTLSv1_2_client_method;
  49944. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49945. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49946. func_cb_server.method = wolfTLSv1_2_server_method;
  49947. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49948. &func_cb_server, NULL), TEST_SUCCESS);
  49949. /* Test fail */
  49950. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49951. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49952. /* set callbacks to use DH functions */
  49953. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49954. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49955. func_cb_client.method = wolfTLSv1_2_client_method;
  49956. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49957. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49958. func_cb_server.method = wolfTLSv1_2_server_method;
  49959. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49960. &func_cb_server, NULL), TEST_FAIL);
  49961. #endif
  49962. return EXPECT_RESULT();
  49963. }
  49964. #endif /* HAVE_PK_CALLBACKS */
  49965. #ifdef HAVE_HASHDRBG
  49966. #ifdef TEST_RESEED_INTERVAL
  49967. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49968. {
  49969. EXPECT_DECLS;
  49970. int i;
  49971. WC_RNG rng;
  49972. byte key[32];
  49973. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49974. ExpectIntEQ(wc_InitRng(&rng), 0);
  49975. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49976. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49977. }
  49978. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49979. return EXPECT_RESULT();
  49980. }
  49981. #endif /* TEST_RESEED_INTERVAL */
  49982. static int test_wc_RNG_GenerateBlock(void)
  49983. {
  49984. EXPECT_DECLS;
  49985. int i;
  49986. WC_RNG rng;
  49987. byte key[32];
  49988. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49989. ExpectIntEQ(wc_InitRng(&rng), 0);
  49990. for (i = 0; i < 10; i++) {
  49991. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49992. }
  49993. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49994. return EXPECT_RESULT();
  49995. }
  49996. #endif /* HAVE_HASHDRBG */
  49997. /*
  49998. * Testing get_rand_digit
  49999. */
  50000. static int test_get_rand_digit(void)
  50001. {
  50002. EXPECT_DECLS;
  50003. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  50004. WC_RNG rng;
  50005. mp_digit d;
  50006. XMEMSET(&rng, 0, sizeof(WC_RNG));
  50007. ExpectIntEQ(wc_InitRng(&rng), 0);
  50008. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  50009. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  50010. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  50011. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  50012. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  50013. #endif
  50014. return EXPECT_RESULT();
  50015. } /* End test_get_rand_digit*/
  50016. /*
  50017. * Testing get_digit_count
  50018. */
  50019. static int test_get_digit_count(void)
  50020. {
  50021. EXPECT_DECLS;
  50022. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  50023. mp_int a;
  50024. XMEMSET(&a, 0, sizeof(mp_int));
  50025. ExpectIntEQ(mp_init(&a), 0);
  50026. ExpectIntEQ(get_digit_count(NULL), 0);
  50027. ExpectIntEQ(get_digit_count(&a), 0);
  50028. mp_clear(&a);
  50029. #endif
  50030. return EXPECT_RESULT();
  50031. } /* End test_get_digit_count*/
  50032. /*
  50033. * Testing mp_cond_copy
  50034. */
  50035. static int test_mp_cond_copy(void)
  50036. {
  50037. EXPECT_DECLS;
  50038. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  50039. defined(WOLFSSL_PUBLIC_MP)
  50040. mp_int a;
  50041. mp_int b;
  50042. int copy = 0;
  50043. XMEMSET(&a, 0, sizeof(mp_int));
  50044. XMEMSET(&b, 0, sizeof(mp_int));
  50045. ExpectIntEQ(mp_init(&a), MP_OKAY);
  50046. ExpectIntEQ(mp_init(&b), MP_OKAY);
  50047. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  50048. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  50049. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  50050. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  50051. mp_clear(&a);
  50052. mp_clear(&b);
  50053. #endif
  50054. return EXPECT_RESULT();
  50055. } /* End test_mp_cond_copy*/
  50056. /*
  50057. * Testing mp_rand
  50058. */
  50059. static int test_mp_rand(void)
  50060. {
  50061. EXPECT_DECLS;
  50062. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  50063. mp_int a;
  50064. WC_RNG rng;
  50065. int digits = 1;
  50066. XMEMSET(&a, 0, sizeof(mp_int));
  50067. XMEMSET(&rng, 0, sizeof(WC_RNG));
  50068. ExpectIntEQ(mp_init(&a), MP_OKAY);
  50069. ExpectIntEQ(wc_InitRng(&rng), 0);
  50070. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  50071. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  50072. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  50073. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  50074. mp_clear(&a);
  50075. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  50076. #endif
  50077. return EXPECT_RESULT();
  50078. } /* End test_mp_rand*/
  50079. /*
  50080. * Testing get_digit
  50081. */
  50082. static int test_get_digit(void)
  50083. {
  50084. EXPECT_DECLS;
  50085. #if defined(WOLFSSL_PUBLIC_MP)
  50086. mp_int a;
  50087. int n = 0;
  50088. XMEMSET(&a, 0, sizeof(mp_int));
  50089. ExpectIntEQ(mp_init(&a), MP_OKAY);
  50090. ExpectIntEQ(get_digit(NULL, n), 0);
  50091. ExpectIntEQ(get_digit(&a, n), 0);
  50092. mp_clear(&a);
  50093. #endif
  50094. return EXPECT_RESULT();
  50095. } /* End test_get_digit*/
  50096. /*
  50097. * Testing wc_export_int
  50098. */
  50099. static int test_wc_export_int(void)
  50100. {
  50101. EXPECT_DECLS;
  50102. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  50103. defined(WOLFSSL_PUBLIC_MP)
  50104. mp_int mp;
  50105. byte buf[32];
  50106. word32 keySz = (word32)sizeof(buf);
  50107. word32 len = (word32)sizeof(buf);
  50108. XMEMSET(&mp, 0, sizeof(mp_int));
  50109. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  50110. ExpectIntEQ(mp_set(&mp, 1234), 0);
  50111. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  50112. BAD_FUNC_ARG);
  50113. len = sizeof(buf)-1;
  50114. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  50115. BUFFER_E);
  50116. len = sizeof(buf);
  50117. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  50118. len = 4; /* test input too small */
  50119. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  50120. len = sizeof(buf);
  50121. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  50122. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  50123. ExpectIntEQ(len, 5);
  50124. mp_clear(&mp);
  50125. #endif
  50126. return EXPECT_RESULT();
  50127. } /* End test_wc_export_int*/
  50128. static int test_wc_InitRngNonce(void)
  50129. {
  50130. EXPECT_DECLS;
  50131. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  50132. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  50133. HAVE_FIPS_VERSION >= 2))
  50134. WC_RNG rng;
  50135. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  50136. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  50137. word32 nonceSz = sizeof(nonce);
  50138. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  50139. ExpectIntEQ(wc_FreeRng(&rng), 0);
  50140. #endif
  50141. return EXPECT_RESULT();
  50142. } /* End test_wc_InitRngNonce*/
  50143. /*
  50144. * Testing wc_InitRngNonce_ex
  50145. */
  50146. static int test_wc_InitRngNonce_ex(void)
  50147. {
  50148. EXPECT_DECLS;
  50149. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  50150. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  50151. HAVE_FIPS_VERSION >= 2))
  50152. WC_RNG rng;
  50153. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  50154. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  50155. word32 nonceSz = sizeof(nonce);
  50156. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  50157. 0);
  50158. ExpectIntEQ(wc_FreeRng(&rng), 0);
  50159. #endif
  50160. return EXPECT_RESULT();
  50161. } /* End test_wc_InitRngNonce_ex */
  50162. static int test_wolfSSL_X509_CRL(void)
  50163. {
  50164. EXPECT_DECLS;
  50165. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  50166. X509_CRL *crl = NULL;
  50167. char pem[][100] = {
  50168. "./certs/crl/crl.pem",
  50169. "./certs/crl/crl2.pem",
  50170. "./certs/crl/caEccCrl.pem",
  50171. "./certs/crl/eccCliCRL.pem",
  50172. "./certs/crl/eccSrvCRL.pem",
  50173. ""
  50174. };
  50175. #ifndef NO_BIO
  50176. BIO *bio = NULL;
  50177. #endif
  50178. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  50179. char der[][100] = {
  50180. "./certs/crl/crl.der",
  50181. "./certs/crl/crl2.der",
  50182. ""};
  50183. #endif
  50184. XFILE fp = XBADFILE;
  50185. int i;
  50186. for (i = 0; pem[i][0] != '\0'; i++)
  50187. {
  50188. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  50189. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50190. NULL, NULL));
  50191. ExpectNotNull(crl);
  50192. X509_CRL_free(crl);
  50193. if (fp != XBADFILE) {
  50194. XFCLOSE(fp);
  50195. fp = XBADFILE;
  50196. }
  50197. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  50198. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  50199. NULL));
  50200. if (EXPECT_FAIL()) {
  50201. crl = NULL;
  50202. }
  50203. ExpectNotNull(crl);
  50204. X509_CRL_free(crl);
  50205. crl = NULL;
  50206. if (fp != XBADFILE) {
  50207. XFCLOSE(fp);
  50208. fp = XBADFILE;
  50209. }
  50210. }
  50211. #ifndef NO_BIO
  50212. for (i = 0; pem[i][0] != '\0'; i++)
  50213. {
  50214. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  50215. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  50216. X509_CRL_free(crl);
  50217. crl = NULL;
  50218. BIO_free(bio);
  50219. bio = NULL;
  50220. }
  50221. #endif
  50222. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  50223. for (i = 0; der[i][0] != '\0'; i++) {
  50224. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  50225. ExpectTrue((fp != XBADFILE));
  50226. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  50227. ExpectNotNull(crl);
  50228. X509_CRL_free(crl);
  50229. if (fp != XBADFILE) {
  50230. XFCLOSE(fp);
  50231. fp = XBADFILE;
  50232. }
  50233. fp = XFOPEN(der[i], "rb");
  50234. ExpectTrue((fp != XBADFILE));
  50235. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  50236. if (EXPECT_FAIL()) {
  50237. crl = NULL;
  50238. }
  50239. ExpectNotNull(crl);
  50240. X509_CRL_free(crl);
  50241. crl = NULL;
  50242. if (fp != XBADFILE) {
  50243. XFCLOSE(fp);
  50244. fp = XBADFILE;
  50245. }
  50246. }
  50247. #endif
  50248. #endif
  50249. return EXPECT_RESULT();
  50250. }
  50251. static int test_wolfSSL_X509_load_crl_file(void)
  50252. {
  50253. EXPECT_DECLS;
  50254. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  50255. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  50256. int i;
  50257. char pem[][100] = {
  50258. "./certs/crl/crl.pem",
  50259. "./certs/crl/crl2.pem",
  50260. "./certs/crl/caEccCrl.pem",
  50261. "./certs/crl/eccCliCRL.pem",
  50262. "./certs/crl/eccSrvCRL.pem",
  50263. #ifdef WC_RSA_PSS
  50264. "./certs/crl/crl_rsapss.pem",
  50265. #endif
  50266. ""
  50267. };
  50268. char der[][100] = {
  50269. "./certs/crl/crl.der",
  50270. "./certs/crl/crl2.der",
  50271. ""
  50272. };
  50273. WOLFSSL_X509_STORE* store = NULL;
  50274. WOLFSSL_X509_LOOKUP* lookup = NULL;
  50275. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  50276. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  50277. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  50278. X509_FILETYPE_PEM), 1);
  50279. #ifdef WC_RSA_PSS
  50280. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  50281. X509_FILETYPE_PEM), 1);
  50282. #endif
  50283. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  50284. X509_FILETYPE_PEM), 1);
  50285. if (store) {
  50286. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  50287. WOLFSSL_FILETYPE_PEM), 1);
  50288. /* since store hasn't yet known the revoked cert*/
  50289. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50290. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  50291. }
  50292. for (i = 0; pem[i][0] != '\0'; i++) {
  50293. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  50294. 1);
  50295. }
  50296. if (store) {
  50297. /* since store knows crl list */
  50298. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50299. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  50300. CRL_CERT_REVOKED);
  50301. #ifdef WC_RSA_PSS
  50302. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50303. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  50304. CRL_CERT_REVOKED);
  50305. #endif
  50306. }
  50307. /* once feeing store */
  50308. X509_STORE_free(store);
  50309. store = NULL;
  50310. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  50311. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  50312. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  50313. X509_FILETYPE_PEM), 1);
  50314. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  50315. X509_FILETYPE_PEM), 1);
  50316. if (store) {
  50317. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  50318. WOLFSSL_FILETYPE_PEM), 1);
  50319. /* since store hasn't yet known the revoked cert*/
  50320. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50321. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  50322. }
  50323. for (i = 0; der[i][0] != '\0'; i++) {
  50324. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  50325. 1);
  50326. }
  50327. if (store) {
  50328. /* since store knows crl list */
  50329. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  50330. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  50331. CRL_CERT_REVOKED);
  50332. }
  50333. /* test for incorrect parameter */
  50334. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  50335. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  50336. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  50337. X509_STORE_free(store);
  50338. store = NULL;
  50339. #endif
  50340. return EXPECT_RESULT();
  50341. }
  50342. static int test_wolfSSL_i2d_X509(void)
  50343. {
  50344. EXPECT_DECLS;
  50345. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  50346. const unsigned char* cert_buf = server_cert_der_2048;
  50347. unsigned char* out = NULL;
  50348. unsigned char* tmp = NULL;
  50349. X509* cert = NULL;
  50350. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  50351. /* Pointer should be advanced */
  50352. ExpectPtrGT(cert_buf, server_cert_der_2048);
  50353. ExpectIntGT(i2d_X509(cert, &out), 0);
  50354. ExpectNotNull(out);
  50355. tmp = out;
  50356. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  50357. ExpectPtrGT(tmp, out);
  50358. if (out != NULL)
  50359. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  50360. X509_free(cert);
  50361. #endif
  50362. return EXPECT_RESULT();
  50363. }
  50364. static int test_wolfSSL_d2i_X509_REQ(void)
  50365. {
  50366. EXPECT_DECLS;
  50367. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  50368. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  50369. !defined(WOLFSSL_SP_MATH)
  50370. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  50371. * were generated by libest
  50372. * ./certs/csr.attr.der contains sample attributes
  50373. * ./certs/csr.ext.der contains sample extensions */
  50374. const char* csrFile = "./certs/csr.signed.der";
  50375. const char* csrPopFile = "./certs/csr.attr.der";
  50376. const char* csrExtFile = "./certs/csr.ext.der";
  50377. /* ./certs/csr.dsa.pem is generated using
  50378. * openssl req -newkey dsa:certs/dsaparams.pem \
  50379. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  50380. * -outform PEM
  50381. * with the passphrase "wolfSSL"
  50382. */
  50383. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  50384. const char* csrDsaFile = "./certs/csr.dsa.pem";
  50385. XFILE f = XBADFILE;
  50386. #endif
  50387. BIO* bio = NULL;
  50388. X509* req = NULL;
  50389. EVP_PKEY *pub_key = NULL;
  50390. {
  50391. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  50392. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  50393. /*
  50394. * Extract the public key from the CSR
  50395. */
  50396. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50397. /*
  50398. * Verify the signature in the CSR
  50399. */
  50400. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50401. X509_free(req);
  50402. req = NULL;
  50403. BIO_free(bio);
  50404. bio = NULL;
  50405. EVP_PKEY_free(pub_key);
  50406. pub_key = NULL;
  50407. }
  50408. {
  50409. #ifdef OPENSSL_ALL
  50410. X509_ATTRIBUTE* attr = NULL;
  50411. ASN1_TYPE *at = NULL;
  50412. #endif
  50413. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  50414. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  50415. /*
  50416. * Extract the public key from the CSR
  50417. */
  50418. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50419. /*
  50420. * Verify the signature in the CSR
  50421. */
  50422. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50423. #ifdef OPENSSL_ALL
  50424. /*
  50425. * Obtain the challenge password from the CSR
  50426. */
  50427. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  50428. -1), 1);
  50429. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  50430. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  50431. ExpectNotNull(at->value.asn1_string);
  50432. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  50433. "2xIE+qqp/rhyTXP+");
  50434. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  50435. #endif
  50436. X509_free(req);
  50437. req = NULL;
  50438. BIO_free(bio);
  50439. bio = NULL;
  50440. EVP_PKEY_free(pub_key);
  50441. pub_key = NULL;
  50442. }
  50443. {
  50444. #ifdef OPENSSL_ALL
  50445. X509_ATTRIBUTE* attr = NULL;
  50446. ASN1_TYPE *at = NULL;
  50447. STACK_OF(X509_EXTENSION) *exts = NULL;
  50448. #endif
  50449. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  50450. /* This CSR contains an Extension Request attribute so
  50451. * we test extension parsing in a CSR attribute here. */
  50452. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  50453. /*
  50454. * Extract the public key from the CSR
  50455. */
  50456. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50457. /*
  50458. * Verify the signature in the CSR
  50459. */
  50460. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50461. #ifdef OPENSSL_ALL
  50462. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  50463. req));
  50464. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  50465. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  50466. /*
  50467. * Obtain the challenge password from the CSR
  50468. */
  50469. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  50470. -1), 0);
  50471. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  50472. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  50473. ExpectNotNull(at->value.asn1_string);
  50474. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  50475. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  50476. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  50477. #endif
  50478. X509_free(req);
  50479. req = NULL;
  50480. BIO_free(bio);
  50481. bio = NULL;
  50482. EVP_PKEY_free(pub_key);
  50483. pub_key = NULL;
  50484. }
  50485. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  50486. {
  50487. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  50488. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  50489. /*
  50490. * Extract the public key from the CSR
  50491. */
  50492. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  50493. /*
  50494. * Verify the signature in the CSR
  50495. */
  50496. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50497. X509_free(req);
  50498. req = NULL;
  50499. BIO_free(bio);
  50500. /* Run the same test, but with a file pointer instead of a BIO.
  50501. * (PEM_read_X509_REQ)*/
  50502. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  50503. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  50504. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  50505. X509_free(req);
  50506. EVP_PKEY_free(pub_key);
  50507. }
  50508. #endif /* !NO_DSA && !HAVE_SELFTEST */
  50509. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  50510. return EXPECT_RESULT();
  50511. }
  50512. static int test_wolfSSL_PEM_read_X509(void)
  50513. {
  50514. EXPECT_DECLS;
  50515. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  50516. !defined(NO_RSA)
  50517. X509 *x509 = NULL;
  50518. XFILE fp = XBADFILE;
  50519. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  50520. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  50521. X509_free(x509);
  50522. if (fp != XBADFILE)
  50523. XFCLOSE(fp);
  50524. #endif
  50525. return EXPECT_RESULT();
  50526. }
  50527. static int test_wolfSSL_PEM_read(void)
  50528. {
  50529. EXPECT_DECLS;
  50530. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  50531. const char* filename = "./certs/server-keyEnc.pem";
  50532. XFILE fp = XBADFILE;
  50533. char* name = NULL;
  50534. char* header = NULL;
  50535. byte* data = NULL;
  50536. long len;
  50537. EVP_CIPHER_INFO cipher;
  50538. WOLFSSL_BIO* bio = NULL;
  50539. byte* fileData = NULL;
  50540. size_t fileDataSz = 0;
  50541. byte* out;
  50542. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  50543. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  50544. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  50545. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  50546. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  50547. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  50548. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50549. ExpectIntGT(XSTRLEN(header), 0);
  50550. ExpectIntGT(len, 0);
  50551. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50552. name = NULL;
  50553. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50554. header = NULL;
  50555. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50556. data = NULL;
  50557. BIO_free(bio);
  50558. bio = NULL;
  50559. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  50560. /* Fail cases. */
  50561. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  50562. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  50563. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  50564. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  50565. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  50566. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50567. ExpectIntGT(XSTRLEN(header), 0);
  50568. ExpectIntGT(len, 0);
  50569. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  50570. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  50571. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  50572. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  50573. DYNAMIC_TYPE_TMP_BUFFER));
  50574. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  50575. if (fp != XBADFILE) {
  50576. XFCLOSE(fp);
  50577. fp = XBADFILE;
  50578. }
  50579. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50580. /* Fail cases. */
  50581. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  50582. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  50583. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  50584. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  50585. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  50586. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  50587. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  50588. /* Fail cases. */
  50589. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  50590. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  50591. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  50592. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  50593. /* Pass case */
  50594. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  50595. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50596. name = NULL;
  50597. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50598. header = NULL;
  50599. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50600. data = NULL;
  50601. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  50602. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  50603. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50604. ExpectIntGT(XSTRLEN(header), 0);
  50605. ExpectIntGT(len, 0);
  50606. /* Fail cases. */
  50607. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  50608. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  50609. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  50610. #ifndef NO_DES3
  50611. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  50612. #endif
  50613. /* Fail cases. */
  50614. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  50615. (void*)"yassl123"), WOLFSSL_FAILURE);
  50616. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  50617. (void*)"yassl123"), WOLFSSL_FAILURE);
  50618. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  50619. (void*)"yassl123"), WOLFSSL_FAILURE);
  50620. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  50621. (void*)"yassl123"), WOLFSSL_FAILURE);
  50622. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  50623. (void*)"yassl123"), WOLFSSL_FAILURE);
  50624. #if !defined(NO_DES3) && !defined(NO_MD5)
  50625. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  50626. (void*)"yassl123"), WOLFSSL_SUCCESS);
  50627. #else
  50628. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  50629. (void*)"yassl123"), WOLFSSL_FAILURE);
  50630. #endif
  50631. BIO_free(bio);
  50632. bio = NULL;
  50633. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50634. fileData = NULL;
  50635. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50636. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50637. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50638. name = NULL;
  50639. header = NULL;
  50640. data = NULL;
  50641. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  50642. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  50643. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50644. ExpectIntEQ(XSTRLEN(header), 0);
  50645. ExpectIntGT(len, 0);
  50646. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  50647. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  50648. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  50649. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  50650. DYNAMIC_TYPE_TMP_BUFFER));
  50651. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  50652. if (fp != XBADFILE)
  50653. XFCLOSE(fp);
  50654. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50655. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  50656. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  50657. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  50658. BIO_free(bio);
  50659. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50660. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50661. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50662. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50663. #endif
  50664. return EXPECT_RESULT();
  50665. }
  50666. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  50667. {
  50668. EXPECT_DECLS;
  50669. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50670. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50671. const byte iv[12] = { 0 };
  50672. const byte key[16] = { 0 };
  50673. const byte cleartext[16] = { 0 };
  50674. const byte aad[] = {
  50675. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  50676. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  50677. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  50678. 0x4f
  50679. };
  50680. byte out1Part[16];
  50681. byte outTag1Part[16];
  50682. byte out2Part[16];
  50683. byte outTag2Part[16];
  50684. byte decryptBuf[16];
  50685. int len = 0;
  50686. int tlen;
  50687. EVP_CIPHER_CTX* ctx = NULL;
  50688. /* ENCRYPT */
  50689. /* Send AAD and data in 1 part */
  50690. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50691. tlen = 0;
  50692. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50693. 1);
  50694. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50695. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  50696. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  50697. sizeof(cleartext)), 1);
  50698. tlen += len;
  50699. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  50700. tlen += len;
  50701. ExpectIntEQ(tlen, sizeof(cleartext));
  50702. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  50703. outTag1Part), 1);
  50704. EVP_CIPHER_CTX_free(ctx);
  50705. ctx = NULL;
  50706. /* DECRYPT */
  50707. /* Send AAD and data in 1 part */
  50708. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50709. tlen = 0;
  50710. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50711. 1);
  50712. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50713. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  50714. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  50715. sizeof(cleartext)), 1);
  50716. tlen += len;
  50717. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  50718. outTag1Part), 1);
  50719. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  50720. tlen += len;
  50721. ExpectIntEQ(tlen, sizeof(cleartext));
  50722. EVP_CIPHER_CTX_free(ctx);
  50723. ctx = NULL;
  50724. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  50725. /* ENCRYPT */
  50726. /* Send AAD and data in 2 parts */
  50727. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50728. tlen = 0;
  50729. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50730. 1);
  50731. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50732. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  50733. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  50734. 1);
  50735. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  50736. tlen += len;
  50737. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  50738. sizeof(cleartext) - 1), 1);
  50739. tlen += len;
  50740. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  50741. tlen += len;
  50742. ExpectIntEQ(tlen, sizeof(cleartext));
  50743. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  50744. outTag2Part), 1);
  50745. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  50746. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  50747. EVP_CIPHER_CTX_free(ctx);
  50748. ctx = NULL;
  50749. /* DECRYPT */
  50750. /* Send AAD and data in 2 parts */
  50751. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50752. tlen = 0;
  50753. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50754. 1);
  50755. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50756. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  50757. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  50758. 1);
  50759. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  50760. tlen += len;
  50761. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  50762. sizeof(cleartext) - 1), 1);
  50763. tlen += len;
  50764. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  50765. outTag1Part), 1);
  50766. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  50767. tlen += len;
  50768. ExpectIntEQ(tlen, sizeof(cleartext));
  50769. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  50770. /* Test AAD reuse */
  50771. EVP_CIPHER_CTX_free(ctx);
  50772. #endif
  50773. return EXPECT_RESULT();
  50774. }
  50775. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  50776. {
  50777. EXPECT_DECLS;
  50778. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50779. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50780. /* Zero length plain text */
  50781. byte key[] = {
  50782. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50783. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50784. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50785. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50786. }; /* align */
  50787. byte iv[] = {
  50788. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50789. }; /* align */
  50790. byte plaintxt[1];
  50791. int ivSz = 12;
  50792. int plaintxtSz = 0;
  50793. unsigned char tag[16];
  50794. unsigned char tag_kat[] = {
  50795. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  50796. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  50797. };
  50798. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50799. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50800. int ciphertxtSz = 0;
  50801. int decryptedtxtSz = 0;
  50802. int len = 0;
  50803. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50804. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50805. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  50806. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50807. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50808. plaintxtSz));
  50809. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50810. ciphertxtSz += len;
  50811. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50812. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50813. ExpectIntEQ(0, ciphertxtSz);
  50814. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50815. EVP_CIPHER_CTX_init(de);
  50816. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  50817. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50818. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50819. decryptedtxtSz = len;
  50820. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50821. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50822. decryptedtxtSz += len;
  50823. ExpectIntEQ(0, decryptedtxtSz);
  50824. EVP_CIPHER_CTX_free(en);
  50825. EVP_CIPHER_CTX_free(de);
  50826. #endif
  50827. return EXPECT_RESULT();
  50828. }
  50829. static int test_wolfssl_EVP_aes_gcm(void)
  50830. {
  50831. EXPECT_DECLS;
  50832. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50833. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50834. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50835. byte *key = (byte*)"01234567890123456789012345678901";
  50836. /* A 128 bit IV */
  50837. byte *iv = (byte*)"0123456789012345";
  50838. int ivSz = AES_BLOCK_SIZE;
  50839. /* Message to be encrypted */
  50840. byte *plaintxt = (byte*)"for things to change you have to change";
  50841. /* Additional non-confidential data */
  50842. byte *aad = (byte*)"Don't spend major time on minor things.";
  50843. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50844. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50845. int aadSz = (int)XSTRLEN((char*)aad);
  50846. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50847. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50848. int ciphertxtSz = 0;
  50849. int decryptedtxtSz = 0;
  50850. int len = 0;
  50851. int i = 0;
  50852. EVP_CIPHER_CTX en[2];
  50853. EVP_CIPHER_CTX de[2];
  50854. for (i = 0; i < 2; i++) {
  50855. EVP_CIPHER_CTX_init(&en[i]);
  50856. if (i == 0) {
  50857. /* Default uses 96-bits IV length */
  50858. #ifdef WOLFSSL_AES_128
  50859. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50860. key, iv));
  50861. #elif defined(WOLFSSL_AES_192)
  50862. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50863. key, iv));
  50864. #elif defined(WOLFSSL_AES_256)
  50865. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50866. key, iv));
  50867. #endif
  50868. }
  50869. else {
  50870. #ifdef WOLFSSL_AES_128
  50871. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50872. NULL, NULL));
  50873. #elif defined(WOLFSSL_AES_192)
  50874. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50875. NULL, NULL));
  50876. #elif defined(WOLFSSL_AES_256)
  50877. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50878. NULL, NULL));
  50879. #endif
  50880. /* non-default must to set the IV length first */
  50881. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50882. ivSz, NULL));
  50883. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50884. }
  50885. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50886. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50887. plaintxtSz));
  50888. ciphertxtSz = len;
  50889. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50890. ciphertxtSz += len;
  50891. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50892. AES_BLOCK_SIZE, tag));
  50893. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50894. EVP_CIPHER_CTX_init(&de[i]);
  50895. if (i == 0) {
  50896. /* Default uses 96-bits IV length */
  50897. #ifdef WOLFSSL_AES_128
  50898. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50899. key, iv));
  50900. #elif defined(WOLFSSL_AES_192)
  50901. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50902. key, iv));
  50903. #elif defined(WOLFSSL_AES_256)
  50904. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50905. key, iv));
  50906. #endif
  50907. }
  50908. else {
  50909. #ifdef WOLFSSL_AES_128
  50910. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50911. NULL, NULL));
  50912. #elif defined(WOLFSSL_AES_192)
  50913. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50914. NULL, NULL));
  50915. #elif defined(WOLFSSL_AES_256)
  50916. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50917. NULL, NULL));
  50918. #endif
  50919. /* non-default must to set the IV length first */
  50920. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50921. ivSz, NULL));
  50922. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50923. }
  50924. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50925. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50926. ciphertxtSz));
  50927. decryptedtxtSz = len;
  50928. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50929. AES_BLOCK_SIZE, tag));
  50930. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50931. decryptedtxtSz += len;
  50932. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50933. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50934. /* modify tag*/
  50935. if (i == 0) {
  50936. /* Default uses 96-bits IV length */
  50937. #ifdef WOLFSSL_AES_128
  50938. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50939. key, iv));
  50940. #elif defined(WOLFSSL_AES_192)
  50941. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50942. key, iv));
  50943. #elif defined(WOLFSSL_AES_256)
  50944. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50945. key, iv));
  50946. #endif
  50947. }
  50948. else {
  50949. #ifdef WOLFSSL_AES_128
  50950. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50951. NULL, NULL));
  50952. #elif defined(WOLFSSL_AES_192)
  50953. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50954. NULL, NULL));
  50955. #elif defined(WOLFSSL_AES_256)
  50956. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50957. NULL, NULL));
  50958. #endif
  50959. /* non-default must to set the IV length first */
  50960. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50961. ivSz, NULL));
  50962. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50963. }
  50964. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50965. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50966. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50967. AES_BLOCK_SIZE, tag));
  50968. /* fail due to wrong tag */
  50969. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50970. ciphertxtSz));
  50971. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50972. ExpectIntEQ(0, len);
  50973. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50974. }
  50975. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50976. return EXPECT_RESULT();
  50977. }
  50978. static int test_wolfssl_EVP_aria_gcm(void)
  50979. {
  50980. int res = TEST_SKIPPED;
  50981. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50982. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50983. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50984. byte *key = (byte*)"01234567890123456789012345678901";
  50985. /* A 128 bit IV */
  50986. byte *iv = (byte*)"0123456789012345";
  50987. int ivSz = ARIA_BLOCK_SIZE;
  50988. /* Message to be encrypted */
  50989. const int plaintxtSz = 40;
  50990. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50991. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50992. /* Additional non-confidential data */
  50993. byte *aad = (byte*)"Don't spend major time on minor things.";
  50994. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50995. int aadSz = (int)XSTRLEN((char*)aad);
  50996. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50997. byte decryptedtxt[plaintxtSz];
  50998. int ciphertxtSz = 0;
  50999. int decryptedtxtSz = 0;
  51000. int len = 0;
  51001. int i = 0;
  51002. #define TEST_ARIA_GCM_COUNT 6
  51003. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  51004. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  51005. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  51006. EVP_CIPHER_CTX_init(&en[i]);
  51007. switch (i) {
  51008. case 0:
  51009. /* Default uses 96-bits IV length */
  51010. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  51011. break;
  51012. case 1:
  51013. /* Default uses 96-bits IV length */
  51014. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  51015. break;
  51016. case 2:
  51017. /* Default uses 96-bits IV length */
  51018. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  51019. break;
  51020. case 3:
  51021. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  51022. /* non-default must to set the IV length first */
  51023. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51024. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51025. break;
  51026. case 4:
  51027. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  51028. /* non-default must to set the IV length first */
  51029. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51030. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51031. break;
  51032. case 5:
  51033. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  51034. /* non-default must to set the IV length first */
  51035. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51036. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51037. break;
  51038. }
  51039. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  51040. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51041. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  51042. ciphertxtSz = len;
  51043. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51044. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  51045. ciphertxtSz += len;
  51046. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  51047. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51048. EVP_CIPHER_CTX_init(&de[i]);
  51049. switch (i) {
  51050. case 0:
  51051. /* Default uses 96-bits IV length */
  51052. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  51053. break;
  51054. case 1:
  51055. /* Default uses 96-bits IV length */
  51056. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  51057. break;
  51058. case 2:
  51059. /* Default uses 96-bits IV length */
  51060. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  51061. break;
  51062. case 3:
  51063. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  51064. /* non-default must to set the IV length first */
  51065. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51066. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51067. break;
  51068. case 4:
  51069. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  51070. /* non-default must to set the IV length first */
  51071. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51072. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51073. break;
  51074. case 5:
  51075. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  51076. /* non-default must to set the IV length first */
  51077. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51078. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51079. break;
  51080. }
  51081. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  51082. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51083. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  51084. decryptedtxtSz = len;
  51085. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  51086. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51087. decryptedtxtSz += len;
  51088. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  51089. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51090. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  51091. /* modify tag*/
  51092. tag[AES_BLOCK_SIZE-1]+=0xBB;
  51093. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51094. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  51095. /* fail due to wrong tag */
  51096. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  51097. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51098. AssertIntEQ(0, len);
  51099. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51100. }
  51101. res = TEST_RES_CHECK(1);
  51102. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  51103. return res;
  51104. }
  51105. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  51106. {
  51107. EXPECT_DECLS;
  51108. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  51109. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  51110. /* Zero length plain text */
  51111. byte key[] = {
  51112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51116. }; /* align */
  51117. byte iv[] = {
  51118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51119. }; /* align */
  51120. byte plaintxt[1];
  51121. int ivSz = 12;
  51122. int plaintxtSz = 0;
  51123. unsigned char tag[16];
  51124. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  51125. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  51126. int ciphertxtSz = 0;
  51127. int decryptedtxtSz = 0;
  51128. int len = 0;
  51129. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51130. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51131. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  51132. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51133. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51134. plaintxtSz));
  51135. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51136. ciphertxtSz += len;
  51137. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  51138. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51139. ExpectIntEQ(0, ciphertxtSz);
  51140. EVP_CIPHER_CTX_init(de);
  51141. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  51142. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51143. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51144. decryptedtxtSz = len;
  51145. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  51146. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51147. decryptedtxtSz += len;
  51148. ExpectIntEQ(0, decryptedtxtSz);
  51149. EVP_CIPHER_CTX_free(en);
  51150. EVP_CIPHER_CTX_free(de);
  51151. #endif
  51152. return EXPECT_RESULT();
  51153. }
  51154. static int test_wolfssl_EVP_aes_ccm(void)
  51155. {
  51156. EXPECT_DECLS;
  51157. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  51158. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  51159. /* A 256 bit key, AES_128 will use the first 128 bit*/
  51160. byte *key = (byte*)"01234567890123456789012345678901";
  51161. /* A 128 bit IV */
  51162. byte *iv = (byte*)"0123456789012";
  51163. int ivSz = (int)XSTRLEN((char*)iv);
  51164. /* Message to be encrypted */
  51165. byte *plaintxt = (byte*)"for things to change you have to change";
  51166. /* Additional non-confidential data */
  51167. byte *aad = (byte*)"Don't spend major time on minor things.";
  51168. unsigned char tag[AES_BLOCK_SIZE] = {0};
  51169. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51170. int aadSz = (int)XSTRLEN((char*)aad);
  51171. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  51172. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  51173. int ciphertxtSz = 0;
  51174. int decryptedtxtSz = 0;
  51175. int len = 0;
  51176. int i = 0;
  51177. int ret;
  51178. EVP_CIPHER_CTX en[2];
  51179. EVP_CIPHER_CTX de[2];
  51180. for (i = 0; i < 2; i++) {
  51181. EVP_CIPHER_CTX_init(&en[i]);
  51182. if (i == 0) {
  51183. /* Default uses 96-bits IV length */
  51184. #ifdef WOLFSSL_AES_128
  51185. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  51186. key, iv));
  51187. #elif defined(WOLFSSL_AES_192)
  51188. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  51189. key, iv));
  51190. #elif defined(WOLFSSL_AES_256)
  51191. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  51192. key, iv));
  51193. #endif
  51194. }
  51195. else {
  51196. #ifdef WOLFSSL_AES_128
  51197. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  51198. NULL, NULL));
  51199. #elif defined(WOLFSSL_AES_192)
  51200. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  51201. NULL, NULL));
  51202. #elif defined(WOLFSSL_AES_256)
  51203. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  51204. NULL, NULL));
  51205. #endif
  51206. /* non-default must to set the IV length first */
  51207. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51208. ivSz, NULL));
  51209. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51210. }
  51211. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51212. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51213. plaintxtSz));
  51214. ciphertxtSz = len;
  51215. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51216. ciphertxtSz += len;
  51217. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51218. AES_BLOCK_SIZE, tag));
  51219. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  51220. ExpectIntEQ(ret, 1);
  51221. EVP_CIPHER_CTX_init(&de[i]);
  51222. if (i == 0) {
  51223. /* Default uses 96-bits IV length */
  51224. #ifdef WOLFSSL_AES_128
  51225. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  51226. key, iv));
  51227. #elif defined(WOLFSSL_AES_192)
  51228. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  51229. key, iv));
  51230. #elif defined(WOLFSSL_AES_256)
  51231. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  51232. key, iv));
  51233. #endif
  51234. }
  51235. else {
  51236. #ifdef WOLFSSL_AES_128
  51237. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  51238. NULL, NULL));
  51239. #elif defined(WOLFSSL_AES_192)
  51240. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  51241. NULL, NULL));
  51242. #elif defined(WOLFSSL_AES_256)
  51243. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  51244. NULL, NULL));
  51245. #endif
  51246. /* non-default must to set the IV length first */
  51247. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51248. ivSz, NULL));
  51249. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51250. }
  51251. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51252. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51253. ciphertxtSz));
  51254. decryptedtxtSz = len;
  51255. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51256. AES_BLOCK_SIZE, tag));
  51257. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51258. decryptedtxtSz += len;
  51259. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51260. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51261. /* modify tag*/
  51262. tag[AES_BLOCK_SIZE-1]+=0xBB;
  51263. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51264. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51265. AES_BLOCK_SIZE, tag));
  51266. /* fail due to wrong tag */
  51267. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51268. ciphertxtSz));
  51269. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51270. ExpectIntEQ(0, len);
  51271. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  51272. ExpectIntEQ(ret, 1);
  51273. }
  51274. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  51275. return EXPECT_RESULT();
  51276. }
  51277. static int test_wolfssl_EVP_chacha20_poly1305(void)
  51278. {
  51279. EXPECT_DECLS;
  51280. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  51281. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  51282. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  51283. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  51284. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  51285. byte cipherText[sizeof(plainText)];
  51286. byte decryptedText[sizeof(plainText)];
  51287. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  51288. EVP_CIPHER_CTX* ctx = NULL;
  51289. int outSz;
  51290. /* Encrypt. */
  51291. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51292. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  51293. NULL), WOLFSSL_SUCCESS);
  51294. /* Invalid IV length. */
  51295. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  51296. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  51297. /* Valid IV length. */
  51298. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  51299. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  51300. /* Invalid tag length. */
  51301. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51302. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  51303. /* Valid tag length. */
  51304. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51305. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  51306. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51307. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  51308. WOLFSSL_SUCCESS);
  51309. ExpectIntEQ(outSz, sizeof(aad));
  51310. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51311. sizeof(plainText)), WOLFSSL_SUCCESS);
  51312. ExpectIntEQ(outSz, sizeof(plainText));
  51313. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51314. ExpectIntEQ(outSz, 0);
  51315. /* Invalid tag length. */
  51316. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  51317. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  51318. /* Valid tag length. */
  51319. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  51320. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  51321. EVP_CIPHER_CTX_free(ctx);
  51322. ctx = NULL;
  51323. /* Decrypt. */
  51324. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51325. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  51326. NULL), WOLFSSL_SUCCESS);
  51327. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  51328. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  51329. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51330. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  51331. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51332. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  51333. WOLFSSL_SUCCESS);
  51334. ExpectIntEQ(outSz, sizeof(aad));
  51335. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51336. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51337. ExpectIntEQ(outSz, sizeof(cipherText));
  51338. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51339. WOLFSSL_SUCCESS);
  51340. ExpectIntEQ(outSz, 0);
  51341. EVP_CIPHER_CTX_free(ctx);
  51342. ctx = NULL;
  51343. /* Test partial Inits. CipherInit() allow setting of key and iv
  51344. * in separate calls. */
  51345. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51346. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  51347. key, NULL, 1), WOLFSSL_SUCCESS);
  51348. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51349. WOLFSSL_SUCCESS);
  51350. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  51351. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  51352. ExpectIntEQ(outSz, sizeof(aad));
  51353. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51354. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51355. ExpectIntEQ(outSz, sizeof(cipherText));
  51356. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51357. WOLFSSL_SUCCESS);
  51358. ExpectIntEQ(outSz, 0);
  51359. EVP_CIPHER_CTX_free(ctx);
  51360. #endif
  51361. return EXPECT_RESULT();
  51362. }
  51363. static int test_wolfssl_EVP_chacha20(void)
  51364. {
  51365. EXPECT_DECLS;
  51366. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  51367. byte key[CHACHA_MAX_KEY_SZ];
  51368. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  51369. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  51370. byte cipherText[sizeof(plainText)];
  51371. byte decryptedText[sizeof(plainText)];
  51372. EVP_CIPHER_CTX* ctx = NULL;
  51373. int outSz;
  51374. XMEMSET(key, 0, sizeof(key));
  51375. XMEMSET(iv, 0, sizeof(iv));
  51376. /* Encrypt. */
  51377. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51378. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  51379. NULL), WOLFSSL_SUCCESS);
  51380. /* Any tag length must fail - not an AEAD cipher. */
  51381. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  51382. 16, NULL), WOLFSSL_FAILURE);
  51383. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51384. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51385. sizeof(plainText)), WOLFSSL_SUCCESS);
  51386. ExpectIntEQ(outSz, sizeof(plainText));
  51387. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51388. ExpectIntEQ(outSz, 0);
  51389. EVP_CIPHER_CTX_free(ctx);
  51390. ctx = NULL;
  51391. /* Decrypt. */
  51392. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51393. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  51394. NULL), WOLFSSL_SUCCESS);
  51395. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51396. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51397. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51398. ExpectIntEQ(outSz, sizeof(cipherText));
  51399. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51400. WOLFSSL_SUCCESS);
  51401. ExpectIntEQ(outSz, 0);
  51402. EVP_CIPHER_CTX_free(ctx);
  51403. ctx = NULL;
  51404. /* Test partial Inits. CipherInit() allow setting of key and iv
  51405. * in separate calls. */
  51406. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51407. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  51408. key, NULL, 1), WOLFSSL_SUCCESS);
  51409. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51410. WOLFSSL_SUCCESS);
  51411. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51412. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51413. ExpectIntEQ(outSz, sizeof(cipherText));
  51414. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51415. WOLFSSL_SUCCESS);
  51416. ExpectIntEQ(outSz, 0);
  51417. EVP_CIPHER_CTX_free(ctx);
  51418. #endif
  51419. return EXPECT_RESULT();
  51420. }
  51421. static int test_wolfssl_EVP_sm4_ecb(void)
  51422. {
  51423. int res = TEST_SKIPPED;
  51424. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  51425. EXPECT_DECLS;
  51426. byte key[SM4_KEY_SIZE];
  51427. byte plainText[SM4_BLOCK_SIZE] = {
  51428. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  51429. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  51430. };
  51431. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51432. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51433. EVP_CIPHER_CTX* ctx;
  51434. int outSz;
  51435. XMEMSET(key, 0, sizeof(key));
  51436. /* Encrypt. */
  51437. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51438. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  51439. WOLFSSL_SUCCESS);
  51440. /* Any tag length must fail - not an AEAD cipher. */
  51441. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51442. WOLFSSL_FAILURE);
  51443. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  51444. WOLFSSL_SUCCESS);
  51445. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51446. sizeof(plainText)), WOLFSSL_SUCCESS);
  51447. ExpectIntEQ(outSz, sizeof(plainText));
  51448. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  51449. WOLFSSL_SUCCESS);
  51450. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  51451. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51452. EVP_CIPHER_CTX_free(ctx);
  51453. /* Decrypt. */
  51454. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51455. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  51456. WOLFSSL_SUCCESS);
  51457. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  51458. WOLFSSL_SUCCESS);
  51459. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51460. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51461. ExpectIntEQ(outSz, sizeof(plainText));
  51462. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  51463. WOLFSSL_SUCCESS);
  51464. ExpectIntEQ(outSz, 0);
  51465. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51466. EVP_CIPHER_CTX_free(ctx);
  51467. res = EXPECT_RESULT();
  51468. #endif
  51469. return res;
  51470. }
  51471. static int test_wolfssl_EVP_sm4_cbc(void)
  51472. {
  51473. int res = TEST_SKIPPED;
  51474. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  51475. EXPECT_DECLS;
  51476. byte key[SM4_KEY_SIZE];
  51477. byte iv[SM4_BLOCK_SIZE];
  51478. byte plainText[SM4_BLOCK_SIZE] = {
  51479. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  51480. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  51481. };
  51482. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51483. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  51484. EVP_CIPHER_CTX* ctx;
  51485. int outSz;
  51486. XMEMSET(key, 0, sizeof(key));
  51487. XMEMSET(iv, 0, sizeof(iv));
  51488. /* Encrypt. */
  51489. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51490. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  51491. WOLFSSL_SUCCESS);
  51492. /* Any tag length must fail - not an AEAD cipher. */
  51493. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51494. WOLFSSL_FAILURE);
  51495. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51496. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51497. sizeof(plainText)), WOLFSSL_SUCCESS);
  51498. ExpectIntEQ(outSz, sizeof(plainText));
  51499. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  51500. WOLFSSL_SUCCESS);
  51501. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  51502. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51503. EVP_CIPHER_CTX_free(ctx);
  51504. /* Decrypt. */
  51505. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51506. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  51507. WOLFSSL_SUCCESS);
  51508. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51509. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51510. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51511. ExpectIntEQ(outSz, sizeof(plainText));
  51512. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  51513. WOLFSSL_SUCCESS);
  51514. ExpectIntEQ(outSz, 0);
  51515. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51516. EVP_CIPHER_CTX_free(ctx);
  51517. /* Test partial Inits. CipherInit() allow setting of key and iv
  51518. * in separate calls. */
  51519. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51520. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  51521. WOLFSSL_SUCCESS);
  51522. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  51523. WOLFSSL_SUCCESS);
  51524. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51525. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51526. ExpectIntEQ(outSz, sizeof(plainText));
  51527. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  51528. WOLFSSL_SUCCESS);
  51529. ExpectIntEQ(outSz, 0);
  51530. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51531. EVP_CIPHER_CTX_free(ctx);
  51532. res = EXPECT_RESULT();
  51533. #endif
  51534. return res;
  51535. }
  51536. static int test_wolfssl_EVP_sm4_ctr(void)
  51537. {
  51538. int res = TEST_SKIPPED;
  51539. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  51540. EXPECT_DECLS;
  51541. byte key[SM4_KEY_SIZE];
  51542. byte iv[SM4_BLOCK_SIZE];
  51543. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  51544. byte cipherText[sizeof(plainText)];
  51545. byte decryptedText[sizeof(plainText)];
  51546. EVP_CIPHER_CTX* ctx;
  51547. int outSz;
  51548. XMEMSET(key, 0, sizeof(key));
  51549. XMEMSET(iv, 0, sizeof(iv));
  51550. /* Encrypt. */
  51551. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51552. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  51553. WOLFSSL_SUCCESS);
  51554. /* Any tag length must fail - not an AEAD cipher. */
  51555. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51556. WOLFSSL_FAILURE);
  51557. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51558. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51559. sizeof(plainText)), WOLFSSL_SUCCESS);
  51560. ExpectIntEQ(outSz, sizeof(plainText));
  51561. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51562. ExpectIntEQ(outSz, 0);
  51563. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51564. EVP_CIPHER_CTX_free(ctx);
  51565. /* Decrypt. */
  51566. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51567. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  51568. WOLFSSL_SUCCESS);
  51569. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51570. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51571. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51572. ExpectIntEQ(outSz, sizeof(cipherText));
  51573. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51574. WOLFSSL_SUCCESS);
  51575. ExpectIntEQ(outSz, 0);
  51576. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51577. EVP_CIPHER_CTX_free(ctx);
  51578. /* Test partial Inits. CipherInit() allow setting of key and iv
  51579. * in separate calls. */
  51580. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51581. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  51582. WOLFSSL_SUCCESS);
  51583. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51584. WOLFSSL_SUCCESS);
  51585. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51586. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51587. ExpectIntEQ(outSz, sizeof(cipherText));
  51588. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51589. WOLFSSL_SUCCESS);
  51590. ExpectIntEQ(outSz, 0);
  51591. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51592. EVP_CIPHER_CTX_free(ctx);
  51593. res = EXPECT_RESULT();
  51594. #endif
  51595. return res;
  51596. }
  51597. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  51598. {
  51599. int res = TEST_SKIPPED;
  51600. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  51601. /* Zero length plain text */
  51602. EXPECT_DECLS;
  51603. byte key[] = {
  51604. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51605. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51608. }; /* align */
  51609. byte iv[] = {
  51610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51611. }; /* align */
  51612. byte plaintxt[1];
  51613. int ivSz = 12;
  51614. int plaintxtSz = 0;
  51615. unsigned char tag[16];
  51616. unsigned char tag_kat[16] = {
  51617. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  51618. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  51619. };
  51620. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51621. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51622. int ciphertxtSz = 0;
  51623. int decryptedtxtSz = 0;
  51624. int len = 0;
  51625. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51626. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51627. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  51628. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51629. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51630. plaintxtSz));
  51631. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51632. ciphertxtSz += len;
  51633. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  51634. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51635. ExpectIntEQ(0, ciphertxtSz);
  51636. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  51637. EVP_CIPHER_CTX_init(de);
  51638. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  51639. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51640. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51641. decryptedtxtSz = len;
  51642. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  51643. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51644. decryptedtxtSz += len;
  51645. ExpectIntEQ(0, decryptedtxtSz);
  51646. EVP_CIPHER_CTX_free(en);
  51647. EVP_CIPHER_CTX_free(de);
  51648. res = EXPECT_RESULT();
  51649. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  51650. return res;
  51651. }
  51652. static int test_wolfssl_EVP_sm4_gcm(void)
  51653. {
  51654. int res = TEST_SKIPPED;
  51655. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  51656. EXPECT_DECLS;
  51657. byte *key = (byte*)"0123456789012345";
  51658. /* A 128 bit IV */
  51659. byte *iv = (byte*)"0123456789012345";
  51660. int ivSz = SM4_BLOCK_SIZE;
  51661. /* Message to be encrypted */
  51662. byte *plaintxt = (byte*)"for things to change you have to change";
  51663. /* Additional non-confidential data */
  51664. byte *aad = (byte*)"Don't spend major time on minor things.";
  51665. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51666. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51667. int aadSz = (int)XSTRLEN((char*)aad);
  51668. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51669. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51670. int ciphertxtSz = 0;
  51671. int decryptedtxtSz = 0;
  51672. int len = 0;
  51673. int i = 0;
  51674. EVP_CIPHER_CTX en[2];
  51675. EVP_CIPHER_CTX de[2];
  51676. for (i = 0; i < 2; i++) {
  51677. EVP_CIPHER_CTX_init(&en[i]);
  51678. if (i == 0) {
  51679. /* Default uses 96-bits IV length */
  51680. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  51681. iv));
  51682. }
  51683. else {
  51684. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  51685. NULL));
  51686. /* non-default must to set the IV length first */
  51687. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  51688. ivSz, NULL));
  51689. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51690. }
  51691. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51692. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51693. plaintxtSz));
  51694. ciphertxtSz = len;
  51695. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51696. ciphertxtSz += len;
  51697. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  51698. SM4_BLOCK_SIZE, tag));
  51699. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51700. EVP_CIPHER_CTX_init(&de[i]);
  51701. if (i == 0) {
  51702. /* Default uses 96-bits IV length */
  51703. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  51704. iv));
  51705. }
  51706. else {
  51707. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  51708. NULL));
  51709. /* non-default must to set the IV length first */
  51710. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  51711. ivSz, NULL));
  51712. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51713. }
  51714. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51715. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51716. ciphertxtSz));
  51717. decryptedtxtSz = len;
  51718. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  51719. SM4_BLOCK_SIZE, tag));
  51720. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51721. decryptedtxtSz += len;
  51722. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51723. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51724. /* modify tag*/
  51725. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51726. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51727. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  51728. SM4_BLOCK_SIZE, tag));
  51729. /* fail due to wrong tag */
  51730. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51731. ciphertxtSz));
  51732. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51733. ExpectIntEQ(0, len);
  51734. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51735. }
  51736. res = EXPECT_RESULT();
  51737. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  51738. return res;
  51739. }
  51740. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  51741. {
  51742. int res = TEST_SKIPPED;
  51743. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51744. /* Zero length plain text */
  51745. EXPECT_DECLS;
  51746. byte key[] = {
  51747. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51748. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51749. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51750. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51751. }; /* align */
  51752. byte iv[] = {
  51753. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51754. }; /* align */
  51755. byte plaintxt[1];
  51756. int ivSz = 12;
  51757. int plaintxtSz = 0;
  51758. unsigned char tag[16];
  51759. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51760. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51761. int ciphertxtSz = 0;
  51762. int decryptedtxtSz = 0;
  51763. int len = 0;
  51764. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51765. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51766. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  51767. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51768. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51769. plaintxtSz));
  51770. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51771. ciphertxtSz += len;
  51772. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  51773. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51774. ExpectIntEQ(0, ciphertxtSz);
  51775. EVP_CIPHER_CTX_init(de);
  51776. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  51777. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51778. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51779. decryptedtxtSz = len;
  51780. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  51781. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51782. decryptedtxtSz += len;
  51783. ExpectIntEQ(0, decryptedtxtSz);
  51784. EVP_CIPHER_CTX_free(en);
  51785. EVP_CIPHER_CTX_free(de);
  51786. res = EXPECT_RESULT();
  51787. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51788. return res;
  51789. }
  51790. static int test_wolfssl_EVP_sm4_ccm(void)
  51791. {
  51792. int res = TEST_SKIPPED;
  51793. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51794. EXPECT_DECLS;
  51795. byte *key = (byte*)"0123456789012345";
  51796. byte *iv = (byte*)"0123456789012";
  51797. int ivSz = (int)XSTRLEN((char*)iv);
  51798. /* Message to be encrypted */
  51799. byte *plaintxt = (byte*)"for things to change you have to change";
  51800. /* Additional non-confidential data */
  51801. byte *aad = (byte*)"Don't spend major time on minor things.";
  51802. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51803. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51804. int aadSz = (int)XSTRLEN((char*)aad);
  51805. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51806. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51807. int ciphertxtSz = 0;
  51808. int decryptedtxtSz = 0;
  51809. int len = 0;
  51810. int i = 0;
  51811. EVP_CIPHER_CTX en[2];
  51812. EVP_CIPHER_CTX de[2];
  51813. for (i = 0; i < 2; i++) {
  51814. EVP_CIPHER_CTX_init(&en[i]);
  51815. if (i == 0) {
  51816. /* Default uses 96-bits IV length */
  51817. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  51818. iv));
  51819. }
  51820. else {
  51821. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  51822. NULL));
  51823. /* non-default must to set the IV length first */
  51824. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51825. ivSz, NULL));
  51826. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51827. }
  51828. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51829. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51830. plaintxtSz));
  51831. ciphertxtSz = len;
  51832. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51833. ciphertxtSz += len;
  51834. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51835. SM4_BLOCK_SIZE, tag));
  51836. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51837. EVP_CIPHER_CTX_init(&de[i]);
  51838. if (i == 0) {
  51839. /* Default uses 96-bits IV length */
  51840. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  51841. iv));
  51842. }
  51843. else {
  51844. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  51845. NULL));
  51846. /* non-default must to set the IV length first */
  51847. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51848. ivSz, NULL));
  51849. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51850. }
  51851. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51852. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51853. ciphertxtSz));
  51854. decryptedtxtSz = len;
  51855. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51856. SM4_BLOCK_SIZE, tag));
  51857. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51858. decryptedtxtSz += len;
  51859. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51860. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51861. /* modify tag*/
  51862. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51863. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51864. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51865. SM4_BLOCK_SIZE, tag));
  51866. /* fail due to wrong tag */
  51867. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51868. ciphertxtSz));
  51869. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51870. ExpectIntEQ(0, len);
  51871. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51872. }
  51873. res = EXPECT_RESULT();
  51874. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51875. return res;
  51876. }
  51877. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  51878. {
  51879. EXPECT_DECLS;
  51880. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  51881. EVP_PKEY_CTX* ctx = NULL;
  51882. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51883. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  51884. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  51885. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  51886. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  51887. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  51888. byte outKey[34];
  51889. size_t outKeySz = sizeof(outKey);
  51890. /* These expected outputs were gathered by running the same test below using
  51891. * OpenSSL. */
  51892. const byte extractAndExpand[] = {
  51893. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51894. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51895. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51896. };
  51897. const byte extractOnly[] = {
  51898. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51899. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51900. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51901. };
  51902. const byte expandOnly[] = {
  51903. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51904. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51905. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51906. };
  51907. const byte extractAndExpandAddInfo[] = {
  51908. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51909. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51910. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51911. };
  51912. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51913. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51914. /* NULL ctx. */
  51915. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51916. /* NULL md. */
  51917. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51918. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51919. /* NULL ctx. */
  51920. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51921. WOLFSSL_FAILURE);
  51922. /* NULL salt is ok. */
  51923. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51924. WOLFSSL_SUCCESS);
  51925. /* Salt length <= 0. */
  51926. /* Length 0 salt is ok. */
  51927. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51928. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51929. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51930. WOLFSSL_SUCCESS);
  51931. /* NULL ctx. */
  51932. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51933. WOLFSSL_FAILURE);
  51934. /* NULL key. */
  51935. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51936. WOLFSSL_FAILURE);
  51937. /* Key length <= 0 */
  51938. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51939. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51940. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51941. WOLFSSL_SUCCESS);
  51942. /* NULL ctx. */
  51943. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51944. WOLFSSL_FAILURE);
  51945. /* NULL info is ok. */
  51946. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51947. WOLFSSL_SUCCESS);
  51948. /* Info length <= 0 */
  51949. /* Length 0 info is ok. */
  51950. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51951. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51952. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51953. WOLFSSL_SUCCESS);
  51954. /* NULL ctx. */
  51955. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51956. WOLFSSL_FAILURE);
  51957. /* Extract and expand (default). */
  51958. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51959. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51960. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51961. /* Extract only. */
  51962. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51963. WOLFSSL_SUCCESS);
  51964. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51965. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51966. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51967. outKeySz = sizeof(outKey);
  51968. /* Expand only. */
  51969. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51970. WOLFSSL_SUCCESS);
  51971. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51972. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51973. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51974. outKeySz = sizeof(outKey);
  51975. /* Extract and expand with appended additional info. */
  51976. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51977. WOLFSSL_SUCCESS);
  51978. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51979. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51980. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51981. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51982. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51983. EVP_PKEY_CTX_free(ctx);
  51984. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51985. return EXPECT_RESULT();
  51986. }
  51987. #ifndef NO_BIO
  51988. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51989. {
  51990. EXPECT_DECLS;
  51991. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51992. BIO* bio = NULL;
  51993. X509_INFO* info = NULL;
  51994. STACK_OF(X509_INFO)* sk = NULL;
  51995. char* subject = NULL;
  51996. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51997. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51998. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51999. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  52000. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  52001. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  52002. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  52003. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  52004. /* using dereference to maintain testing for Apache port*/
  52005. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  52006. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  52007. 0, 0));
  52008. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  52009. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  52010. X509_INFO_free(info);
  52011. info = NULL;
  52012. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  52013. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  52014. 0, 0));
  52015. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  52016. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  52017. X509_INFO_free(info);
  52018. ExpectNull(info = sk_X509_INFO_pop(sk));
  52019. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  52020. BIO_free(bio);
  52021. #endif
  52022. return EXPECT_RESULT();
  52023. }
  52024. #endif /* !NO_BIO */
  52025. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  52026. {
  52027. EXPECT_DECLS;
  52028. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  52029. X509 *x509 = NULL;
  52030. X509_NAME* name = NULL;
  52031. int idx = 0;
  52032. X509_NAME_ENTRY *ne = NULL;
  52033. ASN1_OBJECT *object = NULL;
  52034. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  52035. WOLFSSL_FILETYPE_PEM));
  52036. ExpectNotNull(name = X509_get_subject_name(x509));
  52037. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  52038. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  52039. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  52040. X509_free(x509);
  52041. #endif
  52042. return EXPECT_RESULT();
  52043. }
  52044. static int test_wolfSSL_X509_STORE_get1_certs(void)
  52045. {
  52046. EXPECT_DECLS;
  52047. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  52048. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  52049. X509_STORE_CTX *storeCtx = NULL;
  52050. X509_STORE *store = NULL;
  52051. X509 *caX509 = NULL;
  52052. X509 *svrX509 = NULL;
  52053. X509_NAME *subject = NULL;
  52054. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  52055. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  52056. SSL_FILETYPE_PEM));
  52057. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  52058. SSL_FILETYPE_PEM)));
  52059. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  52060. ExpectNotNull(store = X509_STORE_new());
  52061. ExpectNotNull(subject = X509_get_subject_name(caX509));
  52062. /* Errors */
  52063. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  52064. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  52065. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  52066. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  52067. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  52068. SSL_SUCCESS);
  52069. /* Should find the cert */
  52070. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  52071. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  52072. sk_X509_pop_free(certs, NULL);
  52073. certs = NULL;
  52074. /* Should not find the cert */
  52075. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  52076. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  52077. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  52078. sk_X509_pop_free(certs, NULL);
  52079. certs = NULL;
  52080. X509_STORE_free(store);
  52081. X509_STORE_CTX_free(storeCtx);
  52082. X509_free(svrX509);
  52083. X509_free(caX509);
  52084. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  52085. return EXPECT_RESULT();
  52086. }
  52087. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  52088. defined(WOLFSSL_LOCAL_X509_STORE) && \
  52089. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  52090. static int test_wolfSSL_X509_STORE_set_get_crl_provider(X509_STORE_CTX* ctx,
  52091. X509_CRL** crl_out, X509* cert) {
  52092. X509_CRL *crl = NULL;
  52093. XFILE fp = XBADFILE;
  52094. char* cert_issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
  52095. int ret = 0;
  52096. (void)ctx;
  52097. if (cert_issuer == NULL)
  52098. return 0;
  52099. if ((fp = XFOPEN("certs/crl/crl.pem", "rb")) != XBADFILE) {
  52100. PEM_read_X509_CRL(fp, &crl, NULL, NULL);
  52101. XFCLOSE(fp);
  52102. if (crl != NULL) {
  52103. char* crl_issuer = X509_NAME_oneline(
  52104. X509_CRL_get_issuer(crl), NULL, 0);
  52105. if (XSTRCMP(cert_issuer, crl_issuer) == 0) {
  52106. *crl_out = X509_CRL_dup(crl);
  52107. if (*crl_out != NULL)
  52108. ret = 1;
  52109. }
  52110. OPENSSL_free(crl_issuer);
  52111. }
  52112. }
  52113. X509_CRL_free(crl);
  52114. OPENSSL_free(cert_issuer);
  52115. return ret;
  52116. }
  52117. static int test_wolfSSL_X509_STORE_set_get_crl_provider2(X509_STORE_CTX* ctx,
  52118. X509_CRL** crl_out, X509* cert) {
  52119. (void)ctx;
  52120. (void)cert;
  52121. *crl_out = NULL;
  52122. return 1;
  52123. }
  52124. #ifndef NO_WOLFSSL_STUB
  52125. static int test_wolfSSL_X509_STORE_set_get_crl_check(X509_STORE_CTX* ctx,
  52126. X509_CRL* crl) {
  52127. (void)ctx;
  52128. (void)crl;
  52129. return 1;
  52130. }
  52131. #endif
  52132. static int test_wolfSSL_X509_STORE_set_get_crl_verify(int ok,
  52133. X509_STORE_CTX* ctx) {
  52134. int cert_error = X509_STORE_CTX_get_error(ctx);
  52135. X509_VERIFY_PARAM* param = X509_STORE_CTX_get0_param(ctx);
  52136. int flags = X509_VERIFY_PARAM_get_flags(param);
  52137. if ((flags & (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) !=
  52138. (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) {
  52139. /* Make sure the flags are set */
  52140. return 0;
  52141. }
  52142. /* Ignore CRL missing error */
  52143. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  52144. if (cert_error == CRL_MISSING)
  52145. #else
  52146. if (cert_error == X509_V_ERR_UNABLE_TO_GET_CRL)
  52147. #endif
  52148. return 1;
  52149. return ok;
  52150. }
  52151. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready(WOLFSSL_CTX* ctx)
  52152. {
  52153. EXPECT_DECLS;
  52154. X509_STORE* cert_store = NULL;
  52155. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  52156. WOLFSSL_SUCCESS);
  52157. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  52158. X509_STORE_set_get_crl(cert_store,
  52159. test_wolfSSL_X509_STORE_set_get_crl_provider);
  52160. #ifndef NO_WOLFSSL_STUB
  52161. X509_STORE_set_check_crl(cert_store,
  52162. test_wolfSSL_X509_STORE_set_get_crl_check);
  52163. #endif
  52164. return EXPECT_RESULT();
  52165. }
  52166. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2(WOLFSSL_CTX* ctx)
  52167. {
  52168. EXPECT_DECLS;
  52169. X509_STORE* cert_store = NULL;
  52170. X509_VERIFY_PARAM* param = NULL;
  52171. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  52172. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  52173. WOLFSSL_SUCCESS);
  52174. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  52175. X509_STORE_set_get_crl(cert_store,
  52176. test_wolfSSL_X509_STORE_set_get_crl_provider2);
  52177. #ifndef NO_WOLFSSL_STUB
  52178. X509_STORE_set_check_crl(cert_store,
  52179. test_wolfSSL_X509_STORE_set_get_crl_check);
  52180. #endif
  52181. X509_STORE_set_verify_cb(cert_store,
  52182. test_wolfSSL_X509_STORE_set_get_crl_verify);
  52183. ExpectNotNull(param = X509_STORE_get0_param(cert_store));
  52184. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(
  52185. param, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  52186. ExpectIntEQ(X509_STORE_set_flags(cert_store,
  52187. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  52188. return EXPECT_RESULT();
  52189. }
  52190. #endif
  52191. /* This test mimics the usage of the CRL provider in gRPC */
  52192. static int test_wolfSSL_X509_STORE_set_get_crl(void)
  52193. {
  52194. EXPECT_DECLS;
  52195. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  52196. defined(WOLFSSL_LOCAL_X509_STORE) && \
  52197. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  52198. test_ssl_cbf func_cb_client;
  52199. test_ssl_cbf func_cb_server;
  52200. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  52201. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  52202. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready;
  52203. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  52204. &func_cb_server, NULL), TEST_SUCCESS);
  52205. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  52206. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  52207. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2;
  52208. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  52209. &func_cb_server, NULL), TEST_SUCCESS);
  52210. #endif
  52211. return EXPECT_RESULT();
  52212. }
  52213. static int test_wolfSSL_dup_CA_list(void)
  52214. {
  52215. int res = TEST_SKIPPED;
  52216. #if defined(OPENSSL_ALL)
  52217. EXPECT_DECLS;
  52218. STACK_OF(X509_NAME) *originalStack = NULL;
  52219. STACK_OF(X509_NAME) *copyStack = NULL;
  52220. int originalCount = 0;
  52221. int copyCount = 0;
  52222. X509_NAME *name = NULL;
  52223. int i;
  52224. originalStack = sk_X509_NAME_new_null();
  52225. ExpectNotNull(originalStack);
  52226. for (i = 0; i < 3; i++) {
  52227. name = X509_NAME_new();
  52228. ExpectNotNull(name);
  52229. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  52230. }
  52231. copyStack = SSL_dup_CA_list(originalStack);
  52232. ExpectNotNull(copyStack);
  52233. originalCount = sk_X509_NAME_num(originalStack);
  52234. copyCount = sk_X509_NAME_num(copyStack);
  52235. AssertIntEQ(originalCount, copyCount);
  52236. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  52237. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  52238. originalStack = NULL;
  52239. copyStack = NULL;
  52240. res = EXPECT_RESULT();
  52241. #endif /* OPENSSL_ALL */
  52242. return res;
  52243. }
  52244. static int test_ForceZero(void)
  52245. {
  52246. EXPECT_DECLS;
  52247. unsigned char data[32];
  52248. unsigned int i, j, len;
  52249. /* Test case with 0 length */
  52250. ForceZero(data, 0);
  52251. /* Test ForceZero */
  52252. for (i = 0; i < sizeof(data); i++) {
  52253. for (len = 1; len < sizeof(data) - i; len++) {
  52254. for (j = 0; j < sizeof(data); j++)
  52255. data[j] = j + 1;
  52256. ForceZero(data + i, len);
  52257. for (j = 0; j < sizeof(data); j++) {
  52258. if (j < i || j >= i + len) {
  52259. ExpectIntNE(data[j], 0x00);
  52260. }
  52261. else {
  52262. ExpectIntEQ(data[j], 0x00);
  52263. }
  52264. }
  52265. }
  52266. }
  52267. return EXPECT_RESULT();
  52268. }
  52269. #ifndef NO_BIO
  52270. static int test_wolfSSL_X509_print(void)
  52271. {
  52272. EXPECT_DECLS;
  52273. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52274. !defined(NO_RSA) && defined(XSNPRINTF)
  52275. X509 *x509 = NULL;
  52276. BIO *bio = NULL;
  52277. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  52278. const X509_ALGOR *cert_sig_alg = NULL;
  52279. #endif
  52280. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  52281. WOLFSSL_FILETYPE_PEM));
  52282. /* print to memory */
  52283. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52284. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  52285. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  52286. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  52287. /* Will print IP address subject alt name. */
  52288. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  52289. #elif defined(NO_ASN_TIME)
  52290. /* Will print IP address subject alt name but not Validity. */
  52291. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  52292. #else
  52293. /* Will print IP address subject alt name. */
  52294. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  52295. #endif
  52296. #elif defined(NO_ASN_TIME)
  52297. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  52298. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  52299. #else
  52300. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  52301. #endif
  52302. BIO_free(bio);
  52303. bio = NULL;
  52304. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52305. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  52306. /* Print signature */
  52307. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  52308. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  52309. #endif
  52310. /* print to stderr */
  52311. #if !defined(NO_WOLFSSL_DIR)
  52312. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  52313. #endif
  52314. /* print again */
  52315. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  52316. X509_free(x509);
  52317. BIO_free(bio);
  52318. #endif
  52319. return EXPECT_RESULT();
  52320. }
  52321. static int test_wolfSSL_X509_CRL_print(void)
  52322. {
  52323. EXPECT_DECLS;
  52324. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  52325. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  52326. X509_CRL* crl = NULL;
  52327. BIO *bio = NULL;
  52328. XFILE fp = XBADFILE;
  52329. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  52330. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  52331. NULL, NULL));
  52332. if (fp != XBADFILE)
  52333. XFCLOSE(fp);
  52334. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  52335. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  52336. X509_CRL_free(crl);
  52337. BIO_free(bio);
  52338. #endif
  52339. return EXPECT_RESULT();
  52340. }
  52341. static int test_wolfSSL_BIO_get_len(void)
  52342. {
  52343. EXPECT_DECLS;
  52344. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  52345. BIO *bio = NULL;
  52346. const char txt[] = "Some example text to push to the BIO.";
  52347. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  52348. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  52349. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  52350. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  52351. BIO_free(bio);
  52352. bio = NULL;
  52353. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52354. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  52355. BIO_free(bio);
  52356. #endif
  52357. return EXPECT_RESULT();
  52358. }
  52359. #endif /* !NO_BIO */
  52360. static int test_wolfSSL_RSA(void)
  52361. {
  52362. EXPECT_DECLS;
  52363. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  52364. RSA* rsa = NULL;
  52365. const BIGNUM *n = NULL;
  52366. const BIGNUM *e = NULL;
  52367. const BIGNUM *d = NULL;
  52368. const BIGNUM *p = NULL;
  52369. const BIGNUM *q = NULL;
  52370. const BIGNUM *dmp1 = NULL;
  52371. const BIGNUM *dmq1 = NULL;
  52372. const BIGNUM *iqmp = NULL;
  52373. ExpectNotNull(rsa = RSA_new());
  52374. ExpectIntEQ(RSA_size(NULL), 0);
  52375. ExpectIntEQ(RSA_size(rsa), 0);
  52376. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  52377. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  52378. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  52379. #ifdef WOLFSSL_RSA_KEY_CHECK
  52380. ExpectIntEQ(RSA_check_key(rsa), 0);
  52381. #endif
  52382. RSA_free(rsa);
  52383. rsa = NULL;
  52384. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52385. ExpectIntEQ(RSA_size(rsa), 256);
  52386. #ifdef WOLFSSL_RSA_KEY_CHECK
  52387. ExpectIntEQ(RSA_check_key(NULL), 0);
  52388. ExpectIntEQ(RSA_check_key(rsa), 1);
  52389. #endif
  52390. /* sanity check */
  52391. ExpectIntEQ(RSA_bits(NULL), 0);
  52392. /* key */
  52393. ExpectIntEQ(RSA_bits(rsa), 2048);
  52394. RSA_get0_key(rsa, &n, &e, &d);
  52395. ExpectPtrEq(rsa->n, n);
  52396. ExpectPtrEq(rsa->e, e);
  52397. ExpectPtrEq(rsa->d, d);
  52398. n = NULL;
  52399. e = NULL;
  52400. d = NULL;
  52401. ExpectNotNull(n = BN_new());
  52402. ExpectNotNull(e = BN_new());
  52403. ExpectNotNull(d = BN_new());
  52404. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  52405. if (EXPECT_FAIL()) {
  52406. BN_free((BIGNUM*)n);
  52407. BN_free((BIGNUM*)e);
  52408. BN_free((BIGNUM*)d);
  52409. }
  52410. ExpectPtrEq(rsa->n, n);
  52411. ExpectPtrEq(rsa->e, e);
  52412. ExpectPtrEq(rsa->d, d);
  52413. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  52414. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  52415. /* crt_params */
  52416. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  52417. ExpectPtrEq(rsa->dmp1, dmp1);
  52418. ExpectPtrEq(rsa->dmq1, dmq1);
  52419. ExpectPtrEq(rsa->iqmp, iqmp);
  52420. dmp1 = NULL;
  52421. dmq1 = NULL;
  52422. iqmp = NULL;
  52423. ExpectNotNull(dmp1 = BN_new());
  52424. ExpectNotNull(dmq1 = BN_new());
  52425. ExpectNotNull(iqmp = BN_new());
  52426. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  52427. (BIGNUM*)iqmp), 1);
  52428. if (EXPECT_FAIL()) {
  52429. BN_free((BIGNUM*)dmp1);
  52430. BN_free((BIGNUM*)dmq1);
  52431. BN_free((BIGNUM*)iqmp);
  52432. }
  52433. ExpectPtrEq(rsa->dmp1, dmp1);
  52434. ExpectPtrEq(rsa->dmq1, dmq1);
  52435. ExpectPtrEq(rsa->iqmp, iqmp);
  52436. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  52437. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  52438. (BIGNUM*)iqmp), 0);
  52439. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  52440. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  52441. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  52442. ExpectNull(dmp1);
  52443. ExpectNull(dmq1);
  52444. ExpectNull(iqmp);
  52445. /* factors */
  52446. RSA_get0_factors(rsa, NULL, NULL);
  52447. RSA_get0_factors(rsa, &p, &q);
  52448. ExpectPtrEq(rsa->p, p);
  52449. ExpectPtrEq(rsa->q, q);
  52450. p = NULL;
  52451. q = NULL;
  52452. ExpectNotNull(p = BN_new());
  52453. ExpectNotNull(q = BN_new());
  52454. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  52455. if (EXPECT_FAIL()) {
  52456. BN_free((BIGNUM*)p);
  52457. BN_free((BIGNUM*)q);
  52458. }
  52459. ExpectPtrEq(rsa->p, p);
  52460. ExpectPtrEq(rsa->q, q);
  52461. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  52462. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  52463. RSA_get0_factors(NULL, NULL, NULL);
  52464. RSA_get0_factors(NULL, &p, &q);
  52465. ExpectNull(p);
  52466. ExpectNull(q);
  52467. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  52468. ExpectIntEQ(RSA_bits(rsa), 21);
  52469. RSA_free(rsa);
  52470. rsa = NULL;
  52471. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  52472. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  52473. ExpectIntEQ(RSA_size(rsa), 384);
  52474. ExpectIntEQ(RSA_bits(rsa), 3072);
  52475. RSA_free(rsa);
  52476. rsa = NULL;
  52477. #endif
  52478. /* remove for now with odd key size until adjusting rsa key size check with
  52479. wc_MakeRsaKey()
  52480. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  52481. RSA_free(rsa);
  52482. rsa = NULL;
  52483. */
  52484. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  52485. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  52486. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  52487. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  52488. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  52489. {
  52490. byte buff[FOURK_BUF];
  52491. byte der[FOURK_BUF];
  52492. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  52493. XFILE f = XBADFILE;
  52494. int bytes = 0;
  52495. /* test loading encrypted RSA private pem w/o password */
  52496. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  52497. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  52498. if (f != XBADFILE)
  52499. XFCLOSE(f);
  52500. XMEMSET(der, 0, sizeof(der));
  52501. /* test that error value is returned with no password */
  52502. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  52503. 0);
  52504. }
  52505. #endif
  52506. #endif
  52507. return EXPECT_RESULT();
  52508. }
  52509. static int test_wolfSSL_RSA_DER(void)
  52510. {
  52511. EXPECT_DECLS;
  52512. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52513. RSA *rsa = NULL;
  52514. int i;
  52515. const unsigned char *buff = NULL;
  52516. unsigned char *newBuff = NULL;
  52517. struct tbl_s
  52518. {
  52519. const unsigned char *der;
  52520. int sz;
  52521. } tbl[] = {
  52522. #ifdef USE_CERT_BUFFERS_1024
  52523. {client_key_der_1024, sizeof_client_key_der_1024},
  52524. {server_key_der_1024, sizeof_server_key_der_1024},
  52525. #endif
  52526. #ifdef USE_CERT_BUFFERS_2048
  52527. {client_key_der_2048, sizeof_client_key_der_2048},
  52528. {server_key_der_2048, sizeof_server_key_der_2048},
  52529. #endif
  52530. {NULL, 0}
  52531. };
  52532. /* Public Key DER */
  52533. struct tbl_s pub[] = {
  52534. #ifdef USE_CERT_BUFFERS_1024
  52535. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  52536. #endif
  52537. #ifdef USE_CERT_BUFFERS_2048
  52538. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  52539. #endif
  52540. {NULL, 0}
  52541. };
  52542. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  52543. buff = pub[0].der;
  52544. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  52545. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  52546. buff = tbl[0].der;
  52547. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  52548. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  52549. rsa = RSA_new();
  52550. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  52551. RSA_free(rsa);
  52552. rsa = NULL;
  52553. for (i = 0; tbl[i].der != NULL; i++)
  52554. {
  52555. /* Passing in pointer results in pointer moving. */
  52556. buff = tbl[i].der;
  52557. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  52558. ExpectNotNull(rsa);
  52559. RSA_free(rsa);
  52560. rsa = NULL;
  52561. }
  52562. for (i = 0; tbl[i].der != NULL; i++)
  52563. {
  52564. /* Passing in pointer results in pointer moving. */
  52565. buff = tbl[i].der;
  52566. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  52567. ExpectNotNull(rsa);
  52568. RSA_free(rsa);
  52569. rsa = NULL;
  52570. }
  52571. for (i = 0; pub[i].der != NULL; i++)
  52572. {
  52573. buff = pub[i].der;
  52574. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  52575. ExpectNotNull(rsa);
  52576. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  52577. newBuff = NULL;
  52578. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  52579. ExpectNotNull(newBuff);
  52580. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  52581. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52582. RSA_free(rsa);
  52583. rsa = NULL;
  52584. }
  52585. #endif
  52586. return EXPECT_RESULT();
  52587. }
  52588. static int test_wolfSSL_RSA_print(void)
  52589. {
  52590. EXPECT_DECLS;
  52591. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52592. !defined(NO_STDIO_FILESYSTEM) && \
  52593. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  52594. !defined(NO_BIO) && defined(XFPRINTF)
  52595. BIO *bio = NULL;
  52596. WOLFSSL_RSA* rsa = NULL;
  52597. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52598. ExpectNotNull(rsa = RSA_new());
  52599. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  52600. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  52601. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  52602. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  52603. /* Some very large number of indent spaces. */
  52604. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  52605. /* RSA is empty. */
  52606. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  52607. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  52608. RSA_free(rsa);
  52609. rsa = NULL;
  52610. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52611. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  52612. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  52613. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  52614. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  52615. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  52616. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  52617. BIO_free(bio);
  52618. RSA_free(rsa);
  52619. #endif
  52620. return EXPECT_RESULT();
  52621. }
  52622. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  52623. {
  52624. EXPECT_DECLS;
  52625. #ifndef NO_RSA
  52626. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  52627. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52628. RSA *rsa = NULL;
  52629. const unsigned char *derBuf = client_key_der_2048;
  52630. unsigned char em[256] = {0}; /* len = 2048/8 */
  52631. /* Random data simulating a hash */
  52632. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  52633. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  52634. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  52635. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  52636. };
  52637. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  52638. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  52639. RSA_PSS_SALTLEN_DIGEST), 0);
  52640. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  52641. RSA_PSS_SALTLEN_DIGEST), 0);
  52642. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  52643. RSA_PSS_SALTLEN_DIGEST), 0);
  52644. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  52645. RSA_PSS_SALTLEN_DIGEST), 0);
  52646. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  52647. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  52648. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52649. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  52650. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52651. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  52652. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52653. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  52654. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52655. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52656. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52657. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  52658. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52659. RSA_PSS_SALTLEN_DIGEST), 1);
  52660. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52661. RSA_PSS_SALTLEN_DIGEST), 1);
  52662. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52663. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  52664. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52665. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  52666. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52667. RSA_PSS_SALTLEN_MAX), 1);
  52668. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52669. RSA_PSS_SALTLEN_MAX), 1);
  52670. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  52671. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  52672. RSA_free(rsa);
  52673. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52674. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  52675. #endif
  52676. return EXPECT_RESULT();
  52677. }
  52678. static int test_wolfSSL_RSA_sign_sha3(void)
  52679. {
  52680. EXPECT_DECLS;
  52681. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  52682. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  52683. RSA* rsa = NULL;
  52684. const unsigned char *derBuf = client_key_der_2048;
  52685. unsigned char sigRet[256] = {0};
  52686. unsigned int sigLen = sizeof(sigRet);
  52687. /* Random data simulating a hash */
  52688. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  52689. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  52690. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  52691. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  52692. };
  52693. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  52694. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  52695. rsa), 1);
  52696. RSA_free(rsa);
  52697. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  52698. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  52699. return EXPECT_RESULT();
  52700. }
  52701. static int test_wolfSSL_RSA_get0_key(void)
  52702. {
  52703. EXPECT_DECLS;
  52704. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52705. RSA *rsa = NULL;
  52706. const BIGNUM* n = NULL;
  52707. const BIGNUM* e = NULL;
  52708. const BIGNUM* d = NULL;
  52709. const unsigned char* der;
  52710. int derSz;
  52711. #ifdef USE_CERT_BUFFERS_1024
  52712. der = client_key_der_1024;
  52713. derSz = sizeof_client_key_der_1024;
  52714. #elif defined(USE_CERT_BUFFERS_2048)
  52715. der = client_key_der_2048;
  52716. derSz = sizeof_client_key_der_2048;
  52717. #else
  52718. der = NULL;
  52719. derSz = 0;
  52720. #endif
  52721. if (der != NULL) {
  52722. RSA_get0_key(NULL, NULL, NULL, NULL);
  52723. RSA_get0_key(rsa, NULL, NULL, NULL);
  52724. RSA_get0_key(NULL, &n, &e, &d);
  52725. ExpectNull(n);
  52726. ExpectNull(e);
  52727. ExpectNull(d);
  52728. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  52729. ExpectNotNull(rsa);
  52730. RSA_get0_key(rsa, NULL, NULL, NULL);
  52731. RSA_get0_key(rsa, &n, NULL, NULL);
  52732. ExpectNotNull(n);
  52733. RSA_get0_key(rsa, NULL, &e, NULL);
  52734. ExpectNotNull(e);
  52735. RSA_get0_key(rsa, NULL, NULL, &d);
  52736. ExpectNotNull(d);
  52737. RSA_get0_key(rsa, &n, &e, &d);
  52738. ExpectNotNull(n);
  52739. ExpectNotNull(e);
  52740. ExpectNotNull(d);
  52741. RSA_free(rsa);
  52742. }
  52743. #endif
  52744. return EXPECT_RESULT();
  52745. }
  52746. static int test_wolfSSL_RSA_meth(void)
  52747. {
  52748. EXPECT_DECLS;
  52749. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52750. RSA *rsa = NULL;
  52751. RSA_METHOD *rsa_meth = NULL;
  52752. #ifdef WOLFSSL_KEY_GEN
  52753. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52754. RSA_free(rsa);
  52755. rsa = NULL;
  52756. #else
  52757. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52758. #endif
  52759. ExpectNotNull(RSA_get_default_method());
  52760. wolfSSL_RSA_meth_free(NULL);
  52761. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  52762. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  52763. RSA_METHOD_FLAG_NO_CHECK));
  52764. #ifndef NO_WOLFSSL_STUB
  52765. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  52766. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  52767. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  52768. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  52769. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  52770. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  52771. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  52772. #endif
  52773. ExpectIntEQ(RSA_flags(NULL), 0);
  52774. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  52775. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  52776. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  52777. ExpectNotNull(rsa = RSA_new());
  52778. /* No method set. */
  52779. ExpectIntEQ(RSA_flags(rsa), 0);
  52780. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52781. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52782. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52783. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  52784. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  52785. if (EXPECT_FAIL()) {
  52786. wolfSSL_RSA_meth_free(rsa_meth);
  52787. }
  52788. ExpectNull(RSA_get_method(NULL));
  52789. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  52790. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  52791. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52792. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52793. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  52794. RSA_METHOD_FLAG_NO_CHECK);
  52795. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52796. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52797. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  52798. /* rsa_meth is freed here */
  52799. RSA_free(rsa);
  52800. #endif
  52801. return EXPECT_RESULT();
  52802. }
  52803. static int test_wolfSSL_RSA_verify(void)
  52804. {
  52805. EXPECT_DECLS;
  52806. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  52807. #ifndef NO_BIO
  52808. XFILE fp = XBADFILE;
  52809. RSA *pKey = NULL;
  52810. RSA *pubKey = NULL;
  52811. X509 *cert = NULL;
  52812. const char *text = "Hello wolfSSL !";
  52813. unsigned char hash[SHA256_DIGEST_LENGTH];
  52814. unsigned char signature[2048/8];
  52815. unsigned int signatureLength;
  52816. byte *buf = NULL;
  52817. BIO *bio = NULL;
  52818. SHA256_CTX c;
  52819. EVP_PKEY *evpPkey = NULL;
  52820. EVP_PKEY *evpPubkey = NULL;
  52821. size_t sz;
  52822. /* generate hash */
  52823. SHA256_Init(&c);
  52824. SHA256_Update(&c, text, strlen(text));
  52825. SHA256_Final(hash, &c);
  52826. #ifdef WOLFSSL_SMALL_STACK_CACHE
  52827. /* workaround for small stack cache case */
  52828. wc_Sha256Free((wc_Sha256*)&c);
  52829. #endif
  52830. /* read privete key file */
  52831. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  52832. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  52833. ExpectTrue((sz = XFTELL(fp)) > 0);
  52834. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  52835. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  52836. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  52837. if (fp != XBADFILE) {
  52838. XFCLOSE(fp);
  52839. fp = XBADFILE;
  52840. }
  52841. /* read private key and sign hash data */
  52842. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  52843. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  52844. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  52845. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  52846. signature, &signatureLength, pKey), SSL_SUCCESS);
  52847. /* read public key and verify signed data */
  52848. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  52849. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  52850. if (fp != XBADFILE)
  52851. XFCLOSE(fp);
  52852. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  52853. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  52854. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  52855. signatureLength, pubKey), SSL_SUCCESS);
  52856. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  52857. signatureLength, NULL), SSL_FAILURE);
  52858. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  52859. signatureLength, pubKey), SSL_FAILURE);
  52860. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  52861. signatureLength, pubKey), SSL_FAILURE);
  52862. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  52863. signatureLength, NULL), SSL_FAILURE);
  52864. RSA_free(pKey);
  52865. EVP_PKEY_free(evpPkey);
  52866. RSA_free(pubKey);
  52867. EVP_PKEY_free(evpPubkey);
  52868. X509_free(cert);
  52869. BIO_free(bio);
  52870. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  52871. #endif
  52872. #endif
  52873. return EXPECT_RESULT();
  52874. }
  52875. static int test_wolfSSL_RSA_sign(void)
  52876. {
  52877. EXPECT_DECLS;
  52878. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52879. RSA *rsa;
  52880. unsigned char hash[SHA256_DIGEST_LENGTH];
  52881. #ifdef USE_CERT_BUFFERS_1024
  52882. const unsigned char* privDer = client_key_der_1024;
  52883. size_t privDerSz = sizeof_client_key_der_1024;
  52884. const unsigned char* pubDer = client_keypub_der_1024;
  52885. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52886. unsigned char signature[1024/8];
  52887. #else
  52888. const unsigned char* privDer = client_key_der_2048;
  52889. size_t privDerSz = sizeof_client_key_der_2048;
  52890. const unsigned char* pubDer = client_keypub_der_2048;
  52891. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52892. unsigned char signature[2048/8];
  52893. #endif
  52894. unsigned int signatureLen;
  52895. const unsigned char* der;
  52896. XMEMSET(hash, 0, sizeof(hash));
  52897. der = privDer;
  52898. rsa = NULL;
  52899. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52900. /* Invalid parameters. */
  52901. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  52902. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  52903. &signatureLen, rsa), 0);
  52904. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  52905. &signatureLen, rsa), 0);
  52906. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  52907. &signatureLen, rsa), 0);
  52908. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52909. NULL, rsa), 0);
  52910. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52911. &signatureLen, NULL), 0);
  52912. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52913. &signatureLen, rsa), 1);
  52914. RSA_free(rsa);
  52915. der = pubDer;
  52916. rsa = NULL;
  52917. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52918. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52919. signatureLen, rsa), 1);
  52920. RSA_free(rsa);
  52921. #endif
  52922. return EXPECT_RESULT();
  52923. }
  52924. static int test_wolfSSL_RSA_sign_ex(void)
  52925. {
  52926. EXPECT_DECLS;
  52927. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52928. RSA *rsa = NULL;
  52929. unsigned char hash[SHA256_DIGEST_LENGTH];
  52930. #ifdef USE_CERT_BUFFERS_1024
  52931. const unsigned char* privDer = client_key_der_1024;
  52932. size_t privDerSz = sizeof_client_key_der_1024;
  52933. const unsigned char* pubDer = client_keypub_der_1024;
  52934. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52935. unsigned char signature[1024/8];
  52936. #else
  52937. const unsigned char* privDer = client_key_der_2048;
  52938. size_t privDerSz = sizeof_client_key_der_2048;
  52939. const unsigned char* pubDer = client_keypub_der_2048;
  52940. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52941. unsigned char signature[2048/8];
  52942. #endif
  52943. unsigned int signatureLen;
  52944. const unsigned char* der;
  52945. unsigned char encodedHash[51];
  52946. unsigned int encodedHashLen;
  52947. const unsigned char expEncHash[] = {
  52948. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  52949. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  52950. 0x00, 0x04, 0x20,
  52951. /* Hash data */
  52952. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52953. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52954. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52955. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52956. };
  52957. XMEMSET(hash, 0, sizeof(hash));
  52958. ExpectNotNull(rsa = wolfSSL_RSA_new());
  52959. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52960. &signatureLen, rsa, 1), 0);
  52961. wolfSSL_RSA_free(rsa);
  52962. der = privDer;
  52963. rsa = NULL;
  52964. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52965. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  52966. -1), 0);
  52967. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  52968. signature, &signatureLen, rsa, 1), 0);
  52969. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52970. &signatureLen, rsa, 1), 0);
  52971. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52972. &signatureLen, rsa, 1), 0);
  52973. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52974. NULL, rsa, 1), 0);
  52975. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52976. &signatureLen, NULL, 1), 0);
  52977. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52978. &signatureLen, rsa, -1), 0);
  52979. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52980. &signatureLen, rsa, 0), 0);
  52981. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52982. &signatureLen, rsa, 0), 0);
  52983. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52984. NULL, rsa, 0), 0);
  52985. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52986. &signatureLen, rsa, 1), 1);
  52987. /* Test returning encoded hash. */
  52988. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  52989. &encodedHashLen, rsa, 0), 1);
  52990. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  52991. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  52992. RSA_free(rsa);
  52993. der = pubDer;
  52994. rsa = NULL;
  52995. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52996. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52997. signatureLen, rsa), 1);
  52998. RSA_free(rsa);
  52999. #endif
  53000. return EXPECT_RESULT();
  53001. }
  53002. static int test_wolfSSL_RSA_public_decrypt(void)
  53003. {
  53004. EXPECT_DECLS;
  53005. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53006. RSA *rsa;
  53007. unsigned char msg[SHA256_DIGEST_LENGTH];
  53008. #ifdef USE_CERT_BUFFERS_1024
  53009. const unsigned char* pubDer = client_keypub_der_1024;
  53010. size_t pubDerSz = sizeof_client_keypub_der_1024;
  53011. unsigned char decMsg[1024/8];
  53012. const unsigned char encMsg[] = {
  53013. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  53014. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  53015. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  53016. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  53017. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  53018. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  53019. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  53020. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  53021. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  53022. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  53023. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  53024. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  53025. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  53026. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  53027. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  53028. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  53029. };
  53030. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53031. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53032. defined(WC_RSA_NO_PADDING)
  53033. const unsigned char encMsgNoPad[] = {
  53034. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  53035. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  53036. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  53037. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  53038. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  53039. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  53040. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  53041. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  53042. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  53043. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  53044. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  53045. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  53046. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  53047. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  53048. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  53049. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  53050. };
  53051. #endif
  53052. #else
  53053. const unsigned char* pubDer = client_keypub_der_2048;
  53054. size_t pubDerSz = sizeof_client_keypub_der_2048;
  53055. unsigned char decMsg[2048/8];
  53056. const unsigned char encMsg[] = {
  53057. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  53058. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  53059. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  53060. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  53061. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  53062. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  53063. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  53064. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  53065. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  53066. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  53067. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  53068. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  53069. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  53070. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  53071. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  53072. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  53073. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  53074. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  53075. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  53076. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  53077. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  53078. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  53079. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  53080. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  53081. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  53082. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  53083. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  53084. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  53085. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  53086. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  53087. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  53088. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  53089. };
  53090. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53091. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53092. defined(WC_RSA_NO_PADDING)
  53093. const unsigned char encMsgNoPad[] = {
  53094. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  53095. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  53096. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  53097. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  53098. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  53099. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  53100. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  53101. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  53102. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  53103. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  53104. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  53105. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  53106. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  53107. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  53108. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  53109. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  53110. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  53111. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  53112. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  53113. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  53114. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  53115. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  53116. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  53117. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  53118. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  53119. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  53120. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  53121. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  53122. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  53123. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  53124. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  53125. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  53126. };
  53127. #endif
  53128. #endif
  53129. const unsigned char* der;
  53130. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53131. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53132. defined(WC_RSA_NO_PADDING)
  53133. int i;
  53134. #endif
  53135. XMEMSET(msg, 0, sizeof(msg));
  53136. der = pubDer;
  53137. rsa = NULL;
  53138. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  53139. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  53140. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  53141. RSA_PKCS1_PADDING), -1);
  53142. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  53143. RSA_PKCS1_PADDING), -1);
  53144. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  53145. RSA_PKCS1_PADDING), -1);
  53146. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  53147. RSA_PKCS1_PADDING), -1);
  53148. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  53149. RSA_PKCS1_PSS_PADDING), -1);
  53150. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  53151. RSA_PKCS1_PADDING), 32);
  53152. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  53153. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  53154. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  53155. defined(WC_RSA_NO_PADDING)
  53156. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  53157. rsa, RSA_NO_PADDING), sizeof(decMsg));
  53158. /* Zeros before actual data. */
  53159. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  53160. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  53161. }
  53162. /* Check actual data. */
  53163. XMEMSET(msg, 0x01, sizeof(msg));
  53164. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  53165. #endif
  53166. RSA_free(rsa);
  53167. #endif
  53168. return EXPECT_RESULT();
  53169. }
  53170. static int test_wolfSSL_RSA_private_encrypt(void)
  53171. {
  53172. EXPECT_DECLS;
  53173. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53174. RSA *rsa;
  53175. unsigned char msg[SHA256_DIGEST_LENGTH];
  53176. #ifdef USE_CERT_BUFFERS_1024
  53177. const unsigned char* privDer = client_key_der_1024;
  53178. size_t privDerSz = sizeof_client_key_der_1024;
  53179. unsigned char encMsg[1024/8];
  53180. const unsigned char expEncMsg[] = {
  53181. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  53182. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  53183. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  53184. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  53185. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  53186. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  53187. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  53188. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  53189. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  53190. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  53191. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  53192. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  53193. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  53194. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  53195. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  53196. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  53197. };
  53198. #ifdef WC_RSA_NO_PADDING
  53199. const unsigned char expEncMsgNoPad[] = {
  53200. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  53201. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  53202. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  53203. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  53204. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  53205. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  53206. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  53207. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  53208. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  53209. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  53210. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  53211. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  53212. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  53213. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  53214. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  53215. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  53216. };
  53217. #endif
  53218. #else
  53219. const unsigned char* privDer = client_key_der_2048;
  53220. size_t privDerSz = sizeof_client_key_der_2048;
  53221. unsigned char encMsg[2048/8];
  53222. const unsigned char expEncMsg[] = {
  53223. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  53224. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  53225. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  53226. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  53227. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  53228. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  53229. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  53230. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  53231. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  53232. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  53233. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  53234. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  53235. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  53236. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  53237. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  53238. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  53239. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  53240. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  53241. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  53242. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  53243. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  53244. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  53245. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  53246. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  53247. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  53248. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  53249. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  53250. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  53251. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  53252. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  53253. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  53254. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  53255. };
  53256. #ifdef WC_RSA_NO_PADDING
  53257. const unsigned char expEncMsgNoPad[] = {
  53258. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  53259. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  53260. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  53261. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  53262. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  53263. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  53264. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  53265. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  53266. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  53267. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  53268. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  53269. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  53270. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  53271. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  53272. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  53273. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  53274. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  53275. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  53276. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  53277. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  53278. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  53279. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  53280. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  53281. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  53282. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  53283. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  53284. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  53285. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  53286. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  53287. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  53288. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  53289. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  53290. };
  53291. #endif
  53292. #endif
  53293. const unsigned char* der;
  53294. XMEMSET(msg, 0x00, sizeof(msg));
  53295. der = privDer;
  53296. rsa = NULL;
  53297. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53298. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  53299. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  53300. -1);
  53301. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  53302. RSA_PKCS1_PADDING), -1);
  53303. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  53304. RSA_PKCS1_PADDING), -1);
  53305. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  53306. RSA_PKCS1_PADDING), -1);
  53307. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  53308. RSA_PKCS1_PSS_PADDING), -1);
  53309. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  53310. RSA_PKCS1_PADDING), sizeof(encMsg));
  53311. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  53312. #ifdef WC_RSA_NO_PADDING
  53313. /* Non-zero message. */
  53314. XMEMSET(msg, 0x01, sizeof(msg));
  53315. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  53316. RSA_NO_PADDING), sizeof(encMsg));
  53317. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  53318. #endif
  53319. RSA_free(rsa);
  53320. #endif
  53321. return EXPECT_RESULT();
  53322. }
  53323. static int test_wolfSSL_RSA_public_encrypt(void)
  53324. {
  53325. EXPECT_DECLS;
  53326. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53327. RSA* rsa = NULL;
  53328. const unsigned char msg[2048/8] = { 0 };
  53329. unsigned char encMsg[2048/8];
  53330. ExpectNotNull(rsa = RSA_new());
  53331. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  53332. RSA_PKCS1_PADDING), -1);
  53333. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  53334. RSA_PKCS1_PADDING), -1);
  53335. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  53336. RSA_PKCS1_PADDING), -1);
  53337. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  53338. RSA_PKCS1_PADDING), -1);
  53339. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  53340. RSA_PKCS1_PSS_PADDING), -1);
  53341. /* Empty RSA key. */
  53342. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  53343. RSA_PKCS1_PADDING), -1);
  53344. RSA_free(rsa);
  53345. #endif
  53346. return EXPECT_RESULT();
  53347. }
  53348. static int test_wolfSSL_RSA_private_decrypt(void)
  53349. {
  53350. EXPECT_DECLS;
  53351. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53352. RSA* rsa = NULL;
  53353. unsigned char msg[2048/8];
  53354. const unsigned char encMsg[2048/8] = { 0 };
  53355. ExpectNotNull(rsa = RSA_new());
  53356. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  53357. RSA_PKCS1_PADDING), -1);
  53358. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  53359. RSA_PKCS1_PADDING), -1);
  53360. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  53361. RSA_PKCS1_PADDING), -1);
  53362. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  53363. RSA_PKCS1_PADDING), -1);
  53364. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  53365. RSA_PKCS1_PSS_PADDING), -1);
  53366. /* Empty RSA key. */
  53367. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  53368. RSA_PKCS1_PADDING), -1);
  53369. RSA_free(rsa);
  53370. #endif
  53371. return EXPECT_RESULT();
  53372. }
  53373. static int test_wolfSSL_RSA_GenAdd(void)
  53374. {
  53375. EXPECT_DECLS;
  53376. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53377. RSA *rsa;
  53378. #ifdef USE_CERT_BUFFERS_1024
  53379. const unsigned char* privDer = client_key_der_1024;
  53380. size_t privDerSz = sizeof_client_key_der_1024;
  53381. const unsigned char* pubDer = client_keypub_der_1024;
  53382. size_t pubDerSz = sizeof_client_keypub_der_1024;
  53383. #else
  53384. const unsigned char* privDer = client_key_der_2048;
  53385. size_t privDerSz = sizeof_client_key_der_2048;
  53386. const unsigned char* pubDer = client_keypub_der_2048;
  53387. size_t pubDerSz = sizeof_client_keypub_der_2048;
  53388. #endif
  53389. const unsigned char* der;
  53390. der = privDer;
  53391. rsa = NULL;
  53392. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53393. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  53394. #ifndef RSA_LOW_MEM
  53395. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  53396. #else
  53397. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  53398. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  53399. #endif
  53400. RSA_free(rsa);
  53401. der = pubDer;
  53402. rsa = NULL;
  53403. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  53404. /* Need private values. */
  53405. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  53406. RSA_free(rsa);
  53407. #endif
  53408. return EXPECT_RESULT();
  53409. }
  53410. static int test_wolfSSL_RSA_blinding_on(void)
  53411. {
  53412. EXPECT_DECLS;
  53413. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  53414. RSA *rsa;
  53415. WOLFSSL_BN_CTX *bnCtx = NULL;
  53416. #ifdef USE_CERT_BUFFERS_1024
  53417. const unsigned char* privDer = client_key_der_1024;
  53418. size_t privDerSz = sizeof_client_key_der_1024;
  53419. #else
  53420. const unsigned char* privDer = client_key_der_2048;
  53421. size_t privDerSz = sizeof_client_key_der_2048;
  53422. #endif
  53423. const unsigned char* der;
  53424. der = privDer;
  53425. rsa = NULL;
  53426. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53427. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  53428. /* Does nothing so all parameters are valid. */
  53429. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  53430. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  53431. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  53432. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  53433. wolfSSL_BN_CTX_free(bnCtx);
  53434. RSA_free(rsa);
  53435. #endif
  53436. return EXPECT_RESULT();
  53437. }
  53438. static int test_wolfSSL_RSA_ex_data(void)
  53439. {
  53440. EXPECT_DECLS;
  53441. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  53442. RSA* rsa = NULL;
  53443. unsigned char data[1];
  53444. ExpectNotNull(rsa = RSA_new());
  53445. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  53446. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  53447. #ifdef MAX_EX_DATA
  53448. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  53449. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  53450. #endif
  53451. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  53452. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  53453. #ifdef HAVE_EX_DATA
  53454. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  53455. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  53456. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  53457. #else
  53458. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  53459. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  53460. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  53461. #endif
  53462. RSA_free(rsa);
  53463. #endif /* !NO_RSA && OPENSSL_EXTRA */
  53464. return EXPECT_RESULT();
  53465. }
  53466. static int test_wolfSSL_RSA_LoadDer(void)
  53467. {
  53468. EXPECT_DECLS;
  53469. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  53470. defined(OPENSSL_EXTRA_X509_SMALL))
  53471. RSA *rsa = NULL;
  53472. #ifdef USE_CERT_BUFFERS_1024
  53473. const unsigned char* privDer = client_key_der_1024;
  53474. size_t privDerSz = sizeof_client_key_der_1024;
  53475. #else
  53476. const unsigned char* privDer = client_key_der_2048;
  53477. size_t privDerSz = sizeof_client_key_der_2048;
  53478. #endif
  53479. ExpectNotNull(rsa = RSA_new());
  53480. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  53481. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  53482. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  53483. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  53484. RSA_free(rsa);
  53485. #endif /* !NO_RSA && OPENSSL_EXTRA */
  53486. return EXPECT_RESULT();
  53487. }
  53488. /* Local API. */
  53489. static int test_wolfSSL_RSA_To_Der(void)
  53490. {
  53491. EXPECT_DECLS;
  53492. #ifdef WOLFSSL_TEST_STATIC_BUILD
  53493. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  53494. RSA* rsa;
  53495. #ifdef USE_CERT_BUFFERS_1024
  53496. const unsigned char* privDer = client_key_der_1024;
  53497. size_t privDerSz = sizeof_client_key_der_1024;
  53498. const unsigned char* pubDer = client_keypub_der_1024;
  53499. size_t pubDerSz = sizeof_client_keypub_der_1024;
  53500. unsigned char out[sizeof(client_key_der_1024)];
  53501. #else
  53502. const unsigned char* privDer = client_key_der_2048;
  53503. size_t privDerSz = sizeof_client_key_der_2048;
  53504. const unsigned char* pubDer = client_keypub_der_2048;
  53505. size_t pubDerSz = sizeof_client_keypub_der_2048;
  53506. unsigned char out[sizeof(client_key_der_2048)];
  53507. #endif
  53508. const unsigned char* der;
  53509. unsigned char* outDer = NULL;
  53510. der = privDer;
  53511. rsa = NULL;
  53512. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53513. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  53514. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  53515. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  53516. outDer = out;
  53517. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  53518. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  53519. outDer = NULL;
  53520. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  53521. ExpectNotNull(outDer);
  53522. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  53523. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  53524. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  53525. outDer = out;
  53526. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  53527. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  53528. RSA_free(rsa);
  53529. ExpectNotNull(rsa = RSA_new());
  53530. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  53531. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  53532. RSA_free(rsa);
  53533. der = pubDer;
  53534. rsa = NULL;
  53535. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  53536. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  53537. RSA_free(rsa);
  53538. #endif
  53539. #endif
  53540. return EXPECT_RESULT();
  53541. }
  53542. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  53543. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  53544. {
  53545. EXPECT_DECLS;
  53546. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  53547. XFILE file = XBADFILE;
  53548. const char* fname = "./certs/server-keyPub.pem";
  53549. RSA *rsa = NULL;
  53550. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  53551. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  53552. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  53553. ExpectIntEQ(RSA_size(rsa), 256);
  53554. RSA_free(rsa);
  53555. if (file != XBADFILE)
  53556. XFCLOSE(file);
  53557. #endif
  53558. return EXPECT_RESULT();
  53559. }
  53560. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  53561. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  53562. {
  53563. EXPECT_DECLS;
  53564. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  53565. defined(WOLFSSL_KEY_GEN)
  53566. RSA* rsa = NULL;
  53567. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  53568. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  53569. /* Valid but stub so returns 0. */
  53570. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  53571. #endif
  53572. return EXPECT_RESULT();
  53573. }
  53574. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  53575. {
  53576. EXPECT_DECLS;
  53577. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  53578. (defined(WOLFSSL_PEM_TO_DER) || \
  53579. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  53580. RSA* rsa = NULL;
  53581. #ifdef USE_CERT_BUFFERS_1024
  53582. const unsigned char* privDer = client_key_der_1024;
  53583. size_t privDerSz = sizeof_client_key_der_1024;
  53584. #else
  53585. const unsigned char* privDer = client_key_der_2048;
  53586. size_t privDerSz = sizeof_client_key_der_2048;
  53587. #endif
  53588. const unsigned char* der;
  53589. #ifndef NO_AES
  53590. unsigned char passwd[] = "password";
  53591. #endif
  53592. ExpectNotNull(rsa = RSA_new());
  53593. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  53594. NULL, NULL), 0);
  53595. RSA_free(rsa);
  53596. der = privDer;
  53597. rsa = NULL;
  53598. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53599. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  53600. NULL, NULL), 0);
  53601. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  53602. NULL, NULL), 0);
  53603. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  53604. NULL, NULL), 1);
  53605. #ifndef NO_AES
  53606. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  53607. NULL, 0, NULL, NULL), 1);
  53608. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  53609. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  53610. #endif
  53611. RSA_free(rsa);
  53612. #endif
  53613. return EXPECT_RESULT();
  53614. }
  53615. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  53616. {
  53617. EXPECT_DECLS;
  53618. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  53619. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  53620. RSA* rsa = NULL;
  53621. #ifdef USE_CERT_BUFFERS_1024
  53622. const unsigned char* privDer = client_key_der_1024;
  53623. size_t privDerSz = sizeof_client_key_der_1024;
  53624. #else
  53625. const unsigned char* privDer = client_key_der_2048;
  53626. size_t privDerSz = sizeof_client_key_der_2048;
  53627. #endif
  53628. const unsigned char* der;
  53629. #ifndef NO_AES
  53630. unsigned char passwd[] = "password";
  53631. #endif
  53632. unsigned char* pem = NULL;
  53633. int plen;
  53634. ExpectNotNull(rsa = RSA_new());
  53635. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53636. &plen), 0);
  53637. RSA_free(rsa);
  53638. der = privDer;
  53639. rsa = NULL;
  53640. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53641. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  53642. &plen), 0);
  53643. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  53644. &plen), 0);
  53645. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53646. NULL), 0);
  53647. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53648. &plen), 1);
  53649. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53650. pem = NULL;
  53651. #ifndef NO_AES
  53652. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  53653. NULL, 0, &pem, &plen), 1);
  53654. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53655. pem = NULL;
  53656. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  53657. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  53658. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53659. #endif
  53660. RSA_free(rsa);
  53661. #endif
  53662. return EXPECT_RESULT();
  53663. }
  53664. static int test_wolfSSL_DH(void)
  53665. {
  53666. EXPECT_DECLS;
  53667. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53668. DH *dh = NULL;
  53669. BIGNUM* p;
  53670. BIGNUM* q;
  53671. BIGNUM* g;
  53672. BIGNUM* pub = NULL;
  53673. BIGNUM* priv = NULL;
  53674. #if defined(OPENSSL_ALL)
  53675. #if !defined(HAVE_FIPS) || \
  53676. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  53677. FILE* f = NULL;
  53678. unsigned char buf[268];
  53679. const unsigned char* pt = buf;
  53680. long len = 0;
  53681. dh = NULL;
  53682. XMEMSET(buf, 0, sizeof(buf));
  53683. /* Test 2048 bit parameters */
  53684. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  53685. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53686. if (f != XBADFILE)
  53687. XFCLOSE(f);
  53688. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  53689. ExpectNotNull(dh->p);
  53690. ExpectNotNull(dh->g);
  53691. ExpectTrue(pt == buf);
  53692. ExpectIntEQ(DH_generate_key(dh), 1);
  53693. ExpectIntEQ(DH_generate_key(dh), 1);
  53694. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  53695. ExpectNotNull(pub = BN_new());
  53696. ExpectIntEQ(BN_set_word(pub, 1), 1);
  53697. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  53698. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  53699. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  53700. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  53701. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  53702. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  53703. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  53704. BN_free(pub);
  53705. pub = NULL;
  53706. DH_get0_pqg(dh, (const BIGNUM**)&p,
  53707. (const BIGNUM**)&q,
  53708. (const BIGNUM**)&g);
  53709. ExpectPtrEq(p, dh->p);
  53710. ExpectPtrEq(q, dh->q);
  53711. ExpectPtrEq(g, dh->g);
  53712. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  53713. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  53714. ExpectPtrEq(pub, dh->pub_key);
  53715. ExpectPtrEq(priv, dh->priv_key);
  53716. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  53717. ExpectPtrEq(pub, dh->pub_key);
  53718. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  53719. ExpectPtrEq(priv, dh->priv_key);
  53720. pub = NULL;
  53721. priv = NULL;
  53722. ExpectNotNull(pub = BN_new());
  53723. ExpectNotNull(priv = BN_new());
  53724. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  53725. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  53726. if (EXPECT_FAIL()) {
  53727. BN_free(pub);
  53728. BN_free(priv);
  53729. }
  53730. pub = NULL;
  53731. priv = NULL;
  53732. ExpectNotNull(pub = BN_new());
  53733. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  53734. if (EXPECT_FAIL()) {
  53735. BN_free(pub);
  53736. }
  53737. ExpectNotNull(priv = BN_new());
  53738. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  53739. if (EXPECT_FAIL()) {
  53740. BN_free(priv);
  53741. }
  53742. ExpectPtrEq(pub, dh->pub_key);
  53743. ExpectPtrEq(priv, dh->priv_key);
  53744. pub = NULL;
  53745. priv = NULL;
  53746. DH_free(dh);
  53747. dh = NULL;
  53748. ExpectNotNull(dh = DH_new());
  53749. p = NULL;
  53750. ExpectNotNull(p = BN_new());
  53751. ExpectIntEQ(BN_set_word(p, 1), 1);
  53752. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  53753. ExpectNotNull(pub = BN_new());
  53754. ExpectNotNull(priv = BN_new());
  53755. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  53756. if (EXPECT_FAIL()) {
  53757. BN_free(pub);
  53758. BN_free(priv);
  53759. }
  53760. pub = NULL;
  53761. priv = NULL;
  53762. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  53763. BN_free(p);
  53764. p = NULL;
  53765. DH_free(dh);
  53766. dh = NULL;
  53767. #ifdef WOLFSSL_KEY_GEN
  53768. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  53769. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  53770. DH_free(dh);
  53771. dh = NULL;
  53772. #endif
  53773. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  53774. #endif /* OPENSSL_ALL */
  53775. (void)dh;
  53776. (void)p;
  53777. (void)q;
  53778. (void)g;
  53779. (void)pub;
  53780. (void)priv;
  53781. ExpectNotNull(dh = wolfSSL_DH_new());
  53782. /* invalid parameters test */
  53783. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  53784. (const BIGNUM**)&q,
  53785. (const BIGNUM**)&g);
  53786. DH_get0_pqg(dh, NULL,
  53787. (const BIGNUM**)&q,
  53788. (const BIGNUM**)&g);
  53789. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  53790. DH_get0_pqg(dh, NULL, NULL, NULL);
  53791. DH_get0_pqg(dh, (const BIGNUM**)&p,
  53792. (const BIGNUM**)&q,
  53793. (const BIGNUM**)&g);
  53794. ExpectPtrEq(p, NULL);
  53795. ExpectPtrEq(q, NULL);
  53796. ExpectPtrEq(g, NULL);
  53797. DH_free(dh);
  53798. dh = NULL;
  53799. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  53800. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  53801. #if defined(OPENSSL_ALL) || \
  53802. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53803. dh = wolfSSL_DH_new();
  53804. ExpectNotNull(dh);
  53805. p = wolfSSL_BN_new();
  53806. ExpectNotNull(p);
  53807. ExpectIntEQ(BN_set_word(p, 11), 1);
  53808. g = wolfSSL_BN_new();
  53809. ExpectNotNull(g);
  53810. ExpectIntEQ(BN_set_word(g, 2), 1);
  53811. q = wolfSSL_BN_new();
  53812. ExpectNotNull(q);
  53813. ExpectIntEQ(BN_set_word(q, 5), 1);
  53814. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  53815. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  53816. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  53817. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  53818. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  53819. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  53820. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  53821. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  53822. /* Don't need q. */
  53823. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53824. if (EXPECT_FAIL()) {
  53825. BN_free(p);
  53826. BN_free(g);
  53827. }
  53828. p = NULL;
  53829. g = NULL;
  53830. /* Setting again will free the p and g. */
  53831. wolfSSL_BN_free(q);
  53832. q = NULL;
  53833. DH_free(dh);
  53834. dh = NULL;
  53835. dh = wolfSSL_DH_new();
  53836. ExpectNotNull(dh);
  53837. p = wolfSSL_BN_new();
  53838. ExpectNotNull(p);
  53839. ExpectIntEQ(BN_set_word(p, 11), 1);
  53840. g = wolfSSL_BN_new();
  53841. ExpectNotNull(g);
  53842. ExpectIntEQ(BN_set_word(g, 2), 1);
  53843. q = wolfSSL_BN_new();
  53844. ExpectNotNull(q);
  53845. ExpectIntEQ(BN_set_word(q, 5), 1);
  53846. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  53847. /* p, q and g are now owned by dh - don't free. */
  53848. if (EXPECT_FAIL()) {
  53849. BN_free(p);
  53850. BN_free(q);
  53851. BN_free(g);
  53852. }
  53853. p = NULL;
  53854. q = NULL;
  53855. g = NULL;
  53856. p = wolfSSL_BN_new();
  53857. ExpectNotNull(p);
  53858. ExpectIntEQ(BN_set_word(p, 11), 1);
  53859. g = wolfSSL_BN_new();
  53860. ExpectNotNull(g);
  53861. ExpectIntEQ(BN_set_word(g, 2), 1);
  53862. q = wolfSSL_BN_new();
  53863. ExpectNotNull(q);
  53864. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  53865. if (EXPECT_FAIL()) {
  53866. BN_free(p);
  53867. }
  53868. p = NULL;
  53869. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  53870. if (EXPECT_FAIL()) {
  53871. BN_free(q);
  53872. }
  53873. q = NULL;
  53874. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  53875. if (EXPECT_FAIL()) {
  53876. BN_free(g);
  53877. }
  53878. g = NULL;
  53879. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  53880. /* p, q and g are now owned by dh - don't free. */
  53881. DH_free(dh);
  53882. dh = NULL;
  53883. ExpectIntEQ(DH_generate_key(NULL), 0);
  53884. ExpectNotNull(dh = DH_new());
  53885. ExpectIntEQ(DH_generate_key(dh), 0);
  53886. p = wolfSSL_BN_new();
  53887. ExpectNotNull(p);
  53888. ExpectIntEQ(BN_set_word(p, 0), 1);
  53889. g = wolfSSL_BN_new();
  53890. ExpectNotNull(g);
  53891. ExpectIntEQ(BN_set_word(g, 2), 1);
  53892. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53893. if (EXPECT_FAIL()) {
  53894. BN_free(p);
  53895. BN_free(g);
  53896. }
  53897. p = NULL;
  53898. g = NULL;
  53899. ExpectIntEQ(DH_generate_key(dh), 0);
  53900. DH_free(dh);
  53901. dh = NULL;
  53902. #endif
  53903. #endif
  53904. /* Test DH_up_ref() */
  53905. dh = wolfSSL_DH_new();
  53906. ExpectNotNull(dh);
  53907. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  53908. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  53909. DH_free(dh); /* decrease ref count */
  53910. DH_free(dh); /* free WOLFSSL_DH */
  53911. dh = NULL;
  53912. q = NULL;
  53913. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  53914. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  53915. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  53916. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  53917. #ifdef HAVE_FFDHE_2048
  53918. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53919. DH_free(dh);
  53920. dh = NULL;
  53921. q = NULL;
  53922. #endif
  53923. #ifdef HAVE_FFDHE_3072
  53924. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  53925. DH_free(dh);
  53926. dh = NULL;
  53927. q = NULL;
  53928. #endif
  53929. #ifdef HAVE_FFDHE_4096
  53930. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  53931. DH_free(dh);
  53932. dh = NULL;
  53933. q = NULL;
  53934. #endif
  53935. #else
  53936. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53937. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  53938. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  53939. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  53940. #endif /* OPENSSL_EXTRA && !NO_DH */
  53941. return EXPECT_RESULT();
  53942. }
  53943. static int test_wolfSSL_DH_dup(void)
  53944. {
  53945. EXPECT_DECLS;
  53946. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  53947. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  53948. defined(OPENSSL_EXTRA)
  53949. DH *dh = NULL;
  53950. DH *dhDup = NULL;
  53951. ExpectNotNull(dh = wolfSSL_DH_new());
  53952. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  53953. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  53954. #if defined(OPENSSL_ALL) || \
  53955. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53956. {
  53957. WOLFSSL_BIGNUM* p = NULL;
  53958. WOLFSSL_BIGNUM* g = NULL;
  53959. ExpectNotNull(p = wolfSSL_BN_new());
  53960. ExpectNotNull(g = wolfSSL_BN_new());
  53961. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  53962. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  53963. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53964. if (EXPECT_FAIL()) {
  53965. wolfSSL_BN_free(p);
  53966. wolfSSL_BN_free(g);
  53967. }
  53968. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  53969. wolfSSL_DH_free(dhDup);
  53970. }
  53971. #endif
  53972. wolfSSL_DH_free(dh);
  53973. #endif
  53974. #endif
  53975. return EXPECT_RESULT();
  53976. }
  53977. static int test_wolfSSL_DH_check(void)
  53978. {
  53979. EXPECT_DECLS;
  53980. #ifdef OPENSSL_ALL
  53981. #ifndef NO_DH
  53982. #ifndef NO_BIO
  53983. #ifndef NO_DSA
  53984. byte buf[6000];
  53985. char file[] = "./certs/dsaparams.pem";
  53986. XFILE f = XBADFILE;
  53987. int bytes;
  53988. BIO* bio = NULL;
  53989. DSA* dsa = NULL;
  53990. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53991. static const byte dh2048[] = {
  53992. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53993. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53994. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53995. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53996. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53997. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53998. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53999. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  54000. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  54001. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  54002. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  54003. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  54004. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  54005. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  54006. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  54007. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  54008. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  54009. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  54010. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  54011. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  54012. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  54013. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  54014. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  54015. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  54016. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  54017. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  54018. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  54019. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  54020. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  54021. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  54022. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  54023. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  54024. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  54025. 0x93, 0x02, 0x01, 0x02
  54026. };
  54027. const byte* params;
  54028. #endif
  54029. DH* dh = NULL;
  54030. WOLFSSL_BIGNUM* p = NULL;
  54031. WOLFSSL_BIGNUM* g = NULL;
  54032. WOLFSSL_BIGNUM* pTmp = NULL;
  54033. WOLFSSL_BIGNUM* gTmp = NULL;
  54034. int codes = -1;
  54035. #ifndef NO_DSA
  54036. /* Initialize DH */
  54037. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  54038. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  54039. if (f != XBADFILE)
  54040. XFCLOSE(f);
  54041. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  54042. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  54043. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  54044. ExpectNotNull(dh);
  54045. BIO_free(bio);
  54046. DSA_free(dsa);
  54047. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  54048. params = dh2048;
  54049. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  54050. (long)sizeof(dh2048)));
  54051. #else
  54052. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  54053. #endif
  54054. /* Test assumed to be valid dh.
  54055. * Should return WOLFSSL_SUCCESS
  54056. * codes should be 0
  54057. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  54058. */
  54059. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54060. ExpectIntEQ(codes, 0);
  54061. /* Test NULL dh: expected BAD_FUNC_ARG */
  54062. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  54063. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  54064. if (dh != NULL) {
  54065. pTmp = dh->p;
  54066. dh->p = NULL;
  54067. }
  54068. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54069. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54070. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  54071. /* set dh->p back to normal so it won't fail on next tests */
  54072. if (dh != NULL) {
  54073. dh->p = pTmp;
  54074. pTmp = NULL;
  54075. }
  54076. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  54077. if (dh != NULL) {
  54078. gTmp = dh->g;
  54079. dh->g = NULL;
  54080. }
  54081. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54082. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54083. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  54084. if (dh != NULL) {
  54085. dh->g = gTmp;
  54086. gTmp = NULL;
  54087. }
  54088. /* Cleanup */
  54089. DH_free(dh);
  54090. dh = NULL;
  54091. dh = DH_new();
  54092. ExpectNotNull(dh);
  54093. /* Check empty DH. */
  54094. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54095. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54096. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  54097. /* Check non-prime valued p. */
  54098. ExpectNotNull(p = BN_new());
  54099. ExpectIntEQ(BN_set_word(p, 4), 1);
  54100. ExpectNotNull(g = BN_new());
  54101. ExpectIntEQ(BN_set_word(g, 2), 1);
  54102. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  54103. if (EXPECT_FAIL()) {
  54104. wolfSSL_BN_free(p);
  54105. wolfSSL_BN_free(g);
  54106. }
  54107. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  54108. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  54109. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  54110. DH_free(dh);
  54111. dh = NULL;
  54112. #endif
  54113. #endif /* !NO_DH && !NO_DSA */
  54114. #endif
  54115. return EXPECT_RESULT();
  54116. }
  54117. static int test_wolfSSL_DH_prime(void)
  54118. {
  54119. EXPECT_DECLS;
  54120. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  54121. WOLFSSL_BIGNUM* bn = NULL;
  54122. #if WOLFSSL_MAX_BN_BITS >= 768
  54123. WOLFSSL_BIGNUM* bn2 = NULL;
  54124. #endif
  54125. bn = wolfSSL_DH_768_prime(NULL);
  54126. #if WOLFSSL_MAX_BN_BITS >= 768
  54127. ExpectNotNull(bn);
  54128. bn2 = wolfSSL_DH_768_prime(bn);
  54129. ExpectNotNull(bn2);
  54130. ExpectTrue(bn == bn2);
  54131. wolfSSL_BN_free(bn);
  54132. bn = NULL;
  54133. #else
  54134. ExpectNull(bn);
  54135. #endif
  54136. bn = wolfSSL_DH_1024_prime(NULL);
  54137. #if WOLFSSL_MAX_BN_BITS >= 1024
  54138. ExpectNotNull(bn);
  54139. wolfSSL_BN_free(bn);
  54140. bn = NULL;
  54141. #else
  54142. ExpectNull(bn);
  54143. #endif
  54144. bn = wolfSSL_DH_2048_prime(NULL);
  54145. #if WOLFSSL_MAX_BN_BITS >= 2048
  54146. ExpectNotNull(bn);
  54147. wolfSSL_BN_free(bn);
  54148. bn = NULL;
  54149. #else
  54150. ExpectNull(bn);
  54151. #endif
  54152. bn = wolfSSL_DH_3072_prime(NULL);
  54153. #if WOLFSSL_MAX_BN_BITS >= 3072
  54154. ExpectNotNull(bn);
  54155. wolfSSL_BN_free(bn);
  54156. bn = NULL;
  54157. #else
  54158. ExpectNull(bn);
  54159. #endif
  54160. bn = wolfSSL_DH_4096_prime(NULL);
  54161. #if WOLFSSL_MAX_BN_BITS >= 4096
  54162. ExpectNotNull(bn);
  54163. wolfSSL_BN_free(bn);
  54164. bn = NULL;
  54165. #else
  54166. ExpectNull(bn);
  54167. #endif
  54168. bn = wolfSSL_DH_6144_prime(NULL);
  54169. #if WOLFSSL_MAX_BN_BITS >= 6144
  54170. ExpectNotNull(bn);
  54171. wolfSSL_BN_free(bn);
  54172. bn = NULL;
  54173. #else
  54174. ExpectNull(bn);
  54175. #endif
  54176. bn = wolfSSL_DH_8192_prime(NULL);
  54177. #if WOLFSSL_MAX_BN_BITS >= 8192
  54178. ExpectNotNull(bn);
  54179. wolfSSL_BN_free(bn);
  54180. bn = NULL;
  54181. #else
  54182. ExpectNull(bn);
  54183. #endif
  54184. #endif
  54185. return EXPECT_RESULT();
  54186. }
  54187. static int test_wolfSSL_DH_1536_prime(void)
  54188. {
  54189. EXPECT_DECLS;
  54190. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  54191. BIGNUM* bn = NULL;
  54192. unsigned char bits[200];
  54193. int sz = 192; /* known binary size */
  54194. const byte expected[] = {
  54195. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  54196. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  54197. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  54198. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  54199. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  54200. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  54201. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  54202. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  54203. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  54204. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  54205. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  54206. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  54207. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  54208. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  54209. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  54210. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  54211. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  54212. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  54213. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  54214. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  54215. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  54216. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  54217. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  54218. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  54219. };
  54220. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  54221. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  54222. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  54223. BN_free(bn);
  54224. #endif
  54225. return EXPECT_RESULT();
  54226. }
  54227. static int test_wolfSSL_DH_get_2048_256(void)
  54228. {
  54229. EXPECT_DECLS;
  54230. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  54231. WOLFSSL_DH* dh = NULL;
  54232. const WOLFSSL_BIGNUM* pBn;
  54233. const WOLFSSL_BIGNUM* gBn;
  54234. const WOLFSSL_BIGNUM* qBn;
  54235. const byte pExpected[] = {
  54236. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  54237. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  54238. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  54239. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  54240. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  54241. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  54242. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  54243. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  54244. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  54245. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  54246. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  54247. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  54248. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  54249. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  54250. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  54251. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  54252. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  54253. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  54254. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  54255. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  54256. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  54257. 0x1E, 0x1A, 0x15, 0x97
  54258. };
  54259. const byte gExpected[] = {
  54260. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  54261. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  54262. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  54263. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  54264. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  54265. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  54266. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  54267. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  54268. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  54269. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  54270. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  54271. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  54272. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  54273. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  54274. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  54275. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  54276. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  54277. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  54278. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  54279. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  54280. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  54281. 0x6C, 0xC4, 0x16, 0x59
  54282. };
  54283. const byte qExpected[] = {
  54284. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  54285. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  54286. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  54287. };
  54288. int pSz = 0;
  54289. int qSz = 0;
  54290. int gSz = 0;
  54291. byte* pReturned = NULL;
  54292. byte* qReturned = NULL;
  54293. byte* gReturned = NULL;
  54294. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  54295. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  54296. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  54297. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  54298. DYNAMIC_TYPE_TMP_BUFFER));
  54299. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  54300. ExpectIntEQ(pSz, sizeof(pExpected));
  54301. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  54302. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  54303. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  54304. DYNAMIC_TYPE_TMP_BUFFER));
  54305. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  54306. ExpectIntEQ(qSz, sizeof(qExpected));
  54307. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  54308. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  54309. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  54310. DYNAMIC_TYPE_TMP_BUFFER));
  54311. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  54312. ExpectIntEQ(gSz, sizeof(gExpected));
  54313. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  54314. wolfSSL_DH_free(dh);
  54315. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54316. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54317. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54318. #endif
  54319. return EXPECT_RESULT();
  54320. }
  54321. static int test_wolfSSL_PEM_write_DHparams(void)
  54322. {
  54323. EXPECT_DECLS;
  54324. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  54325. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  54326. DH* dh = NULL;
  54327. BIO* bio = NULL;
  54328. XFILE fp = XBADFILE;
  54329. byte pem[2048];
  54330. int pemSz = 0;
  54331. const char expected[] =
  54332. "-----BEGIN DH PARAMETERS-----\n"
  54333. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  54334. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  54335. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  54336. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  54337. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  54338. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  54339. "-----END DH PARAMETERS-----\n";
  54340. const char badPem[] =
  54341. "-----BEGIN DH PARAMETERS-----\n"
  54342. "-----END DH PARAMETERS-----\n";
  54343. const char emptySeqPem[] =
  54344. "-----BEGIN DH PARAMETERS-----\n"
  54345. "MAA=\n"
  54346. "-----END DH PARAMETERS-----\n";
  54347. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  54348. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  54349. if (fp != XBADFILE) {
  54350. XFCLOSE(fp);
  54351. fp = XBADFILE;
  54352. }
  54353. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  54354. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  54355. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54356. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  54357. (int)sizeof(badPem));
  54358. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54359. BIO_free(bio);
  54360. bio = NULL;
  54361. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  54362. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54363. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  54364. (int)sizeof(emptySeqPem));
  54365. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54366. BIO_free(bio);
  54367. bio = NULL;
  54368. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  54369. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  54370. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  54371. BIO_free(bio);
  54372. bio = NULL;
  54373. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  54374. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  54375. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  54376. DH_free(dh);
  54377. dh = NULL;
  54378. dh = wolfSSL_DH_new();
  54379. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  54380. if (fp != XBADFILE) {
  54381. XFCLOSE(fp);
  54382. fp = XBADFILE;
  54383. }
  54384. wolfSSL_DH_free(dh);
  54385. dh = NULL;
  54386. /* check results */
  54387. XMEMSET(pem, 0, sizeof(pem));
  54388. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  54389. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  54390. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  54391. if (fp != XBADFILE)
  54392. XFCLOSE(fp);
  54393. #endif
  54394. return EXPECT_RESULT();
  54395. }
  54396. static int test_wolfSSL_d2i_DHparams(void)
  54397. {
  54398. EXPECT_DECLS;
  54399. #ifdef OPENSSL_ALL
  54400. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  54401. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54402. XFILE f = XBADFILE;
  54403. unsigned char buf[4096];
  54404. const unsigned char* pt = buf;
  54405. #ifdef HAVE_FFDHE_2048
  54406. const char* params1 = "./certs/dh2048.der";
  54407. #endif
  54408. #ifdef HAVE_FFDHE_3072
  54409. const char* params2 = "./certs/dh3072.der";
  54410. #endif
  54411. long len = 0;
  54412. WOLFSSL_DH* dh = NULL;
  54413. XMEMSET(buf, 0, sizeof(buf));
  54414. /* Test 2048 bit parameters */
  54415. #ifdef HAVE_FFDHE_2048
  54416. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  54417. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54418. if (f != XBADFILE) {
  54419. XFCLOSE(f);
  54420. f = XBADFILE;
  54421. }
  54422. /* Valid case */
  54423. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54424. ExpectNotNull(dh->p);
  54425. ExpectNotNull(dh->g);
  54426. ExpectTrue(pt == buf);
  54427. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  54428. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  54429. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  54430. /* Invalid cases */
  54431. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  54432. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  54433. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  54434. DH_free(dh);
  54435. dh = NULL;
  54436. *buf = 0;
  54437. pt = buf;
  54438. #endif /* HAVE_FFDHE_2048 */
  54439. /* Test 3072 bit parameters */
  54440. #ifdef HAVE_FFDHE_3072
  54441. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  54442. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54443. if (f != XBADFILE) {
  54444. XFCLOSE(f);
  54445. f = XBADFILE;
  54446. }
  54447. /* Valid case */
  54448. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  54449. ExpectNotNull(dh->p);
  54450. ExpectNotNull(dh->g);
  54451. ExpectTrue(pt != buf);
  54452. ExpectIntEQ(DH_generate_key(dh), 1);
  54453. /* Invalid cases */
  54454. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  54455. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  54456. DH_free(dh);
  54457. dh = NULL;
  54458. #endif /* HAVE_FFDHE_3072 */
  54459. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54460. #endif /* !NO_DH */
  54461. #endif
  54462. return EXPECT_RESULT();
  54463. }
  54464. static int test_wolfSSL_DH_LoadDer(void)
  54465. {
  54466. EXPECT_DECLS;
  54467. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  54468. defined(OPENSSL_EXTRA)
  54469. static const byte dh2048[] = {
  54470. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  54471. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  54472. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  54473. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  54474. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  54475. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  54476. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  54477. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  54478. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  54479. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  54480. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  54481. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  54482. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  54483. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  54484. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  54485. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  54486. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  54487. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  54488. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  54489. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  54490. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  54491. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  54492. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  54493. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  54494. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  54495. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  54496. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  54497. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  54498. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  54499. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  54500. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  54501. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  54502. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  54503. 0x93, 0x02, 0x01, 0x02
  54504. };
  54505. WOLFSSL_DH* dh = NULL;
  54506. ExpectNotNull(dh = wolfSSL_DH_new());
  54507. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  54508. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  54509. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  54510. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  54511. wolfSSL_DH_free(dh);
  54512. #endif
  54513. return EXPECT_RESULT();
  54514. }
  54515. static int test_wolfSSL_i2d_DHparams(void)
  54516. {
  54517. EXPECT_DECLS;
  54518. #ifdef OPENSSL_ALL
  54519. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  54520. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54521. XFILE f = XBADFILE;
  54522. unsigned char buf[4096];
  54523. const unsigned char* pt;
  54524. unsigned char* pt2;
  54525. #ifdef HAVE_FFDHE_2048
  54526. const char* params1 = "./certs/dh2048.der";
  54527. #endif
  54528. #ifdef HAVE_FFDHE_3072
  54529. const char* params2 = "./certs/dh3072.der";
  54530. #endif
  54531. long len = 0;
  54532. WOLFSSL_DH* dh = NULL;
  54533. /* Test 2048 bit parameters */
  54534. #ifdef HAVE_FFDHE_2048
  54535. pt = buf;
  54536. pt2 = buf;
  54537. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  54538. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54539. if (f != XBADFILE) {
  54540. XFCLOSE(f);
  54541. f = XBADFILE;
  54542. }
  54543. /* Valid case */
  54544. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54545. ExpectTrue(pt == buf);
  54546. ExpectIntEQ(DH_generate_key(dh), 1);
  54547. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  54548. /* Invalid case */
  54549. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  54550. /* Return length only */
  54551. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  54552. DH_free(dh);
  54553. dh = NULL;
  54554. *buf = 0;
  54555. #endif
  54556. /* Test 3072 bit parameters */
  54557. #ifdef HAVE_FFDHE_3072
  54558. pt = buf;
  54559. pt2 = buf;
  54560. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  54561. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54562. if (f != XBADFILE) {
  54563. XFCLOSE(f);
  54564. f = XBADFILE;
  54565. }
  54566. /* Valid case */
  54567. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54568. ExpectTrue(pt == buf);
  54569. ExpectIntEQ(DH_generate_key(dh), 1);
  54570. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  54571. /* Invalid case */
  54572. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  54573. /* Return length only */
  54574. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  54575. DH_free(dh);
  54576. dh = NULL;
  54577. #endif
  54578. dh = DH_new();
  54579. ExpectNotNull(dh);
  54580. pt2 = buf;
  54581. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  54582. DH_free(dh);
  54583. dh = NULL;
  54584. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54585. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  54586. #endif
  54587. return EXPECT_RESULT();
  54588. }
  54589. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  54590. /*----------------------------------------------------------------------------*
  54591. | EC
  54592. *----------------------------------------------------------------------------*/
  54593. static int test_wolfSSL_EC_GROUP(void)
  54594. {
  54595. EXPECT_DECLS;
  54596. #ifdef OPENSSL_EXTRA
  54597. EC_GROUP *group = NULL;
  54598. EC_GROUP *group2 = NULL;
  54599. EC_GROUP *group3 = NULL;
  54600. #ifndef HAVE_ECC_BRAINPOOL
  54601. EC_GROUP *group4 = NULL;
  54602. #endif
  54603. WOLFSSL_BIGNUM* order = NULL;
  54604. int group_bits;
  54605. int i;
  54606. static const int knownEccNids[] = {
  54607. NID_X9_62_prime192v1,
  54608. NID_X9_62_prime192v2,
  54609. NID_X9_62_prime192v3,
  54610. NID_X9_62_prime239v1,
  54611. NID_X9_62_prime239v2,
  54612. NID_X9_62_prime239v3,
  54613. NID_X9_62_prime256v1,
  54614. NID_secp112r1,
  54615. NID_secp112r2,
  54616. NID_secp128r1,
  54617. NID_secp128r2,
  54618. NID_secp160r1,
  54619. NID_secp160r2,
  54620. NID_secp224r1,
  54621. NID_secp384r1,
  54622. NID_secp521r1,
  54623. NID_secp160k1,
  54624. NID_secp192k1,
  54625. NID_secp224k1,
  54626. NID_secp256k1,
  54627. NID_brainpoolP160r1,
  54628. NID_brainpoolP192r1,
  54629. NID_brainpoolP224r1,
  54630. NID_brainpoolP256r1,
  54631. NID_brainpoolP320r1,
  54632. NID_brainpoolP384r1,
  54633. NID_brainpoolP512r1,
  54634. };
  54635. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  54636. static const int knownEccEnums[] = {
  54637. ECC_SECP192R1,
  54638. ECC_PRIME192V2,
  54639. ECC_PRIME192V3,
  54640. ECC_PRIME239V1,
  54641. ECC_PRIME239V2,
  54642. ECC_PRIME239V3,
  54643. ECC_SECP256R1,
  54644. ECC_SECP112R1,
  54645. ECC_SECP112R2,
  54646. ECC_SECP128R1,
  54647. ECC_SECP128R2,
  54648. ECC_SECP160R1,
  54649. ECC_SECP160R2,
  54650. ECC_SECP224R1,
  54651. ECC_SECP384R1,
  54652. ECC_SECP521R1,
  54653. ECC_SECP160K1,
  54654. ECC_SECP192K1,
  54655. ECC_SECP224K1,
  54656. ECC_SECP256K1,
  54657. ECC_BRAINPOOLP160R1,
  54658. ECC_BRAINPOOLP192R1,
  54659. ECC_BRAINPOOLP224R1,
  54660. ECC_BRAINPOOLP256R1,
  54661. ECC_BRAINPOOLP320R1,
  54662. ECC_BRAINPOOLP384R1,
  54663. ECC_BRAINPOOLP512R1,
  54664. };
  54665. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  54666. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54667. ExpectNotNull(group2 = EC_GROUP_dup(group));
  54668. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  54669. #ifndef HAVE_ECC_BRAINPOOL
  54670. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  54671. NID_brainpoolP256r1));
  54672. #endif
  54673. ExpectNull(EC_GROUP_dup(NULL));
  54674. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  54675. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  54676. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  54677. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  54678. #ifndef HAVE_ECC_BRAINPOOL
  54679. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  54680. #endif
  54681. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  54682. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  54683. ExpectNotNull(order = BN_new());
  54684. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  54685. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  54686. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  54687. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  54688. wolfSSL_BN_free(order);
  54689. ExpectNotNull(EC_GROUP_method_of(group));
  54690. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  54691. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  54692. NID_X9_62_prime_field);
  54693. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  54694. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  54695. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  54696. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  54697. #ifndef NO_WOLFSSL_STUB
  54698. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  54699. #endif
  54700. #ifndef HAVE_ECC_BRAINPOOL
  54701. EC_GROUP_free(group4);
  54702. #endif
  54703. EC_GROUP_free(group3);
  54704. EC_GROUP_free(group2);
  54705. EC_GROUP_free(group);
  54706. for (i = 0; i < knowEccNidsLen; i++) {
  54707. group = NULL;
  54708. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  54709. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  54710. EC_GROUP_free(group);
  54711. }
  54712. for (i = 0; i < knowEccEnumsLen; i++) {
  54713. group = NULL;
  54714. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  54715. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  54716. EC_GROUP_free(group);
  54717. }
  54718. #endif
  54719. return EXPECT_RESULT();
  54720. }
  54721. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  54722. {
  54723. EXPECT_DECLS;
  54724. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54725. EC_GROUP *group = NULL;
  54726. BIO* bio = NULL;
  54727. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  54728. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  54729. EC_GROUP *ret = NULL;
  54730. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  54731. "BgUrgQQAIg==\n"
  54732. "-----END EC PARAMETERS-----";
  54733. #endif
  54734. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  54735. "MAA=\n"
  54736. "-----END EC PARAMETERS-----";
  54737. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  54738. "BgA=\n"
  54739. "-----END EC PARAMETERS-----";
  54740. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  54741. "BgE=\n"
  54742. "-----END EC PARAMETERS-----";
  54743. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  54744. "BgE*\n"
  54745. "-----END EC PARAMETERS-----";
  54746. /* Test that first parameter, bio, being NULL fails. */
  54747. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  54748. /* Test that reading named parameters works. */
  54749. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  54750. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  54751. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54752. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  54753. BIO_free(bio);
  54754. bio = NULL;
  54755. EC_GROUP_free(group);
  54756. group = NULL;
  54757. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  54758. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  54759. /* Test that reusing group works. */
  54760. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  54761. sizeof(ec_nc_p384)));
  54762. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  54763. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  54764. BIO_free(bio);
  54765. bio = NULL;
  54766. EC_GROUP_free(group);
  54767. group = NULL;
  54768. /* Test that returning through group works. */
  54769. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  54770. sizeof(ec_nc_p384)));
  54771. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  54772. ExpectIntEQ(group == ret, 1);
  54773. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  54774. BIO_free(bio);
  54775. bio = NULL;
  54776. EC_GROUP_free(group);
  54777. group = NULL;
  54778. #endif
  54779. /* Test 0x30, 0x00 (not and object id) fails. */
  54780. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  54781. sizeof(ec_nc_bad_1)));
  54782. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54783. BIO_free(bio);
  54784. bio = NULL;
  54785. /* Test 0x06, 0x00 (empty object id) fails. */
  54786. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  54787. sizeof(ec_nc_bad_2)));
  54788. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54789. BIO_free(bio);
  54790. bio = NULL;
  54791. /* Test 0x06, 0x01 (badly formed object id) fails. */
  54792. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  54793. sizeof(ec_nc_bad_3)));
  54794. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54795. BIO_free(bio);
  54796. bio = NULL;
  54797. /* Test invalid PEM encoding - invalid character. */
  54798. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  54799. sizeof(ec_nc_bad_4)));
  54800. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54801. BIO_free(bio);
  54802. #endif
  54803. return EXPECT_RESULT();
  54804. }
  54805. static int test_wolfSSL_EC_POINT(void)
  54806. {
  54807. EXPECT_DECLS;
  54808. #if !defined(WOLFSSL_SP_MATH) && \
  54809. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  54810. #ifdef OPENSSL_EXTRA
  54811. BN_CTX* ctx = NULL;
  54812. EC_GROUP* group = NULL;
  54813. #ifndef HAVE_ECC_BRAINPOOL
  54814. EC_GROUP* group2 = NULL;
  54815. #endif
  54816. EC_POINT* Gxy = NULL;
  54817. EC_POINT* new_point = NULL;
  54818. EC_POINT* set_point = NULL;
  54819. EC_POINT* get_point = NULL;
  54820. EC_POINT* infinity = NULL;
  54821. BIGNUM* k = NULL;
  54822. BIGNUM* Gx = NULL;
  54823. BIGNUM* Gy = NULL;
  54824. BIGNUM* Gz = NULL;
  54825. BIGNUM* X = NULL;
  54826. BIGNUM* Y = NULL;
  54827. BIGNUM* set_point_bn = NULL;
  54828. char* hexStr = NULL;
  54829. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  54830. "17AF381913FF7A96314EA47055EA0FD0";
  54831. /* NISTP256R1 Gx/Gy */
  54832. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  54833. "77037D812DEB33A0F4A13945D898C296";
  54834. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  54835. "2BCE33576B315ECECBB6406837BF51F5";
  54836. const char* uncompG
  54837. = "046B17D1F2E12C4247F8BCE6E563A440F2"
  54838. "77037D812DEB33A0F4A13945D898C296"
  54839. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  54840. "2BCE33576B315ECECBB6406837BF51F5";
  54841. const char* compG
  54842. = "036B17D1F2E12C4247F8BCE6E563A440F2"
  54843. "77037D812DEB33A0F4A13945D898C296";
  54844. #ifndef HAVE_SELFTEST
  54845. EC_POINT *tmp = NULL;
  54846. size_t bin_len;
  54847. unsigned int blen = 0;
  54848. unsigned char* buf = NULL;
  54849. unsigned char bufInf[1] = { 0x00 };
  54850. const unsigned char binUncompG[] = {
  54851. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54852. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54853. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54854. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54855. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54856. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54857. };
  54858. const unsigned char binUncompGBad[] = {
  54859. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54860. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54861. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54862. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54863. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54864. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54865. };
  54866. #ifdef HAVE_COMP_KEY
  54867. const unsigned char binCompG[] = {
  54868. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54869. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54870. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54871. };
  54872. #endif
  54873. #endif
  54874. ExpectNotNull(ctx = BN_CTX_new());
  54875. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54876. #ifndef HAVE_ECC_BRAINPOOL
  54877. /* Used to make groups curve_idx == -1. */
  54878. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  54879. #endif
  54880. ExpectNull(EC_POINT_new(NULL));
  54881. ExpectNotNull(Gxy = EC_POINT_new(group));
  54882. ExpectNotNull(new_point = EC_POINT_new(group));
  54883. ExpectNotNull(set_point = EC_POINT_new(group));
  54884. ExpectNotNull(X = BN_new());
  54885. ExpectNotNull(Y = BN_new());
  54886. ExpectNotNull(set_point_bn = BN_new());
  54887. ExpectNotNull(infinity = EC_POINT_new(group));
  54888. /* load test values */
  54889. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  54890. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  54891. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  54892. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  54893. /* populate coordinates for input point */
  54894. if (Gxy != NULL) {
  54895. Gxy->X = Gx;
  54896. Gxy->Y = Gy;
  54897. Gxy->Z = Gz;
  54898. }
  54899. /* Test handling of NULL point. */
  54900. EC_POINT_clear_free(NULL);
  54901. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54902. NULL, NULL, ctx), 0);
  54903. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  54904. NULL, NULL, ctx), 0);
  54905. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  54906. NULL, NULL, ctx), 0);
  54907. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54908. X, NULL, ctx), 0);
  54909. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54910. NULL, Y, ctx), 0);
  54911. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  54912. X, Y, ctx), 0);
  54913. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  54914. X, Y, ctx), 0);
  54915. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  54916. NULL, Y, ctx), 0);
  54917. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  54918. X, NULL, ctx), 0);
  54919. /* Getting point at infinity returns an error. */
  54920. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  54921. X, Y, ctx), 0);
  54922. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54923. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54924. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54925. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  54926. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  54927. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  54928. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  54929. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  54930. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  54931. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  54932. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  54933. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  54934. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  54935. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  54936. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  54937. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  54938. /* perform point multiplication */
  54939. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  54940. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54941. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54942. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54943. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  54944. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54945. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54946. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54947. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  54948. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54949. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54950. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54951. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  54952. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  54953. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  54954. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54955. /* Set point to something. */
  54956. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  54957. #else
  54958. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  54959. ctx), 1);
  54960. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54961. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54962. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54963. #endif
  54964. /* check if point X coordinate is zero */
  54965. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54966. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  54967. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  54968. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  54969. #endif
  54970. /* extract the coordinates from point */
  54971. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54972. ctx), WOLFSSL_SUCCESS);
  54973. /* check if point X coordinate is zero */
  54974. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  54975. /* set the same X and Y points in another object */
  54976. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  54977. ctx), WOLFSSL_SUCCESS);
  54978. /* compare points as they should be the same */
  54979. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  54980. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  54981. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  54982. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  54983. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  54984. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  54985. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  54986. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  54987. /* Test copying */
  54988. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  54989. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  54990. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  54991. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  54992. /* Test inverting */
  54993. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  54994. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  54995. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  54996. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  54997. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54998. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54999. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55000. {
  55001. EC_POINT* orig_point = NULL;
  55002. ExpectNotNull(orig_point = EC_POINT_new(group));
  55003. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  55004. 1);
  55005. /* new_point should be set_point inverted so adding it will revert
  55006. * the point back to set_point */
  55007. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  55008. NULL), 1);
  55009. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  55010. EC_POINT_free(orig_point);
  55011. }
  55012. #endif
  55013. /* Test getting affine converts from projective. */
  55014. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  55015. /* Force non-affine coordinates */
  55016. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  55017. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  55018. if (new_point != NULL) {
  55019. new_point->inSet = 0;
  55020. }
  55021. /* extract the coordinates from point */
  55022. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  55023. ctx), WOLFSSL_SUCCESS);
  55024. /* check if point ordinates have changed. */
  55025. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  55026. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  55027. /* Test check for infinity */
  55028. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  55029. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  55030. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  55031. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  55032. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  55033. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  55034. #else
  55035. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  55036. #endif
  55037. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  55038. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  55039. /* check bn2hex */
  55040. hexStr = BN_bn2hex(k);
  55041. ExpectStrEQ(hexStr, kTest);
  55042. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  55043. defined(XFPRINTF)
  55044. BN_print_fp(stderr, k);
  55045. fprintf(stderr, "\n");
  55046. #endif
  55047. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55048. hexStr = BN_bn2hex(Gx);
  55049. ExpectStrEQ(hexStr, kGx);
  55050. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  55051. defined(XFPRINTF)
  55052. BN_print_fp(stderr, Gx);
  55053. fprintf(stderr, "\n");
  55054. #endif
  55055. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55056. hexStr = BN_bn2hex(Gy);
  55057. ExpectStrEQ(hexStr, kGy);
  55058. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  55059. defined(XFPRINTF)
  55060. BN_print_fp(stderr, Gy);
  55061. fprintf(stderr, "\n");
  55062. #endif
  55063. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55064. /* Test point to hex */
  55065. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55066. ctx));
  55067. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55068. ctx));
  55069. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55070. ctx));
  55071. #ifndef HAVE_ECC_BRAINPOOL
  55072. /* Group not supported in wolfCrypt. */
  55073. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55074. ctx));
  55075. #endif
  55076. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  55077. ExpectNotNull(hexStr);
  55078. ExpectStrEQ(hexStr, uncompG);
  55079. AssertNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
  55080. AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  55081. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55082. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  55083. ExpectNotNull(hexStr);
  55084. ExpectStrEQ(hexStr, compG);
  55085. #ifdef HAVE_COMP_KEY
  55086. AssertNotNull(get_point = EC_POINT_hex2point
  55087. (group, hexStr, get_point, ctx));
  55088. AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  55089. #endif
  55090. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  55091. EC_POINT_free(get_point);
  55092. #ifndef HAVE_SELFTEST
  55093. /* Test point to oct */
  55094. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55095. NULL, 0, ctx), 0);
  55096. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55097. NULL, 0, ctx), 0);
  55098. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  55099. NULL, 0, ctx), 0);
  55100. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55101. NULL, 0, ctx);
  55102. ExpectIntEQ(bin_len, sizeof(binUncompG));
  55103. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  55104. DYNAMIC_TYPE_ECC));
  55105. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  55106. buf, bin_len, ctx), bin_len);
  55107. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  55108. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  55109. /* Infinity (x=0, y=0) encodes as '0x00'. */
  55110. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  55111. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  55112. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  55113. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  55114. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  55115. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  55116. ExpectIntEQ(bufInf[0], 0);
  55117. wolfSSL_EC_POINT_dump(NULL, NULL);
  55118. /* Test point i2d */
  55119. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  55120. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  55121. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  55122. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  55123. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  55124. ExpectIntEQ(blen, sizeof(binUncompG));
  55125. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  55126. blen -= 1;
  55127. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  55128. blen += 1;
  55129. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  55130. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  55131. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  55132. #ifdef HAVE_COMP_KEY
  55133. /* Test point to oct compressed */
  55134. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  55135. 0, ctx);
  55136. ExpectIntEQ(bin_len, sizeof(binCompG));
  55137. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  55138. DYNAMIC_TYPE_ECC));
  55139. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  55140. bin_len, ctx), bin_len);
  55141. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  55142. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  55143. #endif
  55144. /* Test point BN */
  55145. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  55146. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  55147. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  55148. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  55149. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  55150. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  55151. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  55152. /* Test oct to point */
  55153. ExpectNotNull(tmp = EC_POINT_new(group));
  55154. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  55155. ctx), 0);
  55156. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  55157. ctx), 0);
  55158. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  55159. ctx), 0);
  55160. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  55161. sizeof(binUncompGBad), ctx), 0);
  55162. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  55163. ctx), 1);
  55164. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55165. EC_POINT_free(tmp);
  55166. tmp = NULL;
  55167. /* Test setting BN ordinates. */
  55168. ExpectNotNull(tmp = EC_POINT_new(group));
  55169. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  55170. NULL, ctx), 0);
  55171. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  55172. NULL, ctx), 0);
  55173. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  55174. NULL, ctx), 0);
  55175. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  55176. NULL, ctx), 0);
  55177. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  55178. Gy, ctx), 0);
  55179. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  55180. ctx), 0);
  55181. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  55182. ctx), 0);
  55183. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  55184. Gy, ctx), 0);
  55185. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  55186. NULL, ctx), 0);
  55187. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  55188. ctx), 1);
  55189. EC_POINT_free(tmp);
  55190. tmp = NULL;
  55191. /* Test point d2i */
  55192. ExpectNotNull(tmp = EC_POINT_new(group));
  55193. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  55194. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  55195. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  55196. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  55197. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  55198. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  55199. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  55200. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  55201. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  55202. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55203. EC_POINT_free(tmp);
  55204. tmp = NULL;
  55205. #ifdef HAVE_COMP_KEY
  55206. /* Test oct compressed to point */
  55207. ExpectNotNull(tmp = EC_POINT_new(group));
  55208. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  55209. 1);
  55210. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55211. EC_POINT_free(tmp);
  55212. tmp = NULL;
  55213. /* Test point d2i - compressed */
  55214. ExpectNotNull(tmp = EC_POINT_new(group));
  55215. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  55216. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  55217. EC_POINT_free(tmp);
  55218. tmp = NULL;
  55219. #endif
  55220. #endif
  55221. /* test BN_mod_add */
  55222. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  55223. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  55224. 1);
  55225. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  55226. /* cleanup */
  55227. BN_free(X);
  55228. BN_free(Y);
  55229. BN_free(k);
  55230. BN_free(set_point_bn);
  55231. EC_POINT_free(infinity);
  55232. EC_POINT_free(new_point);
  55233. EC_POINT_free(set_point);
  55234. EC_POINT_clear_free(Gxy);
  55235. #ifndef HAVE_ECC_BRAINPOOL
  55236. EC_GROUP_free(group2);
  55237. #endif
  55238. EC_GROUP_free(group);
  55239. BN_CTX_free(ctx);
  55240. #endif
  55241. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  55242. return EXPECT_RESULT();
  55243. }
  55244. static int test_wolfSSL_SPAKE(void)
  55245. {
  55246. EXPECT_DECLS;
  55247. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  55248. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  55249. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55250. BIGNUM* x = NULL; /* kdc priv */
  55251. BIGNUM* y = NULL; /* client priv */
  55252. BIGNUM* w = NULL; /* shared value */
  55253. byte M_bytes[] = {
  55254. /* uncompressed */
  55255. 0x04,
  55256. /* x */
  55257. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  55258. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  55259. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  55260. /* y */
  55261. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  55262. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  55263. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  55264. };
  55265. EC_POINT* M = NULL; /* shared value */
  55266. byte N_bytes[] = {
  55267. /* uncompressed */
  55268. 0x04,
  55269. /* x */
  55270. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  55271. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  55272. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  55273. /* y */
  55274. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  55275. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  55276. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  55277. };
  55278. EC_POINT* N = NULL; /* shared value */
  55279. EC_POINT* T = NULL; /* kdc pub */
  55280. EC_POINT* tmp1 = NULL; /* kdc pub */
  55281. EC_POINT* tmp2 = NULL; /* kdc pub */
  55282. EC_POINT* S = NULL; /* client pub */
  55283. EC_POINT* client_secret = NULL;
  55284. EC_POINT* kdc_secret = NULL;
  55285. EC_GROUP* group = NULL;
  55286. BN_CTX* bn_ctx = NULL;
  55287. /* Values taken from a test run of Kerberos 5 */
  55288. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  55289. ExpectNotNull(bn_ctx = BN_CTX_new());
  55290. ExpectNotNull(M = EC_POINT_new(group));
  55291. ExpectNotNull(N = EC_POINT_new(group));
  55292. ExpectNotNull(T = EC_POINT_new(group));
  55293. ExpectNotNull(tmp1 = EC_POINT_new(group));
  55294. ExpectNotNull(tmp2 = EC_POINT_new(group));
  55295. ExpectNotNull(S = EC_POINT_new(group));
  55296. ExpectNotNull(client_secret = EC_POINT_new(group));
  55297. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  55298. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  55299. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  55300. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  55301. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  55302. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  55303. "506AB395478F0AAB647752CF117B36250"), 1);
  55304. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  55305. 1);
  55306. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  55307. 1);
  55308. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  55309. /* kdc */
  55310. /* T=x*P+w*M */
  55311. /* All in one function call */
  55312. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  55313. /* Spread into separate calls */
  55314. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  55315. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  55316. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  55317. 1);
  55318. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  55319. /* client */
  55320. /* S=y*P+w*N */
  55321. /* All in one function call */
  55322. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  55323. /* Spread into separate calls */
  55324. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  55325. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  55326. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  55327. 1);
  55328. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  55329. /* K=y*(T-w*M) */
  55330. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  55331. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  55332. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  55333. 1);
  55334. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  55335. bn_ctx), 1);
  55336. /* kdc */
  55337. /* K=x*(S-w*N) */
  55338. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  55339. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  55340. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  55341. 1);
  55342. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  55343. 1);
  55344. /* kdc_secret == client_secret */
  55345. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  55346. BN_free(x);
  55347. BN_free(y);
  55348. BN_free(w);
  55349. EC_POINT_free(M);
  55350. EC_POINT_free(N);
  55351. EC_POINT_free(T);
  55352. EC_POINT_free(tmp1);
  55353. EC_POINT_free(tmp2);
  55354. EC_POINT_free(S);
  55355. EC_POINT_free(client_secret);
  55356. EC_POINT_free(kdc_secret);
  55357. EC_GROUP_free(group);
  55358. BN_CTX_free(bn_ctx);
  55359. #endif
  55360. return EXPECT_RESULT();
  55361. }
  55362. static int test_wolfSSL_EC_KEY_generate(void)
  55363. {
  55364. EXPECT_DECLS;
  55365. #ifdef OPENSSL_EXTRA
  55366. WOLFSSL_EC_KEY* key = NULL;
  55367. #ifndef HAVE_ECC_BRAINPOOL
  55368. WOLFSSL_EC_GROUP* group = NULL;
  55369. #endif
  55370. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55371. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  55372. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  55373. wolfSSL_EC_KEY_free(key);
  55374. key = NULL;
  55375. #ifndef HAVE_ECC_BRAINPOOL
  55376. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  55377. NID_brainpoolP256r1));
  55378. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  55379. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  55380. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  55381. wolfSSL_EC_KEY_free(key);
  55382. wolfSSL_EC_GROUP_free(group);
  55383. #endif
  55384. #endif
  55385. return EXPECT_RESULT();
  55386. }
  55387. static int test_EC_i2d(void)
  55388. {
  55389. EXPECT_DECLS;
  55390. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  55391. EC_KEY *key = NULL;
  55392. EC_KEY *copy = NULL;
  55393. int len = 0;
  55394. unsigned char *buf = NULL;
  55395. unsigned char *p = NULL;
  55396. const unsigned char *tmp = NULL;
  55397. const unsigned char octBad[] = {
  55398. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  55399. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  55400. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  55401. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  55402. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  55403. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  55404. };
  55405. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55406. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  55407. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  55408. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  55409. DYNAMIC_TYPE_TMP_BUFFER));
  55410. p = buf;
  55411. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  55412. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  55413. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  55414. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  55415. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  55416. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  55417. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  55418. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  55419. tmp = buf;
  55420. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  55421. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  55422. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  55423. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  55424. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  55425. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  55426. tmp = buf;
  55427. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  55428. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  55429. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  55430. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  55431. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  55432. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  55433. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  55434. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  55435. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  55436. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  55437. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  55438. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  55439. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  55440. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55441. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  55442. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55443. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  55444. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55445. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  55446. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55447. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  55448. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  55449. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  55450. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  55451. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  55452. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  55453. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  55454. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55455. buf = NULL;
  55456. buf = NULL;
  55457. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  55458. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  55459. DYNAMIC_TYPE_TMP_BUFFER));
  55460. p = buf;
  55461. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  55462. p = NULL;
  55463. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  55464. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55465. p = NULL;
  55466. /* Bad point is also an invalid private key. */
  55467. tmp = octBad;
  55468. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  55469. tmp = buf;
  55470. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  55471. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55472. buf = NULL;
  55473. buf = NULL;
  55474. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  55475. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  55476. DYNAMIC_TYPE_TMP_BUFFER));
  55477. p = buf;
  55478. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  55479. p = NULL;
  55480. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  55481. tmp = buf;
  55482. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  55483. tmp = octBad;
  55484. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  55485. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  55486. ExpectIntEQ(EC_KEY_check_key(key), 1);
  55487. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  55488. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  55489. EC_KEY_free(key);
  55490. EC_KEY_free(copy);
  55491. #endif
  55492. return EXPECT_RESULT();
  55493. }
  55494. static int test_wolfSSL_EC_curve(void)
  55495. {
  55496. EXPECT_DECLS;
  55497. #if defined(OPENSSL_EXTRA)
  55498. int nid = NID_secp160k1;
  55499. const char* nid_name = NULL;
  55500. ExpectNull(EC_curve_nid2nist(NID_sha256));
  55501. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  55502. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  55503. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  55504. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  55505. #endif
  55506. return EXPECT_RESULT();
  55507. }
  55508. static int test_wolfSSL_EC_KEY_dup(void)
  55509. {
  55510. EXPECT_DECLS;
  55511. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  55512. WOLFSSL_EC_KEY* ecKey = NULL;
  55513. WOLFSSL_EC_KEY* dupKey = NULL;
  55514. ecc_key* srcKey = NULL;
  55515. ecc_key* destKey = NULL;
  55516. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55517. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55518. /* Valid cases */
  55519. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55520. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  55521. /* Compare pubkey */
  55522. if (ecKey != NULL) {
  55523. srcKey = (ecc_key*)ecKey->internal;
  55524. }
  55525. if (dupKey != NULL) {
  55526. destKey = (ecc_key*)dupKey->internal;
  55527. }
  55528. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  55529. /* compare EC_GROUP */
  55530. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  55531. /* compare EC_POINT */
  55532. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  55533. dupKey->pub_key, NULL), MP_EQ);
  55534. /* compare BIGNUM */
  55535. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  55536. wolfSSL_EC_KEY_free(dupKey);
  55537. dupKey = NULL;
  55538. /* Invalid cases */
  55539. /* NULL key */
  55540. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  55541. /* NULL ecc_key */
  55542. if (ecKey != NULL) {
  55543. wc_ecc_free((ecc_key*)ecKey->internal);
  55544. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  55545. ecKey->internal = NULL; /* Set ecc_key to NULL */
  55546. }
  55547. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55548. wolfSSL_EC_KEY_free(ecKey);
  55549. ecKey = NULL;
  55550. wolfSSL_EC_KEY_free(dupKey);
  55551. dupKey = NULL;
  55552. /* NULL Group */
  55553. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55554. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55555. if (ecKey != NULL) {
  55556. wolfSSL_EC_GROUP_free(ecKey->group);
  55557. ecKey->group = NULL; /* Set group to NULL */
  55558. }
  55559. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55560. wolfSSL_EC_KEY_free(ecKey);
  55561. ecKey = NULL;
  55562. wolfSSL_EC_KEY_free(dupKey);
  55563. dupKey = NULL;
  55564. /* NULL public key */
  55565. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55566. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55567. if (ecKey != NULL) {
  55568. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  55569. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  55570. }
  55571. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55572. if (ecKey != NULL) {
  55573. wolfSSL_EC_POINT_free(ecKey->pub_key);
  55574. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  55575. }
  55576. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55577. wolfSSL_EC_KEY_free(ecKey);
  55578. ecKey = NULL;
  55579. wolfSSL_EC_KEY_free(dupKey);
  55580. dupKey = NULL;
  55581. /* NULL private key */
  55582. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55583. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55584. if (ecKey != NULL) {
  55585. wolfSSL_BN_free(ecKey->priv_key);
  55586. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  55587. }
  55588. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55589. wolfSSL_EC_KEY_free(ecKey);
  55590. ecKey = NULL;
  55591. wolfSSL_EC_KEY_free(dupKey);
  55592. dupKey = NULL;
  55593. /* Test EC_KEY_up_ref */
  55594. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55595. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  55596. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  55597. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  55598. /* reference count doesn't follow duplicate */
  55599. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55600. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  55601. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  55602. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  55603. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  55604. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  55605. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  55606. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  55607. #endif
  55608. return EXPECT_RESULT();
  55609. }
  55610. static int test_wolfSSL_EC_KEY_set_group(void)
  55611. {
  55612. EXPECT_DECLS;
  55613. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  55614. defined(OPENSSL_EXTRA)
  55615. EC_KEY *key = NULL;
  55616. EC_GROUP *group = NULL;
  55617. const EC_GROUP *group2 = NULL;
  55618. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  55619. ExpectNotNull(key = EC_KEY_new());
  55620. ExpectNull(EC_KEY_get0_group(NULL));
  55621. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  55622. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  55623. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  55624. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  55625. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  55626. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  55627. EC_GROUP_free(group);
  55628. EC_KEY_free(key);
  55629. #endif
  55630. return EXPECT_RESULT();
  55631. }
  55632. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  55633. {
  55634. EXPECT_DECLS;
  55635. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55636. BIO* bio = NULL;
  55637. EC_KEY* key = NULL;
  55638. /* Error condition: NULL key. */
  55639. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  55640. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  55641. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  55642. /* Conversion form defaults to uncompressed. */
  55643. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55644. #ifdef HAVE_COMP_KEY
  55645. /* Explicitly set to compressed. */
  55646. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  55647. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  55648. #else
  55649. /* Will still work just won't change anything. */
  55650. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  55651. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55652. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  55653. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55654. #endif
  55655. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  55656. BIO_free(bio);
  55657. EC_KEY_free(key);
  55658. #endif
  55659. return EXPECT_RESULT();
  55660. }
  55661. static int test_wolfSSL_EC_KEY_private_key(void)
  55662. {
  55663. EXPECT_DECLS;
  55664. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55665. WOLFSSL_EC_KEY* key = NULL;
  55666. WOLFSSL_BIGNUM* priv = NULL;
  55667. WOLFSSL_BIGNUM* priv2 = NULL;
  55668. WOLFSSL_BIGNUM* bn;
  55669. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55670. ExpectNotNull(priv = wolfSSL_BN_new());
  55671. ExpectNotNull(priv2 = wolfSSL_BN_new());
  55672. ExpectIntNE(BN_set_word(priv, 2), 0);
  55673. ExpectIntNE(BN_set_word(priv2, 2), 0);
  55674. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  55675. /* No private key set. */
  55676. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  55677. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  55678. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  55679. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  55680. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  55681. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  55682. ExpectPtrNE(bn, priv);
  55683. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  55684. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  55685. ExpectPtrNE(bn, priv2);
  55686. wolfSSL_BN_free(priv2);
  55687. wolfSSL_BN_free(priv);
  55688. wolfSSL_EC_KEY_free(key);
  55689. #endif
  55690. return EXPECT_RESULT();
  55691. }
  55692. static int test_wolfSSL_EC_KEY_public_key(void)
  55693. {
  55694. EXPECT_DECLS;
  55695. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55696. WOLFSSL_EC_KEY* key = NULL;
  55697. WOLFSSL_EC_POINT* pub = NULL;
  55698. WOLFSSL_EC_POINT* point = NULL;
  55699. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55700. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  55701. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  55702. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  55703. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  55704. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  55705. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  55706. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  55707. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  55708. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  55709. ExpectPtrEq(point, pub);
  55710. wolfSSL_EC_KEY_free(key);
  55711. #endif
  55712. return EXPECT_RESULT();
  55713. }
  55714. static int test_wolfSSL_EC_KEY_print_fp(void)
  55715. {
  55716. EXPECT_DECLS;
  55717. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  55718. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  55719. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  55720. !defined(NO_STDIO_FILESYSTEM)
  55721. EC_KEY* key = NULL;
  55722. /* Bad file pointer. */
  55723. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  55724. /* NULL key. */
  55725. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  55726. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  55727. /* Negative indent. */
  55728. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  55729. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55730. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  55731. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55732. wolfSSL_EC_KEY_free(key);
  55733. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  55734. NID_X9_62_prime256v1)));
  55735. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  55736. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55737. wolfSSL_EC_KEY_free(key);
  55738. #endif
  55739. return EXPECT_RESULT();
  55740. }
  55741. static int test_wolfSSL_EC_get_builtin_curves(void)
  55742. {
  55743. EXPECT_DECLS;
  55744. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55745. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55746. EC_builtin_curve* curves = NULL;
  55747. size_t crv_len = 0;
  55748. size_t i = 0;
  55749. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  55750. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  55751. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  55752. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  55753. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  55754. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  55755. if (curves[i].comment != NULL) {
  55756. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  55757. }
  55758. }
  55759. if (crv_len > 1) {
  55760. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  55761. }
  55762. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55763. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55764. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  55765. return EXPECT_RESULT();
  55766. }
  55767. static int test_wolfSSL_ECDSA_SIG(void)
  55768. {
  55769. EXPECT_DECLS;
  55770. #ifdef OPENSSL_EXTRA
  55771. WOLFSSL_ECDSA_SIG* sig = NULL;
  55772. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  55773. WOLFSSL_BIGNUM* r = NULL;
  55774. WOLFSSL_BIGNUM* s = NULL;
  55775. const WOLFSSL_BIGNUM* r2 = NULL;
  55776. const WOLFSSL_BIGNUM* s2 = NULL;
  55777. const unsigned char* cp = NULL;
  55778. unsigned char* p = NULL;
  55779. unsigned char outSig[8];
  55780. unsigned char sigData[8] =
  55781. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  55782. unsigned char sigDataBad[8] =
  55783. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  55784. wolfSSL_ECDSA_SIG_free(NULL);
  55785. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  55786. ExpectNotNull(r = wolfSSL_BN_new());
  55787. ExpectNotNull(s = wolfSSL_BN_new());
  55788. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  55789. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  55790. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  55791. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  55792. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  55793. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  55794. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  55795. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  55796. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  55797. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  55798. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  55799. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  55800. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  55801. r2 = NULL;
  55802. s2 = NULL;
  55803. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  55804. ExpectNull(r2);
  55805. ExpectNull(s2);
  55806. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  55807. if (EXPECT_FAIL()) {
  55808. wolfSSL_BN_free(r);
  55809. wolfSSL_BN_free(s);
  55810. }
  55811. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  55812. ExpectPtrEq(r2, r);
  55813. ExpectPtrEq(s2, s);
  55814. r2 = NULL;
  55815. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  55816. ExpectPtrEq(r2, r);
  55817. s2 = NULL;
  55818. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  55819. ExpectPtrEq(s2, s);
  55820. /* r and s are freed when sig is freed. */
  55821. wolfSSL_ECDSA_SIG_free(sig);
  55822. sig = NULL;
  55823. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  55824. cp = sigDataBad;
  55825. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  55826. cp = sigData;
  55827. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  55828. ExpectIntEQ((cp == sigData + 8), 1);
  55829. cp = sigData;
  55830. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  55831. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  55832. ExpectIntEQ((sig == sig2), 1);
  55833. cp = outSig;
  55834. p = outSig;
  55835. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  55836. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  55837. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  55838. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  55839. ExpectIntEQ((p == outSig + 8), 1);
  55840. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  55841. p = NULL;
  55842. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
  55843. #ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
  55844. ExpectNull(p);
  55845. #else
  55846. ExpectNotNull(p);
  55847. ExpectIntEQ(XMEMCMP(p, outSig, 8), 0);
  55848. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  55849. #endif
  55850. wolfSSL_ECDSA_SIG_free(sig);
  55851. #endif
  55852. return EXPECT_RESULT();
  55853. }
  55854. static int test_ECDSA_size_sign(void)
  55855. {
  55856. EXPECT_DECLS;
  55857. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  55858. EC_KEY* key = NULL;
  55859. ECDSA_SIG* ecdsaSig = NULL;
  55860. int id;
  55861. byte hash[WC_MAX_DIGEST_SIZE];
  55862. byte hash2[WC_MAX_DIGEST_SIZE];
  55863. byte sig[ECC_MAX_SIG_SIZE];
  55864. unsigned int sigSz = sizeof(sig);
  55865. XMEMSET(hash, 123, sizeof(hash));
  55866. XMEMSET(hash2, 234, sizeof(hash2));
  55867. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  55868. ExpectIntEQ(id, ECC_SECP256R1);
  55869. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55870. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  55871. ExpectIntGE(ECDSA_size(NULL), 0);
  55872. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  55873. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  55874. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  55875. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, NULL), 0);
  55876. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, (int)sigSz, key), 0);
  55877. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, (int)sigSz, key), 0);
  55878. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  55879. ExpectIntGE(ECDSA_size(key), sigSz);
  55880. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, key), 1);
  55881. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, (int)sigSz, key), 0);
  55882. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  55883. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  55884. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  55885. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  55886. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  55887. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  55888. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  55889. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  55890. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  55891. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  55892. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  55893. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  55894. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  55895. ECDSA_SIG_free(ecdsaSig);
  55896. EC_KEY_free(key);
  55897. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  55898. return EXPECT_RESULT();
  55899. }
  55900. static int test_ECDH_compute_key(void)
  55901. {
  55902. EXPECT_DECLS;
  55903. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  55904. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55905. EC_KEY* key1 = NULL;
  55906. EC_KEY* key2 = NULL;
  55907. EC_POINT* pub1 = NULL;
  55908. EC_POINT* pub2 = NULL;
  55909. byte secret1[32];
  55910. byte secret2[32];
  55911. int i;
  55912. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55913. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  55914. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  55915. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55916. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  55917. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  55918. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  55919. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  55920. 0);
  55921. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  55922. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  55923. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  55924. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  55925. 0);
  55926. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  55927. 0);
  55928. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  55929. NULL), 0);
  55930. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  55931. sizeof(secret1));
  55932. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  55933. sizeof(secret2));
  55934. for (i = 0; i < (int)sizeof(secret1); i++) {
  55935. ExpectIntEQ(secret1[i], secret2[i]);
  55936. }
  55937. EC_KEY_free(key2);
  55938. EC_KEY_free(key1);
  55939. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  55940. * !WOLF_CRYPTO_CB_ONLY_ECC */
  55941. return EXPECT_RESULT();
  55942. }
  55943. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  55944. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  55945. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  55946. !defined(NO_ASN_TIME)
  55947. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  55948. int expectedDerSz)
  55949. {
  55950. EXPECT_DECLS;
  55951. X509* x509 = NULL;
  55952. BIGNUM* serial_number = NULL;
  55953. X509_NAME* name = NULL;
  55954. time_t epoch_off = 0;
  55955. ASN1_INTEGER* asn1_serial_number;
  55956. long not_before, not_after;
  55957. int derSz;
  55958. ExpectNotNull(x509 = X509_new());
  55959. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  55960. ExpectNotNull(serial_number = BN_new());
  55961. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  55962. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  55963. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  55964. /* version 3 */
  55965. ExpectIntNE(X509_set_version(x509, 2L), 0);
  55966. ExpectNotNull(name = X509_NAME_new());
  55967. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  55968. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  55969. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  55970. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  55971. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  55972. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  55973. not_before = (long)wc_Time(NULL);
  55974. not_after = not_before + (365 * 24 * 60 * 60);
  55975. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  55976. &epoch_off));
  55977. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  55978. &epoch_off));
  55979. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  55980. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  55981. ExpectIntGE(derSz, expectedDerSz);
  55982. BN_free(serial_number);
  55983. X509_NAME_free(name);
  55984. X509_free(x509);
  55985. return EXPECT_RESULT();
  55986. }
  55987. #endif
  55988. static int test_openssl_generate_key_and_cert(void)
  55989. {
  55990. EXPECT_DECLS;
  55991. #if defined(OPENSSL_EXTRA)
  55992. int expectedDerSz;
  55993. EVP_PKEY* pkey = NULL;
  55994. #ifdef HAVE_ECC
  55995. EC_KEY* ec_key = NULL;
  55996. #endif
  55997. #if !defined(NO_RSA)
  55998. int key_length = 2048;
  55999. BIGNUM* exponent = NULL;
  56000. RSA* rsa = NULL;
  56001. ExpectNotNull(pkey = EVP_PKEY_new());
  56002. ExpectNotNull(exponent = BN_new());
  56003. ExpectNotNull(rsa = RSA_new());
  56004. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  56005. #ifndef WOLFSSL_KEY_GEN
  56006. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  56007. #if defined(USE_CERT_BUFFERS_1024)
  56008. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  56009. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  56010. key_length = 1024;
  56011. #elif defined(USE_CERT_BUFFERS_2048)
  56012. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  56013. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  56014. #else
  56015. RSA_free(rsa);
  56016. rsa = NULL;
  56017. #endif
  56018. #else
  56019. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  56020. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  56021. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  56022. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  56023. #endif
  56024. if (rsa) {
  56025. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  56026. if (EXPECT_FAIL()) {
  56027. RSA_free(rsa);
  56028. }
  56029. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  56030. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  56031. expectedDerSz = 743;
  56032. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  56033. expectedDerSz), TEST_SUCCESS);
  56034. #endif
  56035. }
  56036. EVP_PKEY_free(pkey);
  56037. pkey = NULL;
  56038. BN_free(exponent);
  56039. #endif /* !NO_RSA */
  56040. #ifdef HAVE_ECC
  56041. ExpectNotNull(pkey = EVP_PKEY_new());
  56042. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  56043. #ifndef NO_WOLFSSL_STUB
  56044. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  56045. #endif
  56046. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  56047. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  56048. if (EXPECT_FAIL()) {
  56049. EC_KEY_free(ec_key);
  56050. }
  56051. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  56052. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  56053. expectedDerSz = 344;
  56054. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  56055. TEST_SUCCESS);
  56056. #endif
  56057. EVP_PKEY_free(pkey);
  56058. #endif /* HAVE_ECC */
  56059. (void)pkey;
  56060. (void)expectedDerSz;
  56061. #endif /* OPENSSL_EXTRA */
  56062. return EXPECT_RESULT();
  56063. }
  56064. static int test_stubs_are_stubs(void)
  56065. {
  56066. EXPECT_DECLS;
  56067. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  56068. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  56069. WOLFSSL_CTX* ctx = NULL;
  56070. WOLFSSL_CTX* ctxN = NULL;
  56071. #ifndef NO_WOLFSSL_CLIENT
  56072. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56073. #elif !defined(NO_WOLFSSL_SERVER)
  56074. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56075. #endif
  56076. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  56077. ExpectIntEQ((int) x(z), 0)
  56078. /* test logic, all stubs return same result regardless of ctx being NULL
  56079. * as there are no sanity checks, it's just a stub! If at some
  56080. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  56081. * if invalid inputs are supplied. Test calling both
  56082. * with and without valid inputs, if a stub functionality remains unchanged.
  56083. */
  56084. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  56085. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  56086. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  56087. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  56088. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  56089. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  56090. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  56091. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  56092. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  56093. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  56094. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  56095. wolfSSL_CTX_free(ctx);
  56096. ctx = NULL;
  56097. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  56098. * !NO_WOLFSSL_SERVER) */
  56099. return EXPECT_RESULT();
  56100. }
  56101. static int test_CONF_modules_xxx(void)
  56102. {
  56103. int res = TEST_SKIPPED;
  56104. #if defined(OPENSSL_EXTRA)
  56105. CONF_modules_free();
  56106. CONF_modules_unload(0);
  56107. CONF_modules_unload(1);
  56108. CONF_modules_unload(-1);
  56109. res = TEST_SUCCESS;
  56110. #endif /* OPENSSL_EXTRA */
  56111. return res;
  56112. }
  56113. static int test_CRYPTO_set_dynlock_xxx(void)
  56114. {
  56115. int res = TEST_SKIPPED;
  56116. #if defined(OPENSSL_EXTRA)
  56117. CRYPTO_set_dynlock_create_callback(
  56118. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  56119. CRYPTO_set_dynlock_create_callback(
  56120. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  56121. CRYPTO_set_dynlock_destroy_callback(
  56122. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  56123. CRYPTO_set_dynlock_destroy_callback(
  56124. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  56125. CRYPTO_set_dynlock_lock_callback(
  56126. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  56127. CRYPTO_set_dynlock_lock_callback(
  56128. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  56129. res = TEST_SUCCESS;
  56130. #endif /* OPENSSL_EXTRA */
  56131. return res;
  56132. }
  56133. static int test_CRYPTO_THREADID_xxx(void)
  56134. {
  56135. EXPECT_DECLS;
  56136. #if defined(OPENSSL_EXTRA)
  56137. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  56138. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  56139. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  56140. #endif /* OPENSSL_EXTRA */
  56141. return EXPECT_RESULT();
  56142. }
  56143. static int test_ENGINE_cleanup(void)
  56144. {
  56145. int res = TEST_SKIPPED;
  56146. #if defined(OPENSSL_EXTRA)
  56147. ENGINE_cleanup();
  56148. res = TEST_SUCCESS;
  56149. #endif /* OPENSSL_EXTRA */
  56150. return res;
  56151. }
  56152. static int test_wolfSSL_CTX_LoadCRL(void)
  56153. {
  56154. EXPECT_DECLS;
  56155. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  56156. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  56157. WOLFSSL_CTX* ctx = NULL;
  56158. WOLFSSL* ssl = NULL;
  56159. const char* badPath = "dummypath";
  56160. const char* validPath = "./certs/crl";
  56161. const char* validFilePath = "./certs/crl/cliCrl.pem";
  56162. const char* issuerCert = "./certs/client-cert.pem";
  56163. int derType = WOLFSSL_FILETYPE_ASN1;
  56164. int pemType = WOLFSSL_FILETYPE_PEM;
  56165. #ifdef HAVE_CRL_MONITOR
  56166. int monitor = WOLFSSL_CRL_MONITOR;
  56167. #else
  56168. int monitor = 0;
  56169. #endif
  56170. WOLFSSL_CERT_MANAGER* cm = NULL;
  56171. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  56172. BAD_FUNC_ARG)
  56173. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  56174. NOT_COMPILED_IN)
  56175. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  56176. WOLFSSL_SUCCESS)
  56177. #ifndef NO_WOLFSSL_CLIENT
  56178. #define NEW_CTX(ctx) ExpectNotNull( \
  56179. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  56180. #elif !defined(NO_WOLFSSL_SERVER)
  56181. #define NEW_CTX(ctx) ExpectNotNull( \
  56182. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  56183. #else
  56184. #define NEW_CTX(ctx) return
  56185. #endif
  56186. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  56187. NEW_CTX(ctx);
  56188. #ifndef HAVE_CRL_MONITOR
  56189. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  56190. wolfSSL_CTX_free(ctx);
  56191. NEW_CTX(ctx);
  56192. #endif
  56193. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  56194. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  56195. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  56196. wolfSSL_CTX_free(ctx);
  56197. ctx = NULL;
  56198. NEW_CTX(ctx);
  56199. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  56200. WOLFSSL_SUCCESS);
  56201. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  56202. wolfSSL_CTX_free(ctx);
  56203. ctx = NULL;
  56204. NEW_CTX(ctx);
  56205. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  56206. WOLFSSL_SUCCESS);
  56207. ExpectNotNull(ssl = wolfSSL_new(ctx));
  56208. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  56209. wolfSSL_free(ssl);
  56210. ssl = NULL;
  56211. wolfSSL_CTX_free(ctx);
  56212. ctx = NULL;
  56213. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56214. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  56215. WOLFSSL_SUCCESS);
  56216. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  56217. WOLFSSL_SUCCESS);
  56218. wolfSSL_CertManagerFree(cm);
  56219. #endif
  56220. return EXPECT_RESULT();
  56221. }
  56222. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  56223. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  56224. {
  56225. EXPECT_DECLS;
  56226. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56227. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  56228. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56229. return EXPECT_RESULT();
  56230. }
  56231. #endif
  56232. static int test_multiple_crls_same_issuer(void)
  56233. {
  56234. EXPECT_DECLS;
  56235. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  56236. test_ssl_cbf client_cbs, server_cbs;
  56237. struct {
  56238. const char* server_cert;
  56239. const char* server_key;
  56240. } test_params[] = {
  56241. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  56242. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  56243. };
  56244. size_t i;
  56245. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  56246. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56247. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56248. server_cbs.certPemFile = test_params[i].server_cert;
  56249. server_cbs.keyPemFile = test_params[i].server_key;
  56250. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  56251. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  56252. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56253. &server_cbs, NULL), TEST_FAIL);
  56254. }
  56255. #endif
  56256. return EXPECT_RESULT();
  56257. }
  56258. static int test_SetTmpEC_DHE_Sz(void)
  56259. {
  56260. EXPECT_DECLS;
  56261. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  56262. WOLFSSL_CTX *ctx = NULL;
  56263. WOLFSSL *ssl = NULL;
  56264. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56265. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  56266. ExpectNotNull(ssl = wolfSSL_new(ctx));
  56267. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  56268. wolfSSL_free(ssl);
  56269. wolfSSL_CTX_free(ctx);
  56270. #endif
  56271. return EXPECT_RESULT();
  56272. }
  56273. static int test_wolfSSL_CTX_get0_privatekey(void)
  56274. {
  56275. EXPECT_DECLS;
  56276. #ifdef OPENSSL_ALL
  56277. WOLFSSL_CTX* ctx = NULL;
  56278. (void)ctx;
  56279. #ifndef NO_RSA
  56280. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56281. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56282. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  56283. WOLFSSL_FILETYPE_PEM));
  56284. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56285. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  56286. WOLFSSL_FILETYPE_PEM));
  56287. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  56288. wolfSSL_CTX_free(ctx);
  56289. ctx = NULL;
  56290. #endif
  56291. #ifdef HAVE_ECC
  56292. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56293. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56294. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  56295. WOLFSSL_FILETYPE_PEM));
  56296. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  56297. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  56298. WOLFSSL_FILETYPE_PEM));
  56299. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  56300. wolfSSL_CTX_free(ctx);
  56301. #endif
  56302. #endif
  56303. return EXPECT_RESULT();
  56304. }
  56305. static int test_wolfSSL_dtls_set_mtu(void)
  56306. {
  56307. EXPECT_DECLS;
  56308. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  56309. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  56310. !defined(WOLFSSL_NO_TLS12)
  56311. WOLFSSL_CTX* ctx = NULL;
  56312. WOLFSSL* ssl = NULL;
  56313. const char* testCertFile;
  56314. const char* testKeyFile;
  56315. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  56316. #ifndef NO_RSA
  56317. testCertFile = svrCertFile;
  56318. testKeyFile = svrKeyFile;
  56319. #elif defined(HAVE_ECC)
  56320. testCertFile = eccCertFile;
  56321. testKeyFile = eccKeyFile;
  56322. #endif
  56323. if (testCertFile != NULL && testKeyFile != NULL) {
  56324. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  56325. WOLFSSL_FILETYPE_PEM));
  56326. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56327. WOLFSSL_FILETYPE_PEM));
  56328. }
  56329. ExpectNotNull(ssl = wolfSSL_new(ctx));
  56330. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  56331. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  56332. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  56333. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  56334. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  56335. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  56336. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  56337. wolfSSL_free(ssl);
  56338. wolfSSL_CTX_free(ctx);
  56339. #endif
  56340. return EXPECT_RESULT();
  56341. }
  56342. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56343. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56344. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  56345. enum HandShakeType hsType, word16 length)
  56346. {
  56347. size_t idx = 0;
  56348. byte* l;
  56349. /* record layer */
  56350. /* handshake type */
  56351. out[idx++] = handshake;
  56352. /* protocol version */
  56353. out[idx++] = 0xfe;
  56354. out[idx++] = 0xfd; /* DTLS 1.2 */
  56355. /* epoch 0 */
  56356. XMEMSET(out + idx, 0, 2);
  56357. idx += 2;
  56358. /* sequence number */
  56359. XMEMSET(out + idx, 0, 6);
  56360. c32toa(seq, out + idx + 2);
  56361. idx += 6;
  56362. /* length in BE */
  56363. if (length)
  56364. c16toa(length, out + idx);
  56365. else
  56366. c16toa(outSz - idx - 2, out + idx);
  56367. idx += 2;
  56368. /* handshake layer */
  56369. /* handshake type */
  56370. out[idx++] = (byte)hsType;
  56371. /* length */
  56372. l = out + idx;
  56373. idx += 3;
  56374. /* message seq */
  56375. c16toa(0, out + idx);
  56376. idx += 2;
  56377. /* frag offset */
  56378. c32to24(0, out + idx);
  56379. idx += 3;
  56380. /* frag length */
  56381. c32to24((word32)outSz - (word32)idx - 3, l);
  56382. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  56383. idx += 3;
  56384. XMEMSET(out + idx, 0, outSz - idx);
  56385. }
  56386. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  56387. {
  56388. byte msg[] = "This is a msg for the client";
  56389. byte reply[40];
  56390. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  56391. reply[sizeof(reply) - 1] = '\0';
  56392. fprintf(stderr, "Client message: %s\n", reply);
  56393. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  56394. }
  56395. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  56396. {
  56397. byte ch[50];
  56398. int fd = wolfSSL_get_fd(ssl);
  56399. byte msg[] = "This is a msg for the server";
  56400. byte reply[40];
  56401. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  56402. /* Server should ignore this datagram */
  56403. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  56404. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  56405. /* Server should ignore this datagram */
  56406. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  56407. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  56408. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  56409. reply[sizeof(reply) - 1] = '\0';
  56410. fprintf(stderr, "Server response: %s\n", reply);
  56411. }
  56412. static int test_wolfSSL_dtls_plaintext(void)
  56413. {
  56414. callback_functions func_cb_client;
  56415. callback_functions func_cb_server;
  56416. size_t i;
  56417. struct test_params {
  56418. method_provider client_meth;
  56419. method_provider server_meth;
  56420. ssl_callback on_result_server;
  56421. ssl_callback on_result_client;
  56422. } params[] = {
  56423. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56424. test_wolfSSL_dtls_plaintext_server,
  56425. test_wolfSSL_dtls_plaintext_client},
  56426. };
  56427. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56428. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56429. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56430. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56431. func_cb_server.method = params[i].server_meth;
  56432. func_cb_client.method = params[i].client_meth;
  56433. func_cb_client.on_result = params[i].on_result_client;
  56434. func_cb_server.on_result = params[i].on_result_server;
  56435. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56436. if (!func_cb_client.return_code)
  56437. return TEST_FAIL;
  56438. if (!func_cb_server.return_code)
  56439. return TEST_FAIL;
  56440. }
  56441. return TEST_RES_CHECK(1);
  56442. }
  56443. #else
  56444. static int test_wolfSSL_dtls_plaintext(void) {
  56445. return TEST_SKIPPED;
  56446. }
  56447. #endif
  56448. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56449. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  56450. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  56451. {
  56452. byte b[1100]; /* buffer for the messages to send */
  56453. size_t idx = 0;
  56454. size_t seq_offset = 0;
  56455. size_t msg_offset = 0;
  56456. int i;
  56457. int fd = wolfSSL_get_fd(ssl);
  56458. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  56459. word32 seq_number = 100; /* start high so server definitely reads this */
  56460. word16 msg_number = 50; /* start high so server has to buffer this */
  56461. AssertIntEQ(ret, 1);
  56462. /* Now let's start spamming the peer with fragments it needs to store */
  56463. XMEMSET(b, -1, sizeof(b));
  56464. /* record layer */
  56465. /* handshake type */
  56466. b[idx++] = 22;
  56467. /* protocol version */
  56468. b[idx++] = 0xfe;
  56469. b[idx++] = 0xfd; /* DTLS 1.2 */
  56470. /* epoch 0 */
  56471. XMEMSET(b + idx, 0, 2);
  56472. idx += 2;
  56473. /* sequence number */
  56474. XMEMSET(b + idx, 0, 6);
  56475. seq_offset = idx + 2; /* increment only the low 32 bits */
  56476. idx += 6;
  56477. /* static length in BE */
  56478. c16toa(42, b + idx);
  56479. idx += 2;
  56480. /* handshake layer */
  56481. /* cert type */
  56482. b[idx++] = 11;
  56483. /* length */
  56484. c32to24(1000, b + idx);
  56485. idx += 3;
  56486. /* message seq */
  56487. c16toa(0, b + idx);
  56488. msg_offset = idx;
  56489. idx += 2;
  56490. /* frag offset */
  56491. c32to24(500, b + idx);
  56492. idx += 3;
  56493. /* frag length */
  56494. c32to24(30, b + idx);
  56495. idx += 3;
  56496. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  56497. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  56498. seq_number++, msg_number++, i++) {
  56499. struct timespec delay;
  56500. XMEMSET(&delay, 0, sizeof(delay));
  56501. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  56502. c32toa(seq_number, b + seq_offset);
  56503. c16toa(msg_number, b + msg_offset);
  56504. ret = (int)send(fd, b, 55, 0);
  56505. nanosleep(&delay, NULL);
  56506. }
  56507. }
  56508. #ifdef WOLFSSL_DTLS13
  56509. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  56510. {
  56511. const word16 sendCountMax = 100;
  56512. byte b[150]; /* buffer for the messages to send */
  56513. size_t idx = 0;
  56514. size_t msg_offset = 0;
  56515. int fd = wolfSSL_get_fd(ssl);
  56516. word16 msg_number = 10; /* start high so server has to buffer this */
  56517. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  56518. AssertIntEQ(ret, 1);
  56519. /* Now let's start spamming the peer with fragments it needs to store */
  56520. XMEMSET(b, -1, sizeof(b));
  56521. /* handshake type */
  56522. b[idx++] = 11;
  56523. /* length */
  56524. c32to24(10000, b + idx);
  56525. idx += 3;
  56526. /* message_seq */
  56527. msg_offset = idx;
  56528. idx += 2;
  56529. /* fragment_offset */
  56530. c32to24(5000, b + idx);
  56531. idx += 3;
  56532. /* fragment_length */
  56533. c32to24(100, b + idx);
  56534. idx += 3;
  56535. /* fragment contents */
  56536. idx += 100;
  56537. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  56538. byte sendBuf[150];
  56539. int sendSz = sizeof(sendBuf);
  56540. struct timespec delay;
  56541. XMEMSET(&delay, 0, sizeof(delay));
  56542. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  56543. c16toa(msg_number, b + msg_offset);
  56544. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  56545. (int)idx, handshake, 0, 0, 0);
  56546. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  56547. nanosleep(&delay, NULL);
  56548. }
  56549. }
  56550. #endif
  56551. static int test_wolfSSL_dtls_fragments(void)
  56552. {
  56553. EXPECT_DECLS;
  56554. callback_functions func_cb_client;
  56555. callback_functions func_cb_server;
  56556. size_t i;
  56557. struct test_params {
  56558. method_provider client_meth;
  56559. method_provider server_meth;
  56560. ssl_callback spammer;
  56561. } params[] = {
  56562. #if !defined(WOLFSSL_NO_TLS12)
  56563. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56564. test_wolfSSL_dtls12_fragments_spammer},
  56565. #endif
  56566. #ifdef WOLFSSL_DTLS13
  56567. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56568. test_wolfSSL_dtls13_fragments_spammer},
  56569. #endif
  56570. };
  56571. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56572. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56573. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56574. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56575. func_cb_server.method = params[i].server_meth;
  56576. func_cb_client.method = params[i].client_meth;
  56577. func_cb_client.ssl_ready = params[i].spammer;
  56578. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56579. ExpectFalse(func_cb_client.return_code);
  56580. ExpectFalse(func_cb_server.return_code);
  56581. /* The socket should be closed by the server resulting in a
  56582. * socket error, fatal error or reading a close notify alert */
  56583. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  56584. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  56585. func_cb_client.last_err != FATAL_ERROR) {
  56586. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  56587. }
  56588. /* Check the server returned an error indicating the msg buffer
  56589. * was full */
  56590. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  56591. if (EXPECT_FAIL())
  56592. break;
  56593. }
  56594. return EXPECT_RESULT();
  56595. }
  56596. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  56597. {
  56598. int fd, ret;
  56599. byte alert_msg[] = {
  56600. 0x15, /* alert type */
  56601. 0xfe, 0xfd, /* version */
  56602. 0x00, 0x00, /* epoch */
  56603. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  56604. 0x00, 0x02, /* length */
  56605. 0x02, /* level: fatal */
  56606. 0x46 /* protocol version */
  56607. };
  56608. fd = wolfSSL_get_fd(ssl);
  56609. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  56610. AssertIntGT(ret, 0);
  56611. }
  56612. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  56613. {
  56614. callback_functions client_cbs, server_cbs;
  56615. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56616. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56617. client_cbs.doUdp = server_cbs.doUdp = 1;
  56618. if (version12) {
  56619. #if !defined(WOLFSSL_NO_TLS12)
  56620. client_cbs.method = wolfDTLSv1_2_client_method;
  56621. server_cbs.method = wolfDTLSv1_2_server_method;
  56622. #else
  56623. return TEST_SKIPPED;
  56624. #endif
  56625. }
  56626. else
  56627. {
  56628. #ifdef WOLFSSL_DTLS13
  56629. client_cbs.method = wolfDTLSv1_3_client_method;
  56630. server_cbs.method = wolfDTLSv1_3_server_method;
  56631. #else
  56632. return TEST_SKIPPED;
  56633. #endif /* WOLFSSL_DTLS13 */
  56634. }
  56635. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  56636. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56637. if (!client_cbs.return_code)
  56638. return TEST_FAIL;
  56639. if (!server_cbs.return_code)
  56640. return TEST_FAIL;
  56641. return TEST_SUCCESS;
  56642. }
  56643. static int test_wolfSSL_ignore_alert_before_cookie(void)
  56644. {
  56645. int ret;
  56646. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  56647. if (ret != 0)
  56648. return ret;
  56649. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  56650. if (ret != 0)
  56651. return ret;
  56652. return 0;
  56653. }
  56654. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  56655. {
  56656. int ret;
  56657. int fd;
  56658. byte bad_msg[] = {
  56659. 0x17, /* app data */
  56660. 0xaa, 0xfd, /* bad version */
  56661. 0x00, 0x01, /* epoch 1 */
  56662. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  56663. 0x00, 0x26, /* length: 38 bytes */
  56664. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  56665. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  56666. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  56667. 0x03, 0x18, 0x72
  56668. };
  56669. fd = wolfSSL_get_fd(ssl);
  56670. AssertIntGE(fd, 0);
  56671. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  56672. AssertIntEQ(ret, sizeof(bad_msg));
  56673. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  56674. AssertIntEQ(ret, sizeof("badrecordtest"));
  56675. }
  56676. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  56677. {
  56678. byte buf[100];
  56679. int ret;
  56680. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  56681. AssertIntGT(ret, 0);
  56682. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  56683. }
  56684. static int _test_wolfSSL_dtls_bad_record(
  56685. method_provider client_method, method_provider server_method)
  56686. {
  56687. callback_functions client_cbs, server_cbs;
  56688. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56689. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56690. client_cbs.doUdp = server_cbs.doUdp = 1;
  56691. client_cbs.method = client_method;
  56692. server_cbs.method = server_method;
  56693. client_cbs.on_result = test_wolfSSL_send_bad_record;
  56694. server_cbs.on_result = test_wolfSSL_read_string;
  56695. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56696. if (!client_cbs.return_code)
  56697. return TEST_FAIL;
  56698. if (!server_cbs.return_code)
  56699. return TEST_FAIL;
  56700. return TEST_SUCCESS;
  56701. }
  56702. static int test_wolfSSL_dtls_bad_record(void)
  56703. {
  56704. int ret = TEST_SUCCESS;
  56705. #if !defined(WOLFSSL_NO_TLS12)
  56706. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  56707. wolfDTLSv1_2_server_method);
  56708. #endif
  56709. #ifdef WOLFSSL_DTLS13
  56710. if (ret == TEST_SUCCESS) {
  56711. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  56712. wolfDTLSv1_3_server_method);
  56713. }
  56714. #endif /* WOLFSSL_DTLS13 */
  56715. return ret;
  56716. }
  56717. #else
  56718. static int test_wolfSSL_dtls_fragments(void) {
  56719. return TEST_SKIPPED;
  56720. }
  56721. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  56722. return TEST_SKIPPED;
  56723. }
  56724. static int test_wolfSSL_dtls_bad_record(void) {
  56725. return TEST_SKIPPED;
  56726. }
  56727. #endif
  56728. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  56729. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56730. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56731. static byte test_AEAD_fail_decryption = 0;
  56732. static byte test_AEAD_seq_num = 0;
  56733. static byte test_AEAD_done = 0;
  56734. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  56735. {
  56736. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  56737. if (ret > 0) {
  56738. if (test_AEAD_fail_decryption) {
  56739. /* Modify the packet to trigger a decryption failure */
  56740. buf[ret/2] ^= 0xFF;
  56741. if (test_AEAD_fail_decryption == 1)
  56742. test_AEAD_fail_decryption = 0;
  56743. }
  56744. }
  56745. (void)ctx;
  56746. return ret;
  56747. }
  56748. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  56749. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  56750. {
  56751. if (sendLimit)
  56752. w64Zero(sendLimit);
  56753. switch (ssl->specs.bulk_cipher_algorithm) {
  56754. case wolfssl_aes_gcm:
  56755. if (sendLimit)
  56756. *sendLimit = AEAD_AES_LIMIT;
  56757. FALL_THROUGH;
  56758. case wolfssl_chacha:
  56759. if (hardLimit)
  56760. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  56761. if (keyUpdateLimit)
  56762. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  56763. break;
  56764. case wolfssl_aes_ccm:
  56765. if (sendLimit)
  56766. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  56767. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  56768. if (hardLimit)
  56769. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  56770. if (keyUpdateLimit)
  56771. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  56772. }
  56773. else {
  56774. if (hardLimit)
  56775. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  56776. if (keyUpdateLimit)
  56777. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  56778. }
  56779. break;
  56780. default:
  56781. fprintf(stderr, "Unrecognized bulk cipher");
  56782. AssertFalse(1);
  56783. break;
  56784. }
  56785. }
  56786. static void test_AEAD_limit_client(WOLFSSL* ssl)
  56787. {
  56788. int ret;
  56789. int i;
  56790. int didReKey = 0;
  56791. char msgBuf[20];
  56792. w64wrapper hardLimit;
  56793. w64wrapper keyUpdateLimit;
  56794. w64wrapper counter;
  56795. w64wrapper sendLimit;
  56796. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  56797. w64Zero(&counter);
  56798. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  56799. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  56800. for (i = 0; i < 10; i++) {
  56801. /* Test some failed decryptions */
  56802. test_AEAD_fail_decryption = 1;
  56803. w64Increment(&counter);
  56804. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56805. /* Should succeed since decryption failures are dropped */
  56806. AssertIntGT(ret, 0);
  56807. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  56808. }
  56809. test_AEAD_fail_decryption = 1;
  56810. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  56811. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  56812. /* 100 read calls should be enough to complete the key update */
  56813. w64Zero(&counter);
  56814. for (i = 0; i < 100; i++) {
  56815. /* Key update should be sent and negotiated */
  56816. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56817. AssertIntGT(ret, 0);
  56818. /* Epoch after one key update is 4 */
  56819. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  56820. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  56821. didReKey = 1;
  56822. break;
  56823. }
  56824. }
  56825. AssertTrue(didReKey);
  56826. if (!w64IsZero(sendLimit)) {
  56827. /* Test the sending limit for AEAD ciphers */
  56828. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  56829. test_AEAD_seq_num = 1;
  56830. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  56831. AssertIntGT(ret, 0);
  56832. didReKey = 0;
  56833. w64Zero(&counter);
  56834. /* 100 read calls should be enough to complete the key update */
  56835. for (i = 0; i < 100; i++) {
  56836. /* Key update should be sent and negotiated */
  56837. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56838. AssertIntGT(ret, 0);
  56839. /* Epoch after another key update is 5 */
  56840. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  56841. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  56842. didReKey = 1;
  56843. break;
  56844. }
  56845. }
  56846. AssertTrue(didReKey);
  56847. }
  56848. test_AEAD_fail_decryption = 2;
  56849. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  56850. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  56851. /* Connection should fail with a DECRYPT_ERROR */
  56852. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56853. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  56854. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  56855. test_AEAD_done = 1;
  56856. }
  56857. int counter = 0;
  56858. static void test_AEAD_limit_server(WOLFSSL* ssl)
  56859. {
  56860. char msgBuf[] = "Sending data";
  56861. int ret = WOLFSSL_SUCCESS;
  56862. w64wrapper sendLimit;
  56863. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56864. struct timespec delay;
  56865. XMEMSET(&delay, 0, sizeof(delay));
  56866. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  56867. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  56868. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56869. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  56870. while (!test_AEAD_done && ret > 0) {
  56871. counter++;
  56872. if (test_AEAD_seq_num) {
  56873. /* We need to update the seq number so that we can understand the
  56874. * peer. Otherwise we will incorrectly interpret the seq number. */
  56875. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  56876. AssertNotNull(e);
  56877. e->nextPeerSeqNumber = sendLimit;
  56878. test_AEAD_seq_num = 0;
  56879. }
  56880. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56881. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  56882. nanosleep(&delay, NULL);
  56883. }
  56884. }
  56885. static int test_wolfSSL_dtls_AEAD_limit(void)
  56886. {
  56887. callback_functions func_cb_client;
  56888. callback_functions func_cb_server;
  56889. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56890. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56891. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56892. func_cb_server.method = wolfDTLSv1_3_server_method;
  56893. func_cb_client.method = wolfDTLSv1_3_client_method;
  56894. func_cb_server.on_result = test_AEAD_limit_server;
  56895. func_cb_client.on_result = test_AEAD_limit_client;
  56896. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56897. if (!func_cb_client.return_code)
  56898. return TEST_FAIL;
  56899. if (!func_cb_server.return_code)
  56900. return TEST_FAIL;
  56901. return TEST_SUCCESS;
  56902. }
  56903. #else
  56904. static int test_wolfSSL_dtls_AEAD_limit(void)
  56905. {
  56906. return TEST_SKIPPED;
  56907. }
  56908. #endif
  56909. #if defined(WOLFSSL_DTLS) && \
  56910. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56911. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  56912. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  56913. {
  56914. int fd, ret;
  56915. byte ch_msg[] = {
  56916. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56917. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56918. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  56919. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  56920. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  56921. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56922. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56923. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56924. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56925. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56926. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56927. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56928. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56929. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  56930. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  56931. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  56932. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  56933. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  56934. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  56935. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  56936. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  56937. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  56938. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  56939. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  56940. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  56941. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  56942. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  56943. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  56944. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  56945. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  56946. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  56947. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  56948. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  56949. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  56950. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  56951. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  56952. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  56953. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  56954. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  56955. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  56956. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  56957. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  56958. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  56959. 0x31, 0x68, 0x4c
  56960. };
  56961. fd = wolfSSL_get_fd(ssl);
  56962. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  56963. AssertIntGT(ret, 0);
  56964. /* consume the HRR otherwise handshake will fail */
  56965. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  56966. AssertIntGT(ret, 0);
  56967. }
  56968. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56969. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  56970. {
  56971. int fd, ret;
  56972. byte ch_msh_invalid_cookie[] = {
  56973. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  56974. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  56975. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  56976. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  56977. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  56978. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56979. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56980. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56981. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56982. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56983. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56984. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56985. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56986. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  56987. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56988. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56989. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56990. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56991. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56992. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  56993. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  56994. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  56995. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  56996. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  56997. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  56998. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  56999. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  57000. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  57001. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  57002. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  57003. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  57004. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  57005. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  57006. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  57007. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  57008. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  57009. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  57010. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  57011. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  57012. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  57013. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  57014. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  57015. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  57016. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  57017. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  57018. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  57019. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  57020. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  57021. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  57022. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  57023. 0x23, 0x00, 0x00
  57024. };
  57025. byte alert_reply[50];
  57026. byte expected_alert_reply[] = {
  57027. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  57028. 0x02, 0x02, 0x2f
  57029. };
  57030. fd = wolfSSL_get_fd(ssl);
  57031. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  57032. AssertIntGT(ret, 0);
  57033. /* should reply with an illegal_parameter reply */
  57034. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  57035. AssertIntEQ(ret, sizeof(expected_alert_reply));
  57036. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  57037. }
  57038. #endif
  57039. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  57040. {
  57041. #ifndef NO_MD5
  57042. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  57043. #elif !defined(NO_SHA)
  57044. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  57045. #elif !defined(NO_SHA256)
  57046. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  57047. #else
  57048. #error "We need a digest to hash the WOLFSSL object"
  57049. #endif
  57050. byte hashBuf[WC_MAX_DIGEST_SIZE];
  57051. wc_HashAlg hash;
  57052. const TLSX* exts = ssl->extensions;
  57053. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  57054. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  57055. * InitHandshakeHashes */
  57056. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  57057. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  57058. /* Following fields are not important to compare */
  57059. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  57060. sslCopy.buffers.inputBuffer.buffer = NULL;
  57061. sslCopy.buffers.inputBuffer.bufferSize = 0;
  57062. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  57063. sslCopy.buffers.inputBuffer.offset = 0;
  57064. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  57065. sslCopy.buffers.outputBuffer.buffer = NULL;
  57066. sslCopy.buffers.outputBuffer.bufferSize = 0;
  57067. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  57068. sslCopy.buffers.outputBuffer.offset = 0;
  57069. sslCopy.error = 0;
  57070. sslCopy.curSize = 0;
  57071. sslCopy.curStartIdx = 0;
  57072. sslCopy.keys.curSeq_lo = 0;
  57073. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  57074. #ifdef WOLFSSL_DTLS13
  57075. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  57076. sslCopy.dtls13FastTimeout = 0;
  57077. #endif
  57078. sslCopy.keys.dtls_peer_handshake_number = 0;
  57079. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  57080. sslCopy.hsHashes = NULL;
  57081. #ifdef WOLFSSL_ASYNC_IO
  57082. #ifdef WOLFSSL_ASYNC_CRYPT
  57083. sslCopy.asyncDev = NULL;
  57084. #endif
  57085. sslCopy.async = NULL;
  57086. #endif
  57087. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  57088. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  57089. /* hash extension list */
  57090. while (exts != NULL) {
  57091. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  57092. exts = exts->next;
  57093. }
  57094. /* Hash suites */
  57095. if (sslCopy.suites != NULL) {
  57096. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  57097. sizeof(struct Suites)), 0);
  57098. }
  57099. /* Hash hsHashes */
  57100. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  57101. sizeof(*hsHashes)), 0);
  57102. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  57103. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  57104. return MakeWordFromHash(hashBuf);
  57105. }
  57106. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  57107. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  57108. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  57109. char *buf, int sz, void *ctx)
  57110. {
  57111. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  57112. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  57113. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  57114. }
  57115. else {
  57116. return WOLFSSL_CBIO_ERR_WANT_READ;
  57117. }
  57118. }
  57119. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  57120. {
  57121. /* Compare the ssl object before and after one ClientHello msg */
  57122. SOCKET_T fd = wolfSSL_get_fd(ssl);
  57123. int res;
  57124. int err;
  57125. word32 initHash;
  57126. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  57127. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  57128. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  57129. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  57130. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  57131. (void)initHash;
  57132. res = tcp_select(fd, 5);
  57133. /* We are expecting a msg. A timeout indicates failure. */
  57134. AssertIntEQ(res, TEST_RECV_READY);
  57135. res = wolfSSL_accept(ssl);
  57136. err = wolfSSL_get_error(ssl, res);
  57137. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  57138. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  57139. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  57140. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  57141. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  57142. }
  57143. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  57144. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  57145. {
  57146. int ret;
  57147. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  57148. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  57149. test_wolfSSL_dtls_compare_stateless(ssl);
  57150. }
  57151. #endif
  57152. static int test_wolfSSL_dtls_stateless(void)
  57153. {
  57154. callback_functions client_cbs, server_cbs;
  57155. size_t i;
  57156. struct {
  57157. method_provider client_meth;
  57158. method_provider server_meth;
  57159. ssl_callback client_ssl_ready;
  57160. ssl_callback server_ssl_ready;
  57161. } test_params[] = {
  57162. #if !defined(WOLFSSL_NO_TLS12)
  57163. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  57164. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  57165. #endif
  57166. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  57167. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  57168. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  57169. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  57170. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  57171. #endif
  57172. };
  57173. if (0 == sizeof(test_params)){
  57174. return TEST_SKIPPED;
  57175. }
  57176. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  57177. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57178. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57179. client_cbs.doUdp = server_cbs.doUdp = 1;
  57180. client_cbs.method = test_params[i].client_meth;
  57181. server_cbs.method = test_params[i].server_meth;
  57182. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  57183. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  57184. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  57185. if (!client_cbs.return_code)
  57186. return TEST_FAIL;
  57187. if (!server_cbs.return_code)
  57188. return TEST_FAIL;
  57189. }
  57190. return TEST_SUCCESS;
  57191. }
  57192. #else
  57193. static int test_wolfSSL_dtls_stateless(void)
  57194. {
  57195. return TEST_SKIPPED;
  57196. }
  57197. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  57198. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  57199. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  57200. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57201. !defined(WOLFSSL_NO_CLIENT_AUTH))
  57202. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  57203. {
  57204. int ret;
  57205. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  57206. fprintf(stderr, "loading cert %s failed\n", certA);
  57207. fprintf(stderr, "Error: (%d): %s\n", ret,
  57208. wolfSSL_ERR_reason_error_string(ret));
  57209. return -1;
  57210. }
  57211. return 0;
  57212. }
  57213. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  57214. {
  57215. int ret;
  57216. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  57217. != WOLFSSL_SUCCESS) {
  57218. fprintf(stderr, "could not verify the cert: %s\n", certA);
  57219. fprintf(stderr, "Error: (%d): %s\n", ret,
  57220. wolfSSL_ERR_reason_error_string(ret));
  57221. return -1;
  57222. }
  57223. else {
  57224. fprintf(stderr, "successfully verified: %s\n", certA);
  57225. }
  57226. return 0;
  57227. }
  57228. #define LOAD_ONE_CA(a, b, c, d) \
  57229. do { \
  57230. (a) = load_ca_into_cm(c, d); \
  57231. if ((a) != 0) \
  57232. return (b); \
  57233. else \
  57234. (b)--; \
  57235. } while(0)
  57236. #define VERIFY_ONE_CERT(a, b, c, d) \
  57237. do { \
  57238. (a) = verify_cert_with_cm(c, d);\
  57239. if ((a) != 0) \
  57240. return (b); \
  57241. else \
  57242. (b)--; \
  57243. } while(0)
  57244. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  57245. {
  57246. int ret;
  57247. int i = -1;
  57248. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  57249. char chainGArr[9][50] = {"certs/ca-cert.pem",
  57250. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  57251. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  57252. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  57253. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  57254. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  57255. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  57256. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  57257. "certs/test-pathlen/chainG-entity.pem"};
  57258. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  57259. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  57260. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  57261. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  57262. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  57263. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  57264. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  57265. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  57266. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  57267. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  57268. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  57269. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  57270. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  57271. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  57272. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  57273. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  57274. /* test validating the entity twice, should have no effect on pathLen since
  57275. * entity/leaf cert */
  57276. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  57277. return ret;
  57278. }
  57279. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  57280. {
  57281. int ret;
  57282. int i = -1;
  57283. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  57284. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  57285. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  57286. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  57287. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  57288. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  57289. */
  57290. char chainHArr[6][50] = {"certs/ca-cert.pem",
  57291. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  57292. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  57293. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  57294. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  57295. "certs/test-pathlen/chainH-entity.pem"};
  57296. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  57297. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  57298. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  57299. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  57300. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  57301. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  57302. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  57303. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  57304. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  57305. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  57306. return ret;
  57307. }
  57308. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  57309. {
  57310. int ret;
  57311. int i = -1;
  57312. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  57313. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  57314. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  57315. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  57316. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  57317. */
  57318. char chainIArr[5][50] = {"certs/ca-cert.pem",
  57319. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  57320. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  57321. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  57322. "certs/test-pathlen/chainI-entity.pem"};
  57323. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  57324. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  57325. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  57326. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  57327. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  57328. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  57329. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  57330. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  57331. return ret;
  57332. }
  57333. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  57334. {
  57335. int ret;
  57336. int i = -1;
  57337. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  57338. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  57339. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  57340. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  57341. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  57342. */
  57343. char chainJArr[6][50] = {"certs/ca-cert.pem",
  57344. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  57345. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  57346. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  57347. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  57348. "certs/test-pathlen/chainJ-entity.pem"};
  57349. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  57350. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  57351. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  57352. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  57353. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  57354. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  57355. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  57356. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  57357. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  57358. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  57359. return ret;
  57360. }
  57361. static int test_various_pathlen_chains(void)
  57362. {
  57363. EXPECT_DECLS;
  57364. WOLFSSL_CERT_MANAGER* cm = NULL;
  57365. /* Test chain G (large chain with varying pathLens) */
  57366. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57367. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  57368. ExpectIntEQ(test_chainG(cm), -1);
  57369. #else
  57370. ExpectIntEQ(test_chainG(cm), 0);
  57371. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  57372. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57373. wolfSSL_CertManagerFree(cm);
  57374. /* end test chain G */
  57375. /* Test chain H (5 chain with same pathLens) */
  57376. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57377. ExpectIntLT(test_chainH(cm), 0);
  57378. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57379. wolfSSL_CertManagerFree(cm);
  57380. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57381. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57382. wolfSSL_CertManagerFree(cm);
  57383. /* end test chain H */
  57384. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  57385. * followed by 2 ICA's, should pass) */
  57386. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57387. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  57388. ExpectIntEQ(test_chainI(cm), -1);
  57389. #else
  57390. ExpectIntEQ(test_chainI(cm), 0);
  57391. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  57392. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57393. wolfSSL_CertManagerFree(cm);
  57394. cm = NULL;
  57395. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57396. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57397. wolfSSL_CertManagerFree(cm);
  57398. cm = NULL;
  57399. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  57400. * this time followed by 3 ICA's, should fail */
  57401. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57402. ExpectIntLT(test_chainJ(cm), 0);
  57403. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57404. wolfSSL_CertManagerFree(cm);
  57405. cm = NULL;
  57406. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57407. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  57408. wolfSSL_CertManagerFree(cm);
  57409. return EXPECT_RESULT();
  57410. }
  57411. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  57412. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57413. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  57414. {
  57415. EXPECT_DECLS;
  57416. byte ekm[100] = {0};
  57417. (void)ctx;
  57418. /* Success Cases */
  57419. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57420. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  57421. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57422. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  57423. /* Use some random context */
  57424. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57425. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  57426. /* Failure cases */
  57427. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57428. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  57429. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57430. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  57431. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57432. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  57433. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57434. "extended master secret", XSTR_SIZEOF("extended master secret"),
  57435. NULL, 0, 0), 0);
  57436. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  57437. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  57438. return EXPECT_RESULT();
  57439. }
  57440. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  57441. {
  57442. wolfSSL_KeepArrays(ssl);
  57443. return TEST_SUCCESS;
  57444. }
  57445. static int test_export_keying_material(void)
  57446. {
  57447. EXPECT_DECLS;
  57448. test_ssl_cbf serverCb;
  57449. test_ssl_cbf clientCb;
  57450. XMEMSET(&serverCb, 0, sizeof(serverCb));
  57451. XMEMSET(&clientCb, 0, sizeof(clientCb));
  57452. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  57453. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  57454. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  57455. return EXPECT_RESULT();
  57456. }
  57457. #endif /* HAVE_KEYING_MATERIAL */
  57458. static int test_wolfSSL_THREADID_hash(void)
  57459. {
  57460. EXPECT_DECLS;
  57461. #if defined(OPENSSL_EXTRA)
  57462. CRYPTO_THREADID id;
  57463. CRYPTO_THREADID_current(NULL);
  57464. /* Hash result is unsigned long. */
  57465. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  57466. XMEMSET(&id, 0, sizeof(id));
  57467. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  57468. #endif /* OPENSSL_EXTRA */
  57469. return EXPECT_RESULT();
  57470. }
  57471. static int test_wolfSSL_set_ecdh_auto(void)
  57472. {
  57473. EXPECT_DECLS;
  57474. #if defined(OPENSSL_EXTRA)
  57475. WOLFSSL* ssl = NULL;
  57476. ExpectIntEQ(SSL_set_ecdh_auto(NULL,0), 1);
  57477. ExpectIntEQ(SSL_set_ecdh_auto(NULL,1), 1);
  57478. ExpectIntEQ(SSL_set_ecdh_auto(ssl,0), 1);
  57479. ExpectIntEQ(SSL_set_ecdh_auto(ssl,1), 1);
  57480. #endif /* OPENSSL_EXTRA */
  57481. return EXPECT_RESULT();
  57482. }
  57483. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  57484. {
  57485. EXPECT_DECLS;
  57486. #if defined(OPENSSL_EXTRA)
  57487. WOLFSSL_CTX* ctx = NULL;
  57488. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  57489. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  57490. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  57491. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  57492. #endif /* OPENSSL_EXTRA */
  57493. return EXPECT_RESULT();
  57494. }
  57495. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  57496. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  57497. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  57498. {
  57499. EXPECT_DECLS;
  57500. callback_functions* callbacks = NULL;
  57501. WOLFSSL_CTX* ctx = NULL;
  57502. WOLFSSL* ssl = NULL;
  57503. SOCKET_T sfd = 0;
  57504. SOCKET_T cfd = 0;
  57505. word16 port;
  57506. char msg[] = "I hear you fa shizzle!";
  57507. int len = (int) XSTRLEN(msg);
  57508. char input[1024];
  57509. int ret = 0;
  57510. int err = 0;
  57511. if (!args)
  57512. WOLFSSL_RETURN_FROM_THREAD(0);
  57513. ((func_args*)args)->return_code = TEST_FAIL;
  57514. callbacks = ((func_args*)args)->callbacks;
  57515. ctx = wolfSSL_CTX_new(callbacks->method());
  57516. #if defined(USE_WINDOWS_API)
  57517. port = ((func_args*)args)->signal->port;
  57518. #else
  57519. /* Let tcp_listen assign port */
  57520. port = 0;
  57521. #endif
  57522. #ifdef WOLFSSL_TIRTOS
  57523. fdOpenSession(Task_self());
  57524. #endif
  57525. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  57526. caCertFile, 0));
  57527. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  57528. svrCertFile, WOLFSSL_FILETYPE_PEM));
  57529. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  57530. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  57531. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  57532. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  57533. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57534. #elif !defined(NO_DH)
  57535. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  57536. #endif
  57537. if (callbacks->ctx_ready)
  57538. callbacks->ctx_ready(ctx);
  57539. ssl = wolfSSL_new(ctx);
  57540. ExpectNotNull(ssl);
  57541. /* listen and accept */
  57542. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  57543. CloseSocket(sfd);
  57544. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  57545. if (callbacks->ssl_ready)
  57546. callbacks->ssl_ready(ssl);
  57547. if (EXPECT_SUCCESS()) {
  57548. do {
  57549. err = 0; /* Reset error */
  57550. ret = wolfSSL_accept(ssl);
  57551. if (ret != WOLFSSL_SUCCESS) {
  57552. err = wolfSSL_get_error(ssl, 0);
  57553. }
  57554. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  57555. }
  57556. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57557. /* read and write data */
  57558. XMEMSET(input, 0, sizeof(input));
  57559. while (EXPECT_SUCCESS()) {
  57560. ret = wolfSSL_read(ssl, input, sizeof(input));
  57561. if (ret > 0) {
  57562. break;
  57563. }
  57564. else {
  57565. err = wolfSSL_get_error(ssl,ret);
  57566. if (err == WOLFSSL_ERROR_WANT_READ) {
  57567. continue;
  57568. }
  57569. break;
  57570. }
  57571. }
  57572. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  57573. do {
  57574. ret = wolfSSL_write(ssl, msg, len);
  57575. if (ret > 0) {
  57576. break;
  57577. }
  57578. } while (ret < 0);
  57579. }
  57580. /* bidirectional shutdown */
  57581. while (EXPECT_SUCCESS()) {
  57582. ret = wolfSSL_shutdown(ssl);
  57583. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  57584. if (ret == WOLFSSL_SUCCESS) {
  57585. break;
  57586. }
  57587. }
  57588. if (EXPECT_SUCCESS()) {
  57589. /* wait for the peer to disconnect the tcp connection */
  57590. do {
  57591. ret = wolfSSL_read(ssl, input, sizeof(input));
  57592. err = wolfSSL_get_error(ssl, ret);
  57593. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  57594. }
  57595. /* detect TCP disconnect */
  57596. ExpectIntLE(ret,WOLFSSL_FAILURE);
  57597. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  57598. ((func_args*)args)->return_code = EXPECT_RESULT();
  57599. wolfSSL_free(ssl);
  57600. wolfSSL_CTX_free(ctx);
  57601. CloseSocket(cfd);
  57602. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  57603. wc_ecc_fp_free(); /* free per thread cache */
  57604. #endif
  57605. WOLFSSL_RETURN_FROM_THREAD(0);
  57606. }
  57607. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  57608. {
  57609. EXPECT_DECLS;
  57610. callback_functions* callbacks = NULL;
  57611. WOLFSSL_CTX* ctx = NULL;
  57612. WOLFSSL* ssl = NULL;
  57613. SOCKET_T sfd = 0;
  57614. char msg[] = "hello wolfssl server!";
  57615. int len = (int) XSTRLEN(msg);
  57616. char input[1024];
  57617. int idx;
  57618. int ret, err;
  57619. if (!args)
  57620. WOLFSSL_RETURN_FROM_THREAD(0);
  57621. ((func_args*)args)->return_code = TEST_FAIL;
  57622. callbacks = ((func_args*)args)->callbacks;
  57623. ctx = wolfSSL_CTX_new(callbacks->method());
  57624. #ifdef WOLFSSL_TIRTOS
  57625. fdOpenSession(Task_self());
  57626. #endif
  57627. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  57628. caCertFile, 0));
  57629. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  57630. cliCertFile, WOLFSSL_FILETYPE_PEM));
  57631. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  57632. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  57633. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  57634. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  57635. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  57636. if (EXPECT_SUCCESS()) {
  57637. do {
  57638. err = 0; /* Reset error */
  57639. ret = wolfSSL_connect(ssl);
  57640. if (ret != WOLFSSL_SUCCESS) {
  57641. err = wolfSSL_get_error(ssl, 0);
  57642. }
  57643. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  57644. }
  57645. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  57646. if (EXPECT_SUCCESS()) {
  57647. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  57648. input[idx] = 0;
  57649. }
  57650. }
  57651. if (EXPECT_SUCCESS()) {
  57652. ret = wolfSSL_shutdown(ssl);
  57653. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  57654. ret = wolfSSL_shutdown(ssl);
  57655. }
  57656. }
  57657. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57658. ((func_args*)args)->return_code = EXPECT_RESULT();
  57659. wolfSSL_free(ssl);
  57660. wolfSSL_CTX_free(ctx);
  57661. CloseSocket(sfd);
  57662. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  57663. wc_ecc_fp_free(); /* free per thread cache */
  57664. #endif
  57665. WOLFSSL_RETURN_FROM_THREAD(0);
  57666. }
  57667. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  57668. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  57669. /* This test is to check wolfSSL_read behaves as same as
  57670. * openSSL when it is called after SSL_shutdown completes.
  57671. */
  57672. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  57673. {
  57674. EXPECT_DECLS;
  57675. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  57676. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  57677. tcp_ready ready;
  57678. func_args client_args;
  57679. func_args server_args;
  57680. THREAD_TYPE serverThread;
  57681. THREAD_TYPE clientThread;
  57682. callback_functions server_cbf;
  57683. callback_functions client_cbf;
  57684. #ifdef WOLFSSL_TIRTOS
  57685. fdOpenSession(Task_self());
  57686. #endif
  57687. StartTCP();
  57688. InitTcpReady(&ready);
  57689. #if defined(USE_WINDOWS_API)
  57690. /* use RNG to get random port if using windows */
  57691. ready.port = GetRandomPort();
  57692. #endif
  57693. XMEMSET(&client_args, 0, sizeof(func_args));
  57694. XMEMSET(&server_args, 0, sizeof(func_args));
  57695. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  57696. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  57697. server_cbf.method = wolfTLSv1_2_server_method;
  57698. client_cbf.method = wolfTLSv1_2_client_method;
  57699. server_args.callbacks = &server_cbf;
  57700. client_args.callbacks = &client_cbf;
  57701. server_args.signal = &ready;
  57702. client_args.signal = &ready;
  57703. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  57704. wait_tcp_ready(&server_args);
  57705. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  57706. join_thread(clientThread);
  57707. join_thread(serverThread);
  57708. ExpectTrue(client_args.return_code);
  57709. ExpectTrue(server_args.return_code);
  57710. FreeTcpReady(&ready);
  57711. #endif
  57712. return EXPECT_RESULT();
  57713. }
  57714. static int test_wolfSSL_CTX_get_min_proto_version(void)
  57715. {
  57716. EXPECT_DECLS;
  57717. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  57718. WOLFSSL_CTX *ctx = NULL;
  57719. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57720. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  57721. WOLFSSL_SUCCESS);
  57722. #ifdef WOLFSSL_ALLOW_SSLV3
  57723. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  57724. #else
  57725. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  57726. #endif
  57727. wolfSSL_CTX_free(ctx);
  57728. ctx = NULL;
  57729. #ifndef NO_OLD_TLS
  57730. #ifdef WOLFSSL_ALLOW_TLSV10
  57731. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  57732. #else
  57733. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57734. #endif
  57735. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  57736. WOLFSSL_SUCCESS);
  57737. #ifdef WOLFSSL_ALLOW_TLSV10
  57738. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  57739. #else
  57740. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  57741. #endif
  57742. wolfSSL_CTX_free(ctx);
  57743. ctx = NULL;
  57744. #endif
  57745. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57746. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  57747. WOLFSSL_SUCCESS);
  57748. #ifndef NO_OLD_TLS
  57749. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  57750. #else
  57751. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  57752. #endif
  57753. wolfSSL_CTX_free(ctx);
  57754. ctx = NULL;
  57755. #ifndef WOLFSSL_NO_TLS12
  57756. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  57757. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  57758. WOLFSSL_SUCCESS);
  57759. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  57760. wolfSSL_CTX_free(ctx);
  57761. ctx = NULL;
  57762. #endif
  57763. #ifdef WOLFSSL_TLS13
  57764. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  57765. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  57766. WOLFSSL_SUCCESS);
  57767. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  57768. wolfSSL_CTX_free(ctx);
  57769. ctx = NULL;
  57770. #endif
  57771. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  57772. return EXPECT_RESULT();
  57773. }
  57774. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57775. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57776. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57777. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  57778. static int test_wolfSSL_set_SSL_CTX(void)
  57779. {
  57780. EXPECT_DECLS;
  57781. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  57782. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  57783. !defined(NO_RSA)
  57784. WOLFSSL_CTX *ctx1 = NULL;
  57785. WOLFSSL_CTX *ctx2 = NULL;
  57786. WOLFSSL *ssl = NULL;
  57787. const byte *session_id1 = (const byte *)"CTX1";
  57788. const byte *session_id2 = (const byte *)"CTX2";
  57789. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  57790. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  57791. WOLFSSL_FILETYPE_PEM));
  57792. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  57793. WOLFSSL_FILETYPE_PEM));
  57794. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  57795. WOLFSSL_SUCCESS);
  57796. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  57797. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  57798. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  57799. WOLFSSL_SUCCESS);
  57800. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  57801. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  57802. WOLFSSL_FILETYPE_PEM));
  57803. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  57804. WOLFSSL_FILETYPE_PEM));
  57805. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  57806. WOLFSSL_SUCCESS);
  57807. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  57808. WOLFSSL_SUCCESS);
  57809. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  57810. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  57811. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  57812. WOLFSSL_SUCCESS);
  57813. #ifdef HAVE_SESSION_TICKET
  57814. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  57815. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  57816. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  57817. #endif
  57818. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  57819. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  57820. #ifdef WOLFSSL_INT_H
  57821. #ifdef WOLFSSL_SESSION_ID_CTX
  57822. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  57823. #endif
  57824. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  57825. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  57826. #endif
  57827. #ifdef HAVE_SESSION_TICKET
  57828. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  57829. #endif
  57830. /* Set the ctx1 that has TLSv1.3 as max proto version */
  57831. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  57832. /* MUST not change proto versions of ssl */
  57833. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  57834. #ifdef HAVE_SESSION_TICKET
  57835. /* MUST not change */
  57836. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  57837. #endif
  57838. /* MUST change */
  57839. #ifdef WOLFSSL_INT_H
  57840. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  57841. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  57842. #ifdef WOLFSSL_SESSION_ID_CTX
  57843. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  57844. #endif
  57845. #endif
  57846. wolfSSL_free(ssl);
  57847. wolfSSL_CTX_free(ctx1);
  57848. wolfSSL_CTX_free(ctx2);
  57849. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  57850. return EXPECT_RESULT();
  57851. }
  57852. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57853. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57854. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57855. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  57856. static int test_wolfSSL_security_level(void)
  57857. {
  57858. EXPECT_DECLS;
  57859. #if defined(OPENSSL_EXTRA)
  57860. SSL_CTX *ctx = NULL;
  57861. #ifdef WOLFSSL_TLS13
  57862. #ifdef NO_WOLFSSL_SERVER
  57863. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57864. #else
  57865. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57866. #endif
  57867. SSL_CTX_set_security_level(NULL, 1);
  57868. SSL_CTX_set_security_level(ctx, 1);
  57869. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  57870. /* Stub so nothing happens. */
  57871. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  57872. SSL_CTX_free(ctx);
  57873. #else
  57874. (void)ctx;
  57875. #endif
  57876. #endif
  57877. return EXPECT_RESULT();
  57878. }
  57879. static int test_wolfSSL_SSL_in_init(void)
  57880. {
  57881. EXPECT_DECLS;
  57882. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  57883. SSL_CTX* ctx = NULL;
  57884. SSL* ssl = NULL;
  57885. const char* testCertFile;
  57886. const char* testKeyFile;
  57887. #ifdef WOLFSSL_TLS13
  57888. #ifdef NO_WOLFSSL_SERVER
  57889. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57890. #else
  57891. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57892. #endif
  57893. #else
  57894. #ifdef NO_WOLFSSL_SERVER
  57895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57896. #else
  57897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57898. #endif
  57899. #endif
  57900. #ifndef NO_RSA
  57901. testCertFile = svrCertFile;
  57902. testKeyFile = svrKeyFile;
  57903. #elif defined(HAVE_ECC)
  57904. testCertFile = eccCertFile;
  57905. testKeyFile = eccKeyFile;
  57906. #else
  57907. testCertFile = NULL;
  57908. testKeyFile = NULL;
  57909. #endif
  57910. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57911. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57912. SSL_FILETYPE_PEM));
  57913. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57914. SSL_FILETYPE_PEM));
  57915. }
  57916. ExpectNotNull(ssl = SSL_new(ctx));
  57917. ExpectIntEQ(SSL_in_init(ssl), 1);
  57918. SSL_CTX_free(ctx);
  57919. SSL_free(ssl);
  57920. #endif
  57921. return EXPECT_RESULT();
  57922. }
  57923. static int test_wolfSSL_CTX_set_timeout(void)
  57924. {
  57925. EXPECT_DECLS;
  57926. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  57927. int timeout;
  57928. WOLFSSL_CTX* ctx = NULL;
  57929. (void)timeout;
  57930. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57931. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  57932. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  57933. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  57934. */
  57935. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  57936. /* giving 0 as timeout value sets default timeout */
  57937. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  57938. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  57939. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  57940. #else
  57941. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  57942. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  57943. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  57944. #endif
  57945. wolfSSL_CTX_free(ctx);
  57946. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  57947. return EXPECT_RESULT();
  57948. }
  57949. static int test_wolfSSL_OpenSSL_version(void)
  57950. {
  57951. EXPECT_DECLS;
  57952. #if defined(OPENSSL_EXTRA)
  57953. const char* ver;
  57954. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  57955. ExpectNotNull(ver = OpenSSL_version(0));
  57956. #else
  57957. ExpectNotNull(ver = OpenSSL_version());
  57958. #endif
  57959. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  57960. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  57961. #endif
  57962. return EXPECT_RESULT();
  57963. }
  57964. static int test_CONF_CTX_CMDLINE(void)
  57965. {
  57966. EXPECT_DECLS;
  57967. #if defined(OPENSSL_ALL)
  57968. SSL_CTX* ctx = NULL;
  57969. SSL_CONF_CTX* cctx = NULL;
  57970. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57971. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57972. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57973. /* set flags */
  57974. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  57975. WOLFSSL_CONF_FLAG_CMDLINE);
  57976. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57977. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57978. /* cmd invalid command */
  57979. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57980. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57981. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57982. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57983. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57984. /* cmd Certificate and Private Key*/
  57985. {
  57986. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57987. const char* ourCert = svrCertFile;
  57988. const char* ourKey = svrKeyFile;
  57989. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  57990. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  57991. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  57992. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  57993. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57994. #endif
  57995. }
  57996. /* cmd curves */
  57997. {
  57998. #if defined(HAVE_ECC)
  57999. const char* curve = "secp256r1";
  58000. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  58001. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  58002. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58003. #endif
  58004. }
  58005. /* cmd CipherString */
  58006. {
  58007. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  58008. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  58009. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  58010. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58011. }
  58012. /* cmd DH parameter */
  58013. {
  58014. #if !defined(NO_DH) && !defined(NO_BIO)
  58015. const char* ourdhcert = "./certs/dh2048.pem";
  58016. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  58017. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  58018. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58019. #endif
  58020. }
  58021. SSL_CTX_free(ctx);
  58022. SSL_CONF_CTX_free(cctx);
  58023. #endif /* OPENSSL_EXTRA */
  58024. return EXPECT_RESULT();
  58025. }
  58026. static int test_CONF_CTX_FILE(void)
  58027. {
  58028. EXPECT_DECLS;
  58029. #if defined(OPENSSL_ALL)
  58030. SSL_CTX* ctx = NULL;
  58031. SSL_CONF_CTX* cctx = NULL;
  58032. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  58033. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  58034. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  58035. /* set flags */
  58036. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  58037. WOLFSSL_CONF_FLAG_FILE);
  58038. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  58039. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  58040. /* sanity check */
  58041. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  58042. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  58043. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  58044. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  58045. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  58046. /* cmd Certificate and Private Key*/
  58047. {
  58048. #if !defined(NO_CERTS) && !defined(NO_RSA)
  58049. const char* ourCert = svrCertFile;
  58050. const char* ourKey = svrKeyFile;
  58051. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  58052. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  58053. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  58054. WOLFSSL_SUCCESS);
  58055. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  58056. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58057. #endif
  58058. }
  58059. /* cmd curves */
  58060. {
  58061. #if defined(HAVE_ECC)
  58062. const char* curve = "secp256r1";
  58063. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  58064. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  58065. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58066. #endif
  58067. }
  58068. /* cmd CipherString */
  58069. {
  58070. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  58071. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  58072. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  58073. WOLFSSL_SUCCESS);
  58074. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58075. }
  58076. /* cmd DH parameter */
  58077. {
  58078. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  58079. const char* ourdhcert = "./certs/dh3072.pem";
  58080. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  58081. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  58082. WOLFSSL_SUCCESS);
  58083. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  58084. #endif
  58085. }
  58086. SSL_CTX_free(ctx);
  58087. SSL_CONF_CTX_free(cctx);
  58088. #endif /* OPENSSL_EXTRA */
  58089. return EXPECT_RESULT();
  58090. }
  58091. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  58092. {
  58093. EXPECT_DECLS;
  58094. #ifdef HAVE_EX_DATA
  58095. int idx1, idx2;
  58096. /* test for unsupported class index */
  58097. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  58098. 0,NULL, NULL, NULL, NULL ), -1);
  58099. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  58100. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  58101. 0,NULL, NULL, NULL, NULL ), -1);
  58102. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  58103. 0,NULL, NULL, NULL, NULL ), -1);
  58104. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  58105. 0,NULL, NULL, NULL, NULL ), -1);
  58106. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  58107. 0,NULL, NULL, NULL, NULL ), -1);
  58108. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  58109. 0,NULL, NULL, NULL, NULL ), -1);
  58110. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  58111. 0,NULL, NULL, NULL, NULL ), -1);
  58112. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  58113. 0,NULL, NULL, NULL, NULL ), -1);
  58114. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  58115. 0,NULL, NULL, NULL, NULL ), -1);
  58116. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  58117. 0,NULL, NULL, NULL, NULL ), -1);
  58118. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  58119. 0,NULL, NULL, NULL, NULL ), -1);
  58120. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  58121. 0,NULL, NULL, NULL, NULL ), -1);
  58122. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  58123. 0,NULL, NULL, NULL, NULL ), -1);
  58124. /* test for supported class index */
  58125. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  58126. 0,NULL, NULL, NULL, NULL );
  58127. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  58128. 0,NULL, NULL, NULL, NULL );
  58129. ExpectIntNE(idx1, -1);
  58130. ExpectIntNE(idx2, -1);
  58131. ExpectIntNE(idx1, idx2);
  58132. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  58133. 0,NULL, NULL, NULL, NULL );
  58134. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  58135. 0,NULL, NULL, NULL, NULL );
  58136. ExpectIntNE(idx1, -1);
  58137. ExpectIntNE(idx2, -1);
  58138. ExpectIntNE(idx1, idx2);
  58139. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  58140. 0,NULL, NULL, NULL, NULL );
  58141. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  58142. 0,NULL, NULL, NULL, NULL );
  58143. ExpectIntNE(idx1, -1);
  58144. ExpectIntNE(idx2, -1);
  58145. ExpectIntNE(idx1, idx2);
  58146. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  58147. 0,NULL, NULL, NULL, NULL );
  58148. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  58149. 0,NULL, NULL, NULL, NULL );
  58150. ExpectIntNE(idx1, -1);
  58151. ExpectIntNE(idx2, -1);
  58152. ExpectIntNE(idx1, idx2);
  58153. #endif /* HAVE_EX_DATA */
  58154. return EXPECT_RESULT();
  58155. }
  58156. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  58157. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  58158. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  58159. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  58160. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  58161. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  58162. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  58163. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  58164. #define SESSION_NEW_IDX_PTR "Testing"
  58165. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  58166. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  58167. {
  58168. AssertNotNull(p);
  58169. AssertNull(ptr);
  58170. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  58171. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  58172. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  58173. }
  58174. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  58175. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  58176. void* arg)
  58177. {
  58178. EXPECT_DECLS;
  58179. ExpectNotNull(out);
  58180. ExpectNotNull(in);
  58181. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  58182. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  58183. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  58184. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  58185. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  58186. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  58187. if (EXPECT_SUCCESS()) {
  58188. return SSL_SUCCESS;
  58189. }
  58190. else {
  58191. return SSL_FAILURE;
  58192. }
  58193. }
  58194. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  58195. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  58196. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  58197. {
  58198. EXPECT_DECLS;
  58199. ExpectNotNull(p);
  58200. ExpectNull(ptr);
  58201. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  58202. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  58203. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  58204. if (EXPECT_SUCCESS()) {
  58205. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  58206. }
  58207. }
  58208. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  58209. {
  58210. EXPECT_DECLS;
  58211. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  58212. (void*)SESSION_NEW_IDX_PTR,
  58213. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  58214. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  58215. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  58216. SSL_SESSION* s = SSL_SESSION_new();
  58217. SSL_SESSION* d = NULL;
  58218. ExpectNotNull(s);
  58219. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  58220. ExpectNotNull(d = SSL_SESSION_dup(s));
  58221. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  58222. SSL_SESSION_free(s);
  58223. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  58224. SSL_SESSION_free(d);
  58225. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  58226. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  58227. crypto_ex_cb_ctx_session = NULL;
  58228. return EXPECT_RESULT();
  58229. }
  58230. #else
  58231. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  58232. {
  58233. return TEST_SKIPPED;
  58234. }
  58235. #endif
  58236. static int test_wolfSSL_set_psk_use_session_callback(void)
  58237. {
  58238. EXPECT_DECLS;
  58239. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  58240. SSL_CTX* ctx = NULL;
  58241. SSL* ssl = NULL;
  58242. const char* testCertFile;
  58243. const char* testKeyFile;
  58244. #ifdef WOLFSSL_TLS13
  58245. #ifdef NO_WOLFSSL_SERVER
  58246. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  58247. #else
  58248. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  58249. #endif
  58250. #else
  58251. #ifdef NO_WOLFSSL_SERVER
  58252. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  58253. #else
  58254. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  58255. #endif
  58256. #endif
  58257. #ifndef NO_RSA
  58258. testCertFile = svrCertFile;
  58259. testKeyFile = svrKeyFile;
  58260. #elif defined(HAVE_ECC)
  58261. testCertFile = eccCertFile;
  58262. testKeyFile = eccKeyFile;
  58263. #else
  58264. testCertFile = NULL;
  58265. testKeyFile = NULL;
  58266. #endif
  58267. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  58268. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  58269. SSL_FILETYPE_PEM));
  58270. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  58271. SSL_FILETYPE_PEM));
  58272. }
  58273. ExpectNotNull(ssl = SSL_new(ctx));
  58274. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  58275. SSL_CTX_free(ctx);
  58276. SSL_free(ssl);
  58277. #endif
  58278. return EXPECT_RESULT();
  58279. }
  58280. static int test_wolfSSL_ERR_strings(void)
  58281. {
  58282. EXPECT_DECLS;
  58283. #if !defined(NO_ERROR_STRINGS)
  58284. const char* err1 = "unsupported cipher suite";
  58285. const char* err2 = "wolfSSL PEM routines";
  58286. const char* err = NULL;
  58287. (void)err;
  58288. (void)err1;
  58289. (void)err2;
  58290. #if defined(OPENSSL_EXTRA)
  58291. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  58292. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  58293. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  58294. ExpectIntEQ((*err == '\0'), 1);
  58295. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  58296. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  58297. #else
  58298. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  58299. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  58300. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  58301. ExpectIntEQ((*err == '\0'), 1);
  58302. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  58303. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  58304. ExpectIntEQ((*err == '\0'), 1);
  58305. #endif
  58306. #endif
  58307. return EXPECT_RESULT();
  58308. }
  58309. static int test_wolfSSL_EVP_shake128(void)
  58310. {
  58311. EXPECT_DECLS;
  58312. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  58313. defined(WOLFSSL_SHAKE128)
  58314. const EVP_MD* md = NULL;
  58315. ExpectNotNull(md = EVP_shake128());
  58316. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  58317. #endif
  58318. return EXPECT_RESULT();
  58319. }
  58320. static int test_wolfSSL_EVP_shake256(void)
  58321. {
  58322. EXPECT_DECLS;
  58323. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  58324. defined(WOLFSSL_SHAKE256)
  58325. const EVP_MD* md = NULL;
  58326. ExpectNotNull(md = EVP_shake256());
  58327. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  58328. #endif
  58329. return EXPECT_RESULT();
  58330. }
  58331. /*
  58332. * Testing EVP digest API with SM3
  58333. */
  58334. static int test_wolfSSL_EVP_sm3(void)
  58335. {
  58336. int res = TEST_SKIPPED;
  58337. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  58338. EXPECT_DECLS;
  58339. const EVP_MD* md = NULL;
  58340. EVP_MD_CTX* mdCtx = NULL;
  58341. byte data[WC_SM3_BLOCK_SIZE * 4];
  58342. byte hash[WC_SM3_DIGEST_SIZE];
  58343. byte calcHash[WC_SM3_DIGEST_SIZE];
  58344. byte expHash[WC_SM3_DIGEST_SIZE] = {
  58345. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  58346. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  58347. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  58348. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  58349. };
  58350. word32 chunk;
  58351. word32 i;
  58352. unsigned int sz;
  58353. int ret;
  58354. XMEMSET(data, 0, sizeof(data));
  58355. md = EVP_sm3();
  58356. ExpectTrue(md != NULL);
  58357. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  58358. mdCtx = EVP_MD_CTX_new();
  58359. ExpectTrue(mdCtx != NULL);
  58360. /* Invalid Parameters */
  58361. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  58362. /* Valid Parameters */
  58363. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  58364. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  58365. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  58366. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  58367. /* Valid Parameters */
  58368. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  58369. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  58370. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  58371. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  58372. WOLFSSL_SUCCESS);
  58373. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  58374. WOLFSSL_SUCCESS);
  58375. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  58376. WOLFSSL_SUCCESS);
  58377. /* Ensure too many bytes for lengths. */
  58378. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  58379. WOLFSSL_SUCCESS);
  58380. /* Invalid Parameters */
  58381. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  58382. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  58383. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  58384. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  58385. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  58386. /* Valid Parameters */
  58387. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  58388. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  58389. /* Chunk tests. */
  58390. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  58391. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  58392. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  58393. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  58394. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  58395. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  58396. WOLFSSL_SUCCESS);
  58397. }
  58398. if (i < (word32)sizeof(data)) {
  58399. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  58400. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  58401. }
  58402. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  58403. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  58404. }
  58405. /* Not testing when the low 32-bit length overflows. */
  58406. ret = EVP_MD_CTX_cleanup(mdCtx);
  58407. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  58408. wolfSSL_EVP_MD_CTX_free(mdCtx);
  58409. res = EXPECT_RESULT();
  58410. #endif
  58411. return res;
  58412. } /* END test_EVP_sm3 */
  58413. static int test_EVP_blake2(void)
  58414. {
  58415. EXPECT_DECLS;
  58416. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  58417. const EVP_MD* md = NULL;
  58418. (void)md;
  58419. #if defined(HAVE_BLAKE2)
  58420. ExpectNotNull(md = EVP_blake2b512());
  58421. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  58422. #endif
  58423. #if defined(HAVE_BLAKE2S)
  58424. ExpectNotNull(md = EVP_blake2s256());
  58425. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  58426. #endif
  58427. #endif
  58428. return EXPECT_RESULT();
  58429. }
  58430. #if defined(OPENSSL_EXTRA)
  58431. static void list_md_fn(const EVP_MD* m, const char* from,
  58432. const char* to, void* arg)
  58433. {
  58434. const char* mn;
  58435. BIO *bio;
  58436. (void) from;
  58437. (void) to;
  58438. (void) arg;
  58439. (void) mn;
  58440. (void) bio;
  58441. if (!m) {
  58442. /* alias */
  58443. AssertNull(m);
  58444. AssertNotNull(to);
  58445. }
  58446. else {
  58447. AssertNotNull(m);
  58448. AssertNull(to);
  58449. }
  58450. AssertNotNull(from);
  58451. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  58452. mn = EVP_get_digestbyname(from);
  58453. /* print to stderr */
  58454. AssertNotNull(arg);
  58455. bio = BIO_new(BIO_s_file());
  58456. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  58457. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  58458. BIO_free(bio);
  58459. #endif
  58460. }
  58461. #endif
  58462. static int test_EVP_MD_do_all(void)
  58463. {
  58464. int res = TEST_SKIPPED;
  58465. #if defined(OPENSSL_EXTRA)
  58466. EVP_MD_do_all(NULL, stderr);
  58467. EVP_MD_do_all(list_md_fn, stderr);
  58468. res = TEST_SUCCESS;
  58469. #endif
  58470. return res;
  58471. }
  58472. #if defined(OPENSSL_EXTRA)
  58473. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  58474. {
  58475. (void)arg;
  58476. (void)nm;
  58477. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  58478. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  58479. /* print to stderr */
  58480. AssertNotNull(arg);
  58481. BIO *bio = BIO_new(BIO_s_file());
  58482. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  58483. BIO_printf(bio, "%s\n", nm);
  58484. BIO_free(bio);
  58485. #endif
  58486. }
  58487. #endif
  58488. static int test_OBJ_NAME_do_all(void)
  58489. {
  58490. int res = TEST_SKIPPED;
  58491. #if defined(OPENSSL_EXTRA)
  58492. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  58493. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  58494. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  58495. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  58496. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  58497. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  58498. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  58499. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  58500. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  58501. res = TEST_SUCCESS;
  58502. #endif
  58503. return res;
  58504. }
  58505. static int test_SSL_CIPHER_get_xxx(void)
  58506. {
  58507. EXPECT_DECLS;
  58508. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  58509. !defined(NO_FILESYSTEM)
  58510. const SSL_CIPHER* cipher = NULL;
  58511. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  58512. int i, numCiphers = 0;
  58513. SSL_CTX* ctx = NULL;
  58514. SSL* ssl = NULL;
  58515. const char* testCertFile;
  58516. const char* testKeyFile;
  58517. char buf[256] = {0};
  58518. const char* cipher_id = NULL;
  58519. int expect_nid1 = NID_undef;
  58520. int expect_nid2 = NID_undef;
  58521. int expect_nid3 = NID_undef;
  58522. int expect_nid4 = NID_undef;
  58523. int expect_nid5 = 0;
  58524. const char* cipher_id2 = NULL;
  58525. int expect_nid21 = NID_undef;
  58526. int expect_nid22 = NID_undef;
  58527. int expect_nid23 = NID_undef;
  58528. int expect_nid24 = NID_undef;
  58529. int expect_nid25 = 0;
  58530. (void)cipher;
  58531. (void)supportedCiphers;
  58532. (void)i;
  58533. (void)numCiphers;
  58534. (void)ctx;
  58535. (void)ssl;
  58536. (void)testCertFile;
  58537. (void)testKeyFile;
  58538. #if defined(WOLFSSL_TLS13)
  58539. cipher_id = "TLS13-AES128-GCM-SHA256";
  58540. expect_nid1 = NID_auth_rsa;
  58541. expect_nid2 = NID_aes_128_gcm;
  58542. expect_nid3 = NID_sha256;
  58543. expect_nid4 = NID_kx_any;
  58544. expect_nid5 = 1;
  58545. #if !defined(WOLFSSL_NO_TLS12)
  58546. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  58547. expect_nid21 = NID_auth_rsa;
  58548. expect_nid22 = NID_aes_256_gcm;
  58549. expect_nid23 = NID_sha384;
  58550. expect_nid24 = NID_kx_ecdhe;
  58551. expect_nid25 = 1;
  58552. #endif
  58553. #endif
  58554. #ifdef NO_WOLFSSL_SERVER
  58555. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  58556. #else
  58557. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  58558. #endif
  58559. if (cipher_id) {
  58560. #ifndef NO_RSA
  58561. testCertFile = svrCertFile;
  58562. testKeyFile = svrKeyFile;
  58563. #elif defined(HAVE_ECC)
  58564. testCertFile = eccCertFile;
  58565. testKeyFile = eccKeyFile;
  58566. #else
  58567. testCertFile = NULL;
  58568. testKeyFile = NULL;
  58569. #endif
  58570. if (testCertFile != NULL && testKeyFile != NULL) {
  58571. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  58572. SSL_FILETYPE_PEM));
  58573. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  58574. SSL_FILETYPE_PEM));
  58575. }
  58576. ExpectNotNull(ssl = SSL_new(ctx));
  58577. ExpectIntEQ(SSL_in_init(ssl), 1);
  58578. supportedCiphers = SSL_get_ciphers(ssl);
  58579. numCiphers = sk_num(supportedCiphers);
  58580. for (i = 0; i < numCiphers; ++i) {
  58581. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  58582. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  58583. }
  58584. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  58585. break;
  58586. }
  58587. }
  58588. /* test case for */
  58589. if (i != numCiphers) {
  58590. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  58591. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  58592. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  58593. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  58594. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  58595. }
  58596. if (cipher_id2) {
  58597. for (i = 0; i < numCiphers; ++i) {
  58598. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  58599. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  58600. }
  58601. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  58602. break;
  58603. }
  58604. }
  58605. /* test case for */
  58606. if (i != numCiphers) {
  58607. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  58608. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  58609. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  58610. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  58611. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  58612. }
  58613. }
  58614. }
  58615. SSL_CTX_free(ctx);
  58616. SSL_free(ssl);
  58617. #endif
  58618. return EXPECT_RESULT();
  58619. }
  58620. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58621. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  58622. int* keyFormat)
  58623. {
  58624. int ret;
  58625. byte* key_buf = NULL;
  58626. size_t key_sz = 0;
  58627. EncryptedInfo encInfo;
  58628. XMEMSET(&encInfo, 0, sizeof(encInfo));
  58629. ret = load_file(privKeyFile, &key_buf, &key_sz);
  58630. if (ret == 0) {
  58631. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  58632. NULL, &encInfo, keyFormat);
  58633. }
  58634. if (key_buf != NULL) {
  58635. free(key_buf); key_buf = NULL;
  58636. }
  58637. (void)encInfo; /* not used in this test */
  58638. #ifdef DEBUG_WOLFSSL
  58639. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  58640. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  58641. (*pDer)->length);
  58642. #endif
  58643. return ret;
  58644. }
  58645. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  58646. {
  58647. int ret = CRYPTOCB_UNAVAILABLE;
  58648. const char* privKeyFile = (const char*)ctx;
  58649. DerBuffer* pDer = NULL;
  58650. int keyFormat = 0;
  58651. if (info->algo_type == WC_ALGO_TYPE_PK) {
  58652. #ifdef DEBUG_WOLFSSL
  58653. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  58654. #endif
  58655. #ifndef NO_RSA
  58656. if (info->pk.type == WC_PK_TYPE_RSA) {
  58657. switch (info->pk.rsa.type) {
  58658. case RSA_PUBLIC_ENCRYPT:
  58659. case RSA_PUBLIC_DECRYPT:
  58660. /* perform software based RSA public op */
  58661. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  58662. break;
  58663. case RSA_PRIVATE_ENCRYPT:
  58664. case RSA_PRIVATE_DECRYPT:
  58665. {
  58666. RsaKey key;
  58667. /* perform software based RSA private op */
  58668. #ifdef DEBUG_WOLFSSL
  58669. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  58670. #endif
  58671. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  58672. &keyFormat);
  58673. if (ret != 0) {
  58674. return ret;
  58675. }
  58676. ret = wc_InitRsaKey(&key, HEAP_HINT);
  58677. if (ret == 0) {
  58678. word32 keyIdx = 0;
  58679. /* load RSA private key and perform private transform */
  58680. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  58681. &key, pDer->length);
  58682. if (ret == 0) {
  58683. ret = wc_RsaFunction(
  58684. info->pk.rsa.in, info->pk.rsa.inLen,
  58685. info->pk.rsa.out, info->pk.rsa.outLen,
  58686. info->pk.rsa.type, &key, info->pk.rsa.rng);
  58687. }
  58688. else {
  58689. /* if decode fails, then fall-back to software based crypto */
  58690. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  58691. "key decode failed %d, falling back to "
  58692. "software\n", ret);
  58693. ret = CRYPTOCB_UNAVAILABLE;
  58694. }
  58695. wc_FreeRsaKey(&key);
  58696. }
  58697. wc_FreeDer(&pDer); pDer = NULL;
  58698. break;
  58699. }
  58700. }
  58701. #ifdef DEBUG_WOLFSSL
  58702. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  58703. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  58704. #endif
  58705. }
  58706. #endif /* !NO_RSA */
  58707. #ifdef HAVE_ECC
  58708. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  58709. /* mark this key as ephemeral */
  58710. if (info->pk.eckg.key != NULL) {
  58711. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  58712. sizeof(info->pk.eckg.key->label));
  58713. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  58714. }
  58715. }
  58716. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  58717. ecc_key key;
  58718. /* perform software based ECC sign */
  58719. #ifdef DEBUG_WOLFSSL
  58720. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  58721. #endif
  58722. if (info->pk.eccsign.key != NULL &&
  58723. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  58724. /* this is an empheral key */
  58725. #ifdef DEBUG_WOLFSSL
  58726. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  58727. "ephemeral key\n");
  58728. #endif
  58729. return CRYPTOCB_UNAVAILABLE;
  58730. }
  58731. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  58732. if (ret != 0) {
  58733. return ret;
  58734. }
  58735. ret = wc_ecc_init(&key);
  58736. if (ret == 0) {
  58737. word32 keyIdx = 0;
  58738. /* load ECC private key and perform private transform */
  58739. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  58740. &key, pDer->length);
  58741. if (ret == 0) {
  58742. ret = wc_ecc_sign_hash(
  58743. info->pk.eccsign.in, info->pk.eccsign.inlen,
  58744. info->pk.eccsign.out, info->pk.eccsign.outlen,
  58745. info->pk.eccsign.rng, &key);
  58746. }
  58747. else {
  58748. /* if decode fails, then fall-back to software based crypto */
  58749. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  58750. "decode failed %d, falling back to software\n", ret);
  58751. ret = CRYPTOCB_UNAVAILABLE;
  58752. }
  58753. wc_ecc_free(&key);
  58754. }
  58755. wc_FreeDer(&pDer); pDer = NULL;
  58756. #ifdef DEBUG_WOLFSSL
  58757. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  58758. ret, *info->pk.eccsign.outlen);
  58759. #endif
  58760. }
  58761. #endif /* HAVE_ECC */
  58762. #ifdef HAVE_ED25519
  58763. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  58764. ed25519_key key;
  58765. /* perform software based ED25519 sign */
  58766. #ifdef DEBUG_WOLFSSL
  58767. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  58768. #endif
  58769. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  58770. if (ret != 0) {
  58771. return ret;
  58772. }
  58773. ret = wc_ed25519_init(&key);
  58774. if (ret == 0) {
  58775. word32 keyIdx = 0;
  58776. /* load ED25519 private key and perform private transform */
  58777. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  58778. &key, pDer->length);
  58779. if (ret == 0) {
  58780. /* calculate public key */
  58781. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  58782. if (ret == 0) {
  58783. key.pubKeySet = 1;
  58784. ret = wc_ed25519_sign_msg_ex(
  58785. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  58786. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  58787. &key, info->pk.ed25519sign.type,
  58788. info->pk.ed25519sign.context,
  58789. info->pk.ed25519sign.contextLen);
  58790. }
  58791. }
  58792. else {
  58793. /* if decode fails, then fall-back to software based crypto */
  58794. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  58795. "decode failed %d, falling back to software\n", ret);
  58796. ret = CRYPTOCB_UNAVAILABLE;
  58797. }
  58798. wc_ed25519_free(&key);
  58799. }
  58800. wc_FreeDer(&pDer); pDer = NULL;
  58801. #ifdef DEBUG_WOLFSSL
  58802. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  58803. ret, *info->pk.ed25519sign.outLen);
  58804. #endif
  58805. }
  58806. #endif /* HAVE_ED25519 */
  58807. }
  58808. (void)thisDevId;
  58809. (void)keyFormat;
  58810. return ret;
  58811. }
  58812. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  58813. static int test_wc_CryptoCb_TLS(int tlsVer,
  58814. const char* cliCaPemFile, const char* cliCertPemFile,
  58815. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  58816. const char* svrCaPemFile, const char* svrCertPemFile,
  58817. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  58818. {
  58819. EXPECT_DECLS;
  58820. callback_functions client_cbf;
  58821. callback_functions server_cbf;
  58822. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58823. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58824. if (tlsVer == WOLFSSL_TLSV1_3) {
  58825. #ifdef WOLFSSL_TLS13
  58826. server_cbf.method = wolfTLSv1_3_server_method;
  58827. client_cbf.method = wolfTLSv1_3_client_method;
  58828. #endif
  58829. }
  58830. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58831. #ifndef WOLFSSL_NO_TLS12
  58832. server_cbf.method = wolfTLSv1_2_server_method;
  58833. client_cbf.method = wolfTLSv1_2_client_method;
  58834. #endif
  58835. }
  58836. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58837. #ifndef NO_OLD_TLS
  58838. server_cbf.method = wolfTLSv1_1_server_method;
  58839. client_cbf.method = wolfTLSv1_1_client_method;
  58840. #endif
  58841. }
  58842. else if (tlsVer == WOLFSSL_TLSV1) {
  58843. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58844. server_cbf.method = wolfTLSv1_server_method;
  58845. client_cbf.method = wolfTLSv1_client_method;
  58846. #endif
  58847. }
  58848. else if (tlsVer == WOLFSSL_SSLV3) {
  58849. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58850. defined(WOLFSSL_STATIC_RSA)
  58851. server_cbf.method = wolfSSLv3_server_method;
  58852. client_cbf.method = wolfSSLv3_client_method;
  58853. #endif
  58854. }
  58855. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58856. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58857. server_cbf.method = wolfDTLSv1_2_server_method;
  58858. client_cbf.method = wolfDTLSv1_2_client_method;
  58859. #endif
  58860. }
  58861. else if (tlsVer == WOLFSSL_DTLSV1) {
  58862. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58863. server_cbf.method = wolfDTLSv1_server_method;
  58864. client_cbf.method = wolfDTLSv1_client_method;
  58865. #endif
  58866. }
  58867. if (server_cbf.method == NULL) {
  58868. /* not enabled */
  58869. return TEST_SUCCESS;
  58870. }
  58871. /* Setup the keys for the TLS test */
  58872. client_cbf.certPemFile = cliCertPemFile;
  58873. client_cbf.keyPemFile = cliPubKeyPemFile;
  58874. client_cbf.caPemFile = cliCaPemFile;
  58875. server_cbf.certPemFile = svrCertPemFile;
  58876. server_cbf.keyPemFile = svrPubKeyPemFile;
  58877. server_cbf.caPemFile = svrCaPemFile;
  58878. /* Setup a crypto callback with pointer to private key file for testing */
  58879. client_cbf.devId = 1;
  58880. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  58881. (void*)cliPrivKeyPemFile);
  58882. server_cbf.devId = 2;
  58883. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  58884. (void*)svrPrivKeyPemFile);
  58885. /* Perform TLS server and client test */
  58886. /* First test is at WOLFSSL_CTX level */
  58887. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58888. /* Check for success */
  58889. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58890. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58891. if (EXPECT_SUCCESS()) {
  58892. /* Second test is a WOLFSSL object level */
  58893. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  58894. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58895. }
  58896. /* Check for success */
  58897. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58898. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58899. /* Un register the devId's */
  58900. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  58901. client_cbf.devId = INVALID_DEVID;
  58902. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  58903. server_cbf.devId = INVALID_DEVID;
  58904. return EXPECT_RESULT();
  58905. }
  58906. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  58907. static int test_wc_CryptoCb(void)
  58908. {
  58909. EXPECT_DECLS;
  58910. #ifdef WOLF_CRYPTO_CB
  58911. /* TODO: Add crypto callback API tests */
  58912. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58913. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58914. int tlsVer;
  58915. #endif
  58916. #ifndef NO_RSA
  58917. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58918. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58919. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  58920. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  58921. TEST_SUCCESS);
  58922. }
  58923. #endif
  58924. #ifdef HAVE_ECC
  58925. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58926. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58927. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  58928. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  58929. TEST_SUCCESS);
  58930. }
  58931. #endif
  58932. #ifdef HAVE_ED25519
  58933. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58934. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58935. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58936. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  58937. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  58938. TEST_SUCCESS);
  58939. }
  58940. #endif
  58941. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58942. #endif /* WOLF_CRYPTO_CB */
  58943. return EXPECT_RESULT();
  58944. }
  58945. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58946. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  58947. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  58948. const char* cliCaPemFile, const char* cliCertPemFile,
  58949. const char* cliPrivKeyPemFile,
  58950. const char* svrCaPemFile, const char* svrCertPemFile,
  58951. const char* svrPrivKeyPemFile,
  58952. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  58953. {
  58954. EXPECT_DECLS;
  58955. callback_functions client_cbf;
  58956. callback_functions server_cbf;
  58957. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58958. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58959. if (tlsVer == WOLFSSL_TLSV1_3) {
  58960. #ifdef WOLFSSL_TLS13
  58961. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  58962. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  58963. #endif
  58964. }
  58965. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58966. #ifndef WOLFSSL_NO_TLS12
  58967. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  58968. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  58969. #endif
  58970. }
  58971. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58972. #ifndef NO_OLD_TLS
  58973. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  58974. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  58975. #endif
  58976. }
  58977. else if (tlsVer == WOLFSSL_TLSV1) {
  58978. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58979. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  58980. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  58981. #endif
  58982. }
  58983. else if (tlsVer == WOLFSSL_SSLV3) {
  58984. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58985. defined(WOLFSSL_STATIC_RSA)
  58986. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  58987. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  58988. #endif
  58989. }
  58990. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58991. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58992. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  58993. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  58994. #endif
  58995. }
  58996. else if (tlsVer == WOLFSSL_DTLSV1) {
  58997. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58998. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  58999. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  59000. #endif
  59001. }
  59002. if (server_cbf.method_ex == NULL) {
  59003. /* not enabled */
  59004. return TEST_SUCCESS;
  59005. }
  59006. /* Setup the keys for the TLS test */
  59007. client_cbf.certPemFile = cliCertPemFile;
  59008. client_cbf.keyPemFile = cliPrivKeyPemFile;
  59009. client_cbf.caPemFile = cliCaPemFile;
  59010. server_cbf.certPemFile = svrCertPemFile;
  59011. server_cbf.keyPemFile = svrPrivKeyPemFile;
  59012. server_cbf.caPemFile = svrCaPemFile;
  59013. client_cbf.mem = cliMem;
  59014. client_cbf.memSz = cliMemSz;
  59015. server_cbf.mem = svrMem;
  59016. server_cbf.memSz = svrMemSz;
  59017. client_cbf.devId = INVALID_DEVID;
  59018. server_cbf.devId = INVALID_DEVID;
  59019. /* Perform TLS server and client test */
  59020. /* First test is at WOLFSSL_CTX level */
  59021. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  59022. /* Check for success */
  59023. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  59024. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  59025. if (EXPECT_SUCCESS()) {
  59026. /* Second test is a WOLFSSL object level */
  59027. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  59028. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  59029. }
  59030. /* Check for success */
  59031. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  59032. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  59033. return EXPECT_RESULT();
  59034. }
  59035. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  59036. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  59037. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  59038. {
  59039. EXPECT_DECLS;
  59040. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  59041. WOLFSSL_MEM_STATS mem_stats;
  59042. WOLFSSL_MEM_CONN_STATS ssl_stats;
  59043. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  59044. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  59045. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59046. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  59047. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  59048. #endif
  59049. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  59050. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  59051. /* this should fail because kMaxCtxClients == 2 */
  59052. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  59053. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  59054. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  59055. wolfSSL_PrintStatsConn(&ssl_stats);
  59056. #endif
  59057. (void)ssl_stats;
  59058. }
  59059. /* display collected statistics */
  59060. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  59061. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  59062. wolfSSL_PrintStats(&mem_stats);
  59063. #endif
  59064. (void)mem_stats;
  59065. }
  59066. wolfSSL_free(ssl1);
  59067. wolfSSL_free(ssl2);
  59068. return EXPECT_RESULT();
  59069. }
  59070. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  59071. static int test_wolfSSL_CTX_StaticMemory(void)
  59072. {
  59073. EXPECT_DECLS;
  59074. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  59075. wolfSSL_method_func method_func;
  59076. WOLFSSL_CTX* ctx;
  59077. const int kMaxCtxClients = 2;
  59078. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  59079. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  59080. int tlsVer;
  59081. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  59082. #endif
  59083. #endif
  59084. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  59085. #ifndef NO_WOLFSSL_SERVER
  59086. #ifndef WOLFSSL_NO_TLS12
  59087. method_func = wolfTLSv1_2_server_method_ex;
  59088. #else
  59089. method_func = wolfTLSv1_3_server_method_ex;
  59090. #endif
  59091. #else
  59092. #ifndef WOLFSSL_NO_TLS12
  59093. method_func = wolfTLSv1_2_client_method_ex;
  59094. #else
  59095. method_func = wolfTLSv1_3_client_method_ex;
  59096. #endif
  59097. #endif
  59098. /* Test creating CTX directly from static memory pool */
  59099. ctx = NULL;
  59100. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  59101. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  59102. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  59103. wolfSSL_CTX_free(ctx);
  59104. ctx = NULL;
  59105. /* Test for heap allocated CTX, then assigning static pool to it */
  59106. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  59107. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  59108. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  59109. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  59110. wolfSSL_CTX_free(ctx);
  59111. /* TLS Level Tests using static memory */
  59112. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  59113. #ifndef NO_RSA
  59114. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  59115. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  59116. svrCertFile, cliCertFile, cliKeyFile,
  59117. cliCertFile, svrCertFile, svrKeyFile,
  59118. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  59119. TEST_SUCCESS);
  59120. }
  59121. #endif
  59122. #ifdef HAVE_ECC
  59123. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  59124. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  59125. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  59126. cliEccCertFile, eccCertFile, eccKeyFile,
  59127. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  59128. TEST_SUCCESS);
  59129. }
  59130. #endif
  59131. #ifdef HAVE_ED25519
  59132. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  59133. if (tlsVer == WOLFSSL_DTLSV1) continue;
  59134. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  59135. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  59136. cliEdCertFile, edCertFile, edKeyFile,
  59137. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  59138. TEST_SUCCESS);
  59139. }
  59140. #endif
  59141. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  59142. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  59143. return EXPECT_RESULT();
  59144. }
  59145. static int test_openssl_FIPS_drbg(void)
  59146. {
  59147. EXPECT_DECLS;
  59148. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  59149. DRBG_CTX* dctx = NULL;
  59150. byte data1[32], data2[32], zeroData[32];
  59151. byte testSeed[16];
  59152. size_t dlen = sizeof(data1);
  59153. int i;
  59154. XMEMSET(data1, 0, dlen);
  59155. XMEMSET(data2, 0, dlen);
  59156. XMEMSET(zeroData, 0, sizeof(zeroData));
  59157. for (i = 0; i < (int)sizeof(testSeed); i++) {
  59158. testSeed[i] = (byte)i;
  59159. }
  59160. ExpectNotNull(dctx = FIPS_get_default_drbg());
  59161. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  59162. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  59163. WOLFSSL_SUCCESS);
  59164. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  59165. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  59166. WOLFSSL_SUCCESS);
  59167. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  59168. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  59169. WOLFSSL_SUCCESS);
  59170. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  59171. WOLFSSL_SUCCESS);
  59172. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  59173. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  59174. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  59175. #ifndef HAVE_GLOBAL_RNG
  59176. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  59177. wolfSSL_FIPS_drbg_free(dctx);
  59178. #endif
  59179. #endif
  59180. return EXPECT_RESULT();
  59181. }
  59182. static int test_wolfSSL_FIPS_mode(void)
  59183. {
  59184. EXPECT_DECLS;
  59185. #if defined(OPENSSL_ALL)
  59186. #ifdef HAVE_FIPS
  59187. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  59188. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  59189. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  59190. #else
  59191. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  59192. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  59193. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  59194. #endif
  59195. #endif
  59196. return EXPECT_RESULT();
  59197. }
  59198. #ifdef WOLFSSL_DTLS
  59199. /* Prints out the current window */
  59200. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  59201. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  59202. int i;
  59203. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  59204. word32 b = w[i];
  59205. int j;
  59206. /* Prints out a 32 bit binary number in big endian order */
  59207. for (j = 0; j < 32; j++, b <<= 1) {
  59208. if (b & (((word32)1) << 31))
  59209. fprintf(stderr, "1");
  59210. else
  59211. fprintf(stderr, "0");
  59212. }
  59213. fprintf(stderr, " ");
  59214. }
  59215. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  59216. #else
  59217. (void)h;
  59218. (void)l;
  59219. (void)w;
  59220. #endif
  59221. }
  59222. /* a - cur_hi
  59223. * b - cur_lo
  59224. * c - next_hi
  59225. * d - next_lo
  59226. * e - window
  59227. * f - expected next_hi
  59228. * g - expected next_lo
  59229. * h - expected window[1]
  59230. * i - expected window[0]
  59231. */
  59232. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  59233. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  59234. DUW_TEST_print_window_binary((a), (b), (e)); \
  59235. ExpectIntEQ((c), (f)); \
  59236. ExpectIntEQ((d), (g)); \
  59237. ExpectIntEQ((e)[1], (h)); \
  59238. ExpectIntEQ((e)[0], (i)); \
  59239. } while (0)
  59240. static int test_wolfSSL_DtlsUpdateWindow(void)
  59241. {
  59242. EXPECT_DECLS;
  59243. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  59244. word32 next_lo = 0;
  59245. word16 next_hi = 0;
  59246. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  59247. fprintf(stderr, "\n");
  59248. #endif
  59249. XMEMSET(window, 0, sizeof window);
  59250. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  59251. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  59252. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  59253. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  59254. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  59255. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  59256. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  59257. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  59258. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  59259. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  59260. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  59261. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  59262. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  59263. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  59264. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  59265. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  59266. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  59267. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  59268. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  59269. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  59270. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  59271. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  59272. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  59273. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  59274. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  59275. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  59276. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  59277. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  59278. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  59279. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  59280. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  59281. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  59282. return EXPECT_RESULT();
  59283. }
  59284. #endif /* WOLFSSL_DTLS */
  59285. #ifdef WOLFSSL_DTLS
  59286. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  59287. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  59288. {
  59289. DtlsMsg* cur;
  59290. static byte msg[100];
  59291. static byte msgInit = 0;
  59292. if (!msgInit) {
  59293. int i;
  59294. for (i = 0; i < 100; i++)
  59295. msg[i] = i + 1;
  59296. msgInit = 1;
  59297. }
  59298. /* Sanitize test parameters */
  59299. if (len > sizeof(msg))
  59300. return -1;
  59301. if (f_offset + f_len > sizeof(msg))
  59302. return -1;
  59303. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  59304. if (ssl->dtls_rx_msg_list == NULL)
  59305. return -100;
  59306. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  59307. return -200;
  59308. if (cur->fragBucketListCount != f_count)
  59309. return -300;
  59310. if (cur->ready != ready)
  59311. return -400;
  59312. if (cur->bytesReceived != bytesReceived)
  59313. return -500;
  59314. if (ready) {
  59315. if (cur->fragBucketList != NULL)
  59316. return -600;
  59317. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  59318. return -700;
  59319. }
  59320. else {
  59321. DtlsFragBucket* fb;
  59322. if (cur->fragBucketList == NULL)
  59323. return -800;
  59324. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  59325. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  59326. return -900;
  59327. }
  59328. }
  59329. return 0;
  59330. }
  59331. static int test_wolfSSL_DTLS_fragment_buckets(void)
  59332. {
  59333. EXPECT_DECLS;
  59334. WOLFSSL ssl[1];
  59335. XMEMSET(ssl, 0, sizeof(*ssl));
  59336. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  59337. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  59338. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  59339. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  59340. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  59341. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  59342. /* Test all permutations of 3 regions */
  59343. /* 1 2 3 */
  59344. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  59345. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  59346. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  59347. /* 1 3 2 */
  59348. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  59349. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  59350. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  59351. /* 2 1 3 */
  59352. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  59353. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  59354. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  59355. /* 2 3 1 */
  59356. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  59357. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  59358. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  59359. /* 3 1 2 */
  59360. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  59361. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  59362. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  59363. /* 3 2 1 */
  59364. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  59365. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  59366. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  59367. /* Test overlapping regions */
  59368. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  59369. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  59370. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  59371. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  59372. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  59373. /* Test overlapping multiple regions */
  59374. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  59375. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  59376. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  59377. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  59378. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  59379. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  59380. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  59381. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  59382. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  59383. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  59384. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  59385. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  59386. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  59387. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  59388. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  59389. ssl->dtls_rx_msg_list = NULL;
  59390. ssl->dtls_rx_msg_list_sz = 0;
  59391. return EXPECT_RESULT();
  59392. }
  59393. #endif
  59394. #if !defined(NO_FILESYSTEM) && \
  59395. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59396. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59397. static int test_wolfSSL_dtls_stateless2(void)
  59398. {
  59399. EXPECT_DECLS;
  59400. WOLFSSL *ssl_c = NULL;
  59401. WOLFSSL *ssl_c2 = NULL;
  59402. WOLFSSL *ssl_s = NULL;
  59403. struct test_memio_ctx test_ctx;
  59404. WOLFSSL_CTX *ctx_c = NULL;
  59405. WOLFSSL_CTX *ctx_s = NULL;
  59406. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59407. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59408. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59409. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  59410. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59411. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59412. /* send CH */
  59413. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59414. (ssl_c2->error == WANT_READ));
  59415. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59416. (ssl_s->error == WANT_READ));
  59417. ExpectIntNE(test_ctx.c_len, 0);
  59418. /* consume HRR */
  59419. test_ctx.c_len = 0;
  59420. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59421. wolfSSL_free(ssl_c2);
  59422. wolfSSL_free(ssl_c);
  59423. wolfSSL_free(ssl_s);
  59424. wolfSSL_CTX_free(ctx_c);
  59425. wolfSSL_CTX_free(ctx_s);
  59426. return EXPECT_RESULT();
  59427. }
  59428. #ifdef HAVE_MAX_FRAGMENT
  59429. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  59430. {
  59431. EXPECT_DECLS;
  59432. WOLFSSL *ssl_c = NULL;
  59433. WOLFSSL *ssl_c2 = NULL;
  59434. WOLFSSL *ssl_s = NULL;
  59435. struct test_memio_ctx test_ctx;
  59436. WOLFSSL_CTX *ctx_c = NULL;
  59437. WOLFSSL_CTX *ctx_s = NULL;
  59438. word16 max_fragment = 0;
  59439. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59440. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59441. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59442. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  59443. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  59444. WOLFSSL_SUCCESS);
  59445. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59446. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59447. if (ssl_s != NULL) {
  59448. max_fragment = ssl_s->max_fragment;
  59449. }
  59450. /* send CH */
  59451. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59452. (ssl_c2->error == WANT_READ));
  59453. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59454. (ssl_s->error == WANT_READ));
  59455. /* CH without cookie shouldn't change state */
  59456. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  59457. ExpectIntNE(test_ctx.c_len, 0);
  59458. /* consume HRR from buffer */
  59459. test_ctx.c_len = 0;
  59460. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59461. wolfSSL_free(ssl_c2);
  59462. wolfSSL_free(ssl_c);
  59463. wolfSSL_free(ssl_s);
  59464. wolfSSL_CTX_free(ctx_c);
  59465. wolfSSL_CTX_free(ctx_s);
  59466. return EXPECT_RESULT();
  59467. }
  59468. #endif /* HAVE_MAX_FRAGMENT */
  59469. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  59470. #define ROUNDS_WITH_HVR 4
  59471. #define ROUNDS_WITHOUT_HVR 2
  59472. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  59473. static int buf_is_hvr(const byte *data, int len)
  59474. {
  59475. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  59476. return 0;
  59477. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  59478. }
  59479. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  59480. {
  59481. EXPECT_DECLS;
  59482. struct test_memio_ctx test_ctx;
  59483. WOLFSSL_CTX *ctx_c = NULL;
  59484. WOLFSSL_CTX *ctx_s = NULL;
  59485. WOLFSSL *ssl_c = NULL;
  59486. WOLFSSL *ssl_s = NULL;
  59487. WOLFSSL_SESSION *sess = NULL;
  59488. int round_trips;
  59489. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59490. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  59491. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59492. #ifdef HAVE_SESSION_TICKET
  59493. if (useticket) {
  59494. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  59495. }
  59496. #endif
  59497. round_trips = ROUNDS_WITH_HVR;
  59498. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  59499. &round_trips), 0);
  59500. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  59501. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59502. wolfSSL_shutdown(ssl_c);
  59503. wolfSSL_shutdown(ssl_s);
  59504. wolfSSL_free(ssl_c);
  59505. ssl_c = NULL;
  59506. wolfSSL_free(ssl_s);
  59507. ssl_s = NULL;
  59508. test_ctx.c_len = test_ctx.s_len = 0;
  59509. /* make resumption invalid */
  59510. if (bad && (sess != NULL)) {
  59511. if (useticket) {
  59512. #ifdef HAVE_SESSION_TICKET
  59513. if (sess->ticket != NULL) {
  59514. sess->ticket[0] = !sess->ticket[0];
  59515. }
  59516. #endif /* HAVE_SESSION_TICKET */
  59517. }
  59518. else {
  59519. sess->sessionID[0] = !sess->sessionID[0];
  59520. }
  59521. }
  59522. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59523. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59524. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  59525. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  59526. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  59527. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  59528. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59529. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59530. (ssl_c->error == WANT_READ));
  59531. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59532. (ssl_s->error == WANT_READ));
  59533. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  59534. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  59535. if (!useticket) {
  59536. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  59537. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  59538. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  59539. }
  59540. wolfSSL_SESSION_free(sess);
  59541. wolfSSL_free(ssl_c);
  59542. wolfSSL_free(ssl_s);
  59543. wolfSSL_CTX_free(ctx_c);
  59544. wolfSSL_CTX_free(ctx_s);
  59545. return EXPECT_RESULT();
  59546. }
  59547. static int test_wolfSSL_dtls_stateless_resume(void)
  59548. {
  59549. EXPECT_DECLS;
  59550. #ifdef HAVE_SESSION_TICKET
  59551. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  59552. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  59553. #endif /* HAVE_SESION_TICKET */
  59554. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  59555. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  59556. return EXPECT_RESULT();
  59557. }
  59558. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  59559. #if !defined(NO_OLD_TLS)
  59560. static int test_wolfSSL_dtls_stateless_downgrade(void)
  59561. {
  59562. EXPECT_DECLS;
  59563. WOLFSSL_CTX *ctx_c = NULL;
  59564. WOLFSSL_CTX *ctx_c2 = NULL;
  59565. WOLFSSL_CTX *ctx_s = NULL;
  59566. WOLFSSL *ssl_c = NULL;
  59567. WOLFSSL *ssl_c2 = NULL;
  59568. WOLFSSL *ssl_s = NULL;
  59569. struct test_memio_ctx test_ctx;
  59570. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59571. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59572. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59573. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  59574. WOLFSSL_SUCCESS);
  59575. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  59576. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  59577. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  59578. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  59579. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59580. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59581. /* send CH */
  59582. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59583. (ssl_c2->error == WANT_READ));
  59584. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59585. (ssl_s->error == WANT_READ));
  59586. ExpectIntNE(test_ctx.c_len, 0);
  59587. /* consume HRR */
  59588. test_ctx.c_len = 0;
  59589. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59590. wolfSSL_free(ssl_c2);
  59591. wolfSSL_free(ssl_c);
  59592. wolfSSL_free(ssl_s);
  59593. wolfSSL_CTX_free(ctx_c);
  59594. wolfSSL_CTX_free(ctx_c2);
  59595. wolfSSL_CTX_free(ctx_s);
  59596. return EXPECT_RESULT();
  59597. }
  59598. #endif /* !defined(NO_OLD_TLS) */
  59599. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59600. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  59601. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59602. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59603. static int test_WOLFSSL_dtls_version_alert(void)
  59604. {
  59605. EXPECT_DECLS;
  59606. struct test_memio_ctx test_ctx;
  59607. WOLFSSL_CTX *ctx_c = NULL;
  59608. WOLFSSL_CTX *ctx_s = NULL;
  59609. WOLFSSL *ssl_c = NULL;
  59610. WOLFSSL *ssl_s = NULL;
  59611. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59612. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59613. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  59614. /* client hello */
  59615. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59616. (ssl_c->error == WANT_READ));
  59617. /* hrr */
  59618. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59619. (ssl_s->error == WANT_READ));
  59620. /* client hello 1 */
  59621. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59622. (ssl_c->error == WANT_READ));
  59623. /* server hello */
  59624. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59625. (ssl_s->error == WANT_READ));
  59626. /* should fail */
  59627. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59628. (ssl_c->error == VERSION_ERROR));
  59629. /* shuould fail */
  59630. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59631. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  59632. wolfSSL_free(ssl_c);
  59633. wolfSSL_free(ssl_s);
  59634. wolfSSL_CTX_free(ctx_c);
  59635. wolfSSL_CTX_free(ctx_s);
  59636. return EXPECT_RESULT();
  59637. }
  59638. #else
  59639. static int test_WOLFSSL_dtls_version_alert(void)
  59640. {
  59641. return TEST_SKIPPED;
  59642. }
  59643. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  59644. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  59645. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  59646. */
  59647. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  59648. && defined(WOLFSSL_TLS13) && \
  59649. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  59650. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59651. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  59652. {
  59653. struct test_memio_ctx *test_ctx;
  59654. byte buf[2048];
  59655. int idx, sz;
  59656. word32 tmp;
  59657. int ret;
  59658. idx = 5; /* space for record header */
  59659. buf[idx] = session_ticket; /* type */
  59660. idx++;
  59661. tmp = OPAQUE32_LEN +
  59662. OPAQUE32_LEN +
  59663. OPAQUE8_LEN + nonceLength +
  59664. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  59665. c32to24(tmp, buf + idx);
  59666. idx += OPAQUE24_LEN;
  59667. c32toa((word32)12345, buf+idx); /* lifetime */
  59668. idx += OPAQUE32_LEN;
  59669. c32toa((word32)12345, buf+idx); /* add */
  59670. idx += OPAQUE32_LEN;
  59671. buf[idx] = nonceLength; /* nonce length */
  59672. idx++;
  59673. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  59674. idx += nonceLength;
  59675. tmp = 1; /* ticket len */
  59676. c16toa((word16)tmp, buf+idx);
  59677. idx += 2;
  59678. buf[idx] = 0xFF; /* ticket */
  59679. idx++;
  59680. tmp = 0; /* ext len */
  59681. c16toa((word16)tmp, buf+idx);
  59682. idx += 2;
  59683. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  59684. handshake, 0, 0, 0);
  59685. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  59686. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  59687. return !(ret == sz);
  59688. }
  59689. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  59690. {
  59691. int ret = 0;
  59692. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  59693. ret = -1;
  59694. }
  59695. else {
  59696. int i;
  59697. for (i = 0; i < len; i++) {
  59698. if (sess->ticketNonce.data[i] != len) {
  59699. ret = -1;
  59700. break;
  59701. }
  59702. }
  59703. }
  59704. return ret;
  59705. }
  59706. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  59707. {
  59708. EXPECT_DECLS;
  59709. char *buf[1024];
  59710. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  59711. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  59712. (ssl_c->error == WANT_READ));
  59713. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  59714. return EXPECT_RESULT();
  59715. }
  59716. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  59717. {
  59718. EXPECT_DECLS;
  59719. WOLFSSL_SESSION *sess = NULL;
  59720. WOLFSSL_SESSION *cached = NULL;
  59721. WOLFSSL_CTX *ctx = ssl_c->ctx;
  59722. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  59723. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59724. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  59725. NULL, ssl_c->options.side, 1,NULL), 0);
  59726. ExpectNotNull(cached = wolfSSL_SESSION_new());
  59727. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  59728. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  59729. wolfSSL_SESSION_free(cached);
  59730. wolfSSL_SESSION_free(sess);
  59731. return EXPECT_RESULT();
  59732. }
  59733. static int test_ticket_nonce_malloc(void)
  59734. {
  59735. EXPECT_DECLS;
  59736. struct test_memio_ctx test_ctx;
  59737. WOLFSSL_CTX *ctx_c = NULL;
  59738. WOLFSSL_CTX *ctx_s = NULL;
  59739. WOLFSSL *ssl_c = NULL;
  59740. WOLFSSL *ssl_s = NULL;
  59741. byte small;
  59742. byte medium;
  59743. byte big;
  59744. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59745. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59746. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59747. /* will send ticket manually */
  59748. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  59749. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  59750. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  59751. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  59752. (ssl_s->options.handShakeDone == 0)) {
  59753. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  59754. (ssl_c->error == WANT_READ));
  59755. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  59756. (ssl_s->error == WANT_READ));
  59757. }
  59758. small = TLS13_TICKET_NONCE_STATIC_SZ;
  59759. medium = small + 20 <= 255 ? small + 20 : 255;
  59760. big = medium + 20 <= 255 ? small + 20 : 255;
  59761. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  59762. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  59763. ssl_c->session->ticketNonce.dataStatic);
  59764. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  59765. TEST_SUCCESS);
  59766. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  59767. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  59768. TEST_SUCCESS);
  59769. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  59770. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  59771. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  59772. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  59773. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  59774. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  59775. wolfSSL_free(ssl_c);
  59776. wolfSSL_free(ssl_s);
  59777. wolfSSL_CTX_free(ctx_c);
  59778. wolfSSL_CTX_free(ctx_s);
  59779. return EXPECT_RESULT();
  59780. }
  59781. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  59782. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  59783. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  59784. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59785. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  59786. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59787. static int test_ticket_ret_create(void)
  59788. {
  59789. EXPECT_DECLS;
  59790. WOLFSSL_CTX *ctx_c = NULL;
  59791. WOLFSSL_CTX *ctx_s = NULL;
  59792. WOLFSSL *ssl_c = NULL;
  59793. WOLFSSL *ssl_s = NULL;
  59794. byte ticket[SESSION_TICKET_LEN];
  59795. struct test_memio_ctx test_ctx;
  59796. WOLFSSL_SESSION *sess = NULL;
  59797. word16 ticketLen = 0;
  59798. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59799. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59800. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59801. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  59802. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  59803. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  59804. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59805. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59806. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  59807. if (sess != NULL) {
  59808. ticketLen = sess->ticketLen;
  59809. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  59810. }
  59811. wolfSSL_free(ssl_c);
  59812. ssl_c = NULL;
  59813. wolfSSL_free(ssl_s);
  59814. ssl_s = NULL;
  59815. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59816. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  59817. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  59818. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59819. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  59820. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  59821. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59822. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59823. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  59824. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  59825. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  59826. wolfSSL_SESSION_free(sess);
  59827. wolfSSL_free(ssl_c);
  59828. wolfSSL_free(ssl_s);
  59829. wolfSSL_CTX_free(ctx_c);
  59830. wolfSSL_CTX_free(ctx_s);
  59831. return EXPECT_RESULT();
  59832. }
  59833. #else
  59834. static int test_ticket_ret_create(void)
  59835. {
  59836. return TEST_SKIPPED;
  59837. }
  59838. #endif
  59839. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  59840. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  59841. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59842. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59843. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  59844. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  59845. {
  59846. int ret;
  59847. WOLFSSL_SESSION* session = NULL;
  59848. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  59849. if (!wolfSSL_is_server(ssl)) {
  59850. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  59851. AssertNotNull(session);
  59852. }
  59853. do {
  59854. ret = wolfSSL_shutdown(ssl);
  59855. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  59856. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  59857. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  59858. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  59859. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  59860. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  59861. #endif
  59862. if (!wolfSSL_is_server(ssl)) {
  59863. /* client */
  59864. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59865. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59866. wolfSSL_set_session(ssl, session);
  59867. wolfSSL_SESSION_free(session);
  59868. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  59869. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  59870. }
  59871. else {
  59872. /* server */
  59873. /* Different ciphersuite so that the ticket will be invalidated based on
  59874. * the ciphersuite */
  59875. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  59876. WOLFSSL_SUCCESS);
  59877. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  59878. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59879. }
  59880. }
  59881. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  59882. {
  59883. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  59884. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59885. WOLFSSL_SUCCESS);
  59886. }
  59887. static int test_ticket_and_psk_mixing(void)
  59888. {
  59889. EXPECT_DECLS;
  59890. /* Test mixing tickets and regular PSK */
  59891. callback_functions client_cbs, server_cbs;
  59892. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59893. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59894. client_cbs.method = wolfTLSv1_3_client_method;
  59895. server_cbs.method = wolfTLSv1_3_server_method;
  59896. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  59897. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  59898. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  59899. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59900. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59901. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59902. return EXPECT_RESULT();
  59903. }
  59904. #else
  59905. static int test_ticket_and_psk_mixing(void)
  59906. {
  59907. return TEST_SKIPPED;
  59908. }
  59909. #endif
  59910. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  59911. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  59912. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59913. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  59914. static int test_prioritize_psk_cb_called = FALSE;
  59915. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  59916. const char* identity, unsigned char* key, unsigned int key_max_len,
  59917. const char** ciphersuite)
  59918. {
  59919. test_prioritize_psk_cb_called = TRUE;
  59920. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  59921. }
  59922. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  59923. {
  59924. int ret;
  59925. WOLFSSL_SESSION* session = NULL;
  59926. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  59927. if (!wolfSSL_is_server(ssl)) {
  59928. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  59929. AssertNotNull(session);
  59930. }
  59931. do {
  59932. ret = wolfSSL_shutdown(ssl);
  59933. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  59934. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  59935. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  59936. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  59937. * important. */
  59938. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59939. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59940. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  59941. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  59942. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  59943. #endif
  59944. if (!wolfSSL_is_server(ssl)) {
  59945. /* client */
  59946. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  59947. wolfSSL_set_session(ssl, session);
  59948. wolfSSL_SESSION_free(session);
  59949. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  59950. }
  59951. else {
  59952. /* server */
  59953. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  59954. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59955. #ifdef WOLFSSL_PRIORITIZE_PSK
  59956. /* The ticket should be first tried with all ciphersuites and chosen */
  59957. AssertFalse(test_prioritize_psk_cb_called);
  59958. #else
  59959. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  59960. * callback that sets this var. */
  59961. AssertTrue(test_prioritize_psk_cb_called);
  59962. #endif
  59963. }
  59964. }
  59965. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  59966. {
  59967. if (!wolfSSL_is_server(ssl))
  59968. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59969. WOLFSSL_SUCCESS);
  59970. else
  59971. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59972. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59973. }
  59974. static int test_prioritize_psk(void)
  59975. {
  59976. EXPECT_DECLS;
  59977. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  59978. * of the PSK's will be followed instead of the ciphersuite. */
  59979. callback_functions client_cbs, server_cbs;
  59980. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59981. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59982. client_cbs.method = wolfTLSv1_3_client_method;
  59983. server_cbs.method = wolfTLSv1_3_server_method;
  59984. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59985. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59986. client_cbs.on_result = test_prioritize_psk_on_result;
  59987. server_cbs.on_result = test_prioritize_psk_on_result;
  59988. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59989. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59990. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59991. return EXPECT_RESULT();
  59992. }
  59993. #else
  59994. static int test_prioritize_psk(void)
  59995. {
  59996. return TEST_SKIPPED;
  59997. }
  59998. #endif
  59999. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  60000. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  60001. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  60002. !defined(WOLFSSL_NO_TLS12)
  60003. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  60004. {
  60005. EXPECT_DECLS;
  60006. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  60007. /* Set TLS 1.3 specific suite */
  60008. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  60009. return EXPECT_RESULT();
  60010. }
  60011. static int test_wolfSSL_CTX_set_ciphersuites(void)
  60012. {
  60013. EXPECT_DECLS;
  60014. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  60015. * do a 1.2 connection. */
  60016. test_ssl_cbf client_cbs, server_cbs;
  60017. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60018. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60019. client_cbs.method = wolfTLSv1_2_client_method;
  60020. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  60021. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  60022. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60023. &server_cbs, NULL), TEST_SUCCESS);
  60024. return EXPECT_RESULT();
  60025. }
  60026. #else
  60027. static int test_wolfSSL_CTX_set_ciphersuites(void)
  60028. {
  60029. return TEST_SKIPPED;
  60030. }
  60031. #endif
  60032. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  60033. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60034. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  60035. {
  60036. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  60037. return TEST_SUCCESS;
  60038. }
  60039. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  60040. {
  60041. EXPECT_DECLS;
  60042. WOLFSSL_ALERT_HISTORY h;
  60043. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  60044. ExpectIntEQ(h.last_rx.level, alert_fatal);
  60045. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  60046. return EXPECT_RESULT();
  60047. }
  60048. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  60049. {
  60050. EXPECT_DECLS;
  60051. test_ssl_cbf client_cbs, server_cbs;
  60052. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60053. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60054. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  60055. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  60056. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  60057. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  60058. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  60059. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60060. &server_cbs, NULL), TEST_FAIL);
  60061. return EXPECT_RESULT();
  60062. }
  60063. #else
  60064. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  60065. {
  60066. return TEST_SKIPPED;
  60067. }
  60068. #endif
  60069. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  60070. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  60071. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  60072. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  60073. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  60074. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  60075. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  60076. static int test_TLS_13_ticket_different_ciphers_run = 0;
  60077. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  60078. {
  60079. EXPECT_DECLS;
  60080. switch (test_TLS_13_ticket_different_ciphers_run) {
  60081. case 0:
  60082. /* First run */
  60083. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  60084. WOLFSSL_SUCCESS);
  60085. if (wolfSSL_is_server(ssl)) {
  60086. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  60087. wolfSSL_get_SSL_CTX(ssl));
  60088. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  60089. test_TLS_13_ticket_different_ciphers_ctx));
  60090. }
  60091. break;
  60092. case 1:
  60093. /* Second run */
  60094. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  60095. "TLS13-AES128-GCM-SHA256"),
  60096. WOLFSSL_SUCCESS);
  60097. if (!wolfSSL_is_server(ssl)) {
  60098. ExpectIntEQ(wolfSSL_set_session(ssl,
  60099. test_TLS_13_ticket_different_ciphers_session),
  60100. WOLFSSL_SUCCESS);
  60101. }
  60102. break;
  60103. default:
  60104. /* Bad state? */
  60105. Fail(("Should not enter here"), ("Should not enter here"));
  60106. }
  60107. return EXPECT_RESULT();
  60108. }
  60109. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  60110. {
  60111. EXPECT_DECLS;
  60112. switch (test_TLS_13_ticket_different_ciphers_run) {
  60113. case 0:
  60114. /* First run */
  60115. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  60116. wolfSSL_get1_session(ssl));
  60117. break;
  60118. case 1:
  60119. /* Second run */
  60120. ExpectTrue(wolfSSL_session_reused(ssl));
  60121. break;
  60122. default:
  60123. /* Bad state? */
  60124. Fail(("Should not enter here"), ("Should not enter here"));
  60125. }
  60126. return EXPECT_RESULT();
  60127. }
  60128. static int test_TLS_13_ticket_different_ciphers(void)
  60129. {
  60130. EXPECT_DECLS;
  60131. /* Check that we handle the connection when the ticket doesn't match
  60132. * the first ciphersuite. */
  60133. test_ssl_cbf client_cbs, server_cbs;
  60134. struct test_params {
  60135. method_provider client_meth;
  60136. method_provider server_meth;
  60137. int doUdp;
  60138. } params[] = {
  60139. #ifdef WOLFSSL_DTLS13
  60140. /* Test that the stateless code handles sessions correctly */
  60141. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  60142. #endif
  60143. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  60144. };
  60145. size_t i;
  60146. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  60147. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60148. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60149. test_TLS_13_ticket_different_ciphers_run = 0;
  60150. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  60151. client_cbs.method = params[i].client_meth;
  60152. server_cbs.method = params[i].server_meth;
  60153. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  60154. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  60155. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  60156. server_cbs.ticNoInit = 1;
  60157. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60158. &server_cbs, NULL), TEST_SUCCESS);
  60159. test_TLS_13_ticket_different_ciphers_run++;
  60160. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  60161. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60162. &server_cbs, NULL), TEST_SUCCESS);
  60163. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  60164. test_TLS_13_ticket_different_ciphers_session = NULL;
  60165. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  60166. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  60167. }
  60168. return EXPECT_RESULT();
  60169. }
  60170. #else
  60171. static int test_TLS_13_ticket_different_ciphers(void)
  60172. {
  60173. return TEST_SKIPPED;
  60174. }
  60175. #endif
  60176. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  60177. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  60178. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  60179. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  60180. byte test_extra_alerts_wrong_cs_sh[] = {
  60181. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  60182. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  60183. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  60184. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  60185. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  60186. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  60187. 0xbb, 0x07, 0x54, 0x1c,
  60188. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  60189. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  60190. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  60191. };
  60192. static int test_extra_alerts_wrong_cs(void)
  60193. {
  60194. EXPECT_DECLS;
  60195. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  60196. struct test_memio_ctx test_ctx;
  60197. WOLFSSL_CTX *ctx_c = NULL;
  60198. WOLFSSL_ALERT_HISTORY h;
  60199. WOLFSSL *ssl_c = NULL;
  60200. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60201. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60202. wolfTLSv1_2_client_method, NULL), 0);
  60203. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  60204. WOLFSSL_SUCCESS);
  60205. /* CH */
  60206. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60207. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60208. WOLFSSL_ERROR_WANT_READ);
  60209. /* consume CH */
  60210. test_ctx.s_len = 0;
  60211. /* inject SH */
  60212. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  60213. sizeof(test_extra_alerts_wrong_cs_sh));
  60214. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  60215. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60216. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60217. WOLFSSL_ERROR_WANT_READ);
  60218. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60219. ExpectIntEQ(h.last_tx.code, handshake_failure);
  60220. ExpectIntEQ(h.last_tx.level, alert_fatal);
  60221. wolfSSL_free(ssl_c);
  60222. wolfSSL_CTX_free(ctx_c);
  60223. #endif
  60224. return EXPECT_RESULT();
  60225. }
  60226. #else
  60227. static int test_extra_alerts_wrong_cs(void)
  60228. {
  60229. return TEST_SKIPPED;
  60230. }
  60231. #endif
  60232. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  60233. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  60234. #define TEST_CS_DOWNGRADE_CLIENT "ECDHE-RSA-AES256-GCM-SHA384"
  60235. byte test_wrong_cs_downgrade_sh[] = {
  60236. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0x10,
  60237. 0x2c, 0x88, 0xd9, 0x7a, 0x23, 0xc9, 0xbd, 0x11, 0x3b, 0x64, 0x24, 0xab,
  60238. 0x5b, 0x45, 0x33, 0xf6, 0x2c, 0x34, 0xe4, 0xcf, 0xf4, 0x78, 0xc8, 0x62,
  60239. 0x06, 0xc7, 0xe5, 0x30, 0x39, 0xbf, 0xa1, 0x20, 0xa3, 0x06, 0x74, 0xc3,
  60240. 0xa9, 0x74, 0x52, 0x8a, 0xfb, 0xae, 0xf0, 0xd8, 0x6f, 0xb2, 0x9d, 0xfe,
  60241. 0x78, 0xf0, 0x3f, 0x51, 0x8f, 0x9c, 0xcf, 0xbe, 0x61, 0x43, 0x9d, 0xf8,
  60242. 0x85, 0xe5, 0x2f, 0x54,
  60243. 0xc0, 0x2f, /* ECDHE-RSA-AES128-GCM-SHA256 */
  60244. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  60245. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  60246. };
  60247. static int test_wrong_cs_downgrade(void)
  60248. {
  60249. EXPECT_DECLS;
  60250. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  60251. struct test_memio_ctx test_ctx;
  60252. WOLFSSL_CTX *ctx_c = NULL;
  60253. WOLFSSL *ssl_c = NULL;
  60254. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60255. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60256. wolfSSLv23_client_method, NULL), 0);
  60257. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_CS_DOWNGRADE_CLIENT),
  60258. WOLFSSL_SUCCESS);
  60259. /* CH */
  60260. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60261. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60262. WOLFSSL_ERROR_WANT_READ);
  60263. /* consume CH */
  60264. test_ctx.s_len = 0;
  60265. /* inject SH */
  60266. XMEMCPY(test_ctx.c_buff, test_wrong_cs_downgrade_sh,
  60267. sizeof(test_wrong_cs_downgrade_sh));
  60268. test_ctx.c_len = sizeof(test_wrong_cs_downgrade_sh);
  60269. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60270. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60271. MATCH_SUITE_ERROR);
  60272. wolfSSL_free(ssl_c);
  60273. wolfSSL_CTX_free(ctx_c);
  60274. #endif
  60275. return EXPECT_RESULT();
  60276. }
  60277. #else
  60278. static int test_wrong_cs_downgrade(void)
  60279. {
  60280. return TEST_SKIPPED;
  60281. }
  60282. #endif
  60283. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  60284. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  60285. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  60286. {
  60287. tail_len -= msg_length;
  60288. XMEMMOVE(msg, msg + msg_length, tail_len);
  60289. *len = *len - msg_length;
  60290. }
  60291. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  60292. byte *found)
  60293. {
  60294. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  60295. const unsigned int _RECORD_HEADER_SZ = 5;
  60296. const int _change_cipher_hs = 55;
  60297. const int _change_cipher = 20;
  60298. const int _handshake = 22;
  60299. unsigned int tail_len;
  60300. byte *idx, *curr;
  60301. word8 currType;
  60302. word16 rLength;
  60303. word32 hLength;
  60304. idx = buf;
  60305. tail_len = (unsigned int)*len;
  60306. *found = 0;
  60307. while (tail_len > _RECORD_HEADER_SZ) {
  60308. curr = idx;
  60309. currType = *idx;
  60310. ato16(idx + 3, &rLength);
  60311. idx += _RECORD_HEADER_SZ;
  60312. tail_len -= _RECORD_HEADER_SZ;
  60313. if (tail_len < rLength)
  60314. return -1;
  60315. if (type == _change_cipher_hs && currType == _change_cipher) {
  60316. if (rLength != 1)
  60317. return -1;
  60318. /* match */
  60319. test_remove_msg(curr, *len - (int)(curr - buf),
  60320. len, _RECORD_HEADER_SZ + 1);
  60321. *found = 1;
  60322. return 0;
  60323. }
  60324. if (currType != _handshake) {
  60325. idx += rLength;
  60326. tail_len -= rLength;
  60327. continue;
  60328. }
  60329. if (rLength < _HANDSHAKE_HEADER_SZ)
  60330. return -1;
  60331. currType = *idx;
  60332. ato24(idx+1, &hLength);
  60333. hLength += _HANDSHAKE_HEADER_SZ;
  60334. if (tail_len < hLength)
  60335. return -1;
  60336. if (currType != type) {
  60337. idx += hLength;
  60338. tail_len -= hLength;
  60339. continue;
  60340. }
  60341. /* match */
  60342. test_remove_msg(curr, *len - (int)(curr - buf), len,
  60343. hLength + _RECORD_HEADER_SZ);
  60344. *found = 1;
  60345. return 0;
  60346. }
  60347. /* not found */
  60348. return 0;
  60349. }
  60350. static int test_remove_hs_message(byte hs_message_type,
  60351. int extra_round, byte alert_type)
  60352. {
  60353. EXPECT_DECLS;
  60354. WOLFSSL_CTX *ctx_c = NULL;
  60355. WOLFSSL_CTX *ctx_s = NULL;
  60356. WOLFSSL *ssl_c = NULL;
  60357. WOLFSSL *ssl_s = NULL;
  60358. struct test_memio_ctx test_ctx;
  60359. WOLFSSL_ALERT_HISTORY h;
  60360. byte found = 0;
  60361. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60362. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60363. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60364. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60365. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60366. WOLFSSL_ERROR_WANT_READ);
  60367. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60368. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60369. WOLFSSL_ERROR_WANT_READ);
  60370. if (extra_round) {
  60371. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60372. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60373. WOLFSSL_ERROR_WANT_READ);
  60374. /* this will complete handshake from server side */
  60375. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60376. }
  60377. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  60378. &test_ctx.c_len, hs_message_type, &found), 0);
  60379. if (!found) {
  60380. wolfSSL_free(ssl_c);
  60381. wolfSSL_CTX_free(ctx_c);
  60382. wolfSSL_free(ssl_s);
  60383. wolfSSL_CTX_free(ctx_s);
  60384. return TEST_SKIPPED;
  60385. }
  60386. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60387. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60388. WOLFSSL_ERROR_WANT_READ);
  60389. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60390. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  60391. ExpectIntEQ(h.last_tx.level, alert_fatal);
  60392. wolfSSL_free(ssl_c);
  60393. wolfSSL_CTX_free(ctx_c);
  60394. wolfSSL_free(ssl_s);
  60395. wolfSSL_CTX_free(ctx_s);
  60396. return EXPECT_RESULT();
  60397. }
  60398. static int test_extra_alerts_skip_hs(void)
  60399. {
  60400. EXPECT_DECLS;
  60401. const byte _server_key_exchange = 12;
  60402. const byte _server_hello = 2;
  60403. const byte _certificate = 11;
  60404. /* server_hello */
  60405. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  60406. unexpected_message), TEST_FAIL);
  60407. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  60408. 0xff), TEST_FAIL);
  60409. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  60410. unexpected_message), TEST_FAIL);
  60411. return EXPECT_RESULT();
  60412. }
  60413. #else
  60414. static int test_extra_alerts_skip_hs(void)
  60415. {
  60416. return TEST_SKIPPED;
  60417. }
  60418. #endif
  60419. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  60420. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  60421. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  60422. unsigned char* key, unsigned int key_max_len)
  60423. {
  60424. (void)ssl;
  60425. (void)id;
  60426. (void)key_max_len;
  60427. /* zero means error */
  60428. key[0] = 0x10;
  60429. return 1;
  60430. }
  60431. static int test_extra_alerts_bad_psk(void)
  60432. {
  60433. EXPECT_DECLS;
  60434. WOLFSSL_CTX *ctx_c = NULL;
  60435. WOLFSSL_CTX *ctx_s = NULL;
  60436. WOLFSSL *ssl_c = NULL;
  60437. WOLFSSL *ssl_s = NULL;
  60438. struct test_memio_ctx test_ctx;
  60439. WOLFSSL_ALERT_HISTORY h;
  60440. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60441. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60442. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  60443. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  60444. WOLFSSL_SUCCESS);
  60445. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  60446. WOLFSSL_SUCCESS);
  60447. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  60448. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60449. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60450. WOLFSSL_ERROR_WANT_READ);
  60451. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60452. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60453. WOLFSSL_ERROR_WANT_READ);
  60454. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60455. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60456. WOLFSSL_ERROR_WANT_READ);
  60457. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60458. ExpectIntEQ(h.last_tx.code, handshake_failure);
  60459. ExpectIntEQ(h.last_tx.level, alert_fatal);
  60460. wolfSSL_free(ssl_c);
  60461. wolfSSL_CTX_free(ctx_c);
  60462. wolfSSL_free(ssl_s);
  60463. wolfSSL_CTX_free(ctx_s);
  60464. return EXPECT_RESULT();
  60465. }
  60466. #else
  60467. static int test_extra_alerts_bad_psk(void)
  60468. {
  60469. return TEST_SKIPPED;
  60470. }
  60471. #endif
  60472. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  60473. && !defined(NO_PSK)
  60474. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  60475. const char* hint, char* identity, unsigned int id_max_len,
  60476. unsigned char* key, unsigned int key_max_len)
  60477. {
  60478. (void)ssl;
  60479. (void)hint;
  60480. (void)key_max_len;
  60481. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  60482. XSTRNCPY(identity, "Client_identity", id_max_len);
  60483. key[0] = 0x20;
  60484. return 1;
  60485. }
  60486. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  60487. const char* id, unsigned char* key, unsigned int key_max_len)
  60488. {
  60489. (void)ssl;
  60490. (void)id;
  60491. (void)key_max_len;
  60492. /* zero means error */
  60493. key[0] = 0x10;
  60494. return 1;
  60495. }
  60496. #endif
  60497. static int test_tls13_bad_psk_binder(void)
  60498. {
  60499. EXPECT_DECLS;
  60500. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  60501. && !defined(NO_PSK)
  60502. WOLFSSL_CTX *ctx_c = NULL;
  60503. WOLFSSL_CTX *ctx_s = NULL;
  60504. WOLFSSL *ssl_c = NULL;
  60505. WOLFSSL *ssl_s = NULL;
  60506. struct test_memio_ctx test_ctx;
  60507. WOLFSSL_ALERT_HISTORY h;
  60508. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60509. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60510. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  60511. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  60512. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  60513. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60514. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60515. WOLFSSL_ERROR_WANT_READ);
  60516. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60517. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60518. BAD_BINDER);
  60519. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60520. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60521. FATAL_ERROR);
  60522. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  60523. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  60524. ExpectIntEQ(h.last_rx.level, alert_fatal);
  60525. wolfSSL_free(ssl_c);
  60526. wolfSSL_CTX_free(ctx_c);
  60527. wolfSSL_free(ssl_s);
  60528. wolfSSL_CTX_free(ctx_s);
  60529. #endif
  60530. return EXPECT_RESULT();
  60531. }
  60532. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  60533. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60534. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  60535. int sz, void *ctx)
  60536. {
  60537. static int sentServerHello = FALSE;
  60538. if (!sentServerHello) {
  60539. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  60540. size_t i;
  60541. if (sz < (int)sizeof(renegExt))
  60542. return WOLFSSL_CBIO_ERR_GENERAL;
  60543. /* Remove SCR from ServerHello */
  60544. for (i = 0; i < sz - sizeof(renegExt); i++) {
  60545. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  60546. /* Found the extension. Change it to something unrecognized. */
  60547. buf[i+1] = 0x11;
  60548. break;
  60549. }
  60550. }
  60551. sentServerHello = TRUE;
  60552. }
  60553. return EmbedSend(ssl, buf, sz, ctx);
  60554. }
  60555. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  60556. {
  60557. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  60558. }
  60559. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  60560. {
  60561. WOLFSSL_ALERT_HISTORY h;
  60562. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  60563. AssertIntEQ(h.last_rx.code, handshake_failure);
  60564. AssertIntEQ(h.last_rx.level, alert_fatal);
  60565. }
  60566. static int test_harden_no_secure_renegotiation(void)
  60567. {
  60568. EXPECT_DECLS;
  60569. callback_functions client_cbs, server_cbs;
  60570. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60571. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60572. client_cbs.method = wolfTLSv1_2_client_method;
  60573. server_cbs.method = wolfTLSv1_2_server_method;
  60574. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  60575. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  60576. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  60577. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  60578. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  60579. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  60580. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  60581. server_cbs.last_err == FATAL_ERROR);
  60582. return EXPECT_RESULT();
  60583. }
  60584. #else
  60585. static int test_harden_no_secure_renegotiation(void)
  60586. {
  60587. return TEST_SKIPPED;
  60588. }
  60589. #endif
  60590. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60591. static int test_override_alt_cert_chain_cert_cb(int preverify,
  60592. WOLFSSL_X509_STORE_CTX* store)
  60593. {
  60594. fprintf(stderr, "preverify: %d\n", preverify);
  60595. fprintf(stderr, "store->error: %d\n", store->error);
  60596. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  60597. if (store->error == OCSP_INVALID_STATUS) {
  60598. fprintf(stderr, "Overriding OCSP error\n");
  60599. return 1;
  60600. }
  60601. #ifndef WOLFSSL_ALT_CERT_CHAINS
  60602. else if ((store->error == ASN_NO_SIGNER_E ||
  60603. store->error == ASN_SELF_SIGNED_E
  60604. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  60605. defined(HAVE_WEBSERVER)
  60606. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  60607. #endif
  60608. ) && store->error_depth == store->totalCerts - 1) {
  60609. fprintf(stderr, "Overriding no signer error only for root cert\n");
  60610. return 1;
  60611. }
  60612. #endif
  60613. else
  60614. return preverify;
  60615. }
  60616. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  60617. int urlSz, unsigned char* request, int requestSz,
  60618. unsigned char** response)
  60619. {
  60620. (void)ioCtx;
  60621. (void)url;
  60622. (void)urlSz;
  60623. (void)request;
  60624. (void)requestSz;
  60625. (void)response;
  60626. return -1;
  60627. }
  60628. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  60629. {
  60630. EXPECT_DECLS;
  60631. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  60632. test_override_alt_cert_chain_cert_cb);
  60633. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  60634. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  60635. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  60636. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  60637. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  60638. WOLFSSL_SUCCESS);
  60639. return EXPECT_RESULT();
  60640. }
  60641. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  60642. {
  60643. EXPECT_DECLS;
  60644. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  60645. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  60646. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  60647. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  60648. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  60649. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  60650. WOLFSSL_SUCCESS);
  60651. return EXPECT_RESULT();
  60652. }
  60653. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  60654. {
  60655. EXPECT_DECLS;
  60656. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  60657. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  60658. return EXPECT_RESULT();
  60659. }
  60660. static int test_override_alt_cert_chain(void)
  60661. {
  60662. EXPECT_DECLS;
  60663. size_t i;
  60664. struct test_params {
  60665. ctx_cb client_ctx_cb;
  60666. ctx_cb server_ctx_cb;
  60667. int result;
  60668. } params[] = {
  60669. {test_override_alt_cert_chain_client_ctx_ready,
  60670. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  60671. {test_override_alt_cert_chain_client_ctx_ready2,
  60672. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  60673. };
  60674. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  60675. test_ssl_cbf client_cbs, server_cbs;
  60676. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60677. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60678. fprintf(stderr, "test config: %d\n", (int)i);
  60679. client_cbs.ctx_ready = params[i].client_ctx_cb;
  60680. server_cbs.ctx_ready = params[i].server_ctx_cb;
  60681. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60682. &server_cbs, NULL), params[i].result);
  60683. ExpectIntEQ(client_cbs.return_code, params[i].result);
  60684. ExpectIntEQ(server_cbs.return_code, params[i].result);
  60685. }
  60686. return EXPECT_RESULT();
  60687. }
  60688. #else
  60689. static int test_override_alt_cert_chain(void)
  60690. {
  60691. return TEST_SKIPPED;
  60692. }
  60693. #endif
  60694. #if defined(HAVE_RPK)
  60695. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  60696. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  60697. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60698. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  60699. {
  60700. int ret = WOLFSSL_SUCCESS;
  60701. (void)mode;
  60702. (void)strctx;
  60703. WOLFSSL_ENTER("MyRpkVerifyCb");
  60704. return ret;
  60705. }
  60706. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60707. static WC_INLINE int test_rpk_memio_setup(
  60708. struct test_memio_ctx *ctx,
  60709. WOLFSSL_CTX **ctx_c,
  60710. WOLFSSL_CTX **ctx_s,
  60711. WOLFSSL **ssl_c,
  60712. WOLFSSL **ssl_s,
  60713. method_provider method_c,
  60714. method_provider method_s,
  60715. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  60716. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  60717. const char* pkey_c, int fmt_kc, /* client private key and format */
  60718. const char* pkey_s, int fmt_ks /* server private key and format */
  60719. )
  60720. {
  60721. int ret;
  60722. if (ctx_c != NULL && *ctx_c == NULL) {
  60723. *ctx_c = wolfSSL_CTX_new(method_c());
  60724. if (*ctx_c == NULL) {
  60725. return -1;
  60726. }
  60727. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  60728. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  60729. if (ret != WOLFSSL_SUCCESS) {
  60730. return -1;
  60731. }
  60732. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  60733. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  60734. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  60735. if (ret != WOLFSSL_SUCCESS) {
  60736. return -1;
  60737. }
  60738. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  60739. if (ret != WOLFSSL_SUCCESS) {
  60740. return -1;
  60741. }
  60742. }
  60743. if (ctx_s != NULL && *ctx_s == NULL) {
  60744. *ctx_s = wolfSSL_CTX_new(method_s());
  60745. if (*ctx_s == NULL) {
  60746. return -1;
  60747. }
  60748. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  60749. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  60750. if (ret != WOLFSSL_SUCCESS) {
  60751. return -1;
  60752. }
  60753. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  60754. if (ret != WOLFSSL_SUCCESS) {
  60755. return -1;
  60756. }
  60757. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  60758. if (ret != WOLFSSL_SUCCESS) {
  60759. return -1;
  60760. }
  60761. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  60762. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  60763. if (ctx->s_ciphers != NULL) {
  60764. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  60765. if (ret != WOLFSSL_SUCCESS) {
  60766. return -1;
  60767. }
  60768. }
  60769. }
  60770. if (ctx_c != NULL && ssl_c != NULL) {
  60771. *ssl_c = wolfSSL_new(*ctx_c);
  60772. if (*ssl_c == NULL) {
  60773. return -1;
  60774. }
  60775. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  60776. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  60777. }
  60778. if (ctx_s != NULL && ssl_s != NULL) {
  60779. *ssl_s = wolfSSL_new(*ctx_s);
  60780. if (*ssl_s == NULL) {
  60781. return -1;
  60782. }
  60783. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  60784. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  60785. #if !defined(NO_DH)
  60786. SetDH(*ssl_s);
  60787. #endif
  60788. }
  60789. return 0;
  60790. }
  60791. #endif /* HAVE_RPK */
  60792. static int test_rpk_set_xxx_cert_type(void)
  60793. {
  60794. EXPECT_DECLS;
  60795. #if defined(HAVE_RPK)
  60796. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  60797. WOLFSSL_CTX* ctx = NULL;
  60798. WOLFSSL* ssl = NULL;
  60799. int tp;
  60800. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  60801. ExpectNotNull(ctx);
  60802. ssl = wolfSSL_new(ctx);
  60803. ExpectNotNull(ssl);
  60804. /*--------------------------------------------*/
  60805. /* tests for wolfSSL_CTX_set_client_cert_type */
  60806. /*--------------------------------------------*/
  60807. /* illegal parameter test caces */
  60808. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  60809. MAX_CLIENT_CERT_TYPE_CNT),
  60810. BAD_FUNC_ARG);
  60811. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60812. sizeof(ctype)),
  60813. BAD_FUNC_ARG);
  60814. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60815. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60816. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60817. MAX_CLIENT_CERT_TYPE_CNT),
  60818. BAD_FUNC_ARG);
  60819. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60820. ctype[1] = 10; /* set unknown cert type */
  60821. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60822. MAX_CLIENT_CERT_TYPE_CNT),
  60823. BAD_FUNC_ARG);
  60824. /* pass larger type count */
  60825. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60826. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60827. ctype[2] = 1; /* pass unacceptable type count */
  60828. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60829. MAX_CLIENT_CERT_TYPE_CNT + 1),
  60830. BAD_FUNC_ARG);
  60831. /* should accept NULL for type buffer */
  60832. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  60833. MAX_CLIENT_CERT_TYPE_CNT),
  60834. WOLFSSL_SUCCESS);
  60835. /* should accept zero for type count */
  60836. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60837. 0),
  60838. WOLFSSL_SUCCESS);
  60839. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60840. MAX_CLIENT_CERT_TYPE_CNT),
  60841. WOLFSSL_SUCCESS);
  60842. /*--------------------------------------------*/
  60843. /* tests for wolfSSL_CTX_set_server_cert_type */
  60844. /*--------------------------------------------*/
  60845. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  60846. MAX_SERVER_CERT_TYPE_CNT),
  60847. BAD_FUNC_ARG);
  60848. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60849. sizeof(ctype)),
  60850. BAD_FUNC_ARG);
  60851. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60852. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60853. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60854. MAX_SERVER_CERT_TYPE_CNT),
  60855. BAD_FUNC_ARG);
  60856. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60857. ctype[1] = 10; /* set unknown cert type */
  60858. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60859. MAX_SERVER_CERT_TYPE_CNT),
  60860. BAD_FUNC_ARG);
  60861. /* pass larger type count */
  60862. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60863. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60864. ctype[2] = 1; /* pass unacceptable type count */
  60865. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60866. MAX_SERVER_CERT_TYPE_CNT + 1),
  60867. BAD_FUNC_ARG);
  60868. /* should accept NULL for type buffer */
  60869. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  60870. MAX_SERVER_CERT_TYPE_CNT),
  60871. WOLFSSL_SUCCESS);
  60872. /* should accept zero for type count */
  60873. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60874. 0),
  60875. WOLFSSL_SUCCESS);
  60876. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60877. MAX_CLIENT_CERT_TYPE_CNT),
  60878. WOLFSSL_SUCCESS);
  60879. /*--------------------------------------------*/
  60880. /* tests for wolfSSL_set_client_cert_type */
  60881. /*--------------------------------------------*/
  60882. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  60883. MAX_CLIENT_CERT_TYPE_CNT),
  60884. BAD_FUNC_ARG);
  60885. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60886. sizeof(ctype)),
  60887. BAD_FUNC_ARG);
  60888. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60889. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60890. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60891. MAX_CLIENT_CERT_TYPE_CNT),
  60892. BAD_FUNC_ARG);
  60893. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60894. ctype[1] = 10; /* set unknown cert type */
  60895. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60896. MAX_CLIENT_CERT_TYPE_CNT),
  60897. BAD_FUNC_ARG);
  60898. /* pass larger type count */
  60899. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60900. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60901. ctype[2] = 1; /* pass unacceptable type count */
  60902. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60903. MAX_CLIENT_CERT_TYPE_CNT + 1),
  60904. BAD_FUNC_ARG);
  60905. /* should accept NULL for type buffer */
  60906. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  60907. MAX_CLIENT_CERT_TYPE_CNT),
  60908. WOLFSSL_SUCCESS);
  60909. /* should accept zero for type count */
  60910. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60911. 0),
  60912. WOLFSSL_SUCCESS);
  60913. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60914. MAX_CLIENT_CERT_TYPE_CNT),
  60915. WOLFSSL_SUCCESS);
  60916. /*--------------------------------------------*/
  60917. /* tests for wolfSSL_CTX_set_server_cert_type */
  60918. /*--------------------------------------------*/
  60919. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  60920. MAX_SERVER_CERT_TYPE_CNT),
  60921. BAD_FUNC_ARG);
  60922. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60923. sizeof(ctype)),
  60924. BAD_FUNC_ARG);
  60925. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60926. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60927. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60928. MAX_SERVER_CERT_TYPE_CNT),
  60929. BAD_FUNC_ARG);
  60930. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60931. ctype[1] = 10; /* set unknown cert type */
  60932. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60933. MAX_SERVER_CERT_TYPE_CNT),
  60934. BAD_FUNC_ARG);
  60935. /* pass larger type count */
  60936. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60937. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60938. ctype[2] = 1; /* pass unacceptable type count */
  60939. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60940. MAX_SERVER_CERT_TYPE_CNT + 1),
  60941. BAD_FUNC_ARG);
  60942. /* should accept NULL for type buffer */
  60943. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  60944. MAX_SERVER_CERT_TYPE_CNT),
  60945. WOLFSSL_SUCCESS);
  60946. /* should accept zero for type count */
  60947. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60948. 0),
  60949. WOLFSSL_SUCCESS);
  60950. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60951. MAX_SERVER_CERT_TYPE_CNT),
  60952. WOLFSSL_SUCCESS);
  60953. /*------------------------------------------------*/
  60954. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  60955. /*------------------------------------------------*/
  60956. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  60957. BAD_FUNC_ARG);
  60958. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  60959. BAD_FUNC_ARG);
  60960. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  60961. BAD_FUNC_ARG);
  60962. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  60963. BAD_FUNC_ARG);
  60964. /* clean up */
  60965. wolfSSL_free(ssl);
  60966. wolfSSL_CTX_free(ctx);
  60967. #endif
  60968. return EXPECT_RESULT();
  60969. }
  60970. static int test_tls13_rpk_handshake(void)
  60971. {
  60972. EXPECT_DECLS;
  60973. #if defined(HAVE_RPK)
  60974. int ret = 0;
  60975. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60976. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60977. struct test_memio_ctx test_ctx;
  60978. int err;
  60979. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  60980. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  60981. int typeCnt_c;
  60982. int typeCnt_s;
  60983. int tp;
  60984. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60985. int isServer;
  60986. #endif
  60987. (void)err;
  60988. (void)typeCnt_c;
  60989. (void)typeCnt_s;
  60990. (void)certType_c;
  60991. (void)certType_s;
  60992. /* TLS1.2
  60993. * Both client and server load x509 cert and start handshaking.
  60994. * Check no negotiation occurred.
  60995. */
  60996. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60997. ExpectIntEQ(
  60998. test_rpk_memio_setup(
  60999. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61000. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61001. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61002. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61003. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61004. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  61005. , 0);
  61006. /* set client certificate type in client end */
  61007. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61008. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61009. typeCnt_c = 2;
  61010. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61011. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61012. typeCnt_s = 2;
  61013. /* both client and server do not call client/server_cert_type APIs,
  61014. * expecting default settings works and no negotiation performed.
  61015. */
  61016. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61017. return TEST_FAIL;
  61018. /* confirm no negotiation occurred */
  61019. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61020. WOLFSSL_SUCCESS);
  61021. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61022. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61023. WOLFSSL_SUCCESS);
  61024. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61025. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61026. WOLFSSL_SUCCESS);
  61027. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61028. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61029. WOLFSSL_SUCCESS);
  61030. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61031. (void)typeCnt_c;
  61032. (void)typeCnt_s;
  61033. wolfSSL_free(ssl_c);
  61034. wolfSSL_CTX_free(ctx_c);
  61035. wolfSSL_free(ssl_s);
  61036. wolfSSL_CTX_free(ctx_s);
  61037. ssl_c = ssl_s = NULL;
  61038. ctx_c = ctx_s = NULL;
  61039. /* Both client and server load x509 cert and start handshaking.
  61040. * Check no negotiation occurred.
  61041. */
  61042. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61043. ExpectIntEQ(
  61044. test_rpk_memio_setup(
  61045. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61046. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61047. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61048. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61049. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61050. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61051. , 0);
  61052. /* set client certificate type in client end */
  61053. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61054. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61055. typeCnt_c = 2;
  61056. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61057. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61058. typeCnt_s = 2;
  61059. /* both client and server do not call client/server_cert_type APIs,
  61060. * expecting default settings works and no negotiation performed.
  61061. */
  61062. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61063. return TEST_FAIL;
  61064. /* confirm no negotiation occurred */
  61065. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61066. WOLFSSL_SUCCESS);
  61067. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61068. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61069. WOLFSSL_SUCCESS);
  61070. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61071. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61072. WOLFSSL_SUCCESS);
  61073. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61074. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61075. WOLFSSL_SUCCESS);
  61076. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61077. (void)typeCnt_c;
  61078. (void)typeCnt_s;
  61079. wolfSSL_free(ssl_c);
  61080. wolfSSL_CTX_free(ctx_c);
  61081. wolfSSL_free(ssl_s);
  61082. wolfSSL_CTX_free(ctx_s);
  61083. ssl_c = ssl_s = NULL;
  61084. ctx_c = ctx_s = NULL;
  61085. /* Both client and server load RPK cert and start handshaking.
  61086. * Confirm negotiated cert types match as expected.
  61087. */
  61088. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61089. ExpectIntEQ(
  61090. test_rpk_memio_setup(
  61091. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61092. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61093. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61094. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61095. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61096. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61097. , 0);
  61098. /* set client certificate type in client end */
  61099. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61100. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61101. typeCnt_c = 2;
  61102. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61103. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61104. typeCnt_s = 2;
  61105. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61106. WOLFSSL_SUCCESS);
  61107. /* set server certificate type in client end */
  61108. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61109. WOLFSSL_SUCCESS);
  61110. /* set client certificate type in server end */
  61111. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61112. WOLFSSL_SUCCESS);
  61113. /* set server certificate type in server end */
  61114. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61115. WOLFSSL_SUCCESS);
  61116. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61117. return TEST_FAIL;
  61118. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61119. WOLFSSL_SUCCESS);
  61120. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61121. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61122. WOLFSSL_SUCCESS);
  61123. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61124. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61125. WOLFSSL_SUCCESS);
  61126. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61127. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61128. WOLFSSL_SUCCESS);
  61129. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61130. wolfSSL_free(ssl_c);
  61131. wolfSSL_CTX_free(ctx_c);
  61132. wolfSSL_free(ssl_s);
  61133. wolfSSL_CTX_free(ctx_s);
  61134. ssl_c = ssl_s = NULL;
  61135. ctx_c = ctx_s = NULL;
  61136. /* TLS1.2
  61137. * Both client and server load RPK cert and start handshaking.
  61138. * Confirm negotiated cert types match as expected.
  61139. */
  61140. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61141. ExpectIntEQ(
  61142. test_rpk_memio_setup(
  61143. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61144. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  61145. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61146. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61147. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61148. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61149. , 0);
  61150. /* set client certificate type in client end */
  61151. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61152. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61153. typeCnt_c = 2;
  61154. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61155. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61156. typeCnt_s = 2;
  61157. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61158. WOLFSSL_SUCCESS);
  61159. /* set server certificate type in client end */
  61160. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61161. WOLFSSL_SUCCESS);
  61162. /* set client certificate type in server end */
  61163. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61164. WOLFSSL_SUCCESS);
  61165. /* set server certificate type in server end */
  61166. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61167. WOLFSSL_SUCCESS);
  61168. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61169. return TEST_FAIL;
  61170. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61171. WOLFSSL_SUCCESS);
  61172. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61173. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61174. WOLFSSL_SUCCESS);
  61175. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61176. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61177. WOLFSSL_SUCCESS);
  61178. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61179. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61180. WOLFSSL_SUCCESS);
  61181. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61182. wolfSSL_free(ssl_c);
  61183. wolfSSL_CTX_free(ctx_c);
  61184. wolfSSL_free(ssl_s);
  61185. wolfSSL_CTX_free(ctx_s);
  61186. ssl_c = ssl_s = NULL;
  61187. ctx_c = ctx_s = NULL;
  61188. /* Both client and server load x509 cert.
  61189. * Have client call set_client_cert_type with both RPK and x509.
  61190. * This doesn't makes client add client cert type extension to ClientHello,
  61191. * since it does not load RPK cert actually.
  61192. */
  61193. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61194. ExpectIntEQ(
  61195. test_rpk_memio_setup(
  61196. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61197. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61198. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61199. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61200. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61201. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61202. , 0);
  61203. /* set client certificate type in client end
  61204. *
  61205. * client indicates both RPK and x509 certs are available but loaded RPK
  61206. * cert only. It does not have client add client-cert-type extension in CH.
  61207. */
  61208. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61209. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61210. typeCnt_c = 2;
  61211. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61212. WOLFSSL_SUCCESS);
  61213. /* client indicates both RPK and x509 certs are acceptable */
  61214. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61215. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61216. typeCnt_s = 2;
  61217. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61218. WOLFSSL_SUCCESS);
  61219. /* server indicates both RPK and x509 certs are acceptable */
  61220. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61221. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61222. typeCnt_c = 2;
  61223. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61224. WOLFSSL_SUCCESS);
  61225. /* server should indicate only RPK cert is available */
  61226. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61227. certType_s[1] = -1;
  61228. typeCnt_s = 1;
  61229. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61230. WOLFSSL_SUCCESS);
  61231. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61232. return TEST_FAIL;
  61233. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  61234. * be returned as cert type.
  61235. */
  61236. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61237. WOLFSSL_SUCCESS);
  61238. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61239. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61240. WOLFSSL_SUCCESS);
  61241. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61242. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61243. WOLFSSL_SUCCESS);
  61244. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61245. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61246. WOLFSSL_SUCCESS);
  61247. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61248. wolfSSL_free(ssl_c);
  61249. wolfSSL_CTX_free(ctx_c);
  61250. wolfSSL_free(ssl_s);
  61251. wolfSSL_CTX_free(ctx_s);
  61252. ssl_c = ssl_s = NULL;
  61253. ctx_c = ctx_s = NULL;
  61254. /* Have client load RPK cert and have server load x509 cert.
  61255. * Check the negotiation result from both ends.
  61256. */
  61257. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61258. ExpectIntEQ(
  61259. test_rpk_memio_setup(
  61260. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61261. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61262. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61263. svrCertFile, WOLFSSL_FILETYPE_PEM,
  61264. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61265. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61266. , 0);
  61267. /* have client tell to use RPK cert */
  61268. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61269. certType_c[1] = -1;
  61270. typeCnt_c = 1;
  61271. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61272. WOLFSSL_SUCCESS);
  61273. /* have client tell to accept both RPK and x509 cert */
  61274. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61275. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  61276. typeCnt_s = 2;
  61277. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61278. WOLFSSL_SUCCESS);
  61279. /* have server accept to both RPK and x509 cert */
  61280. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  61281. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  61282. typeCnt_c = 2;
  61283. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61284. WOLFSSL_SUCCESS);
  61285. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  61286. * end, expecting the default setting works.
  61287. */
  61288. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  61289. return TEST_FAIL;
  61290. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61291. WOLFSSL_SUCCESS);
  61292. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61293. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61294. WOLFSSL_SUCCESS);
  61295. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61296. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61297. WOLFSSL_SUCCESS);
  61298. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61299. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61300. WOLFSSL_SUCCESS);
  61301. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61302. wolfSSL_free(ssl_c);
  61303. wolfSSL_CTX_free(ctx_c);
  61304. wolfSSL_free(ssl_s);
  61305. wolfSSL_CTX_free(ctx_s);
  61306. ssl_c = ssl_s = NULL;
  61307. ctx_c = ctx_s = NULL;
  61308. /* Have both client and server load RPK cert, however, have server
  61309. * indicate its cert type x509.
  61310. * Client is expected to detect the cert type mismatch then to send alert
  61311. * with "unsupported_certificate".
  61312. */
  61313. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61314. ExpectIntEQ(
  61315. test_rpk_memio_setup(
  61316. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61317. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61318. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61319. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  61320. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61321. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61322. , 0);
  61323. /* have client tell to use RPK cert */
  61324. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61325. certType_c[1] = -1;
  61326. typeCnt_c = 1;
  61327. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61328. WOLFSSL_SUCCESS);
  61329. /* have client tell to accept both RPK and x509 cert */
  61330. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61331. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  61332. typeCnt_s = 2;
  61333. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61334. WOLFSSL_SUCCESS);
  61335. /* have server accept to both RPK and x509 cert */
  61336. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  61337. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  61338. typeCnt_c = 2;
  61339. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61340. WOLFSSL_SUCCESS);
  61341. /* have server tell to use x509 cert intentionally. This will bring
  61342. * certificate type mismatch in client side.
  61343. */
  61344. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61345. certType_s[1] = -1;
  61346. typeCnt_s = 1;
  61347. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61348. WOLFSSL_SUCCESS);
  61349. /* expect client detect cert type mismatch then send Alert */
  61350. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  61351. if (ret != -1)
  61352. return TEST_FAIL;
  61353. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  61354. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61355. WOLFSSL_SUCCESS);
  61356. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61357. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61358. WOLFSSL_SUCCESS);
  61359. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61360. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61361. WOLFSSL_SUCCESS);
  61362. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61363. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61364. WOLFSSL_SUCCESS);
  61365. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61366. wolfSSL_free(ssl_c);
  61367. wolfSSL_CTX_free(ctx_c);
  61368. wolfSSL_free(ssl_s);
  61369. wolfSSL_CTX_free(ctx_s);
  61370. ssl_c = ssl_s = NULL;
  61371. ctx_c = ctx_s = NULL;
  61372. /* Have client load x509 cert and server load RPK cert,
  61373. * however, have client indicate its cert type RPK.
  61374. * Server is expected to detect the cert type mismatch then to send alert
  61375. * with "unsupported_certificate".
  61376. */
  61377. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61378. ExpectIntEQ(
  61379. test_rpk_memio_setup(
  61380. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61381. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61382. cliCertFile, WOLFSSL_FILETYPE_PEM,
  61383. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61384. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61385. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61386. , 0);
  61387. /* have client tell to use RPK cert intentionally */
  61388. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61389. certType_c[1] = -1;
  61390. typeCnt_c = 1;
  61391. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61392. WOLFSSL_SUCCESS);
  61393. /* have client tell to accept both RPK and x509 cert */
  61394. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61395. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  61396. typeCnt_s = 2;
  61397. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61398. WOLFSSL_SUCCESS);
  61399. /* have server accept to both RPK and x509 cert */
  61400. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  61401. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  61402. typeCnt_c = 2;
  61403. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61404. WOLFSSL_SUCCESS);
  61405. /* have server tell to use x509 cert intentionally. This will bring
  61406. * certificate type mismatch in client side.
  61407. */
  61408. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  61409. certType_s[1] = -1;
  61410. typeCnt_s = 1;
  61411. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61412. WOLFSSL_SUCCESS);
  61413. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  61414. /* expect server detect cert type mismatch then send Alert */
  61415. ExpectIntNE(ret, 0);
  61416. err = wolfSSL_get_error(ssl_c, ret);
  61417. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  61418. /* client did not load RPK cert actually, so negotiation did not happen */
  61419. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61420. WOLFSSL_SUCCESS);
  61421. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61422. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61423. WOLFSSL_SUCCESS);
  61424. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61425. /* client did not load RPK cert actually, so negotiation did not happen */
  61426. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61427. WOLFSSL_SUCCESS);
  61428. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  61429. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61430. WOLFSSL_SUCCESS);
  61431. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  61432. wolfSSL_free(ssl_c);
  61433. wolfSSL_CTX_free(ctx_c);
  61434. wolfSSL_free(ssl_s);
  61435. wolfSSL_CTX_free(ctx_s);
  61436. ssl_c = ssl_s = NULL;
  61437. ctx_c = ctx_s = NULL;
  61438. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  61439. /* Both client and server load RPK cert and set certificate verify
  61440. * callbacks then start handshaking.
  61441. * Confirm both side can refer the peer's cert.
  61442. */
  61443. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61444. ExpectIntEQ(
  61445. test_rpk_memio_setup(
  61446. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61447. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  61448. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61449. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  61450. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  61451. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  61452. , 0);
  61453. /* set client certificate type in client end */
  61454. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  61455. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  61456. typeCnt_c = 2;
  61457. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  61458. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  61459. typeCnt_s = 2;
  61460. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  61461. WOLFSSL_SUCCESS);
  61462. /* set server certificate type in client end */
  61463. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  61464. WOLFSSL_SUCCESS);
  61465. /* set client certificate type in server end */
  61466. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  61467. WOLFSSL_SUCCESS);
  61468. /* set server certificate type in server end */
  61469. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  61470. WOLFSSL_SUCCESS);
  61471. /* set certificate verify callback to both client and server */
  61472. isServer = 0;
  61473. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  61474. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  61475. isServer = 1;
  61476. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  61477. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  61478. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  61479. if (ret != 0)
  61480. return TEST_FAIL;
  61481. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  61482. WOLFSSL_SUCCESS);
  61483. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61484. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  61485. WOLFSSL_SUCCESS);
  61486. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61487. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  61488. WOLFSSL_SUCCESS);
  61489. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61490. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  61491. WOLFSSL_SUCCESS);
  61492. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  61493. wolfSSL_free(ssl_c);
  61494. wolfSSL_CTX_free(ctx_c);
  61495. wolfSSL_free(ssl_s);
  61496. wolfSSL_CTX_free(ctx_s);
  61497. ssl_c = ssl_s = NULL;
  61498. ctx_c = ctx_s = NULL;
  61499. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  61500. #endif /* HAVE_RPK */
  61501. return EXPECT_RESULT();
  61502. }
  61503. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61504. static int test_dtls13_bad_epoch_ch(void)
  61505. {
  61506. EXPECT_DECLS;
  61507. WOLFSSL_CTX *ctx_c = NULL;
  61508. WOLFSSL_CTX *ctx_s = NULL;
  61509. WOLFSSL *ssl_c = NULL;
  61510. WOLFSSL *ssl_s = NULL;
  61511. struct test_memio_ctx test_ctx;
  61512. const int EPOCH_OFF = 3;
  61513. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61514. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61515. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61516. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  61517. * with just one message */
  61518. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  61519. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61520. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61521. WOLFSSL_ERROR_WANT_READ);
  61522. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  61523. /* first CH should use epoch 0x0 */
  61524. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  61525. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  61526. /* change epoch to 2 */
  61527. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  61528. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61529. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61530. WOLFSSL_ERROR_WANT_READ);
  61531. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  61532. /* resend the CH */
  61533. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61534. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61535. wolfSSL_free(ssl_c);
  61536. wolfSSL_CTX_free(ctx_c);
  61537. wolfSSL_free(ssl_s);
  61538. wolfSSL_CTX_free(ctx_s);
  61539. return EXPECT_RESULT();
  61540. }
  61541. #else
  61542. static int test_dtls13_bad_epoch_ch(void)
  61543. {
  61544. return TEST_SKIPPED;
  61545. }
  61546. #endif
  61547. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  61548. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  61549. {
  61550. EXPECT_DECLS;
  61551. WOLFSSL_SESSION *sess = NULL;
  61552. /* Setup the session to avoid errors */
  61553. ssl->session->timeout = -1;
  61554. ssl->session->side = WOLFSSL_CLIENT_END;
  61555. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  61556. defined(HAVE_SESSION_TICKET))
  61557. ssl->session->version = ssl->version;
  61558. #endif
  61559. /* Force a short session ID to be sent */
  61560. ssl->session->sessionIDSz = 4;
  61561. #ifndef NO_SESSION_CACHE_REF
  61562. /* Allow the client cache to be used */
  61563. ssl->session->idLen = 4;
  61564. #endif
  61565. ssl->session->isSetup = 1;
  61566. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  61567. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  61568. return EXPECT_RESULT();
  61569. }
  61570. static int test_short_session_id(void)
  61571. {
  61572. EXPECT_DECLS;
  61573. test_ssl_cbf client_cbf;
  61574. test_ssl_cbf server_cbf;
  61575. size_t i;
  61576. struct {
  61577. method_provider client_meth;
  61578. method_provider server_meth;
  61579. const char* tls_version;
  61580. } params[] = {
  61581. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  61582. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  61583. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  61584. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  61585. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  61586. #ifdef WOLFSSL_DTLS13
  61587. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  61588. #endif
  61589. #endif
  61590. #ifndef WOLFSSL_NO_TLS12
  61591. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  61592. #ifdef WOLFSSL_DTLS
  61593. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  61594. #endif
  61595. #endif
  61596. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  61597. !defined(NO_DES3))
  61598. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  61599. #ifdef WOLFSSL_DTLS
  61600. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  61601. #endif
  61602. #endif
  61603. };
  61604. fprintf(stderr, "\n");
  61605. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  61606. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61607. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61608. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  61609. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  61610. client_cbf.method = params[i].client_meth;
  61611. server_cbf.method = params[i].server_meth;
  61612. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61613. &server_cbf, NULL), TEST_SUCCESS);
  61614. }
  61615. return EXPECT_RESULT();
  61616. }
  61617. #else
  61618. static int test_short_session_id(void)
  61619. {
  61620. return TEST_SKIPPED;
  61621. }
  61622. #endif
  61623. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  61624. && defined(WOLFSSL_DTLS13)
  61625. static byte* test_find_string(const char *string,
  61626. byte *buf, int buf_size)
  61627. {
  61628. int string_size, i;
  61629. string_size = (int)XSTRLEN(string);
  61630. for (i = 0; i < buf_size - string_size - 1; i++) {
  61631. if (XSTRCMP((char*)&buf[i], string) == 0)
  61632. return &buf[i];
  61633. }
  61634. return NULL;
  61635. }
  61636. static int test_wolfSSL_dtls13_null_cipher(void)
  61637. {
  61638. EXPECT_DECLS;
  61639. WOLFSSL_CTX *ctx_c = NULL;
  61640. WOLFSSL_CTX *ctx_s = NULL;
  61641. WOLFSSL *ssl_c = NULL;
  61642. WOLFSSL *ssl_s = NULL;
  61643. struct test_memio_ctx test_ctx;
  61644. const char *test_str = "test";
  61645. int test_str_size;
  61646. byte buf[255], *ptr = NULL;
  61647. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61648. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  61649. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61650. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61651. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61652. test_str_size = XSTRLEN("test") + 1;
  61653. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61654. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61655. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61656. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61657. /* check that the packet was sent cleartext */
  61658. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  61659. test_ctx.s_len));
  61660. if (ptr != NULL) {
  61661. /* modify the message */
  61662. *ptr = 'H';
  61663. /* bad messages should be ignored in DTLS */
  61664. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  61665. ExpectIntEQ(ssl_s->error, WANT_READ);
  61666. }
  61667. wolfSSL_free(ssl_c);
  61668. wolfSSL_free(ssl_s);
  61669. wolfSSL_CTX_free(ctx_c);
  61670. wolfSSL_CTX_free(ctx_s);
  61671. return TEST_SUCCESS;
  61672. }
  61673. #else
  61674. static int test_wolfSSL_dtls13_null_cipher(void)
  61675. {
  61676. return TEST_SKIPPED;
  61677. }
  61678. #endif
  61679. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61680. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61681. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  61682. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  61683. {
  61684. SOCKADDR_S peer;
  61685. XSOCKLENT len;
  61686. int ret;
  61687. XMEMSET((byte*)&peer, 0, sizeof(peer));
  61688. len = sizeof(peer);
  61689. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  61690. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  61691. return -1;
  61692. switch (peer.ss_family) {
  61693. #ifdef WOLFSSL_IPV6
  61694. case WOLFSSL_IP6: {
  61695. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  61696. break;
  61697. }
  61698. #endif /* WOLFSSL_IPV6 */
  61699. case WOLFSSL_IP4:
  61700. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  61701. break;
  61702. default:
  61703. return -1;
  61704. }
  61705. return 0;
  61706. }
  61707. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  61708. {
  61709. char buf[1] = {'t'};
  61710. SOCKADDR_IN_T addr;
  61711. int sock_fd;
  61712. word16 port;
  61713. int err;
  61714. (void)ssl;
  61715. (void)ctx;
  61716. if (ssl == NULL)
  61717. return -1;
  61718. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  61719. if (err != 0)
  61720. return -1;
  61721. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  61722. if (sock_fd == -1)
  61723. return -1;
  61724. build_addr(&addr, wolfSSLIP, port, 1, 0);
  61725. /* send a packet to the server. Being another socket, the kernel will ensure
  61726. * the source port will be different. */
  61727. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  61728. sizeof(addr));
  61729. close(sock_fd);
  61730. if (err == -1)
  61731. return -1;
  61732. return 0;
  61733. }
  61734. /* setup a SSL session but just after the handshake send a packet to the server
  61735. * with a source address different than the one of the connected client. The I/O
  61736. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  61737. * is done in test_dtls_msg_from_other_peer_cb */
  61738. static int test_dtls_msg_from_other_peer(void)
  61739. {
  61740. EXPECT_DECLS;
  61741. callback_functions client_cbs;
  61742. callback_functions server_cbs;
  61743. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  61744. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  61745. client_cbs.method = wolfDTLSv1_2_client_method;
  61746. server_cbs.method = wolfDTLSv1_2_server_method;
  61747. client_cbs.doUdp = 1;
  61748. server_cbs.doUdp = 1;
  61749. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  61750. test_dtls_msg_from_other_peer_cb);
  61751. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  61752. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  61753. return EXPECT_RESULT();
  61754. }
  61755. #else
  61756. static int test_dtls_msg_from_other_peer(void)
  61757. {
  61758. return TEST_SKIPPED;
  61759. }
  61760. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61761. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61762. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  61763. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  61764. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61765. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61766. static int test_dtls_ipv6_check(void)
  61767. {
  61768. EXPECT_DECLS;
  61769. WOLFSSL_CTX *ctx_c = NULL;
  61770. WOLFSSL_CTX *ctx_s = NULL;
  61771. WOLFSSL *ssl_c = NULL;
  61772. WOLFSSL *ssl_s = NULL;
  61773. SOCKADDR_IN fake_addr6;
  61774. int sockfd = -1;
  61775. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  61776. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  61777. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  61778. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  61779. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61780. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  61781. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61782. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  61783. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  61784. /* mimic a sockaddr_in6 struct, this way we can't test without
  61785. * WOLFSSL_IPV6 */
  61786. fake_addr6.sin_family = WOLFSSL_IP6;
  61787. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  61788. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  61789. /* can't return error here, as the peer is opaque for wolfssl library at
  61790. * this point */
  61791. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  61792. WOLFSSL_SUCCESS);
  61793. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  61794. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  61795. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61796. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  61797. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  61798. WOLFSSL_SUCCESS);
  61799. /* reuse the socket */
  61800. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  61801. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  61802. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61803. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  61804. if (sockfd != -1)
  61805. close(sockfd);
  61806. wolfSSL_free(ssl_c);
  61807. wolfSSL_CTX_free(ctx_c);
  61808. wolfSSL_free(ssl_s);
  61809. wolfSSL_CTX_free(ctx_s);
  61810. return EXPECT_RESULT();
  61811. }
  61812. #else
  61813. static int test_dtls_ipv6_check(void)
  61814. {
  61815. return TEST_SKIPPED;
  61816. }
  61817. #endif
  61818. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61819. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61820. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  61821. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  61822. {
  61823. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61824. }
  61825. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  61826. {
  61827. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  61828. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  61829. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  61830. }
  61831. else {
  61832. char testMsg[] = "Message after SCR";
  61833. char msgBuf[sizeof(testMsg)];
  61834. int ret;
  61835. if (!wolfSSL_is_server(ssl)) {
  61836. AssertIntEQ(WOLFSSL_SUCCESS,
  61837. wolfSSL_set_session(ssl,
  61838. test_wolfSSL_SCR_after_resumption_session));
  61839. }
  61840. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61841. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61842. sizeof(testMsg));
  61843. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  61844. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  61845. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  61846. AssertIntEQ(ret, sizeof(msgBuf));
  61847. }
  61848. }
  61849. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  61850. {
  61851. AssertIntEQ(WOLFSSL_SUCCESS,
  61852. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  61853. }
  61854. static int test_wolfSSL_SCR_after_resumption(void)
  61855. {
  61856. EXPECT_DECLS;
  61857. callback_functions func_cb_client;
  61858. callback_functions func_cb_server;
  61859. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61860. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61861. func_cb_client.method = wolfTLSv1_2_client_method;
  61862. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  61863. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  61864. func_cb_server.method = wolfTLSv1_2_server_method;
  61865. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  61866. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61867. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61868. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61869. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  61870. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  61871. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61872. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61873. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61874. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  61875. return EXPECT_RESULT();
  61876. }
  61877. #else
  61878. static int test_wolfSSL_SCR_after_resumption(void)
  61879. {
  61880. return TEST_SKIPPED;
  61881. }
  61882. #endif
  61883. static int test_wolfSSL_configure_args(void)
  61884. {
  61885. EXPECT_DECLS;
  61886. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  61887. ExpectNotNull(wolfSSL_configure_args());
  61888. #endif
  61889. return EXPECT_RESULT();
  61890. }
  61891. static int test_dtls_no_extensions(void)
  61892. {
  61893. EXPECT_DECLS;
  61894. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61895. !defined(WOLFSSL_NO_TLS12)
  61896. WOLFSSL *ssl_s = NULL;
  61897. WOLFSSL_CTX *ctx_s = NULL;
  61898. struct test_memio_ctx test_ctx;
  61899. const byte chNoExtensions[] = {
  61900. /* Handshake type */
  61901. 0x16,
  61902. /* Version */
  61903. 0xfe, 0xff,
  61904. /* Epoch */
  61905. 0x00, 0x00,
  61906. /* Seq number */
  61907. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61908. /* Length */
  61909. 0x00, 0x40,
  61910. /* CH type */
  61911. 0x01,
  61912. /* Length */
  61913. 0x00, 0x00, 0x34,
  61914. /* Msg Seq */
  61915. 0x00, 0x00,
  61916. /* Frag offset */
  61917. 0x00, 0x00, 0x00,
  61918. /* Frag length */
  61919. 0x00, 0x00, 0x34,
  61920. /* Version */
  61921. 0xfe, 0xff,
  61922. /* Random */
  61923. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  61924. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  61925. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  61926. /* Session Length */
  61927. 0x00,
  61928. /* Cookie Length */
  61929. 0x00,
  61930. /* CS Length */
  61931. 0x00, 0x0c,
  61932. /* CS */
  61933. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  61934. /* Comp Meths Length */
  61935. 0x01,
  61936. /* Comp Meths */
  61937. 0x00
  61938. /* And finally... no extensions */
  61939. };
  61940. int i;
  61941. #ifdef OPENSSL_EXTRA
  61942. int repeats = 2;
  61943. #else
  61944. int repeats = 1;
  61945. #endif
  61946. for (i = 0; i < repeats; i++) {
  61947. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61948. ssl_s = NULL;
  61949. ctx_s = NULL;
  61950. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61951. NULL, wolfDTLS_server_method), 0);
  61952. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  61953. test_ctx.s_len = sizeof(chNoExtensions);
  61954. #ifdef OPENSSL_EXTRA
  61955. if (i > 0) {
  61956. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  61957. WOLFSSL_SUCCESS);
  61958. }
  61959. #endif
  61960. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  61961. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61962. /* Expecting a handshake msg. Either HVR or SH. */
  61963. ExpectIntGT(test_ctx.c_len, 0);
  61964. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  61965. wolfSSL_free(ssl_s);
  61966. wolfSSL_CTX_free(ctx_s);
  61967. }
  61968. #endif
  61969. return EXPECT_RESULT();
  61970. }
  61971. static int test_TLSX_CA_NAMES_bad_extension(void)
  61972. {
  61973. EXPECT_DECLS;
  61974. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61975. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  61976. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  61977. defined(HAVE_NULL_CIPHER)
  61978. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  61979. * parse the CA Names extension. Otherwise it will return other non-related
  61980. * errors. If CA Names will be parsed in more configurations, that should
  61981. * be reflected in the macro guard above. */
  61982. WOLFSSL *ssl_c = NULL;
  61983. WOLFSSL_CTX *ctx_c = NULL;
  61984. struct test_memio_ctx test_ctx;
  61985. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  61986. const byte shBadCaNamesExt[] = {
  61987. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  61988. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  61989. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  61990. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  61991. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  61992. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  61993. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  61994. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  61995. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  61996. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  61997. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  61998. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  61999. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  62000. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  62001. };
  62002. const byte shBadCaNamesExt2[] = {
  62003. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  62004. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  62005. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  62006. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  62007. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  62008. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  62009. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  62010. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  62011. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  62012. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  62013. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  62014. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  62015. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  62016. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  62017. };
  62018. int i = 0;
  62019. for (i = 0; i < 2; i++) {
  62020. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62021. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  62022. wolfTLSv1_3_client_method, NULL), 0);
  62023. switch (i) {
  62024. case 0:
  62025. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  62026. sizeof(shBadCaNamesExt));
  62027. test_ctx.c_len = sizeof(shBadCaNamesExt);
  62028. break;
  62029. case 1:
  62030. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  62031. sizeof(shBadCaNamesExt2));
  62032. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  62033. break;
  62034. }
  62035. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62036. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  62037. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  62038. #else
  62039. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  62040. #endif
  62041. wolfSSL_free(ssl_c);
  62042. ssl_c = NULL;
  62043. wolfSSL_CTX_free(ctx_c);
  62044. ctx_c = NULL;
  62045. }
  62046. #endif
  62047. return EXPECT_RESULT();
  62048. }
  62049. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62050. defined(HAVE_IO_TESTS_DEPENDENCIES)
  62051. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  62052. {
  62053. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  62054. WOLFSSL_SUCCESS);
  62055. }
  62056. static int test_dtls_1_0_hvr_downgrade(void)
  62057. {
  62058. EXPECT_DECLS;
  62059. callback_functions func_cb_client;
  62060. callback_functions func_cb_server;
  62061. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62062. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62063. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  62064. func_cb_client.method = wolfDTLS_client_method;
  62065. func_cb_server.method = wolfDTLSv1_2_server_method;
  62066. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  62067. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62068. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62069. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62070. return EXPECT_RESULT();
  62071. }
  62072. #else
  62073. static int test_dtls_1_0_hvr_downgrade(void)
  62074. {
  62075. EXPECT_DECLS;
  62076. return EXPECT_RESULT();
  62077. }
  62078. #endif
  62079. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  62080. defined(HAVE_SESSION_TICKET)
  62081. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  62082. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  62083. {
  62084. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  62085. AssertNotNull(test_session_ticket_no_id_session);
  62086. }
  62087. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  62088. {
  62089. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  62090. }
  62091. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  62092. {
  62093. test_session_ticket_no_id_session->sessionIDSz = 0;
  62094. AssertIntEQ(WOLFSSL_SUCCESS,
  62095. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  62096. }
  62097. static int test_session_ticket_no_id(void)
  62098. {
  62099. /* We are testing an expired (invalid crypto context in out case since the
  62100. * ctx changes) session ticket being sent with the session ID being 0
  62101. * length. */
  62102. EXPECT_DECLS;
  62103. callback_functions func_cb_client;
  62104. callback_functions func_cb_server;
  62105. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  62106. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  62107. func_cb_client.method = wolfTLSv1_2_client_method;
  62108. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62109. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  62110. func_cb_server.method = wolfTLSv1_2_server_method;
  62111. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62112. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62113. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62114. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62115. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  62116. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  62117. func_cb_client.method = wolfTLSv1_2_client_method;
  62118. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62119. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  62120. func_cb_server.method = wolfTLSv1_2_server_method;
  62121. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  62122. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62123. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62124. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62125. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  62126. return EXPECT_RESULT();
  62127. }
  62128. #else
  62129. static int test_session_ticket_no_id(void)
  62130. {
  62131. EXPECT_DECLS;
  62132. return EXPECT_RESULT();
  62133. }
  62134. #endif
  62135. static int test_session_ticket_hs_update(void)
  62136. {
  62137. EXPECT_DECLS;
  62138. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62139. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  62140. struct test_memio_ctx test_ctx;
  62141. struct test_memio_ctx test_ctx2;
  62142. struct test_memio_ctx test_ctx3;
  62143. WOLFSSL_CTX *ctx_c = NULL;
  62144. WOLFSSL_CTX *ctx_s = NULL;
  62145. WOLFSSL *ssl_c = NULL;
  62146. WOLFSSL *ssl_c2 = NULL;
  62147. WOLFSSL *ssl_c3 = NULL;
  62148. WOLFSSL *ssl_s = NULL;
  62149. WOLFSSL *ssl_s2 = NULL;
  62150. WOLFSSL *ssl_s3 = NULL;
  62151. WOLFSSL_SESSION *sess = NULL;
  62152. byte read_data[1];
  62153. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62154. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  62155. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  62156. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62157. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  62158. /* Generate tickets */
  62159. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62160. wolfSSL_SetLoggingPrefix("client");
  62161. /* Read the ticket msg */
  62162. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  62163. WOLFSSL_FATAL_ERROR);
  62164. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62165. WOLFSSL_ERROR_WANT_READ);
  62166. wolfSSL_SetLoggingPrefix(NULL);
  62167. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  62168. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  62169. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  62170. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  62171. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62172. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  62173. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  62174. wolfSSL_SetLoggingPrefix("client");
  62175. /* Exchange initial flights for the second connection */
  62176. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  62177. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  62178. WOLFSSL_ERROR_WANT_READ);
  62179. wolfSSL_SetLoggingPrefix(NULL);
  62180. wolfSSL_SetLoggingPrefix("server");
  62181. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  62182. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  62183. WOLFSSL_ERROR_WANT_READ);
  62184. wolfSSL_SetLoggingPrefix(NULL);
  62185. /* Complete third connection so that new tickets are exchanged */
  62186. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  62187. /* Read the ticket msg */
  62188. wolfSSL_SetLoggingPrefix("client");
  62189. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  62190. WOLFSSL_FATAL_ERROR);
  62191. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  62192. WOLFSSL_ERROR_WANT_READ);
  62193. wolfSSL_SetLoggingPrefix(NULL);
  62194. /* Complete second connection */
  62195. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  62196. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  62197. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  62198. wolfSSL_free(ssl_c);
  62199. wolfSSL_free(ssl_c2);
  62200. wolfSSL_free(ssl_c3);
  62201. wolfSSL_free(ssl_s);
  62202. wolfSSL_free(ssl_s2);
  62203. wolfSSL_free(ssl_s3);
  62204. wolfSSL_CTX_free(ctx_c);
  62205. wolfSSL_CTX_free(ctx_s);
  62206. wolfSSL_SESSION_free(sess);
  62207. #endif
  62208. return EXPECT_RESULT();
  62209. }
  62210. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62211. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  62212. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  62213. {
  62214. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  62215. WOLFSSL_SUCCESS);
  62216. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  62217. }
  62218. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  62219. {
  62220. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  62221. }
  62222. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  62223. {
  62224. char testMsg[] = "Message after SCR";
  62225. char msgBuf[sizeof(testMsg)];
  62226. if (wolfSSL_is_server(ssl)) {
  62227. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  62228. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  62229. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62230. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  62231. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62232. sizeof(testMsg));
  62233. }
  62234. else {
  62235. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62236. sizeof(testMsg));
  62237. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62238. }
  62239. }
  62240. static int test_dtls_downgrade_scr_server(void)
  62241. {
  62242. EXPECT_DECLS;
  62243. callback_functions func_cb_client;
  62244. callback_functions func_cb_server;
  62245. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62246. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62247. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  62248. func_cb_client.method = wolfDTLSv1_2_client_method;
  62249. func_cb_server.method = wolfDTLS_server_method;
  62250. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  62251. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  62252. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  62253. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  62254. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62255. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62256. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62257. return EXPECT_RESULT();
  62258. }
  62259. #else
  62260. static int test_dtls_downgrade_scr_server(void)
  62261. {
  62262. EXPECT_DECLS;
  62263. return EXPECT_RESULT();
  62264. }
  62265. #endif
  62266. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  62267. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  62268. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  62269. {
  62270. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  62271. WOLFSSL_SUCCESS);
  62272. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  62273. }
  62274. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  62275. {
  62276. char testMsg[] = "Message after SCR";
  62277. char msgBuf[sizeof(testMsg)];
  62278. if (wolfSSL_is_server(ssl)) {
  62279. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  62280. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  62281. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62282. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  62283. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62284. sizeof(testMsg));
  62285. }
  62286. else {
  62287. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  62288. sizeof(testMsg));
  62289. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  62290. }
  62291. }
  62292. static int test_dtls_downgrade_scr(void)
  62293. {
  62294. EXPECT_DECLS;
  62295. callback_functions func_cb_client;
  62296. callback_functions func_cb_server;
  62297. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62298. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62299. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  62300. func_cb_client.method = wolfDTLS_client_method;
  62301. func_cb_server.method = wolfDTLSv1_2_server_method;
  62302. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  62303. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  62304. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  62305. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62306. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62307. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62308. return EXPECT_RESULT();
  62309. }
  62310. #else
  62311. static int test_dtls_downgrade_scr(void)
  62312. {
  62313. EXPECT_DECLS;
  62314. return EXPECT_RESULT();
  62315. }
  62316. #endif
  62317. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62318. && !defined(WOLFSSL_NO_TLS12)
  62319. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  62320. char *data, int sz, void *ctx)
  62321. {
  62322. static int call_counter = 0;
  62323. call_counter++;
  62324. (void)ssl;
  62325. (void)data;
  62326. (void)sz;
  62327. (void)ctx;
  62328. switch (call_counter) {
  62329. case 1:
  62330. case 2:
  62331. return WOLFSSL_CBIO_ERR_TIMEOUT;
  62332. case 3:
  62333. return WOLFSSL_CBIO_ERR_WANT_READ;
  62334. default:
  62335. AssertIntLE(call_counter, 3);
  62336. return -1;
  62337. }
  62338. }
  62339. #endif
  62340. /* Make sure we don't send acks before getting a server hello */
  62341. static int test_dtls_client_hello_timeout_downgrade(void)
  62342. {
  62343. EXPECT_DECLS;
  62344. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62345. && !defined(WOLFSSL_NO_TLS12)
  62346. WOLFSSL_CTX *ctx_c = NULL;
  62347. WOLFSSL_CTX *ctx_s = NULL;
  62348. WOLFSSL *ssl_c = NULL;
  62349. WOLFSSL *ssl_s = NULL;
  62350. struct test_memio_ctx test_ctx;
  62351. DtlsRecordLayerHeader* dtlsRH;
  62352. size_t len;
  62353. byte sequence_number[8];
  62354. int i;
  62355. for (i = 0; i < 2; i++) {
  62356. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62357. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62358. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  62359. if (i == 0) {
  62360. /* First time simulate timeout in IO layer */
  62361. /* CH1 */
  62362. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62363. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62364. /* HVR */
  62365. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62366. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62367. /* CH2 */
  62368. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62369. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62370. /* SH flight */
  62371. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62372. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62373. /* Drop the SH */
  62374. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  62375. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62376. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  62377. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  62378. (sizeof(DtlsRecordLayerHeader) + len));
  62379. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  62380. /* Read the remainder of the flight */
  62381. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62382. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62383. wolfSSL_SSLSetIORecv(ssl_c,
  62384. test_dtls_client_hello_timeout_downgrade_read_cb);
  62385. /* CH3 */
  62386. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62387. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62388. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  62389. }
  62390. else {
  62391. /* Second time call wolfSSL_dtls_got_timeout */
  62392. /* CH1 */
  62393. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62394. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62395. /* HVR */
  62396. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62397. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62398. /* CH2 */
  62399. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62400. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62401. /* SH flight */
  62402. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62403. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62404. /* Drop the SH */
  62405. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  62406. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62407. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  62408. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  62409. (sizeof(DtlsRecordLayerHeader) + len));
  62410. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  62411. /* Read the remainder of the flight */
  62412. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62413. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62414. /* Quick timeout should be set as we received at least one msg */
  62415. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  62416. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62417. /* Quick timeout should be cleared after a quick timeout */
  62418. /* CH3 */
  62419. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  62420. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62421. }
  62422. /* Parse out to make sure we got exactly one ClientHello message */
  62423. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62424. /* Second ClientHello after HVR */
  62425. sequence_number[7] = 2;
  62426. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  62427. ExpectIntEQ(dtlsRH->type, handshake);
  62428. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62429. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62430. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62431. sizeof(sequence_number)), 0);
  62432. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62433. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  62434. /* Connection should be able to continue */
  62435. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62436. wolfSSL_free(ssl_c);
  62437. wolfSSL_free(ssl_s);
  62438. wolfSSL_CTX_free(ctx_c);
  62439. wolfSSL_CTX_free(ctx_s);
  62440. ssl_c = NULL;
  62441. ssl_s = NULL;
  62442. ctx_c = NULL;
  62443. ctx_s = NULL;
  62444. if (!EXPECT_SUCCESS())
  62445. break;
  62446. }
  62447. #endif
  62448. return EXPECT_RESULT();
  62449. }
  62450. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62451. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  62452. int sz, void *ctx)
  62453. {
  62454. static int call_counter = 0;
  62455. call_counter++;
  62456. (void)ssl;
  62457. (void)data;
  62458. (void)sz;
  62459. (void)ctx;
  62460. switch (call_counter) {
  62461. case 1:
  62462. return WOLFSSL_CBIO_ERR_TIMEOUT;
  62463. case 2:
  62464. return WOLFSSL_CBIO_ERR_WANT_READ;
  62465. default:
  62466. AssertIntLE(call_counter, 2);
  62467. return -1;
  62468. }
  62469. }
  62470. #endif
  62471. /* Make sure we don't send acks before getting a server hello */
  62472. static int test_dtls_client_hello_timeout(void)
  62473. {
  62474. EXPECT_DECLS;
  62475. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62476. WOLFSSL *ssl_c = NULL;
  62477. WOLFSSL_CTX *ctx_c = NULL;
  62478. struct test_memio_ctx test_ctx;
  62479. DtlsRecordLayerHeader* dtlsRH;
  62480. size_t idx;
  62481. size_t len;
  62482. byte sequence_number[8];
  62483. int i;
  62484. for (i = 0; i < 2; i++) {
  62485. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62486. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  62487. wolfDTLSv1_3_client_method, NULL), 0);
  62488. if (i == 0) {
  62489. /* First time simulate timeout in IO layer */
  62490. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  62491. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62492. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62493. }
  62494. else {
  62495. /* Second time call wolfSSL_dtls_got_timeout */
  62496. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62497. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62498. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62499. }
  62500. /* Parse out to make sure we got exactly two ClientHello messages */
  62501. idx = 0;
  62502. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62503. /* First ClientHello */
  62504. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  62505. ExpectIntEQ(dtlsRH->type, handshake);
  62506. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62507. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62508. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62509. sizeof(sequence_number)), 0);
  62510. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62511. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  62512. idx += sizeof(DtlsRecordLayerHeader) + len;
  62513. /* Second ClientHello */
  62514. sequence_number[7] = 1;
  62515. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  62516. ExpectIntEQ(dtlsRH->type, handshake);
  62517. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62518. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62519. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62520. sizeof(sequence_number)), 0);
  62521. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62522. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  62523. wolfSSL_free(ssl_c);
  62524. wolfSSL_CTX_free(ctx_c);
  62525. ssl_c = NULL;
  62526. ctx_c = NULL;
  62527. if (!EXPECT_SUCCESS())
  62528. break;
  62529. }
  62530. #endif
  62531. return EXPECT_RESULT();
  62532. }
  62533. /* DTLS test when dropping the changed cipher spec message */
  62534. static int test_dtls_dropped_ccs(void)
  62535. {
  62536. EXPECT_DECLS;
  62537. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62538. && !defined(WOLFSSL_NO_TLS12)
  62539. WOLFSSL_CTX *ctx_c = NULL;
  62540. WOLFSSL_CTX *ctx_s = NULL;
  62541. WOLFSSL *ssl_c = NULL;
  62542. WOLFSSL *ssl_s = NULL;
  62543. struct test_memio_ctx test_ctx;
  62544. DtlsRecordLayerHeader* dtlsRH;
  62545. size_t len;
  62546. byte data[1];
  62547. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62548. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62549. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  62550. /* CH1 */
  62551. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62552. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62553. /* HVR */
  62554. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62555. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62556. /* CH2 */
  62557. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62558. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62559. /* Server first flight */
  62560. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62561. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62562. /* Client flight */
  62563. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62564. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62565. /* Server ccs + finished */
  62566. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  62567. /* Drop the ccs */
  62568. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  62569. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  62570. ExpectIntEQ(len, 1);
  62571. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  62572. if (EXPECT_SUCCESS()) {
  62573. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  62574. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  62575. (sizeof(DtlsRecordLayerHeader) + len));
  62576. }
  62577. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  62578. /* Client rtx flight */
  62579. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62580. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62581. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62582. /* Server ccs + finished rtx */
  62583. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  62584. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62585. /* Client processes finished */
  62586. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  62587. wolfSSL_free(ssl_c);
  62588. wolfSSL_free(ssl_s);
  62589. wolfSSL_CTX_free(ctx_c);
  62590. wolfSSL_CTX_free(ctx_s);
  62591. #endif
  62592. return EXPECT_RESULT();
  62593. }
  62594. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62595. && !defined(WOLFSSL_NO_TLS12)
  62596. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  62597. byte seq_num)
  62598. {
  62599. EXPECT_DECLS;
  62600. DtlsRecordLayerHeader* dtlsRH;
  62601. byte sequence_number[8];
  62602. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62603. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  62604. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  62605. ExpectIntEQ(dtlsRH->type, handshake);
  62606. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62607. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62608. sequence_number[7] = seq_num;
  62609. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62610. sizeof(sequence_number)), 0);
  62611. return EXPECT_RESULT();
  62612. }
  62613. #endif
  62614. /*
  62615. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  62616. * and after a HelloRetryRequest. This is testing the server side as it is
  62617. * operating statelessly and should copy the sequence number of the ClientHello.
  62618. */
  62619. static int test_dtls_seq_num_downgrade(void)
  62620. {
  62621. EXPECT_DECLS;
  62622. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62623. && !defined(WOLFSSL_NO_TLS12)
  62624. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62625. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62626. struct test_memio_ctx test_ctx;
  62627. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62628. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62629. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  62630. /* CH1 */
  62631. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62632. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62633. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  62634. test_ctx.s_len, 0), TEST_SUCCESS);
  62635. /* HVR */
  62636. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62637. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62638. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  62639. test_ctx.c_len, 0), TEST_SUCCESS);
  62640. /* CH2 */
  62641. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62642. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62643. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  62644. test_ctx.s_len, 1), TEST_SUCCESS);
  62645. /* Server first flight */
  62646. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62647. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62648. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  62649. test_ctx.c_len, 1), TEST_SUCCESS);
  62650. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62651. wolfSSL_free(ssl_c);
  62652. wolfSSL_CTX_free(ctx_c);
  62653. wolfSSL_free(ssl_s);
  62654. wolfSSL_CTX_free(ctx_s);
  62655. #endif
  62656. return EXPECT_RESULT();
  62657. }
  62658. /**
  62659. * Make sure we don't send RSA Signature Hash Algorithms in the
  62660. * CertificateRequest when we don't have any such ciphers set.
  62661. * @return EXPECT_RESULT()
  62662. */
  62663. static int test_certreq_sighash_algos(void)
  62664. {
  62665. EXPECT_DECLS;
  62666. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62667. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  62668. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  62669. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  62670. WOLFSSL_CTX *ctx_c = NULL;
  62671. WOLFSSL_CTX *ctx_s = NULL;
  62672. WOLFSSL *ssl_c = NULL;
  62673. WOLFSSL *ssl_s = NULL;
  62674. struct test_memio_ctx test_ctx;
  62675. int idx = 0;
  62676. int maxIdx = 0;
  62677. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62678. test_ctx.c_ciphers = test_ctx.s_ciphers =
  62679. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  62680. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62681. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  62682. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  62683. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  62684. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  62685. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  62686. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62687. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  62688. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62689. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  62690. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62691. WOLFSSL_ERROR_WANT_READ);
  62692. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  62693. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  62694. WOLFSSL_ERROR_WANT_READ);
  62695. /* Find the CertificateRequest message */
  62696. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  62697. word16 len;
  62698. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  62699. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  62700. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  62701. ato16(test_ctx.c_buff + idx, &len);
  62702. idx += OPAQUE16_LEN;
  62703. if (test_ctx.c_buff[idx] == certificate_request) {
  62704. idx++;
  62705. /* length */
  62706. idx += OPAQUE24_LEN;
  62707. /* cert types */
  62708. idx += 1 + test_ctx.c_buff[idx];
  62709. /* Sig algos */
  62710. ato16(test_ctx.c_buff + idx, &len);
  62711. idx += OPAQUE16_LEN;
  62712. maxIdx = idx + (int)len;
  62713. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  62714. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  62715. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  62716. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  62717. else
  62718. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  62719. }
  62720. break;
  62721. }
  62722. else {
  62723. idx += (int)len;
  62724. }
  62725. }
  62726. ExpectIntLT(idx, test_ctx.c_len);
  62727. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62728. wolfSSL_free(ssl_c);
  62729. wolfSSL_free(ssl_s);
  62730. wolfSSL_CTX_free(ctx_c);
  62731. wolfSSL_CTX_free(ctx_s);
  62732. #endif
  62733. return EXPECT_RESULT();
  62734. }
  62735. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62736. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  62737. {
  62738. EXPECT_DECLS;
  62739. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  62740. myVerifyAction = VERIFY_USE_PREVERFIY;
  62741. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62742. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62743. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62744. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62745. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  62746. WOLFSSL_SUCCESS);
  62747. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62748. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  62749. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62750. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62751. "./certs/crl/ca-int.pem",
  62752. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62753. return EXPECT_RESULT();
  62754. }
  62755. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  62756. {
  62757. EXPECT_DECLS;
  62758. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  62759. myVerifyAction = VERIFY_USE_PREVERFIY;
  62760. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62761. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62762. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62763. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62764. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62765. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62766. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  62767. WOLFSSL_SUCCESS);
  62768. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62769. "./certs/crl/ca-int2.pem",
  62770. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62771. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62772. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  62773. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62774. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62775. "./certs/crl/ca-int.pem",
  62776. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62777. return EXPECT_RESULT();
  62778. }
  62779. #endif
  62780. static int test_revoked_loaded_int_cert(void)
  62781. {
  62782. EXPECT_DECLS;
  62783. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62784. test_ssl_cbf client_cbf;
  62785. test_ssl_cbf server_cbf;
  62786. struct {
  62787. const char* certPemFile;
  62788. const char* keyPemFile;
  62789. ctx_cb client_ctx_ready;
  62790. } test_params[] = {
  62791. {"./certs/intermediate/ca-int2-cert.pem",
  62792. "./certs/intermediate/ca-int2-key.pem",
  62793. test_revoked_loaded_int_cert_ctx_ready1},
  62794. {"./certs/intermediate/server-chain.pem",
  62795. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  62796. {"./certs/intermediate/server-chain-short.pem",
  62797. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  62798. };
  62799. size_t i;
  62800. printf("\n");
  62801. for (i = 0; i < XELEM_CNT(test_params); i++) {
  62802. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62803. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62804. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  62805. server_cbf.certPemFile = test_params[i].certPemFile;
  62806. server_cbf.keyPemFile = test_params[i].keyPemFile;
  62807. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  62808. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62809. &server_cbf, NULL), TEST_FAIL);
  62810. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  62811. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  62812. if (!EXPECT_SUCCESS())
  62813. break;
  62814. printf("\t%s passed\n", test_params[i].certPemFile);
  62815. }
  62816. #endif
  62817. return EXPECT_RESULT();
  62818. }
  62819. static int test_dtls13_frag_ch_pq(void)
  62820. {
  62821. EXPECT_DECLS;
  62822. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62823. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  62824. WOLFSSL_CTX *ctx_c = NULL;
  62825. WOLFSSL_CTX *ctx_s = NULL;
  62826. WOLFSSL *ssl_c = NULL;
  62827. WOLFSSL *ssl_s = NULL;
  62828. struct test_memio_ctx test_ctx;
  62829. const char *test_str = "test";
  62830. int test_str_size;
  62831. byte buf[255];
  62832. int group = WOLFSSL_KYBER_LEVEL5;
  62833. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62834. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62835. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62836. /* Add in a large post-quantum key share to make the CH long. */
  62837. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  62838. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  62839. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  62840. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62841. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  62842. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  62843. test_str_size = XSTRLEN("test") + 1;
  62844. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  62845. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  62846. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  62847. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  62848. wolfSSL_free(ssl_c);
  62849. wolfSSL_free(ssl_s);
  62850. wolfSSL_CTX_free(ctx_c);
  62851. wolfSSL_CTX_free(ctx_s);
  62852. #endif
  62853. return EXPECT_RESULT();
  62854. }
  62855. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62856. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  62857. static int test_dtls_frag_ch_count_records(byte* b, int len)
  62858. {
  62859. DtlsRecordLayerHeader* dtlsRH;
  62860. int records = 0;
  62861. size_t recordLen;
  62862. while (len > 0) {
  62863. records++;
  62864. dtlsRH = (DtlsRecordLayerHeader*)b;
  62865. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  62866. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  62867. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  62868. }
  62869. return records;
  62870. }
  62871. #endif
  62872. static int test_dtls_frag_ch(void)
  62873. {
  62874. EXPECT_DECLS;
  62875. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62876. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  62877. WOLFSSL_CTX *ctx_c = NULL;
  62878. WOLFSSL_CTX *ctx_s = NULL;
  62879. WOLFSSL *ssl_c = NULL;
  62880. WOLFSSL *ssl_s = NULL;
  62881. struct test_memio_ctx test_ctx;
  62882. static unsigned int DUMMY_MTU = 256;
  62883. unsigned char four_frag_CH[] = {
  62884. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62885. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62886. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  62887. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  62888. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  62889. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  62890. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  62891. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  62892. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  62893. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  62894. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  62895. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  62896. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  62897. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  62898. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  62899. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  62900. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  62901. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  62902. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  62903. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62904. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  62905. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  62906. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  62907. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  62908. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  62909. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  62910. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  62911. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  62912. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  62913. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  62914. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  62915. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  62916. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  62917. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  62918. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  62919. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  62920. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  62921. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  62922. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  62923. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  62924. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  62925. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  62926. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  62927. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  62928. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  62929. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  62930. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  62931. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  62932. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  62933. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  62934. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  62935. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  62936. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  62937. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  62938. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  62939. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  62940. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  62941. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  62942. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  62943. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  62944. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  62945. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  62946. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  62947. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  62948. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  62949. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  62950. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  62951. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  62952. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  62953. 0xa5, 0xcb, 0x68, 0xf3
  62954. };
  62955. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62956. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62957. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62958. /* Fragment msgs */
  62959. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  62960. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  62961. /* Add in some key shares to make the CH long */
  62962. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  62963. WOLFSSL_SUCCESS);
  62964. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  62965. WOLFSSL_SUCCESS);
  62966. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  62967. WOLFSSL_SUCCESS);
  62968. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  62969. WOLFSSL_SUCCESS);
  62970. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  62971. /* Reject fragmented first CH */
  62972. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  62973. sizeof(four_frag_CH)), 4);
  62974. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  62975. test_ctx.s_len = sizeof(four_frag_CH);
  62976. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  62977. int s_len = test_ctx.s_len;
  62978. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62979. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62980. /* Fail if we didn't advance the buffer to avoid infinite loops */
  62981. ExpectIntLT(test_ctx.s_len, s_len);
  62982. }
  62983. /* Expect all fragments read */
  62984. ExpectIntEQ(test_ctx.s_len, 0);
  62985. /* Expect quietly dropping fragmented first CH */
  62986. ExpectIntEQ(test_ctx.c_len, 0);
  62987. /* CH1 */
  62988. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62989. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62990. /* Count records. Expect 1 unfragmented CH */
  62991. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  62992. test_ctx.s_len), 1);
  62993. /* HRR */
  62994. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62995. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62996. /* CH2 */
  62997. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62998. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62999. /* Count records. Expect fragmented CH */
  63000. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  63001. test_ctx.s_len), 1);
  63002. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63003. wolfSSL_free(ssl_c);
  63004. wolfSSL_free(ssl_s);
  63005. wolfSSL_CTX_free(ctx_c);
  63006. wolfSSL_CTX_free(ctx_s);
  63007. ssl_c = ssl_s = NULL;
  63008. ctx_c = ctx_s = NULL;
  63009. #endif
  63010. return EXPECT_RESULT();
  63011. }
  63012. static int test_dtls_empty_keyshare_with_cookie(void)
  63013. {
  63014. EXPECT_DECLS;
  63015. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  63016. WOLFSSL_CTX *ctx_s = NULL;
  63017. WOLFSSL *ssl_s = NULL;
  63018. struct test_memio_ctx test_ctx;
  63019. unsigned char ch_empty_keyshare_with_cookie[] = {
  63020. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  63021. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  63022. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  63023. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  63024. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  63025. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  63026. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  63027. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  63028. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  63029. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  63030. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  63031. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  63032. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  63033. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  63034. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  63035. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  63036. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  63037. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  63038. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  63039. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  63040. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  63041. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  63042. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  63043. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  63044. };
  63045. DtlsRecordLayerHeader* dtlsRH;
  63046. byte sequence_number[8];
  63047. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  63048. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63049. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  63050. sizeof(ch_empty_keyshare_with_cookie));
  63051. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  63052. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  63053. NULL, wolfDTLSv1_3_server_method), 0);
  63054. /* CH1 */
  63055. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63056. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63057. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  63058. ExpectIntEQ(test_ctx.c_len, 15);
  63059. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  63060. ExpectIntEQ(dtlsRH->type, alert);
  63061. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63062. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63063. sequence_number[7] = 1;
  63064. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63065. sizeof(sequence_number)), 0);
  63066. ExpectIntEQ(dtlsRH->length[0], 0);
  63067. ExpectIntEQ(dtlsRH->length[1], 2);
  63068. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  63069. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  63070. wolfSSL_free(ssl_s);
  63071. wolfSSL_CTX_free(ctx_s);
  63072. #endif
  63073. return EXPECT_RESULT();
  63074. }
  63075. static int test_dtls_old_seq_number(void)
  63076. {
  63077. EXPECT_DECLS;
  63078. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  63079. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63080. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63081. struct test_memio_ctx test_ctx;
  63082. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63083. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63084. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  63085. /* CH1 */
  63086. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63087. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63088. /* HVR */
  63089. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63090. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63091. /* CH2 */
  63092. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63093. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63094. /* Server first flight */
  63095. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63096. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63097. /* Client second flight */
  63098. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63099. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63100. /* Modify the sequence number */
  63101. {
  63102. DtlsRecordLayerHeader* dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  63103. XMEMSET(dtlsRH->sequence_number, 0, sizeof(dtlsRH->sequence_number));
  63104. }
  63105. /* Server second flight */
  63106. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63107. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63108. /* Server should not do anything as a pkt was dropped */
  63109. ExpectIntEQ(test_ctx.c_len, 0);
  63110. ExpectIntEQ(test_ctx.s_len, 0);
  63111. /* Trigger rtx */
  63112. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63113. /* Complete connection */
  63114. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63115. wolfSSL_free(ssl_c);
  63116. wolfSSL_CTX_free(ctx_c);
  63117. wolfSSL_free(ssl_s);
  63118. wolfSSL_CTX_free(ctx_s);
  63119. #endif
  63120. return EXPECT_RESULT();
  63121. }
  63122. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63123. defined(HAVE_LIBOQS)
  63124. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  63125. {
  63126. int group = WOLFSSL_KYBER_LEVEL5;
  63127. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  63128. }
  63129. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  63130. {
  63131. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  63132. }
  63133. #endif
  63134. static int test_tls13_pq_groups(void)
  63135. {
  63136. EXPECT_DECLS;
  63137. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63138. defined(HAVE_LIBOQS)
  63139. callback_functions func_cb_client;
  63140. callback_functions func_cb_server;
  63141. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63142. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63143. func_cb_client.method = wolfTLSv1_3_client_method;
  63144. func_cb_server.method = wolfTLSv1_3_server_method;
  63145. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  63146. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  63147. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  63148. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63149. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63150. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63151. #endif
  63152. return EXPECT_RESULT();
  63153. }
  63154. static int test_tls13_early_data(void)
  63155. {
  63156. EXPECT_DECLS;
  63157. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  63158. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  63159. int written = 0;
  63160. int read = 0;
  63161. size_t i;
  63162. int splitEarlyData;
  63163. char msg[] = "This is early data";
  63164. char msg2[] = "This is client data";
  63165. char msg3[] = "This is server data";
  63166. char msg4[] = "This is server immediate data";
  63167. char msgBuf[50];
  63168. struct {
  63169. method_provider client_meth;
  63170. method_provider server_meth;
  63171. const char* tls_version;
  63172. int isUdp;
  63173. } params[] = {
  63174. #ifdef WOLFSSL_TLS13
  63175. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  63176. "TLS 1.3", 0 },
  63177. #endif
  63178. #ifdef WOLFSSL_DTLS13
  63179. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  63180. "DTLS 1.3", 1 },
  63181. #endif
  63182. };
  63183. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63184. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  63185. struct test_memio_ctx test_ctx;
  63186. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63187. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63188. WOLFSSL_SESSION *sess = NULL;
  63189. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63190. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  63191. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  63192. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  63193. /* Get a ticket so that we can do 0-RTT on the next connection */
  63194. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63195. /* Make sure we read the ticket */
  63196. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  63197. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63198. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  63199. wolfSSL_free(ssl_c);
  63200. ssl_c = NULL;
  63201. wolfSSL_free(ssl_s);
  63202. ssl_s = NULL;
  63203. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63204. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63205. params[i].client_meth, params[i].server_meth), 0);
  63206. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  63207. #ifdef WOLFSSL_DTLS13
  63208. if (params[i].isUdp) {
  63209. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  63210. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  63211. #else
  63212. /* Let's test this but we generally don't recommend turning off the
  63213. * cookie exchange */
  63214. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  63215. #endif
  63216. }
  63217. #endif
  63218. /* Test 0-RTT data */
  63219. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  63220. &written), sizeof(msg));
  63221. ExpectIntEQ(written, sizeof(msg));
  63222. if (splitEarlyData) {
  63223. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  63224. &written), sizeof(msg));
  63225. ExpectIntEQ(written, sizeof(msg));
  63226. }
  63227. /* Read first 0-RTT data (if split otherwise entire data) */
  63228. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63229. &read), sizeof(msg));
  63230. ExpectIntEQ(read, sizeof(msg));
  63231. ExpectStrEQ(msg, msgBuf);
  63232. /* Test 0.5-RTT data */
  63233. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  63234. if (splitEarlyData) {
  63235. /* Read second 0-RTT data */
  63236. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63237. &read), sizeof(msg));
  63238. ExpectIntEQ(read, sizeof(msg));
  63239. ExpectStrEQ(msg, msgBuf);
  63240. }
  63241. if (params[i].isUdp) {
  63242. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63243. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  63244. /* Read server 0.5-RTT data */
  63245. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  63246. ExpectStrEQ(msg4, msgBuf);
  63247. /* Complete handshake */
  63248. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63249. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63250. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  63251. * a user would loop until it is true but here we control both sides so we
  63252. * just assert the expected value. wolfSSL_read_early_data does not provide
  63253. * handshake status to us with non-blocking IO and we can't use
  63254. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  63255. * parsing logic. */
  63256. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  63257. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63258. &read), 0);
  63259. ExpectIntEQ(read, 0);
  63260. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  63261. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  63262. }
  63263. else {
  63264. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  63265. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  63266. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  63267. &read), 0);
  63268. ExpectIntEQ(read, 0);
  63269. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  63270. /* Read server 0.5-RTT data */
  63271. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  63272. ExpectStrEQ(msg4, msgBuf);
  63273. }
  63274. /* Test bi-directional write */
  63275. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  63276. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  63277. ExpectStrEQ(msg2, msgBuf);
  63278. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  63279. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  63280. ExpectStrEQ(msg3, msgBuf);
  63281. ExpectTrue(wolfSSL_session_reused(ssl_c));
  63282. ExpectTrue(wolfSSL_session_reused(ssl_s));
  63283. wolfSSL_SESSION_free(sess);
  63284. wolfSSL_free(ssl_c);
  63285. wolfSSL_free(ssl_s);
  63286. wolfSSL_CTX_free(ctx_c);
  63287. wolfSSL_CTX_free(ctx_s);
  63288. }
  63289. }
  63290. #endif
  63291. return EXPECT_RESULT();
  63292. }
  63293. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  63294. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  63295. {
  63296. EXPECT_DECLS;
  63297. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63298. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  63299. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  63300. return EXPECT_RESULT();
  63301. }
  63302. #endif
  63303. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  63304. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  63305. {
  63306. EXPECT_DECLS;
  63307. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63308. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  63309. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  63310. return EXPECT_RESULT();
  63311. }
  63312. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  63313. {
  63314. EXPECT_DECLS;
  63315. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63316. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  63317. 0), 1);
  63318. return EXPECT_RESULT();
  63319. }
  63320. #endif
  63321. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  63322. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  63323. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  63324. {
  63325. EXPECT_DECLS;
  63326. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  63327. return EXPECT_RESULT();
  63328. }
  63329. #endif
  63330. static int test_self_signed_stapling(void)
  63331. {
  63332. EXPECT_DECLS;
  63333. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  63334. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  63335. test_ssl_cbf client_cbf;
  63336. test_ssl_cbf server_cbf;
  63337. size_t i;
  63338. struct {
  63339. method_provider client_meth;
  63340. method_provider server_meth;
  63341. ctx_cb client_ctx;
  63342. const char* tls_version;
  63343. } params[] = {
  63344. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  63345. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  63346. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  63347. #endif
  63348. #ifndef WOLFSSL_NO_TLS12
  63349. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  63350. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  63351. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  63352. #endif
  63353. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  63354. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  63355. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  63356. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  63357. test_self_signed_stapling_client_v2_multi_ctx_ready,
  63358. "TLSv1_2 v2 multi" },
  63359. #endif
  63360. #endif
  63361. };
  63362. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63363. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63364. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63365. printf("\nTesting self-signed cert with status request: %s\n",
  63366. params[i].tls_version);
  63367. client_cbf.method = params[i].client_meth;
  63368. client_cbf.ctx_ready = params[i].client_ctx;
  63369. server_cbf.method = params[i].server_meth;
  63370. server_cbf.certPemFile = "certs/ca-cert.pem";
  63371. server_cbf.keyPemFile = "certs/ca-key.pem";
  63372. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  63373. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63374. &server_cbf, NULL), TEST_SUCCESS);
  63375. }
  63376. #endif
  63377. return EXPECT_RESULT();
  63378. }
  63379. static int test_tls_multi_handshakes_one_record(void)
  63380. {
  63381. EXPECT_DECLS;
  63382. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  63383. struct test_memio_ctx test_ctx;
  63384. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63385. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63386. RecordLayerHeader* rh = NULL;
  63387. byte *len ;
  63388. int newRecIdx = RECORD_HEADER_SZ;
  63389. int idx = 0;
  63390. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63391. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63392. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  63393. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63394. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63395. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  63396. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63397. /* Combine server handshake msgs into one record */
  63398. while (idx < test_ctx.c_len) {
  63399. word16 recLen;
  63400. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  63401. len = &rh->length[0];
  63402. ato16((const byte*)len, &recLen);
  63403. idx += RECORD_HEADER_SZ;
  63404. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  63405. (size_t)recLen);
  63406. newRecIdx += recLen;
  63407. idx += recLen;
  63408. }
  63409. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  63410. len = &rh->length[0];
  63411. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  63412. test_ctx.c_len = newRecIdx;
  63413. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63414. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63415. wolfSSL_free(ssl_c);
  63416. wolfSSL_free(ssl_s);
  63417. wolfSSL_CTX_free(ctx_c);
  63418. wolfSSL_CTX_free(ctx_s);
  63419. #endif
  63420. return EXPECT_RESULT();
  63421. }
  63422. static int test_write_dup(void)
  63423. {
  63424. EXPECT_DECLS;
  63425. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  63426. size_t i, j;
  63427. char hiWorld[] = "dup message";
  63428. char readData[sizeof(hiWorld) + 5];
  63429. struct {
  63430. method_provider client_meth;
  63431. method_provider server_meth;
  63432. const char* version_name;
  63433. int version;
  63434. } methods[] = {
  63435. #ifndef WOLFSSL_NO_TLS12
  63436. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  63437. #endif
  63438. #ifdef WOLFSSL_TLS13
  63439. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  63440. #endif
  63441. };
  63442. struct {
  63443. const char* cipher;
  63444. int version;
  63445. } ciphers[] = {
  63446. /* For simplicity the macros are copied from internal.h */
  63447. /* TLS 1.2 */
  63448. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  63449. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  63450. #ifndef NO_RSA
  63451. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  63452. #endif
  63453. #endif
  63454. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  63455. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  63456. #endif
  63457. #endif
  63458. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  63459. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  63460. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63461. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  63462. #endif
  63463. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  63464. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  63465. #endif
  63466. #endif
  63467. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  63468. && !defined(NO_TLS) && !defined(NO_AES)
  63469. #ifdef HAVE_AESGCM
  63470. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63471. #ifndef NO_RSA
  63472. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  63473. #endif
  63474. #endif
  63475. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  63476. #ifndef NO_RSA
  63477. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  63478. #endif
  63479. #endif
  63480. #endif
  63481. #endif
  63482. /* TLS 1.3 */
  63483. #ifdef WOLFSSL_TLS13
  63484. #ifdef HAVE_AESGCM
  63485. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63486. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  63487. #endif
  63488. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  63489. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  63490. #endif
  63491. #endif
  63492. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  63493. #ifndef NO_SHA256
  63494. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  63495. #endif
  63496. #endif
  63497. #ifdef HAVE_AESCCM
  63498. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  63499. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  63500. #endif
  63501. #endif
  63502. #endif
  63503. };
  63504. for (i = 0; i < XELEM_CNT(methods); i++) {
  63505. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  63506. struct test_memio_ctx test_ctx;
  63507. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  63508. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  63509. WOLFSSL *ssl_c2 = NULL;
  63510. if (methods[i].version != ciphers[j].version)
  63511. continue;
  63512. if (i == 0 && j == 0)
  63513. printf("\n");
  63514. printf("Testing %s with %s... ", methods[i].version_name,
  63515. ciphers[j].cipher);
  63516. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63517. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  63518. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63519. methods[i].client_meth, methods[i].server_meth), 0);
  63520. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63521. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  63522. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  63523. WRITE_DUP_WRITE_E);
  63524. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  63525. sizeof(hiWorld));
  63526. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  63527. sizeof(hiWorld));
  63528. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  63529. sizeof(hiWorld));
  63530. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  63531. WRITE_DUP_READ_E);
  63532. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  63533. sizeof(hiWorld));
  63534. if (EXPECT_SUCCESS())
  63535. printf("ok\n");
  63536. else
  63537. printf("failed\n");
  63538. wolfSSL_free(ssl_c);
  63539. wolfSSL_free(ssl_c2);
  63540. wolfSSL_free(ssl_s);
  63541. wolfSSL_CTX_free(ctx_c);
  63542. wolfSSL_CTX_free(ctx_s);
  63543. }
  63544. }
  63545. #endif
  63546. return EXPECT_RESULT();
  63547. }
  63548. static int test_read_write_hs(void)
  63549. {
  63550. EXPECT_DECLS;
  63551. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  63552. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  63553. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  63554. struct test_memio_ctx test_ctx;
  63555. byte test_buffer[16];
  63556. unsigned int test;
  63557. /* test == 0 : client writes, server reads */
  63558. /* test == 1 : server writes, client reads */
  63559. for (test = 0; test < 2; test++) {
  63560. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63561. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63562. wolfTLSv1_2_client_method,
  63563. wolfTLSv1_2_server_method), 0);
  63564. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  63565. /* CH -> */
  63566. if (test == 0) {
  63567. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  63568. } else {
  63569. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63570. sizeof(test_buffer)), -1);
  63571. }
  63572. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63573. /* <- SH + SKE + SHD */
  63574. if (test == 0) {
  63575. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63576. sizeof(test_buffer)), -1);
  63577. } else {
  63578. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  63579. }
  63580. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63581. /* -> CKE + CLIENT FINISHED */
  63582. if (test == 0) {
  63583. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  63584. } else {
  63585. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63586. sizeof(test_buffer)), -1);
  63587. }
  63588. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63589. /* abide clang static analyzer */
  63590. if (ssl_s != NULL) {
  63591. /* disable group message to separate sending of ChangeCipherspec
  63592. * from Finished */
  63593. ssl_s->options.groupMessages = 0;
  63594. }
  63595. /* allow writing of CS, but not FINISHED */
  63596. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  63597. /* <- CS */
  63598. if (test == 0) {
  63599. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63600. sizeof(test_buffer)), -1);
  63601. } else {
  63602. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  63603. }
  63604. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  63605. /* move CS message where the client can read it */
  63606. memmove(test_ctx.c_buff,
  63607. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  63608. test_ctx.c_len = 6;
  63609. /* read CS */
  63610. if (test == 0) {
  63611. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  63612. } else {
  63613. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63614. sizeof(test_buffer)), -1);
  63615. }
  63616. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63617. ExpectIntEQ(test_ctx.c_len, 0);
  63618. if (test == 0) {
  63619. /* send SERVER FINISHED */
  63620. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63621. sizeof(test_buffer)), -1);
  63622. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  63623. WOLFSSL_ERROR_WANT_READ);
  63624. } else {
  63625. /* send SERVER FINISHED + App Data */
  63626. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  63627. }
  63628. ExpectIntGT(test_ctx.c_len, 0);
  63629. /* Send and receive the data */
  63630. if (test == 0) {
  63631. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  63632. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63633. sizeof(test_buffer)), 5);
  63634. } else {
  63635. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63636. sizeof(test_buffer)), 5);
  63637. }
  63638. ExpectBufEQ(test_buffer, "hello", 5);
  63639. wolfSSL_free(ssl_c);
  63640. wolfSSL_free(ssl_s);
  63641. wolfSSL_CTX_free(ctx_c);
  63642. wolfSSL_CTX_free(ctx_s);
  63643. ssl_c = ssl_s = NULL;
  63644. ctx_c = ctx_s = NULL;
  63645. }
  63646. #endif
  63647. return EXPECT_RESULT();
  63648. }
  63649. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  63650. static const char* test_get_signature_nid_siglag;
  63651. static int test_get_signature_nid_sig;
  63652. static int test_get_signature_nid_hash;
  63653. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  63654. {
  63655. EXPECT_DECLS;
  63656. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  63657. if (!wolfSSL_is_server(ssl)) {
  63658. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  63659. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  63660. }
  63661. return EXPECT_RESULT();
  63662. }
  63663. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63664. {
  63665. EXPECT_DECLS;
  63666. int nid = 0;
  63667. (void)ctx;
  63668. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  63669. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63670. ExpectIntEQ(nid, test_get_signature_nid_sig);
  63671. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63672. ExpectIntEQ(nid, test_get_signature_nid_hash);
  63673. }
  63674. else /* No sigalg info on static ciphersuite */
  63675. return TEST_SUCCESS;
  63676. return EXPECT_RESULT();
  63677. }
  63678. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63679. {
  63680. EXPECT_DECLS;
  63681. int nid = 0;
  63682. (void)ctx;
  63683. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63684. ExpectIntEQ(nid, test_get_signature_nid_sig);
  63685. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63686. ExpectIntEQ(nid, test_get_signature_nid_hash);
  63687. return EXPECT_RESULT();
  63688. }
  63689. #endif
  63690. static int test_get_signature_nid(void)
  63691. {
  63692. EXPECT_DECLS;
  63693. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  63694. test_ssl_cbf client_cbf;
  63695. test_ssl_cbf server_cbf;
  63696. size_t i;
  63697. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  63698. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  63699. caCertFile }
  63700. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  63701. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  63702. caEccCertFile }
  63703. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  63704. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  63705. caCertFile }
  63706. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  63707. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  63708. caEccCertFile }
  63709. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  63710. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  63711. caEdCertFile }
  63712. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  63713. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  63714. caEd448CertFile }
  63715. struct {
  63716. const char* siglag;
  63717. int sig_nid;
  63718. int hash_nid;
  63719. int tls_ver;
  63720. const char* server_cert;
  63721. const char* server_key;
  63722. const char* client_ca;
  63723. } params[] = {
  63724. #ifndef NO_RSA
  63725. #ifndef NO_SHA256
  63726. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  63727. #ifdef WC_RSA_PSS
  63728. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  63729. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  63730. #endif
  63731. #endif
  63732. #ifdef WOLFSSL_SHA512
  63733. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  63734. #ifdef WC_RSA_PSS
  63735. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  63736. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  63737. #endif
  63738. #endif
  63739. #endif
  63740. #ifdef HAVE_ECC
  63741. #ifndef NO_SHA256
  63742. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  63743. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  63744. #endif
  63745. #endif
  63746. #ifdef HAVE_ED25519
  63747. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  63748. #endif
  63749. #ifdef HAVE_ED448
  63750. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  63751. #endif
  63752. };
  63753. printf("\n");
  63754. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  63755. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63756. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63757. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  63758. params[i].siglag);
  63759. switch (params[i].tls_ver) {
  63760. #ifndef WOLFSSL_NO_TLS12
  63761. case WOLFSSL_TLSV1_2:
  63762. client_cbf.method = wolfTLSv1_2_client_method;
  63763. server_cbf.method = wolfTLSv1_2_server_method;
  63764. break;
  63765. #endif
  63766. #ifdef WOLFSSL_TLS13
  63767. case WOLFSSL_TLSV1_3:
  63768. client_cbf.method = wolfTLSv1_3_client_method;
  63769. server_cbf.method = wolfTLSv1_3_server_method;
  63770. break;
  63771. #endif
  63772. default:
  63773. printf("skipping\n");
  63774. continue;
  63775. }
  63776. test_get_signature_nid_siglag = params[i].siglag;
  63777. test_get_signature_nid_sig = params[i].sig_nid;
  63778. test_get_signature_nid_hash = params[i].hash_nid;
  63779. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  63780. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  63781. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  63782. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  63783. server_cbf.certPemFile = params[i].server_cert;
  63784. server_cbf.keyPemFile = params[i].server_key;
  63785. client_cbf.caPemFile = params[i].client_ca;
  63786. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63787. &server_cbf, NULL), TEST_SUCCESS);
  63788. if (EXPECT_SUCCESS())
  63789. printf("passed\n");
  63790. }
  63791. #endif
  63792. return EXPECT_RESULT();
  63793. }
  63794. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63795. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  63796. {
  63797. #ifndef NO_MD5
  63798. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  63799. #elif !defined(NO_SHA)
  63800. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  63801. #elif !defined(NO_SHA256)
  63802. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  63803. #else
  63804. #error "We need a digest to hash the Signer object"
  63805. #endif
  63806. byte hashBuf[WC_MAX_DIGEST_SIZE];
  63807. wc_HashAlg hash;
  63808. size_t i;
  63809. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  63810. for (i = 0; i < CA_TABLE_SIZE; i++) {
  63811. Signer* cur;
  63812. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  63813. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  63814. sizeof(*cur)), 0);
  63815. }
  63816. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  63817. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  63818. return MakeWordFromHash(hashBuf);
  63819. }
  63820. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  63821. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  63822. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  63823. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  63824. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  63825. {
  63826. EXPECT_DECLS;
  63827. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  63828. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  63829. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  63830. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  63831. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  63832. return EXPECT_RESULT();
  63833. }
  63834. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  63835. {
  63836. EXPECT_DECLS;
  63837. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  63838. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  63839. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  63840. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  63841. return EXPECT_RESULT();
  63842. }
  63843. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63844. {
  63845. EXPECT_DECLS;
  63846. WOLFSSL_CERT_MANAGER* cm;
  63847. (void)ssl;
  63848. /* WARNING: this approach bypasses the reference counter check in
  63849. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  63850. * cause unexpected behaviour when other active connections try accessing
  63851. * the caTable. */
  63852. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  63853. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  63854. WOLFSSL_SUCCESS);
  63855. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  63856. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  63857. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  63858. return EXPECT_RESULT();
  63859. }
  63860. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  63861. {
  63862. EXPECT_DECLS;
  63863. WOLFSSL_CTX* ctx;
  63864. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  63865. return EXPECT_RESULT();
  63866. }
  63867. #endif
  63868. static int test_tls_cert_store_unchanged(void)
  63869. {
  63870. EXPECT_DECLS;
  63871. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63872. test_ssl_cbf client_cbf;
  63873. test_ssl_cbf server_cbf;
  63874. int i;
  63875. for (i = 0; i < 2; i++) {
  63876. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63877. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63878. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  63879. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  63880. sizeof(test_tls_cert_store_unchanged_before_hashes));
  63881. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  63882. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  63883. sizeof(test_tls_cert_store_unchanged_after_hashes));
  63884. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  63885. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  63886. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  63887. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  63888. switch (i) {
  63889. case 0:
  63890. client_cbf.on_ctx_cleanup =
  63891. test_tls_cert_store_unchanged_ctx_cleanup;
  63892. server_cbf.on_ctx_cleanup =
  63893. test_tls_cert_store_unchanged_ctx_cleanup;
  63894. break;
  63895. case 1:
  63896. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  63897. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  63898. break;
  63899. default:
  63900. Fail(("Should not enter here"), ("Entered here"));
  63901. }
  63902. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  63903. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  63904. server_cbf.caPemFile = caCertFile;
  63905. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63906. &server_cbf, NULL), TEST_SUCCESS);
  63907. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  63908. test_tls_cert_store_unchanged_after_hashes,
  63909. sizeof(test_tls_cert_store_unchanged_after_hashes));
  63910. }
  63911. #endif
  63912. return EXPECT_RESULT();
  63913. }
  63914. static int test_wolfSSL_SendUserCanceled(void)
  63915. {
  63916. EXPECT_DECLS;
  63917. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  63918. size_t i;
  63919. struct {
  63920. method_provider client_meth;
  63921. method_provider server_meth;
  63922. const char* tls_version;
  63923. } params[] = {
  63924. #if defined(WOLFSSL_TLS13)
  63925. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  63926. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  63927. #ifdef WOLFSSL_DTLS13
  63928. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  63929. #endif
  63930. #endif
  63931. #ifndef WOLFSSL_NO_TLS12
  63932. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  63933. #ifdef WOLFSSL_DTLS
  63934. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  63935. #endif
  63936. #endif
  63937. #if !defined(NO_OLD_TLS)
  63938. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  63939. #ifdef WOLFSSL_DTLS
  63940. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  63941. #endif
  63942. #endif
  63943. };
  63944. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63945. WOLFSSL_CTX *ctx_c = NULL;
  63946. WOLFSSL_CTX *ctx_s = NULL;
  63947. WOLFSSL *ssl_c = NULL;
  63948. WOLFSSL *ssl_s = NULL;
  63949. struct test_memio_ctx test_ctx;
  63950. WOLFSSL_ALERT_HISTORY h;
  63951. printf("Testing %s\n", params[i].tls_version);
  63952. XMEMSET(&h, 0, sizeof(h));
  63953. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63954. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63955. params[i].client_meth, params[i].server_meth), 0);
  63956. /* CH1 */
  63957. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63958. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63959. ExpectIntEQ(wolfSSL_SendUserCanceled(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
  63960. /* Alert closed connection */
  63961. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63962. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_ZERO_RETURN);
  63963. /* Last alert will be close notify because user_canceled should be
  63964. * followed by a close_notify */
  63965. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  63966. ExpectIntEQ(h.last_rx.code, close_notify);
  63967. ExpectIntEQ(h.last_rx.level, alert_warning);
  63968. wolfSSL_free(ssl_c);
  63969. wolfSSL_free(ssl_s);
  63970. wolfSSL_CTX_free(ctx_c);
  63971. wolfSSL_CTX_free(ctx_s);
  63972. }
  63973. #endif
  63974. return EXPECT_RESULT();
  63975. }
  63976. /*----------------------------------------------------------------------------*
  63977. | Main
  63978. *----------------------------------------------------------------------------*/
  63979. typedef int (*TEST_FUNC)(void);
  63980. typedef struct {
  63981. const char *name;
  63982. TEST_FUNC func;
  63983. byte run:1;
  63984. byte fail:1;
  63985. } TEST_CASE;
  63986. #define TEST_DECL(func) { #func, func, 0, 0 }
  63987. int testAll = 1;
  63988. TEST_CASE testCases[] = {
  63989. TEST_DECL(test_fileAccess),
  63990. /*********************************
  63991. * wolfcrypt
  63992. *********************************/
  63993. TEST_DECL(test_ForceZero),
  63994. TEST_DECL(test_wolfCrypt_Init),
  63995. TEST_DECL(test_wc_LoadStaticMemory_ex),
  63996. /* Locking with Compat Mutex */
  63997. TEST_DECL(test_wc_SetMutexCb),
  63998. TEST_DECL(test_wc_LockMutex_ex),
  63999. /* Digests */
  64000. TEST_DECL(test_wc_InitMd5),
  64001. TEST_DECL(test_wc_Md5Update),
  64002. TEST_DECL(test_wc_Md5Final),
  64003. TEST_DECL(test_wc_InitSha),
  64004. TEST_DECL(test_wc_ShaUpdate),
  64005. TEST_DECL(test_wc_ShaFinal),
  64006. TEST_DECL(test_wc_InitSha256),
  64007. TEST_DECL(test_wc_Sha256Update),
  64008. TEST_DECL(test_wc_Sha256Final),
  64009. TEST_DECL(test_wc_Sha256FinalRaw),
  64010. TEST_DECL(test_wc_Sha256GetFlags),
  64011. TEST_DECL(test_wc_Sha256Free),
  64012. TEST_DECL(test_wc_Sha256GetHash),
  64013. TEST_DECL(test_wc_Sha256Copy),
  64014. TEST_DECL(test_wc_InitSha224),
  64015. TEST_DECL(test_wc_Sha224Update),
  64016. TEST_DECL(test_wc_Sha224Final),
  64017. TEST_DECL(test_wc_Sha224SetFlags),
  64018. TEST_DECL(test_wc_Sha224GetFlags),
  64019. TEST_DECL(test_wc_Sha224Free),
  64020. TEST_DECL(test_wc_Sha224GetHash),
  64021. TEST_DECL(test_wc_Sha224Copy),
  64022. TEST_DECL(test_wc_InitSha512),
  64023. TEST_DECL(test_wc_Sha512Update),
  64024. TEST_DECL(test_wc_Sha512Final),
  64025. TEST_DECL(test_wc_Sha512GetFlags),
  64026. TEST_DECL(test_wc_Sha512FinalRaw),
  64027. TEST_DECL(test_wc_Sha512Free),
  64028. TEST_DECL(test_wc_Sha512GetHash),
  64029. TEST_DECL(test_wc_Sha512Copy),
  64030. TEST_DECL(test_wc_InitSha512_224),
  64031. TEST_DECL(test_wc_Sha512_224Update),
  64032. TEST_DECL(test_wc_Sha512_224Final),
  64033. TEST_DECL(test_wc_Sha512_224GetFlags),
  64034. TEST_DECL(test_wc_Sha512_224FinalRaw),
  64035. TEST_DECL(test_wc_Sha512_224Free),
  64036. TEST_DECL(test_wc_Sha512_224GetHash),
  64037. TEST_DECL(test_wc_Sha512_224Copy),
  64038. TEST_DECL(test_wc_InitSha512_256),
  64039. TEST_DECL(test_wc_Sha512_256Update),
  64040. TEST_DECL(test_wc_Sha512_256Final),
  64041. TEST_DECL(test_wc_Sha512_256GetFlags),
  64042. TEST_DECL(test_wc_Sha512_256FinalRaw),
  64043. TEST_DECL(test_wc_Sha512_256Free),
  64044. TEST_DECL(test_wc_Sha512_256GetHash),
  64045. TEST_DECL(test_wc_Sha512_256Copy),
  64046. TEST_DECL(test_wc_InitSha384),
  64047. TEST_DECL(test_wc_Sha384Update),
  64048. TEST_DECL(test_wc_Sha384Final),
  64049. TEST_DECL(test_wc_Sha384GetFlags),
  64050. TEST_DECL(test_wc_Sha384FinalRaw),
  64051. TEST_DECL(test_wc_Sha384Free),
  64052. TEST_DECL(test_wc_Sha384GetHash),
  64053. TEST_DECL(test_wc_Sha384Copy),
  64054. TEST_DECL(test_wc_InitBlake2b),
  64055. TEST_DECL(test_wc_InitBlake2b_WithKey),
  64056. TEST_DECL(test_wc_InitBlake2s_WithKey),
  64057. TEST_DECL(test_wc_InitRipeMd),
  64058. TEST_DECL(test_wc_RipeMdUpdate),
  64059. TEST_DECL(test_wc_RipeMdFinal),
  64060. TEST_DECL(test_wc_InitSha3),
  64061. TEST_DECL(testing_wc_Sha3_Update),
  64062. TEST_DECL(test_wc_Sha3_224_Final),
  64063. TEST_DECL(test_wc_Sha3_256_Final),
  64064. TEST_DECL(test_wc_Sha3_384_Final),
  64065. TEST_DECL(test_wc_Sha3_512_Final),
  64066. TEST_DECL(test_wc_Sha3_224_Copy),
  64067. TEST_DECL(test_wc_Sha3_256_Copy),
  64068. TEST_DECL(test_wc_Sha3_384_Copy),
  64069. TEST_DECL(test_wc_Sha3_512_Copy),
  64070. TEST_DECL(test_wc_Sha3_GetFlags),
  64071. TEST_DECL(test_wc_InitShake256),
  64072. TEST_DECL(testing_wc_Shake256_Update),
  64073. TEST_DECL(test_wc_Shake256_Final),
  64074. TEST_DECL(test_wc_Shake256_Copy),
  64075. TEST_DECL(test_wc_Shake256Hash),
  64076. /* SM3 Digest */
  64077. TEST_DECL(test_wc_InitSm3Free),
  64078. TEST_DECL(test_wc_Sm3UpdateFinal),
  64079. TEST_DECL(test_wc_Sm3GetHash),
  64080. TEST_DECL(test_wc_Sm3Copy),
  64081. TEST_DECL(test_wc_Sm3FinalRaw),
  64082. TEST_DECL(test_wc_Sm3GetSetFlags),
  64083. TEST_DECL(test_wc_Sm3Hash),
  64084. TEST_DECL(test_wc_HashInit),
  64085. TEST_DECL(test_wc_HashSetFlags),
  64086. TEST_DECL(test_wc_HashGetFlags),
  64087. /* HMAC */
  64088. TEST_DECL(test_wc_Md5HmacSetKey),
  64089. TEST_DECL(test_wc_Md5HmacUpdate),
  64090. TEST_DECL(test_wc_Md5HmacFinal),
  64091. TEST_DECL(test_wc_ShaHmacSetKey),
  64092. TEST_DECL(test_wc_ShaHmacUpdate),
  64093. TEST_DECL(test_wc_ShaHmacFinal),
  64094. TEST_DECL(test_wc_Sha224HmacSetKey),
  64095. TEST_DECL(test_wc_Sha224HmacUpdate),
  64096. TEST_DECL(test_wc_Sha224HmacFinal),
  64097. TEST_DECL(test_wc_Sha256HmacSetKey),
  64098. TEST_DECL(test_wc_Sha256HmacUpdate),
  64099. TEST_DECL(test_wc_Sha256HmacFinal),
  64100. TEST_DECL(test_wc_Sha384HmacSetKey),
  64101. TEST_DECL(test_wc_Sha384HmacUpdate),
  64102. TEST_DECL(test_wc_Sha384HmacFinal),
  64103. /* CMAC */
  64104. TEST_DECL(test_wc_InitCmac),
  64105. TEST_DECL(test_wc_CmacUpdate),
  64106. TEST_DECL(test_wc_CmacFinal),
  64107. TEST_DECL(test_wc_AesCmacGenerate),
  64108. /* Cipher */
  64109. TEST_DECL(test_wc_AesGcmStream),
  64110. TEST_DECL(test_wc_Des3_SetIV),
  64111. TEST_DECL(test_wc_Des3_SetKey),
  64112. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  64113. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  64114. TEST_DECL(test_wc_Des3_EcbEncrypt),
  64115. TEST_DECL(test_wc_Chacha_SetKey),
  64116. TEST_DECL(test_wc_Chacha_Process),
  64117. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  64118. TEST_DECL(test_wc_Poly1305SetKey),
  64119. TEST_DECL(test_wc_CamelliaSetKey),
  64120. TEST_DECL(test_wc_CamelliaSetIV),
  64121. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  64122. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  64123. TEST_DECL(test_wc_Arc4SetKey),
  64124. TEST_DECL(test_wc_Arc4Process),
  64125. TEST_DECL(test_wc_Rc2SetKey),
  64126. TEST_DECL(test_wc_Rc2SetIV),
  64127. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  64128. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  64129. /* AES cipher and GMAC. */
  64130. TEST_DECL(test_wc_AesSetKey),
  64131. TEST_DECL(test_wc_AesSetIV),
  64132. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  64133. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  64134. TEST_DECL(test_wc_AesGcmSetKey),
  64135. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  64136. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  64137. TEST_DECL(test_wc_GmacSetKey),
  64138. TEST_DECL(test_wc_GmacUpdate),
  64139. TEST_DECL(test_wc_AesCcmSetKey),
  64140. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  64141. #if defined(WOLFSSL_AES_EAX) && \
  64142. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  64143. TEST_DECL(test_wc_AesEaxVectors),
  64144. TEST_DECL(test_wc_AesEaxEncryptAuth),
  64145. TEST_DECL(test_wc_AesEaxDecryptAuth),
  64146. #endif /* WOLFSSL_AES_EAX */
  64147. /* SM4 cipher */
  64148. TEST_DECL(test_wc_Sm4),
  64149. TEST_DECL(test_wc_Sm4Ecb),
  64150. TEST_DECL(test_wc_Sm4Cbc),
  64151. TEST_DECL(test_wc_Sm4Ctr),
  64152. TEST_DECL(test_wc_Sm4Gcm),
  64153. TEST_DECL(test_wc_Sm4Ccm),
  64154. /* RNG tests */
  64155. #ifdef HAVE_HASHDRBG
  64156. #ifdef TEST_RESEED_INTERVAL
  64157. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  64158. #endif
  64159. TEST_DECL(test_wc_RNG_GenerateBlock),
  64160. #endif
  64161. TEST_DECL(test_get_rand_digit),
  64162. TEST_DECL(test_wc_InitRngNonce),
  64163. TEST_DECL(test_wc_InitRngNonce_ex),
  64164. /* MP API tests */
  64165. TEST_DECL(test_get_digit_count),
  64166. TEST_DECL(test_mp_cond_copy),
  64167. TEST_DECL(test_mp_rand),
  64168. TEST_DECL(test_get_digit),
  64169. TEST_DECL(test_wc_export_int),
  64170. /* RSA */
  64171. TEST_DECL(test_wc_InitRsaKey),
  64172. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  64173. TEST_DECL(test_wc_RsaPublicKeyDecode),
  64174. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  64175. TEST_DECL(test_wc_RsaPrivateKeyDecodeRaw),
  64176. TEST_DECL(test_wc_MakeRsaKey),
  64177. TEST_DECL(test_wc_CheckProbablePrime),
  64178. TEST_DECL(test_wc_RsaPSS_Verify),
  64179. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  64180. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  64181. TEST_DECL(test_wc_RsaKeyToDer),
  64182. TEST_DECL(test_wc_RsaKeyToPublicDer),
  64183. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  64184. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  64185. TEST_DECL(test_wc_RsaEncryptSize),
  64186. TEST_DECL(test_wc_RsaSSL_SignVerify),
  64187. TEST_DECL(test_wc_RsaFlattenPublicKey),
  64188. TEST_DECL(test_RsaDecryptBoundsCheck),
  64189. /* DSA */
  64190. TEST_DECL(test_wc_InitDsaKey),
  64191. TEST_DECL(test_wc_DsaSignVerify),
  64192. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  64193. TEST_DECL(test_wc_MakeDsaKey),
  64194. TEST_DECL(test_wc_DsaKeyToDer),
  64195. TEST_DECL(test_wc_DsaKeyToPublicDer),
  64196. TEST_DECL(test_wc_DsaImportParamsRaw),
  64197. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  64198. TEST_DECL(test_wc_DsaExportParamsRaw),
  64199. TEST_DECL(test_wc_DsaExportKeyRaw),
  64200. /* DH */
  64201. TEST_DECL(test_wc_DhPublicKeyDecode),
  64202. /* wolfCrypt ECC tests */
  64203. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  64204. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  64205. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  64206. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  64207. !defined(HAVE_SELFTEST) && \
  64208. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  64209. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  64210. #endif
  64211. TEST_DECL(test_wc_ecc_make_key),
  64212. TEST_DECL(test_wc_ecc_init),
  64213. TEST_DECL(test_wc_ecc_check_key),
  64214. TEST_DECL(test_wc_ecc_get_generator),
  64215. TEST_DECL(test_wc_ecc_size),
  64216. TEST_DECL(test_wc_ecc_params),
  64217. TEST_DECL(test_wc_ecc_signVerify_hash),
  64218. TEST_DECL(test_wc_ecc_shared_secret),
  64219. TEST_DECL(test_wc_ecc_export_x963),
  64220. TEST_DECL(test_wc_ecc_export_x963_ex),
  64221. TEST_DECL(test_wc_ecc_import_x963),
  64222. TEST_DECL(test_wc_ecc_import_private_key),
  64223. TEST_DECL(test_wc_ecc_export_private_only),
  64224. TEST_DECL(test_wc_ecc_rs_to_sig),
  64225. TEST_DECL(test_wc_ecc_import_raw),
  64226. TEST_DECL(test_wc_ecc_import_unsigned),
  64227. TEST_DECL(test_wc_ecc_sig_size),
  64228. TEST_DECL(test_wc_ecc_ctx_new),
  64229. TEST_DECL(test_wc_ecc_ctx_reset),
  64230. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  64231. TEST_DECL(test_wc_ecc_ctx_set_info),
  64232. TEST_DECL(test_wc_ecc_encryptDecrypt),
  64233. TEST_DECL(test_wc_ecc_del_point),
  64234. TEST_DECL(test_wc_ecc_pointFns),
  64235. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  64236. TEST_DECL(test_wc_ecc_verify_hash_ex),
  64237. TEST_DECL(test_wc_ecc_mulmod),
  64238. TEST_DECL(test_wc_ecc_is_valid_idx),
  64239. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  64240. TEST_DECL(test_wc_ecc_sig_size_calc),
  64241. TEST_DECL(test_wc_EccPrivateKeyToDer),
  64242. /* SM2 elliptic curve */
  64243. TEST_DECL(test_wc_ecc_sm2_make_key),
  64244. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  64245. TEST_DECL(test_wc_ecc_sm2_create_digest),
  64246. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  64247. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  64248. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  64249. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  64250. /* Curve25519 */
  64251. TEST_DECL(test_wc_curve25519_init),
  64252. TEST_DECL(test_wc_curve25519_size),
  64253. TEST_DECL(test_wc_curve25519_export_key_raw),
  64254. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  64255. TEST_DECL(test_wc_curve25519_make_key),
  64256. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  64257. TEST_DECL(test_wc_curve25519_make_pub),
  64258. TEST_DECL(test_wc_curve25519_export_public_ex),
  64259. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  64260. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  64261. TEST_DECL(test_wc_curve25519_import_private),
  64262. /* ED25519 */
  64263. TEST_DECL(test_wc_ed25519_make_key),
  64264. TEST_DECL(test_wc_ed25519_init),
  64265. TEST_DECL(test_wc_ed25519_sign_msg),
  64266. TEST_DECL(test_wc_ed25519_import_public),
  64267. TEST_DECL(test_wc_ed25519_import_private_key),
  64268. TEST_DECL(test_wc_ed25519_export),
  64269. TEST_DECL(test_wc_ed25519_size),
  64270. TEST_DECL(test_wc_ed25519_exportKey),
  64271. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  64272. TEST_DECL(test_wc_Ed25519KeyToDer),
  64273. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  64274. /* Curve448 */
  64275. TEST_DECL(test_wc_curve448_make_key),
  64276. TEST_DECL(test_wc_curve448_shared_secret_ex),
  64277. TEST_DECL(test_wc_curve448_export_public_ex),
  64278. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  64279. TEST_DECL(test_wc_curve448_export_key_raw),
  64280. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  64281. TEST_DECL(test_wc_curve448_import_private),
  64282. TEST_DECL(test_wc_curve448_init),
  64283. TEST_DECL(test_wc_curve448_size),
  64284. /* Ed448 */
  64285. TEST_DECL(test_wc_ed448_make_key),
  64286. TEST_DECL(test_wc_ed448_init),
  64287. TEST_DECL(test_wc_ed448_sign_msg),
  64288. TEST_DECL(test_wc_ed448_import_public),
  64289. TEST_DECL(test_wc_ed448_import_private_key),
  64290. TEST_DECL(test_wc_ed448_export),
  64291. TEST_DECL(test_wc_ed448_size),
  64292. TEST_DECL(test_wc_ed448_exportKey),
  64293. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  64294. TEST_DECL(test_wc_Ed448KeyToDer),
  64295. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  64296. TEST_DECL(test_wc_Curve448PrivateKeyToDer),
  64297. /* Signature API */
  64298. TEST_DECL(test_wc_SignatureGetSize_ecc),
  64299. TEST_DECL(test_wc_SignatureGetSize_rsa),
  64300. /* PEM and DER APIs. */
  64301. TEST_DECL(test_wc_PemToDer),
  64302. TEST_DECL(test_wc_AllocDer),
  64303. TEST_DECL(test_wc_CertPemToDer),
  64304. TEST_DECL(test_wc_KeyPemToDer),
  64305. TEST_DECL(test_wc_PubKeyPemToDer),
  64306. TEST_DECL(test_wc_PemPubKeyToDer),
  64307. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  64308. TEST_DECL(test_wc_CheckCertSigPubKey),
  64309. /* wolfCrypt ASN tests */
  64310. TEST_DECL(test_ToTraditional),
  64311. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  64312. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  64313. /* Certificate */
  64314. TEST_DECL(test_wc_SetSubjectRaw),
  64315. TEST_DECL(test_wc_GetSubjectRaw),
  64316. TEST_DECL(test_wc_SetIssuerRaw),
  64317. TEST_DECL(test_wc_SetIssueBuffer),
  64318. TEST_DECL(test_wc_SetSubjectKeyId),
  64319. TEST_DECL(test_wc_SetSubject),
  64320. TEST_DECL(test_CheckCertSignature),
  64321. TEST_DECL(test_wc_ParseCert),
  64322. TEST_DECL(test_wc_ParseCert_Error),
  64323. TEST_DECL(test_MakeCertWithPathLen),
  64324. TEST_DECL(test_MakeCertWith0Ser),
  64325. TEST_DECL(test_MakeCertWithCaFalse),
  64326. TEST_DECL(test_wc_SetKeyUsage),
  64327. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  64328. TEST_DECL(test_wc_SetSubjectBuffer),
  64329. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  64330. /* wolfcrypt PKCS#7 */
  64331. TEST_DECL(test_wc_PKCS7_New),
  64332. TEST_DECL(test_wc_PKCS7_Init),
  64333. TEST_DECL(test_wc_PKCS7_InitWithCert),
  64334. TEST_DECL(test_wc_PKCS7_EncodeData),
  64335. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  64336. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  64337. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  64338. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  64339. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  64340. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  64341. TEST_DECL(test_wc_PKCS7_Degenerate),
  64342. TEST_DECL(test_wc_PKCS7_BER),
  64343. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  64344. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  64345. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  64346. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  64347. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  64348. /* wolfCrypt PKCS#12 */
  64349. TEST_DECL(test_wc_i2d_PKCS12),
  64350. /*
  64351. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  64352. * avoid memory leaks.
  64353. */
  64354. TEST_DECL(test_wolfCrypt_Cleanup),
  64355. TEST_DECL(test_wolfSSL_Init),
  64356. TEST_DECL(test_dual_alg_support),
  64357. /*********************************
  64358. * OpenSSL compatibility API tests
  64359. *********************************/
  64360. /* If at some point a stub get implemented this test should fail indicating
  64361. * a need to implement a new test case
  64362. */
  64363. TEST_DECL(test_stubs_are_stubs),
  64364. /* ASN.1 compatibility API tests */
  64365. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  64366. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  64367. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  64368. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  64369. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  64370. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  64371. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  64372. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  64373. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  64374. TEST_DECL(test_wolfSSL_ASN1_get_object),
  64375. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  64376. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  64377. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  64378. TEST_DECL(test_wolfSSL_ASN1_STRING),
  64379. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  64380. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  64381. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  64382. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  64383. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  64384. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  64385. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  64386. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  64387. TEST_DECL(test_wolfSSL_ASN1_TIME),
  64388. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  64389. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  64390. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  64391. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  64392. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  64393. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  64394. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  64395. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  64396. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  64397. TEST_DECL(test_wolfSSL_lhash),
  64398. TEST_DECL(test_wolfSSL_certs),
  64399. TEST_DECL(test_wolfSSL_private_keys),
  64400. TEST_DECL(test_wolfSSL_PEM_def_callback),
  64401. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  64402. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  64403. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  64404. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  64405. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  64406. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  64407. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  64408. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  64409. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  64410. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  64411. #ifndef NO_BIO
  64412. TEST_DECL(test_wolfSSL_BIO),
  64413. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  64414. TEST_DECL(test_wolfSSL_PEM_read_bio),
  64415. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  64416. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  64417. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  64418. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  64419. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  64420. #endif
  64421. /* EVP API testing */
  64422. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  64423. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  64424. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  64425. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  64426. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  64427. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  64428. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  64429. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  64430. TEST_DECL(test_wolfSSL_EVP_shake128),
  64431. TEST_DECL(test_wolfSSL_EVP_shake256),
  64432. TEST_DECL(test_wolfSSL_EVP_sm3),
  64433. TEST_DECL(test_EVP_blake2),
  64434. #ifdef OPENSSL_ALL
  64435. TEST_DECL(test_wolfSSL_EVP_md4),
  64436. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  64437. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  64438. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  64439. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  64440. #endif
  64441. TEST_DECL(test_EVP_MD_do_all),
  64442. TEST_DECL(test_wolfSSL_EVP_MD_size),
  64443. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  64444. TEST_DECL(test_wolfSSL_EVP_Digest),
  64445. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  64446. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  64447. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  64448. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  64449. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  64450. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  64451. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  64452. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  64453. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  64454. TEST_DECL(test_wolfssl_EVP_chacha20),
  64455. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  64456. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  64457. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  64458. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  64459. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  64460. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  64461. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  64462. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  64463. #ifdef OPENSSL_ALL
  64464. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  64465. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  64466. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  64467. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  64468. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  64469. TEST_DECL(test_wolfSSL_EVP_rc4),
  64470. TEST_DECL(test_wolfSSL_EVP_enc_null),
  64471. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  64472. TEST_DECL(test_wolfSSL_EVP_mdc2),
  64473. TEST_DECL(test_evp_cipher_aes_gcm),
  64474. #endif
  64475. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  64476. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  64477. #ifdef OPENSSL_EXTRA
  64478. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  64479. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  64480. #endif
  64481. #ifdef OPENSSL_ALL
  64482. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  64483. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  64484. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  64485. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  64486. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  64487. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  64488. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  64489. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  64490. #endif
  64491. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  64492. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  64493. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  64494. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  64495. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  64496. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  64497. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  64498. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  64499. #ifndef NO_BIO
  64500. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  64501. #endif
  64502. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  64503. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  64504. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  64505. #ifndef NO_BIO
  64506. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  64507. #endif /* !NO_BIO */
  64508. #endif
  64509. #ifdef OPENSSL_ALL
  64510. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  64511. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  64512. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  64513. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  64514. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  64515. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  64516. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  64517. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  64518. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  64519. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  64520. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  64521. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  64522. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  64523. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  64524. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  64525. #endif
  64526. TEST_DECL(test_EVP_PKEY_rsa),
  64527. TEST_DECL(test_EVP_PKEY_ec),
  64528. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  64529. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  64530. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  64531. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  64532. TEST_DECL(test_EVP_PKEY_cmp),
  64533. #ifdef OPENSSL_ALL
  64534. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  64535. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  64536. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  64537. #endif
  64538. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  64539. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  64540. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  64541. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  64542. #endif
  64543. #ifndef NO_BIO
  64544. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  64545. TEST_DECL(test_wolfSSL_GetLoggingCb),
  64546. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  64547. TEST_DECL(test_wc_ERR_remove_state),
  64548. TEST_DECL(test_wc_ERR_print_errors_fp),
  64549. #endif
  64550. TEST_DECL(test_wolfSSL_configure_args),
  64551. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  64552. TEST_DECL(test_wolfSSL_set1_curves_list),
  64553. TEST_DECL(test_wolfSSL_curves_mismatch),
  64554. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  64555. TEST_DECL(test_wolfSSL_OtherName),
  64556. TEST_DECL(test_wolfSSL_FPKI),
  64557. TEST_DECL(test_wolfSSL_URI),
  64558. TEST_DECL(test_wolfSSL_TBS),
  64559. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  64560. TEST_DECL(test_X509_STORE_untrusted),
  64561. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  64562. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  64563. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  64564. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  64565. TEST_DECL(test_wolfSSL_X509_Name_canon),
  64566. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  64567. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  64568. TEST_DECL(test_wolfSSL_X509_NID),
  64569. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  64570. TEST_DECL(test_wolfSSL_get0_param),
  64571. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  64572. TEST_DECL(test_wolfSSL_set1_host),
  64573. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  64574. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  64575. TEST_DECL(test_wolfSSL_X509_STORE),
  64576. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  64577. TEST_DECL(test_X509_STORE_get0_objects),
  64578. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  64579. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  64580. TEST_DECL(test_wolfSSL_X509_STORE_set_get_crl),
  64581. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  64582. TEST_DECL(test_wolfSSL_X509_cmp_time),
  64583. TEST_DECL(test_wolfSSL_X509_time_adj),
  64584. /* X509 tests */
  64585. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  64586. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  64587. TEST_DECL(test_wolfSSL_X509_check_host),
  64588. TEST_DECL(test_wolfSSL_X509_check_email),
  64589. TEST_DECL(test_wolfSSL_X509_check_private_key),
  64590. TEST_DECL(test_wolfSSL_X509),
  64591. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  64592. TEST_DECL(test_wolfSSL_X509_sign),
  64593. TEST_DECL(test_wolfSSL_X509_sign2),
  64594. TEST_DECL(test_wolfSSL_X509_verify),
  64595. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  64596. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  64597. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  64598. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  64599. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  64600. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  64601. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  64602. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  64603. TEST_DECL(test_wolfSSL_X509_NAME),
  64604. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  64605. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  64606. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  64607. TEST_DECL(test_wolfSSL_X509_set_name),
  64608. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  64609. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  64610. TEST_DECL(test_wolfSSL_X509_set_version),
  64611. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  64612. TEST_DECL(test_wolfSSL_X509_CRL),
  64613. TEST_DECL(test_wolfSSL_i2d_X509),
  64614. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  64615. TEST_DECL(test_wolfSSL_PEM_read_X509),
  64616. TEST_DECL(test_wolfSSL_X509_check_ca),
  64617. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  64618. TEST_DECL(test_wolfSSL_X509_bad_altname),
  64619. TEST_DECL(test_wolfSSL_X509_name_match),
  64620. TEST_DECL(test_wolfSSL_X509_name_match2),
  64621. TEST_DECL(test_wolfSSL_X509_name_match3),
  64622. TEST_DECL(test_wolfSSL_X509_max_altnames),
  64623. TEST_DECL(test_wolfSSL_X509_max_name_constraints),
  64624. TEST_DECL(test_wolfSSL_make_cert),
  64625. #ifndef NO_BIO
  64626. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  64627. TEST_DECL(test_wolfSSL_X509_INFO),
  64628. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  64629. #endif
  64630. #ifdef OPENSSL_ALL
  64631. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  64632. #endif
  64633. TEST_DECL(test_wolfSSL_X509_CA_num),
  64634. TEST_DECL(test_wolfSSL_X509_get_version),
  64635. #ifndef NO_BIO
  64636. TEST_DECL(test_wolfSSL_X509_print),
  64637. TEST_DECL(test_wolfSSL_X509_CRL_print),
  64638. #endif
  64639. TEST_DECL(test_X509_get_signature_nid),
  64640. /* X509 extension testing. */
  64641. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  64642. TEST_DECL(test_wolfSSL_X509_get_ext),
  64643. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  64644. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  64645. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  64646. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  64647. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  64648. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  64649. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  64650. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  64651. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  64652. TEST_DECL(test_wolfSSL_X509V3_EXT),
  64653. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  64654. TEST_DECL(test_wolfSSL_X509_cmp),
  64655. TEST_DECL(test_GENERAL_NAME_set0_othername),
  64656. TEST_DECL(test_othername_and_SID_ext),
  64657. TEST_DECL(test_wolfSSL_dup_CA_list),
  64658. /* OpenSSL sk_X509 API test */
  64659. TEST_DECL(test_sk_X509),
  64660. /* OpenSSL sk_X509_CRL API test */
  64661. TEST_DECL(test_sk_X509_CRL),
  64662. /* OpenSSL X509 REQ API test */
  64663. TEST_DECL(test_X509_REQ),
  64664. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  64665. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  64666. TEST_DECL(test_X509_LOOKUP_add_dir),
  64667. /* RAND compatibility API */
  64668. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  64669. TEST_DECL(test_wolfSSL_RAND_bytes),
  64670. TEST_DECL(test_wolfSSL_RAND),
  64671. /* BN compatibility API */
  64672. TEST_DECL(test_wolfSSL_BN_CTX),
  64673. TEST_DECL(test_wolfSSL_BN),
  64674. TEST_DECL(test_wolfSSL_BN_init),
  64675. TEST_DECL(test_wolfSSL_BN_enc_dec),
  64676. TEST_DECL(test_wolfSSL_BN_word),
  64677. TEST_DECL(test_wolfSSL_BN_bits),
  64678. TEST_DECL(test_wolfSSL_BN_shift),
  64679. TEST_DECL(test_wolfSSL_BN_math),
  64680. TEST_DECL(test_wolfSSL_BN_math_mod),
  64681. TEST_DECL(test_wolfSSL_BN_math_other),
  64682. TEST_DECL(test_wolfSSL_BN_rand),
  64683. TEST_DECL(test_wolfSSL_BN_prime),
  64684. /* OpenSSL PKCS5 API test */
  64685. TEST_DECL(test_wolfSSL_PKCS5),
  64686. /* OpenSSL PKCS8 API test */
  64687. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  64688. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  64689. /* OpenSSL PKCS7 API test */
  64690. TEST_DECL(test_wolfssl_PKCS7),
  64691. TEST_DECL(test_wolfSSL_PKCS7_certs),
  64692. TEST_DECL(test_wolfSSL_PKCS7_sign),
  64693. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  64694. #ifndef NO_BIO
  64695. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  64696. #ifdef HAVE_SMIME
  64697. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  64698. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  64699. #endif /* HAVE_SMIME */
  64700. #endif /* !NO_BIO */
  64701. /* OpenSSL PKCS12 API test */
  64702. TEST_DECL(test_wolfSSL_PKCS12),
  64703. /* Can't memory test as callbacks use Assert. */
  64704. TEST_DECL(test_error_queue_per_thread),
  64705. TEST_DECL(test_wolfSSL_ERR_put_error),
  64706. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  64707. #ifndef NO_BIO
  64708. TEST_DECL(test_wolfSSL_ERR_print_errors),
  64709. #endif
  64710. TEST_DECL(test_OBJ_NAME_do_all),
  64711. TEST_DECL(test_wolfSSL_OBJ),
  64712. TEST_DECL(test_wolfSSL_OBJ_cmp),
  64713. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  64714. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  64715. #ifdef OPENSSL_ALL
  64716. TEST_DECL(test_wolfSSL_OBJ_ln),
  64717. TEST_DECL(test_wolfSSL_OBJ_sn),
  64718. #endif
  64719. #ifndef NO_BIO
  64720. TEST_DECL(test_wolfSSL_BIO_gets),
  64721. TEST_DECL(test_wolfSSL_BIO_puts),
  64722. TEST_DECL(test_wolfSSL_BIO_dump),
  64723. /* Can't memory test as server hangs. */
  64724. TEST_DECL(test_wolfSSL_BIO_should_retry),
  64725. TEST_DECL(test_wolfSSL_BIO_write),
  64726. TEST_DECL(test_wolfSSL_BIO_printf),
  64727. TEST_DECL(test_wolfSSL_BIO_f_md),
  64728. TEST_DECL(test_wolfSSL_BIO_up_ref),
  64729. TEST_DECL(test_wolfSSL_BIO_reset),
  64730. TEST_DECL(test_wolfSSL_BIO_get_len),
  64731. #endif
  64732. TEST_DECL(test_wolfSSL_check_domain),
  64733. TEST_DECL(test_wolfSSL_cert_cb),
  64734. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  64735. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  64736. TEST_DECL(test_wolfSSL_sigalg_info),
  64737. /* Can't memory test as tcp_connect aborts. */
  64738. TEST_DECL(test_wolfSSL_SESSION),
  64739. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  64740. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  64741. TEST_DECL(test_wolfSSL_ticket_keys),
  64742. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  64743. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  64744. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  64745. TEST_DECL(test_wolfSSL_verify_mode),
  64746. TEST_DECL(test_wolfSSL_verify_depth),
  64747. TEST_DECL(test_wolfSSL_verify_result),
  64748. TEST_DECL(test_wolfSSL_msg_callback),
  64749. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  64750. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  64751. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  64752. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  64753. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  64754. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  64755. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  64756. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  64757. TEST_DECL(test_wolfSSL_PEM_read),
  64758. TEST_DECL(test_wolfSSL_OpenSSL_version),
  64759. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  64760. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  64761. TEST_DECL(test_CONF_modules_xxx),
  64762. #ifdef OPENSSL_ALL
  64763. TEST_DECL(test_wolfSSL_TXT_DB),
  64764. TEST_DECL(test_wolfSSL_NCONF),
  64765. #endif
  64766. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  64767. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  64768. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  64769. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  64770. TEST_DECL(test_CRYPTO_THREADID_xxx),
  64771. TEST_DECL(test_ENGINE_cleanup),
  64772. /* test the no op functions for compatibility */
  64773. TEST_DECL(test_no_op_functions),
  64774. /* OpenSSL error API tests */
  64775. TEST_DECL(test_ERR_load_crypto_strings),
  64776. #ifdef OPENSSL_ALL
  64777. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  64778. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  64779. TEST_DECL(test_wolfSSL_CTX_ctrl),
  64780. #endif /* OPENSSL_ALL */
  64781. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  64782. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  64783. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  64784. /*********************************
  64785. * Crypto API tests
  64786. *********************************/
  64787. TEST_DECL(test_wolfSSL_MD4),
  64788. TEST_DECL(test_wolfSSL_MD5),
  64789. TEST_DECL(test_wolfSSL_MD5_Transform),
  64790. TEST_DECL(test_wolfSSL_SHA),
  64791. TEST_DECL(test_wolfSSL_SHA_Transform),
  64792. TEST_DECL(test_wolfSSL_SHA224),
  64793. TEST_DECL(test_wolfSSL_SHA256),
  64794. TEST_DECL(test_wolfSSL_SHA256_Transform),
  64795. TEST_DECL(test_wolfSSL_SHA512_Transform),
  64796. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  64797. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  64798. TEST_DECL(test_wolfSSL_HMAC_CTX),
  64799. TEST_DECL(test_wolfSSL_HMAC),
  64800. TEST_DECL(test_wolfSSL_CMAC),
  64801. TEST_DECL(test_wolfSSL_DES),
  64802. TEST_DECL(test_wolfSSL_DES_ncbc),
  64803. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  64804. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  64805. TEST_DECL(test_wolfSSL_AES_encrypt),
  64806. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  64807. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  64808. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  64809. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  64810. TEST_DECL(test_wolfSSL_RC4),
  64811. TEST_DECL(test_wolfSSL_RSA),
  64812. TEST_DECL(test_wolfSSL_RSA_DER),
  64813. TEST_DECL(test_wolfSSL_RSA_print),
  64814. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  64815. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  64816. TEST_DECL(test_wolfSSL_RSA_get0_key),
  64817. TEST_DECL(test_wolfSSL_RSA_meth),
  64818. TEST_DECL(test_wolfSSL_RSA_verify),
  64819. TEST_DECL(test_wolfSSL_RSA_sign),
  64820. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  64821. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  64822. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  64823. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  64824. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  64825. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  64826. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  64827. TEST_DECL(test_wolfSSL_RSA_ex_data),
  64828. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  64829. TEST_DECL(test_wolfSSL_RSA_To_Der),
  64830. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  64831. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  64832. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  64833. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  64834. TEST_DECL(test_wolfSSL_DH),
  64835. TEST_DECL(test_wolfSSL_DH_dup),
  64836. TEST_DECL(test_wolfSSL_DH_check),
  64837. TEST_DECL(test_wolfSSL_DH_prime),
  64838. TEST_DECL(test_wolfSSL_DH_1536_prime),
  64839. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  64840. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  64841. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  64842. TEST_DECL(test_wolfSSL_d2i_DHparams),
  64843. TEST_DECL(test_wolfSSL_DH_LoadDer),
  64844. TEST_DECL(test_wolfSSL_i2d_DHparams),
  64845. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  64846. TEST_DECL(test_wolfSSL_EC_GROUP),
  64847. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  64848. TEST_DECL(test_wolfSSL_EC_POINT),
  64849. TEST_DECL(test_wolfSSL_SPAKE),
  64850. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  64851. TEST_DECL(test_EC_i2d),
  64852. TEST_DECL(test_wolfSSL_EC_curve),
  64853. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  64854. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  64855. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  64856. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  64857. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  64858. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  64859. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  64860. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  64861. TEST_DECL(test_ECDSA_size_sign),
  64862. TEST_DECL(test_ECDH_compute_key),
  64863. #endif
  64864. #ifdef OPENSSL_EXTRA
  64865. TEST_DECL(test_EC25519),
  64866. TEST_DECL(test_ED25519),
  64867. TEST_DECL(test_EC448),
  64868. TEST_DECL(test_ED448),
  64869. #endif
  64870. TEST_DECL(test_DSA_do_sign_verify),
  64871. #ifdef OPENSSL_ALL
  64872. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  64873. TEST_DECL(test_wolfSSL_DSA_SIG),
  64874. #endif
  64875. TEST_DECL(test_openssl_generate_key_and_cert),
  64876. TEST_DECL(test_wolfSSL_FIPS_mode),
  64877. TEST_DECL(test_openssl_FIPS_drbg),
  64878. /*********************************
  64879. * CertManager API tests
  64880. *********************************/
  64881. TEST_DECL(test_wolfSSL_CertManagerAPI),
  64882. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  64883. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  64884. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  64885. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  64886. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  64887. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  64888. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  64889. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  64890. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  64891. TEST_DECL(test_wolfSSL_CertManagerCRL),
  64892. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  64893. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  64894. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  64895. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  64896. !defined(WOLFSSL_NO_CLIENT_AUTH))
  64897. TEST_DECL(test_various_pathlen_chains),
  64898. #endif
  64899. /*********************************
  64900. * SSL/TLS API tests
  64901. *********************************/
  64902. TEST_DECL(test_wolfSSL_Method_Allocators),
  64903. #ifndef NO_WOLFSSL_SERVER
  64904. TEST_DECL(test_wolfSSL_CTX_new),
  64905. #endif
  64906. TEST_DECL(test_server_wolfSSL_new),
  64907. TEST_DECL(test_client_wolfSSL_new),
  64908. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  64909. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  64910. TEST_DECL(test_for_double_Free),
  64911. #endif
  64912. TEST_DECL(test_wolfSSL_set_options),
  64913. #ifdef WOLFSSL_TLS13
  64914. /* TLS v1.3 API tests */
  64915. TEST_DECL(test_tls13_apis),
  64916. TEST_DECL(test_tls13_cipher_suites),
  64917. #endif
  64918. TEST_DECL(test_wolfSSL_tmp_dh),
  64919. TEST_DECL(test_wolfSSL_ctrl),
  64920. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  64921. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  64922. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  64923. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  64924. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  64925. #endif
  64926. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  64927. TEST_DECL(test_wolfSSL_security_level),
  64928. TEST_DECL(test_wolfSSL_SSL_in_init),
  64929. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  64930. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  64931. TEST_DECL(test_CONF_CTX_FILE),
  64932. TEST_DECL(test_CONF_CTX_CMDLINE),
  64933. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  64934. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  64935. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  64936. /* Bad certificate signature tests */
  64937. TEST_DECL(test_EccSigFailure_cm),
  64938. TEST_DECL(test_RsaSigFailure_cm),
  64939. #endif /* NO_CERTS */
  64940. /* PKCS8 testing */
  64941. TEST_DECL(test_wolfSSL_no_password_cb),
  64942. TEST_DECL(test_wolfSSL_PKCS8),
  64943. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  64944. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  64945. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  64946. TEST_DECL(test_wolfSSL_get_finished),
  64947. /* Uses Assert in handshake callback. */
  64948. TEST_DECL(test_wolfSSL_CTX_add_session),
  64949. /* Large number of memory allocations. */
  64950. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  64951. /* Large number of memory allocations. */
  64952. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  64953. /* Large number of memory allocations. */
  64954. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  64955. /* Large number of memory allocations. */
  64956. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  64957. /* Large number of memory allocations. */
  64958. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  64959. /* Large number of memory allocations. */
  64960. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  64961. #endif
  64962. TEST_DECL(test_SSL_CIPHER_get_xxx),
  64963. TEST_DECL(test_wolfSSL_ERR_strings),
  64964. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  64965. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  64966. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  64967. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  64968. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  64969. /* Large number of memory allocations. */
  64970. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  64971. TEST_DECL(test_wolfSSL_CertRsaPss),
  64972. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  64973. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  64974. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  64975. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  64976. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  64977. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  64978. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  64979. TEST_DECL(test_multiple_crls_same_issuer),
  64980. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  64981. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  64982. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  64983. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  64984. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  64985. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  64986. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  64987. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  64988. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  64989. TEST_DECL(test_SetTmpEC_DHE_Sz),
  64990. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  64991. #ifdef WOLFSSL_DTLS
  64992. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  64993. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  64994. #endif
  64995. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  64996. /* Uses Assert in handshake callback. */
  64997. TEST_DECL(test_wolfSSL_dtls_plaintext),
  64998. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  64999. defined(HAVE_IO_TESTS_DEPENDENCIES)
  65000. TEST_DECL(test_wolfSSL_read_write),
  65001. /* Can't memory test as server hangs if client fails before second connect.
  65002. */
  65003. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  65004. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  65005. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  65006. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  65007. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  65008. /* Can't memory test as server hangs. */
  65009. TEST_DECL(test_wolfSSL_dtls_export),
  65010. /* Uses Assert in handshake callback. */
  65011. TEST_DECL(test_wolfSSL_tls_export),
  65012. #endif
  65013. TEST_DECL(test_wolfSSL_dtls_export_peers),
  65014. TEST_DECL(test_wolfSSL_SetMinVersion),
  65015. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  65016. /* wolfSSL handshake APIs. */
  65017. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  65018. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  65019. TEST_DECL(test_wolfSSL_BUF),
  65020. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  65021. /* Can't memory test as server hangs. */
  65022. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  65023. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  65024. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  65025. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  65026. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  65027. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  65028. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  65029. /* Can't memory test as server hangs. */
  65030. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  65031. TEST_DECL(test_wolfSSL_Tls13_postauth),
  65032. TEST_DECL(test_wolfSSL_set_ecdh_auto),
  65033. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  65034. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  65035. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  65036. TEST_DECL(test_wolfSSL_THREADID_hash),
  65037. /* TLS extensions tests */
  65038. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  65039. #ifdef HAVE_SNI
  65040. TEST_DECL(test_wolfSSL_UseSNI_params),
  65041. /* Uses Assert in handshake callback. */
  65042. TEST_DECL(test_wolfSSL_UseSNI_connection),
  65043. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  65044. #endif /* HAVE_SNI */
  65045. #endif
  65046. TEST_DECL(test_wolfSSL_UseTrustedCA),
  65047. TEST_DECL(test_wolfSSL_UseMaxFragment),
  65048. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  65049. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  65050. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  65051. /* Uses Assert in handshake callback. */
  65052. TEST_DECL(test_wolfSSL_UseALPN_connection),
  65053. TEST_DECL(test_wolfSSL_UseALPN_params),
  65054. #endif
  65055. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  65056. /* Uses Assert in handshake callback. */
  65057. TEST_DECL(test_wolfSSL_set_alpn_protos),
  65058. #endif
  65059. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  65060. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  65061. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  65062. TEST_DECL(test_tls_ext_duplicate),
  65063. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  65064. defined(HAVE_IO_TESTS_DEPENDENCIES)
  65065. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  65066. /* Uses Assert in handshake callback. */
  65067. TEST_DECL(test_wolfSSL_Tls13_ECH),
  65068. #endif
  65069. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  65070. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  65071. /* OCSP Stapling */
  65072. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  65073. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  65074. TEST_DECL(test_self_signed_stapling),
  65075. /* Multicast */
  65076. TEST_DECL(test_wolfSSL_mcast),
  65077. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  65078. TEST_DECL(test_wolfSSL_msgCb),
  65079. TEST_DECL(test_wolfSSL_either_side),
  65080. TEST_DECL(test_wolfSSL_DTLS_either_side),
  65081. /* Uses Assert in handshake callback. */
  65082. TEST_DECL(test_wolfSSL_dtls_fragments),
  65083. /* Uses Assert in handshake callback. */
  65084. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  65085. /* Uses Assert in handshake callback. */
  65086. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  65087. /* Uses Assert in handshake callback. */
  65088. TEST_DECL(test_wolfSSL_dtls_bad_record),
  65089. /* Uses Assert in handshake callback. */
  65090. TEST_DECL(test_wolfSSL_dtls_stateless),
  65091. TEST_DECL(test_generate_cookie),
  65092. #ifndef NO_BIO
  65093. /* Can't memory test as server hangs. */
  65094. TEST_DECL(test_wolfSSL_BIO_connect),
  65095. /* Can't memory test as server Asserts in thread. */
  65096. TEST_DECL(test_wolfSSL_BIO_accept),
  65097. TEST_DECL(test_wolfSSL_BIO_tls),
  65098. #endif
  65099. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  65100. TEST_DECL(test_DhCallbacks),
  65101. #endif
  65102. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  65103. TEST_DECL(test_export_keying_material),
  65104. #endif
  65105. /* Can't memory test as client/server Asserts in thread. */
  65106. TEST_DECL(test_ticket_and_psk_mixing),
  65107. /* Can't memory test as client/server Asserts in thread. */
  65108. TEST_DECL(test_prioritize_psk),
  65109. /* Can't memory test as client/server hangs. */
  65110. TEST_DECL(test_wc_CryptoCb),
  65111. /* Can't memory test as client/server hangs. */
  65112. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  65113. #if !defined(NO_FILESYSTEM) && \
  65114. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  65115. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  65116. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  65117. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  65118. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  65119. #ifdef HAVE_MAX_FRAGMENT
  65120. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  65121. #endif /* HAVE_MAX_FRAGMENT */
  65122. #ifndef NO_RSA
  65123. TEST_DECL(test_wolfSSL_dtls_stateless2),
  65124. #if !defined(NO_OLD_TLS)
  65125. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  65126. #endif /* !defined(NO_OLD_TLS) */
  65127. #endif /* ! NO_RSA */
  65128. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  65129. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  65130. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  65131. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  65132. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  65133. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  65134. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  65135. && defined(WOLFSSL_TLS13) && \
  65136. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  65137. TEST_DECL(test_ticket_nonce_malloc),
  65138. #endif
  65139. TEST_DECL(test_ticket_ret_create),
  65140. TEST_DECL(test_wrong_cs_downgrade),
  65141. TEST_DECL(test_extra_alerts_wrong_cs),
  65142. TEST_DECL(test_extra_alerts_skip_hs),
  65143. TEST_DECL(test_extra_alerts_bad_psk),
  65144. TEST_DECL(test_tls13_bad_psk_binder),
  65145. /* Can't memory test as client/server Asserts. */
  65146. TEST_DECL(test_harden_no_secure_renegotiation),
  65147. TEST_DECL(test_override_alt_cert_chain),
  65148. TEST_DECL(test_rpk_set_xxx_cert_type),
  65149. TEST_DECL(test_tls13_rpk_handshake),
  65150. TEST_DECL(test_dtls13_bad_epoch_ch),
  65151. TEST_DECL(test_short_session_id),
  65152. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  65153. /* Can't memory test as client/server hangs. */
  65154. TEST_DECL(test_dtls_msg_from_other_peer),
  65155. TEST_DECL(test_dtls_ipv6_check),
  65156. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  65157. TEST_DECL(test_dtls_no_extensions),
  65158. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  65159. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  65160. TEST_DECL(test_session_ticket_no_id),
  65161. TEST_DECL(test_session_ticket_hs_update),
  65162. TEST_DECL(test_dtls_downgrade_scr_server),
  65163. TEST_DECL(test_dtls_downgrade_scr),
  65164. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  65165. TEST_DECL(test_dtls_client_hello_timeout),
  65166. TEST_DECL(test_dtls_dropped_ccs),
  65167. TEST_DECL(test_dtls_seq_num_downgrade),
  65168. TEST_DECL(test_certreq_sighash_algos),
  65169. TEST_DECL(test_revoked_loaded_int_cert),
  65170. TEST_DECL(test_dtls_frag_ch),
  65171. TEST_DECL(test_dtls13_frag_ch_pq),
  65172. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  65173. TEST_DECL(test_dtls_old_seq_number),
  65174. TEST_DECL(test_tls13_pq_groups),
  65175. TEST_DECL(test_tls13_early_data),
  65176. TEST_DECL(test_tls_multi_handshakes_one_record),
  65177. TEST_DECL(test_write_dup),
  65178. TEST_DECL(test_read_write_hs),
  65179. TEST_DECL(test_get_signature_nid),
  65180. TEST_DECL(test_tls_cert_store_unchanged),
  65181. TEST_DECL(test_wolfSSL_SendUserCanceled),
  65182. /* This test needs to stay at the end to clean up any caches allocated. */
  65183. TEST_DECL(test_wolfSSL_Cleanup)
  65184. };
  65185. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  65186. static void TestSetup(void)
  65187. {
  65188. /* Stub, for now. Add common test setup code here. */
  65189. }
  65190. static void TestCleanup(void)
  65191. {
  65192. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  65193. /* Clear any errors added to the error queue during the test run. */
  65194. wolfSSL_ERR_clear_error();
  65195. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  65196. }
  65197. /* Print out all API test cases with numeric identifier.
  65198. */
  65199. void ApiTest_PrintTestCases(void)
  65200. {
  65201. int i;
  65202. printf("All Test Cases:\n");
  65203. for (i = 0; i < TEST_CASE_CNT; i++) {
  65204. printf("%3d: %s\n", i + 1, testCases[i].name);
  65205. }
  65206. }
  65207. /* Add test case with index to the list to run.
  65208. *
  65209. * @param [in] idx Index of test case to run starting at 1.
  65210. * @return 0 on success.
  65211. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  65212. */
  65213. int ApiTest_RunIdx(int idx)
  65214. {
  65215. if (idx < 1 || idx > TEST_CASE_CNT) {
  65216. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  65217. return BAD_FUNC_ARG;
  65218. }
  65219. testAll = 0;
  65220. testCases[idx-1].run = 1;
  65221. return 0;
  65222. }
  65223. /* Add test case with name to the list to run.
  65224. *
  65225. * @param [in] name Name of test case to run.
  65226. * @return 0 on success.
  65227. * @return BAD_FUNC_ARG when name is not a known test case name.
  65228. */
  65229. int ApiTest_RunName(char* name)
  65230. {
  65231. int i;
  65232. for (i = 0; i < TEST_CASE_CNT; i++) {
  65233. if (XSTRCMP(testCases[i].name, name) == 0) {
  65234. testAll = 0;
  65235. testCases[i].run = 1;
  65236. return 0;
  65237. }
  65238. }
  65239. printf("Test case name not found: %s\n", name);
  65240. printf("Use --list to see all test case names.\n");
  65241. return BAD_FUNC_ARG;
  65242. }
  65243. /* Converts the result code to a string.
  65244. *
  65245. * @param [in] res Test result code.
  65246. * @return String describing test result.
  65247. */
  65248. static const char* apitest_res_string(int res)
  65249. {
  65250. const char* str = "invalid result";
  65251. switch (res) {
  65252. case TEST_SUCCESS:
  65253. str = "passed";
  65254. break;
  65255. case TEST_FAIL:
  65256. str = "failed";
  65257. break;
  65258. case TEST_SKIPPED:
  65259. str = "skipped";
  65260. break;
  65261. }
  65262. return str;
  65263. }
  65264. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65265. static double gettime_secs(void)
  65266. #if defined(_MSC_VER) && defined(_WIN32)
  65267. {
  65268. /* there's no gettimeofday for Windows, so we'll use system time */
  65269. #define EPOCH_DIFF 11644473600LL
  65270. FILETIME currentFileTime;
  65271. GetSystemTimePreciseAsFileTime(&currentFileTime);
  65272. ULARGE_INTEGER uli = { 0, 0 };
  65273. uli.LowPart = currentFileTime.dwLowDateTime;
  65274. uli.HighPart = currentFileTime.dwHighDateTime;
  65275. /* Convert to seconds since Unix epoch */
  65276. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  65277. }
  65278. #else
  65279. {
  65280. struct timeval tv;
  65281. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  65282. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  65283. }
  65284. #endif
  65285. #endif
  65286. int ApiTest(void)
  65287. {
  65288. int i;
  65289. int ret;
  65290. int res = 0;
  65291. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65292. double timeDiff;
  65293. #endif
  65294. printf(" Begin API Tests\n");
  65295. fflush(stdout);
  65296. /* we must perform init and cleanup if not all tests are running */
  65297. if (!testAll) {
  65298. #ifdef WOLFCRYPT_ONLY
  65299. if (wolfCrypt_Init() != 0) {
  65300. printf("wolfCrypt Initialization failed\n");
  65301. res = 1;
  65302. }
  65303. #else
  65304. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  65305. printf("wolfSSL Initialization failed\n");
  65306. res = 1;
  65307. }
  65308. #endif
  65309. }
  65310. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  65311. if (res == 0) {
  65312. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  65313. printf("failed to create tmp dir\n");
  65314. res = 1;
  65315. }
  65316. else {
  65317. tmpDirNameSet = 1;
  65318. }
  65319. }
  65320. #endif
  65321. if (res == 0) {
  65322. for (i = 0; i < TEST_CASE_CNT; ++i) {
  65323. EXPECT_DECLS;
  65324. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  65325. currentTestName = testCases[i].name;
  65326. #endif
  65327. /* When not testing all cases then skip if not marked for running.
  65328. */
  65329. if (!testAll && !testCases[i].run) {
  65330. continue;
  65331. }
  65332. TestSetup();
  65333. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  65334. fflush(stdout);
  65335. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65336. timeDiff = gettime_secs();
  65337. #endif
  65338. ret = testCases[i].func();
  65339. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65340. timeDiff = gettime_secs() - timeDiff;
  65341. #endif
  65342. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  65343. if (ret != TEST_SKIPPED) {
  65344. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  65345. }
  65346. else
  65347. #endif
  65348. {
  65349. printf(" %s\n", apitest_res_string(ret));
  65350. }
  65351. fflush(stdout);
  65352. /* if return code is < 0 and not skipped then assert error */
  65353. Expect((ret > 0 || ret == TEST_SKIPPED),
  65354. ("Test failed\n"),
  65355. ("ret %d", ret));
  65356. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  65357. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  65358. TestCleanup();
  65359. }
  65360. }
  65361. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  65362. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  65363. wc_ecc_fp_free(); /* free per thread cache */
  65364. #endif
  65365. if (!testAll) {
  65366. #ifdef WOLFCRYPT_ONLY
  65367. wolfCrypt_Cleanup();
  65368. #else
  65369. wolfSSL_Cleanup();
  65370. #endif
  65371. }
  65372. (void)testDevId;
  65373. if (res != 0) {
  65374. printf("\nFAILURES:\n");
  65375. for (i = 0; i < TEST_CASE_CNT; ++i) {
  65376. if (testCases[i].fail) {
  65377. printf(" %3d: %s\n", i + 1, testCases[i].name);
  65378. }
  65379. }
  65380. printf("\n");
  65381. fflush(stdout);
  65382. }
  65383. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  65384. if (tmpDirNameSet) {
  65385. printf("\nBinary dumps of the memio streams can be found in the\n"
  65386. "%s directory. This can be imported into\n"
  65387. "Wireshark by transforming the file with\n"
  65388. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  65389. "And then loading test_output.dump.hex into Wireshark using\n"
  65390. "the \"Import from Hex Dump...\" option and selecting the\n"
  65391. "TCP encapsulation option.\n", tmpDirName);
  65392. }
  65393. #endif
  65394. printf(" End API Tests\n");
  65395. fflush(stdout);
  65396. return res;
  65397. }