api.c 2.3 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #ifndef USER_CRYPTO_ERROR
  215. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  216. #endif
  217. #endif
  218. #ifndef NO_SIG_WRAPPER
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #endif
  221. #ifdef HAVE_AESCCM
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #endif
  224. #ifdef HAVE_PKCS7
  225. #include <wolfssl/wolfcrypt/pkcs7.h>
  226. #include <wolfssl/wolfcrypt/asn.h>
  227. #ifdef HAVE_LIBZ
  228. #include <wolfssl/wolfcrypt/compress.h>
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  232. #include <wolfssl/wolfcrypt/asn.h>
  233. #endif
  234. #ifndef NO_DSA
  235. #include <wolfssl/wolfcrypt/dsa.h>
  236. #ifndef ONEK_BUF
  237. #define ONEK_BUF 1024
  238. #endif
  239. #ifndef TWOK_BUF
  240. #define TWOK_BUF 2048
  241. #endif
  242. #ifndef FOURK_BUF
  243. #define FOURK_BUF 4096
  244. #endif
  245. #ifndef DSA_SIG_SIZE
  246. #define DSA_SIG_SIZE 40
  247. #endif
  248. #ifndef MAX_DSA_PARAM_SIZE
  249. #define MAX_DSA_PARAM_SIZE 256
  250. #endif
  251. #endif
  252. #ifdef WOLFSSL_CMAC
  253. #include <wolfssl/wolfcrypt/cmac.h>
  254. #endif
  255. #ifdef HAVE_ED25519
  256. #include <wolfssl/wolfcrypt/ed25519.h>
  257. #endif
  258. #ifdef HAVE_CURVE25519
  259. #include <wolfssl/wolfcrypt/curve25519.h>
  260. #endif
  261. #ifdef HAVE_ED448
  262. #include <wolfssl/wolfcrypt/ed448.h>
  263. #endif
  264. #ifdef HAVE_CURVE448
  265. #include <wolfssl/wolfcrypt/curve448.h>
  266. #endif
  267. #ifdef HAVE_PKCS12
  268. #include <wolfssl/wolfcrypt/pkcs12.h>
  269. #endif
  270. #include <wolfssl/wolfcrypt/logging.h>
  271. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  272. #include <wolfssl/openssl/ssl.h>
  273. #ifndef NO_ASN
  274. /* for ASN_COMMON_NAME DN_tags enum */
  275. #include <wolfssl/wolfcrypt/asn.h>
  276. #endif
  277. #ifdef HAVE_OCSP
  278. #include <wolfssl/openssl/ocsp.h>
  279. #endif
  280. #endif
  281. #ifdef OPENSSL_EXTRA
  282. #include <wolfssl/openssl/cmac.h>
  283. #include <wolfssl/openssl/x509v3.h>
  284. #include <wolfssl/openssl/asn1.h>
  285. #include <wolfssl/openssl/crypto.h>
  286. #include <wolfssl/openssl/pkcs12.h>
  287. #include <wolfssl/openssl/evp.h>
  288. #include <wolfssl/openssl/dh.h>
  289. #include <wolfssl/openssl/bn.h>
  290. #include <wolfssl/openssl/buffer.h>
  291. #include <wolfssl/openssl/pem.h>
  292. #include <wolfssl/openssl/ec.h>
  293. #include <wolfssl/openssl/ecdh.h>
  294. #include <wolfssl/openssl/engine.h>
  295. #include <wolfssl/openssl/hmac.h>
  296. #include <wolfssl/openssl/objects.h>
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/modes.h>
  299. #include <wolfssl/openssl/fips_rand.h>
  300. #include <wolfssl/openssl/kdf.h>
  301. #ifdef OPENSSL_ALL
  302. #include <wolfssl/openssl/txt_db.h>
  303. #include <wolfssl/openssl/lhash.h>
  304. #endif
  305. #ifndef NO_AES
  306. #include <wolfssl/openssl/aes.h>
  307. #endif
  308. #ifndef NO_DES3
  309. #include <wolfssl/openssl/des.h>
  310. #endif
  311. #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_ED25519
  318. #include <wolfssl/openssl/ed25519.h>
  319. #endif
  320. #ifdef HAVE_ED448
  321. #include <wolfssl/openssl/ed448.h>
  322. #endif
  323. #endif /* OPENSSL_EXTRA */
  324. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  325. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  326. #include <wolfssl/wolfcrypt/srp.h>
  327. #endif
  328. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  329. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  330. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  331. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  332. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  333. || !defined(WOLFSSL_NO_TLS12)
  334. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  335. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  336. * number tracking */
  337. #include "wolfssl/internal.h"
  338. #endif
  339. /* force enable test buffers */
  340. #ifndef USE_CERT_BUFFERS_2048
  341. #define USE_CERT_BUFFERS_2048
  342. #endif
  343. #ifndef USE_CERT_BUFFERS_256
  344. #define USE_CERT_BUFFERS_256
  345. #endif
  346. #include <wolfssl/certs_test.h>
  347. #include "tests/utils.h"
  348. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  349. /* FIPS build has replaced ecc.h. */
  350. #define wc_ecc_key_get_priv(key) (&((key)->k))
  351. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  352. #endif
  353. typedef struct testVector {
  354. const char* input;
  355. const char* output;
  356. size_t inLen;
  357. size_t outLen;
  358. } testVector;
  359. #if defined(HAVE_PKCS7)
  360. typedef struct {
  361. const byte* content;
  362. word32 contentSz;
  363. int contentOID;
  364. int encryptOID;
  365. int keyWrapOID;
  366. int keyAgreeOID;
  367. byte* cert;
  368. size_t certSz;
  369. byte* privateKey;
  370. word32 privateKeySz;
  371. } pkcs7EnvelopedVector;
  372. #ifndef NO_PKCS7_ENCRYPTED_DATA
  373. typedef struct {
  374. const byte* content;
  375. word32 contentSz;
  376. int contentOID;
  377. int encryptOID;
  378. byte* encryptionKey;
  379. word32 encryptionKeySz;
  380. } pkcs7EncryptedVector;
  381. #endif
  382. #endif /* HAVE_PKCS7 */
  383. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  384. typedef int (*ssl_cb)(WOLFSSL* ssl);
  385. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  386. typedef struct test_ssl_cbf {
  387. method_provider method;
  388. ctx_cb ctx_ready;
  389. ssl_cb ssl_ready;
  390. ssl_cb on_result;
  391. ssl_cb on_cleanup;
  392. WOLFSSL_CTX* ctx;
  393. const char* caPemFile;
  394. const char* certPemFile;
  395. const char* keyPemFile;
  396. const char* crlPemFile;
  397. #ifdef WOLFSSL_STATIC_MEMORY
  398. byte* mem;
  399. word32 memSz;
  400. wolfSSL_method_func method_ex;
  401. #endif
  402. int devId;
  403. int return_code;
  404. int last_err;
  405. unsigned char isSharedCtx:1;
  406. unsigned char loadToSSL:1;
  407. unsigned char ticNoInit:1;
  408. unsigned char doUdp:1;
  409. } test_ssl_cbf;
  410. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  411. typedef struct test_ssl_memio_ctx {
  412. WOLFSSL_CTX* s_ctx;
  413. WOLFSSL_CTX* c_ctx;
  414. WOLFSSL* s_ssl;
  415. WOLFSSL* c_ssl;
  416. const char* c_ciphers;
  417. const char* s_ciphers;
  418. char* c_msg;
  419. int c_msglen;
  420. char* s_msg;
  421. int s_msglen;
  422. test_ssl_cbf s_cb;
  423. test_ssl_cbf c_cb;
  424. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  425. int c_len;
  426. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  427. int s_len;
  428. } test_ssl_memio_ctx;
  429. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  430. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  431. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  432. const char* currentTestName;
  433. char tmpDirName[16];
  434. int tmpDirNameSet = 0;
  435. #endif
  436. /*----------------------------------------------------------------------------*
  437. | Constants
  438. *----------------------------------------------------------------------------*/
  439. /* Test result constants and macros. */
  440. /* Test succeeded. */
  441. #define TEST_SUCCESS (1)
  442. /* Test failed. */
  443. #define TEST_FAIL (0)
  444. /* Test skipped - not run. */
  445. #define TEST_SKIPPED (-7777)
  446. /* Returns the result based on whether check is true.
  447. *
  448. * @param [in] check Condition for success.
  449. * @return When condition is true: TEST_SUCCESS.
  450. * @return When condition is false: TEST_FAIL.
  451. */
  452. #ifdef DEBUG_WOLFSSL_VERBOSE
  453. #define XSTRINGIFY(s) STRINGIFY(s)
  454. #define STRINGIFY(s) #s
  455. #define TEST_RES_CHECK(check) ({ \
  456. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  457. if (_ret == TEST_FAIL) { \
  458. fprintf(stderr, " check \"%s\" at %d ", \
  459. XSTRINGIFY(check), __LINE__); \
  460. } \
  461. _ret; })
  462. #else
  463. #define TEST_RES_CHECK(check) \
  464. ((check) ? TEST_SUCCESS : TEST_FAIL)
  465. #endif /* DEBUG_WOLFSSL_VERBOSE */
  466. #define TEST_STRING "Everyone gets Friday off."
  467. #define TEST_STRING_SZ 25
  468. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  469. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  470. #define TEST_RSA_BITS 1024
  471. #else
  472. #define TEST_RSA_BITS 2048
  473. #endif
  474. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  475. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  476. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  477. static const char* bogusFile =
  478. #ifdef _WIN32
  479. "NUL"
  480. #else
  481. "/dev/null"
  482. #endif
  483. ;
  484. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  485. enum {
  486. TESTING_RSA = 1,
  487. TESTING_ECC = 2
  488. };
  489. #ifdef WOLFSSL_QNX_CAAM
  490. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  491. static int testDevId = WOLFSSL_CAAM_DEVID;
  492. #else
  493. static int testDevId = INVALID_DEVID;
  494. #endif
  495. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  496. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  497. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  498. #define HAVE_IO_TESTS_DEPENDENCIES
  499. #endif
  500. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  501. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  502. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  503. #endif
  504. /*----------------------------------------------------------------------------*
  505. | BIO with fixed read/write size
  506. *----------------------------------------------------------------------------*/
  507. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  508. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  509. int len)
  510. {
  511. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  512. len = 0;
  513. }
  514. else {
  515. if (bio->wrSz - bio->wrIdx < len) {
  516. len = bio->wrSz - bio->wrIdx;
  517. }
  518. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  519. bio->wrIdx += len;
  520. }
  521. return len;
  522. }
  523. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  524. {
  525. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  526. len = 0;
  527. }
  528. else {
  529. if (bio->wrSz - bio->rdIdx < len) {
  530. len = bio->wrSz - bio->rdIdx;
  531. }
  532. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  533. bio->rdIdx += len;
  534. }
  535. return len;
  536. }
  537. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  538. {
  539. static WOLFSSL_BIO_METHOD meth;
  540. meth.type = WOLFSSL_BIO_BIO;
  541. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  542. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  543. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  544. return &meth;
  545. }
  546. #endif
  547. /*----------------------------------------------------------------------------*
  548. | Setup
  549. *----------------------------------------------------------------------------*/
  550. static int test_wolfSSL_Init(void)
  551. {
  552. EXPECT_DECLS;
  553. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  554. return EXPECT_RESULT();
  555. }
  556. static int test_wolfSSL_Cleanup(void)
  557. {
  558. EXPECT_DECLS;
  559. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  560. return EXPECT_RESULT();
  561. }
  562. /* Initialize the wolfCrypt state.
  563. * POST: 0 success.
  564. */
  565. static int test_wolfCrypt_Init(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfCrypt_Init(), 0);
  569. return EXPECT_RESULT();
  570. } /* END test_wolfCrypt_Init */
  571. static int test_wolfCrypt_Cleanup(void)
  572. {
  573. EXPECT_DECLS;
  574. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  575. return EXPECT_RESULT();
  576. }
  577. /*----------------------------------------------------------------------------*
  578. | Platform dependent function test
  579. *----------------------------------------------------------------------------*/
  580. static int test_fileAccess(void)
  581. {
  582. EXPECT_DECLS;
  583. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  584. const char *fname[] = {
  585. svrCertFile, svrKeyFile, caCertFile,
  586. eccCertFile, eccKeyFile, eccRsaCertFile,
  587. cliCertFile, cliCertDerFile, cliKeyFile,
  588. dhParamFile,
  589. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  590. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  591. NULL
  592. };
  593. const char derfile[] = "./certs/server-cert.der";
  594. XFILE f = XBADFILE;
  595. size_t sz;
  596. byte *buff = NULL;
  597. int i;
  598. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  599. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  600. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  601. XFCLOSE(f);
  602. }
  603. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  604. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  605. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  606. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  607. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  608. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  609. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  610. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  611. XFCLOSE(f);
  612. #endif
  613. return EXPECT_RESULT();
  614. }
  615. /*----------------------------------------------------------------------------*
  616. | Method Allocators
  617. *----------------------------------------------------------------------------*/
  618. static int test_wolfSSL_Method_Allocators(void)
  619. {
  620. EXPECT_DECLS;
  621. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  622. do { \
  623. WOLFSSL_METHOD *method = NULL; \
  624. condition(method = allocator()); \
  625. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  626. } while (0)
  627. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  628. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  629. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  630. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  631. #ifndef NO_OLD_TLS
  632. #ifdef WOLFSSL_ALLOW_SSLV3
  633. #ifndef NO_WOLFSSL_SERVER
  634. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  635. #endif
  636. #ifndef NO_WOLFSSL_CLIENT
  637. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  638. #endif
  639. #endif
  640. #ifdef WOLFSSL_ALLOW_TLSV10
  641. #ifndef NO_WOLFSSL_SERVER
  642. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  643. #endif
  644. #ifndef NO_WOLFSSL_CLIENT
  645. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  646. #endif
  647. #endif
  648. #ifndef NO_WOLFSSL_SERVER
  649. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  650. #endif
  651. #ifndef NO_WOLFSSL_CLIENT
  652. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  653. #endif
  654. #endif /* !NO_OLD_TLS */
  655. #ifndef WOLFSSL_NO_TLS12
  656. #ifndef NO_WOLFSSL_SERVER
  657. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  658. #endif
  659. #ifndef NO_WOLFSSL_CLIENT
  660. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  661. #endif
  662. #endif /* !WOLFSSL_NO_TLS12 */
  663. #ifdef WOLFSSL_TLS13
  664. #ifndef NO_WOLFSSL_SERVER
  665. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  666. #endif
  667. #ifndef NO_WOLFSSL_CLIENT
  668. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  669. #endif
  670. #endif /* WOLFSSL_TLS13 */
  671. #ifndef NO_WOLFSSL_SERVER
  672. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  673. #endif
  674. #ifndef NO_WOLFSSL_CLIENT
  675. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  676. #endif
  677. #ifdef WOLFSSL_DTLS
  678. #ifndef NO_OLD_TLS
  679. #ifndef NO_WOLFSSL_SERVER
  680. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  681. #endif
  682. #ifndef NO_WOLFSSL_CLIENT
  683. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  684. #endif
  685. #endif
  686. #ifndef WOLFSSL_NO_TLS12
  687. #ifndef NO_WOLFSSL_SERVER
  688. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  689. #endif
  690. #ifndef NO_WOLFSSL_CLIENT
  691. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  692. #endif
  693. #endif
  694. #endif /* WOLFSSL_DTLS */
  695. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  696. /* Stubs */
  697. #ifndef NO_WOLFSSL_SERVER
  698. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  699. #endif
  700. #ifndef NO_WOLFSSL_CLIENT
  701. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  702. #endif
  703. #endif
  704. /* Test Either Method (client or server) */
  705. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  706. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  707. #ifndef NO_OLD_TLS
  708. #ifdef WOLFSSL_ALLOW_TLSV10
  709. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  710. #endif
  711. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  712. #endif /* !NO_OLD_TLS */
  713. #ifndef WOLFSSL_NO_TLS12
  714. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  715. #endif /* !WOLFSSL_NO_TLS12 */
  716. #ifdef WOLFSSL_TLS13
  717. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  718. #endif /* WOLFSSL_TLS13 */
  719. #ifdef WOLFSSL_DTLS
  720. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  721. #ifndef NO_OLD_TLS
  722. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  723. #endif /* !NO_OLD_TLS */
  724. #ifndef WOLFSSL_NO_TLS12
  725. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  726. #endif /* !WOLFSSL_NO_TLS12 */
  727. #endif /* WOLFSSL_DTLS */
  728. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  729. return EXPECT_RESULT();
  730. }
  731. /*----------------------------------------------------------------------------*
  732. | Context
  733. *----------------------------------------------------------------------------*/
  734. #ifndef NO_WOLFSSL_SERVER
  735. static int test_wolfSSL_CTX_new(void)
  736. {
  737. EXPECT_DECLS;
  738. WOLFSSL_CTX *ctx;
  739. WOLFSSL_METHOD* method;
  740. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  741. ExpectNotNull(method = wolfSSLv23_server_method());
  742. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  743. wolfSSL_CTX_free(ctx);
  744. return EXPECT_RESULT();
  745. }
  746. #endif
  747. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  748. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  749. static int test_for_double_Free(void)
  750. {
  751. EXPECT_DECLS;
  752. WOLFSSL_CTX* ctx = NULL;
  753. WOLFSSL* ssl = NULL;
  754. int skipTest = 0;
  755. const char* testCertFile;
  756. const char* testKeyFile;
  757. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  758. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  759. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  760. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  761. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  762. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  763. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  764. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  765. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  766. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  767. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  768. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  769. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  770. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  771. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  772. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  773. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  774. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  775. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  776. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  777. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  778. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  779. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  780. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  781. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  782. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  783. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  784. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  785. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  786. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  787. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  788. #ifdef OPENSSL_EXTRA
  789. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  790. "!SRP:!kRSA:!aNULL:!eNULL";
  791. #endif
  792. #ifndef NO_RSA
  793. testCertFile = svrCertFile;
  794. testKeyFile = svrKeyFile;
  795. #elif defined(HAVE_ECC)
  796. testCertFile = eccCertFile;
  797. testKeyFile = eccKeyFile;
  798. #else
  799. skipTest = 1;
  800. #endif
  801. if (skipTest != 1) {
  802. #ifndef NO_WOLFSSL_SERVER
  803. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  804. #else
  805. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  806. #endif
  807. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  808. WOLFSSL_FILETYPE_PEM));
  809. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  810. WOLFSSL_FILETYPE_PEM));
  811. ExpectNotNull(ssl = wolfSSL_new(ctx));
  812. /* First test freeing SSL, then CTX */
  813. wolfSSL_free(ssl);
  814. ssl = NULL;
  815. wolfSSL_CTX_free(ctx);
  816. ctx = NULL;
  817. #ifndef NO_WOLFSSL_CLIENT
  818. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  819. #else
  820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  821. #endif
  822. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  823. WOLFSSL_FILETYPE_PEM));
  824. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  825. WOLFSSL_FILETYPE_PEM));
  826. ExpectNotNull(ssl = wolfSSL_new(ctx));
  827. /* Next test freeing CTX then SSL */
  828. wolfSSL_CTX_free(ctx);
  829. ctx = NULL;
  830. wolfSSL_free(ssl);
  831. ssl = NULL;
  832. #ifndef NO_WOLFSSL_SERVER
  833. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  834. #else
  835. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  836. #endif
  837. /* Test setting ciphers at ctx level */
  838. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  839. WOLFSSL_FILETYPE_PEM));
  840. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  841. WOLFSSL_FILETYPE_PEM));
  842. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  843. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  844. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  845. /* only update TLSv13 suites */
  846. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  847. #endif
  848. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  849. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  850. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  851. /* only update pre-TLSv13 suites */
  852. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  853. "ECDHE-RSA-AES128-GCM-SHA256"));
  854. #endif
  855. #ifdef OPENSSL_EXTRA
  856. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  857. #endif
  858. ExpectNotNull(ssl = wolfSSL_new(ctx));
  859. wolfSSL_CTX_free(ctx);
  860. ctx = NULL;
  861. wolfSSL_free(ssl);
  862. ssl = NULL;
  863. #ifndef NO_WOLFSSL_CLIENT
  864. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  865. #else
  866. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  867. #endif
  868. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  869. WOLFSSL_FILETYPE_PEM));
  870. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  871. WOLFSSL_FILETYPE_PEM));
  872. ExpectNotNull(ssl = wolfSSL_new(ctx));
  873. /* test setting ciphers at SSL level */
  874. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  875. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  876. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  877. /* only update TLSv13 suites */
  878. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  879. #endif
  880. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  881. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  882. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  883. /* only update pre-TLSv13 suites */
  884. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  885. #endif
  886. wolfSSL_CTX_free(ctx);
  887. ctx = NULL;
  888. wolfSSL_free(ssl);
  889. ssl = NULL;
  890. }
  891. return EXPECT_RESULT();
  892. }
  893. #endif
  894. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  895. {
  896. EXPECT_DECLS;
  897. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  898. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  899. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  900. const char* testCertFile;
  901. const char* testKeyFile;
  902. WOLFSSL_CTX* ctx = NULL;
  903. WOLFSSL* ssl = NULL;
  904. const byte cipherList[] =
  905. {
  906. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  907. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  908. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  909. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  910. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  911. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  912. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  913. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  914. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  915. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  916. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  917. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  918. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  919. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  920. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  921. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  922. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  923. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  924. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  925. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  926. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  927. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  928. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  929. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  930. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  931. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  932. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  933. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  934. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  935. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  936. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  937. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  938. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  939. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  940. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  941. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  942. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  943. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  944. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  945. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  946. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  947. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  948. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  949. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  950. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  951. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  952. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  953. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  954. /* SHA256 */
  955. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  956. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  957. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  958. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  959. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  960. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  961. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  962. /* SHA384 */
  963. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  964. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  965. /* AES-GCM */
  966. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  967. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  968. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  969. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  970. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  971. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  972. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  973. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  974. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  975. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  976. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  977. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  978. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  979. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  980. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  981. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  982. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  983. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  984. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  985. * also, in some of the other AES-CCM suites
  986. * there will be second byte number conflicts
  987. * with non-ECC AES-GCM */
  988. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  989. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  990. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  991. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  992. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  993. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  994. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  995. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  996. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  997. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  998. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  999. /* Camellia */
  1000. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1001. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1002. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1003. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1004. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1005. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1006. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1007. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1008. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1009. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1010. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1011. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1012. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1013. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1014. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1015. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1016. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1017. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1018. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1019. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1020. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1021. /* TLS v1.3 cipher suites */
  1022. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1023. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1024. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1025. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1026. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1027. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1028. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1029. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1030. };
  1031. #ifndef NO_RSA
  1032. testCertFile = svrCertFile;
  1033. testKeyFile = svrKeyFile;
  1034. #elif defined(HAVE_ECC)
  1035. testCertFile = eccCertFile;
  1036. testKeyFile = eccKeyFile;
  1037. #endif
  1038. #ifndef NO_WOLFSSL_SERVER
  1039. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1040. #else
  1041. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1042. #endif
  1043. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1044. sizeof(cipherList)));
  1045. wolfSSL_CTX_free(ctx);
  1046. ctx = NULL;
  1047. #ifndef NO_WOLFSSL_SERVER
  1048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1049. #else
  1050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1051. #endif
  1052. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1053. WOLFSSL_FILETYPE_PEM));
  1054. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1055. WOLFSSL_FILETYPE_PEM));
  1056. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1057. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1058. sizeof(cipherList)));
  1059. wolfSSL_free(ssl);
  1060. wolfSSL_CTX_free(ctx);
  1061. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1062. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1063. return EXPECT_RESULT();
  1064. }
  1065. static int test_wolfSSL_CTX_use_certificate_file(void)
  1066. {
  1067. EXPECT_DECLS;
  1068. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1069. WOLFSSL_CTX *ctx = NULL;
  1070. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1071. /* invalid context */
  1072. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1073. WOLFSSL_FILETYPE_PEM));
  1074. /* invalid cert file */
  1075. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1076. WOLFSSL_FILETYPE_PEM));
  1077. /* invalid cert type */
  1078. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1079. #ifdef NO_RSA
  1080. /* rsa needed */
  1081. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1082. WOLFSSL_FILETYPE_PEM));
  1083. #else
  1084. /* success */
  1085. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1086. WOLFSSL_FILETYPE_PEM));
  1087. #endif
  1088. wolfSSL_CTX_free(ctx);
  1089. #endif
  1090. return EXPECT_RESULT();
  1091. }
  1092. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1093. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1094. {
  1095. EXPECT_DECLS;
  1096. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1097. WOLFSSL_CTX* ctx = NULL;
  1098. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1099. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1100. server_cert_der_2048), WOLFSSL_SUCCESS);
  1101. wolfSSL_CTX_free(ctx);
  1102. #endif
  1103. return EXPECT_RESULT();
  1104. }
  1105. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1106. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1107. * context using buffer.
  1108. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1109. * --enable-testcert flag.
  1110. */
  1111. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1112. {
  1113. EXPECT_DECLS;
  1114. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1115. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1116. WOLFSSL_CTX* ctx = NULL;
  1117. int ret;
  1118. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1119. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1120. server_cert_der_2048, sizeof_server_cert_der_2048,
  1121. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1122. wolfSSL_CTX_free(ctx);
  1123. #endif
  1124. return EXPECT_RESULT();
  1125. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1126. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1127. {
  1128. EXPECT_DECLS;
  1129. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1130. WOLFSSL_CTX *ctx = NULL;
  1131. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1132. /* invalid context */
  1133. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1134. WOLFSSL_FILETYPE_PEM));
  1135. /* invalid key file */
  1136. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1137. WOLFSSL_FILETYPE_PEM));
  1138. /* invalid key type */
  1139. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1140. /* success */
  1141. #ifdef NO_RSA
  1142. /* rsa needed */
  1143. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1144. WOLFSSL_FILETYPE_PEM));
  1145. #else
  1146. /* success */
  1147. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1148. WOLFSSL_FILETYPE_PEM));
  1149. #endif
  1150. wolfSSL_CTX_free(ctx);
  1151. #endif
  1152. return EXPECT_RESULT();
  1153. }
  1154. /* test both file and buffer versions along with unloading trusted peer certs */
  1155. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1156. {
  1157. EXPECT_DECLS;
  1158. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1159. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1160. WOLFSSL_CTX *ctx = NULL;
  1161. WOLFSSL* ssl = NULL;
  1162. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1163. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1164. #if !defined(NO_FILESYSTEM)
  1165. /* invalid file */
  1166. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1167. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1168. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1169. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1170. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1171. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1172. /* success */
  1173. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1174. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1175. /* unload cert */
  1176. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1177. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1178. /* invalid file */
  1179. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1180. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1181. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1182. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1183. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1184. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1185. /* success */
  1186. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1187. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1188. #ifdef WOLFSSL_LOCAL_X509_STORE
  1189. /* unload cert */
  1190. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1191. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1192. #endif
  1193. #endif
  1194. /* Test of loading certs from buffers */
  1195. /* invalid buffer */
  1196. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1197. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1198. /* success */
  1199. #ifdef USE_CERT_BUFFERS_1024
  1200. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1201. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1202. #endif
  1203. #ifdef USE_CERT_BUFFERS_2048
  1204. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1205. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1206. #endif
  1207. /* unload cert */
  1208. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1209. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1210. wolfSSL_free(ssl);
  1211. wolfSSL_CTX_free(ctx);
  1212. #endif
  1213. return EXPECT_RESULT();
  1214. }
  1215. static int test_wolfSSL_CTX_load_verify_locations(void)
  1216. {
  1217. EXPECT_DECLS;
  1218. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1219. WOLFSSL_CTX *ctx = NULL;
  1220. #ifndef NO_RSA
  1221. WOLFSSL_CERT_MANAGER* cm = NULL;
  1222. #ifdef PERSIST_CERT_CACHE
  1223. int cacheSz = 0;
  1224. unsigned char* cache = NULL;
  1225. int used = 0;
  1226. #ifndef NO_FILESYSTEM
  1227. const char* cacheFile = "./tests/cert_cache.tmp";
  1228. #endif
  1229. int i;
  1230. int t;
  1231. int* p;
  1232. #endif
  1233. #endif
  1234. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1235. const char* load_certs_path = "./certs/external";
  1236. const char* load_no_certs_path = "./examples";
  1237. const char* load_expired_path = "./certs/test/expired";
  1238. #endif
  1239. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1240. /* invalid arguments */
  1241. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1242. WOLFSSL_FAILURE);
  1243. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1244. WOLFSSL_FAILURE);
  1245. /* invalid ca file */
  1246. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1247. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1248. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1249. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1250. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1251. /* invalid path */
  1252. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1253. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1254. #endif
  1255. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1256. /* test ignoring the invalid path */
  1257. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1258. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1259. #endif
  1260. /* load ca cert */
  1261. #ifdef NO_RSA
  1262. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1263. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1264. #else /* Skip the following test without RSA certs. */
  1265. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1266. WOLFSSL_SUCCESS);
  1267. #ifdef PERSIST_CERT_CACHE
  1268. /* Get cert cache size */
  1269. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1270. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1271. DYNAMIC_TYPE_TMP_BUFFER));
  1272. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1273. BAD_FUNC_ARG);
  1274. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1275. BAD_FUNC_ARG);
  1276. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1277. BAD_FUNC_ARG);
  1278. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1279. BAD_FUNC_ARG);
  1280. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1281. BAD_FUNC_ARG);
  1282. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1283. BAD_FUNC_ARG);
  1284. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1285. BAD_FUNC_ARG);
  1286. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1287. BAD_FUNC_ARG);
  1288. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1289. BAD_FUNC_ARG);
  1290. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1291. BUFFER_E);
  1292. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1293. ExpectIntEQ(cacheSz, used);
  1294. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1295. BAD_FUNC_ARG);
  1296. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1297. BAD_FUNC_ARG);
  1298. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1299. BAD_FUNC_ARG);
  1300. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1301. BAD_FUNC_ARG);
  1302. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1303. BAD_FUNC_ARG);
  1304. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1305. BAD_FUNC_ARG);
  1306. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1307. BAD_FUNC_ARG);
  1308. /* Smaller than header. */
  1309. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1310. for (i = 1; i < cacheSz; i++) {
  1311. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1312. BUFFER_E);
  1313. }
  1314. if (EXPECT_SUCCESS()) {
  1315. /* Modify header for bad results! */
  1316. p = (int*)cache;
  1317. /* version */
  1318. t = p[0]; p[0] = 0xff;
  1319. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1320. CACHE_MATCH_ERROR);
  1321. p[0] = t; p++;
  1322. /* rows */
  1323. t = p[0]; p[0] = 0xff;
  1324. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1325. CACHE_MATCH_ERROR);
  1326. p[0] = t; p++;
  1327. /* columns[0] */
  1328. t = p[0]; p[0] = -1;
  1329. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1330. PARSE_ERROR);
  1331. p[0] = t; p += CA_TABLE_SIZE;
  1332. /* signerSz*/
  1333. t = p[0]; p[0] = 0xff;
  1334. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1335. CACHE_MATCH_ERROR);
  1336. p[0] = t;
  1337. }
  1338. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1339. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1340. #ifndef NO_FILESYSTEM
  1341. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1342. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1343. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1344. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1345. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1346. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1347. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1348. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1349. WOLFSSL_BAD_FILE);
  1350. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1351. /* File contents is not a cache. */
  1352. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1353. CACHE_MATCH_ERROR);
  1354. #endif
  1355. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1356. #endif
  1357. /* Test unloading CA's */
  1358. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1359. #ifdef PERSIST_CERT_CACHE
  1360. /* Verify no certs (result is less than cacheSz) */
  1361. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1362. #endif
  1363. /* load ca cert again */
  1364. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1365. WOLFSSL_SUCCESS);
  1366. /* Test getting CERT_MANAGER */
  1367. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1368. /* Test unloading CA's using CM */
  1369. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1370. #ifdef PERSIST_CERT_CACHE
  1371. /* Verify no certs (result is less than cacheSz) */
  1372. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1373. #endif
  1374. #endif
  1375. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1376. /* Test loading CA certificates using a path */
  1377. #ifdef NO_RSA
  1378. /* failure here okay since certs in external directory are RSA */
  1379. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1380. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1381. #else
  1382. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1383. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1384. #endif
  1385. /* Test loading path with no files */
  1386. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1387. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1388. /* Test loading expired CA certificates */
  1389. #ifdef NO_RSA
  1390. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1391. load_expired_path,
  1392. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1393. WOLFSSL_SUCCESS);
  1394. #else
  1395. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1396. load_expired_path,
  1397. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1398. WOLFSSL_SUCCESS);
  1399. #endif
  1400. /* Test loading CA certificates and ignoring all errors */
  1401. #ifdef NO_RSA
  1402. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1403. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1404. #else
  1405. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1406. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1407. #endif
  1408. #endif
  1409. wolfSSL_CTX_free(ctx);
  1410. #endif
  1411. return EXPECT_RESULT();
  1412. }
  1413. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1414. {
  1415. int res = TEST_SKIPPED;
  1416. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1417. (!defined(NO_RSA) || defined(HAVE_ECC))
  1418. WOLFSSL_CTX* ctx;
  1419. byte dirValid = 0;
  1420. int ret = 0;
  1421. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1422. if (ctx == NULL) {
  1423. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1424. ret = -1;
  1425. }
  1426. if (ret == 0) {
  1427. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1428. dirValid = 1;
  1429. #else
  1430. word32 numDirs;
  1431. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1432. if (caDirs == NULL || numDirs == 0) {
  1433. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1434. ret = -1;
  1435. }
  1436. else {
  1437. ReadDirCtx dirCtx;
  1438. word32 i;
  1439. for (i = 0; i < numDirs; ++i) {
  1440. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1441. /* Directory isn't empty. */
  1442. dirValid = 1;
  1443. wc_ReadDirClose(&dirCtx);
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. #endif
  1449. }
  1450. /*
  1451. * If the directory isn't empty, we should be able to load CA
  1452. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1453. * usable.
  1454. */
  1455. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1456. WOLFSSL_SUCCESS) {
  1457. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1458. ret = -1;
  1459. }
  1460. #ifdef OPENSSL_EXTRA
  1461. if (ret == 0 &&
  1462. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1463. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1464. ret = -1;
  1465. }
  1466. #endif /* OPENSSL_EXTRA */
  1467. wolfSSL_CTX_free(ctx);
  1468. res = TEST_RES_CHECK(ret == 0);
  1469. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1470. return res;
  1471. }
  1472. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1473. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1474. int file_type)
  1475. {
  1476. int ret;
  1477. WOLFSSL_CERT_MANAGER* cm;
  1478. cm = wolfSSL_CertManagerNew();
  1479. if (cm == NULL) {
  1480. fprintf(stderr, "test_cm_load_ca failed\n");
  1481. return -1;
  1482. }
  1483. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1484. wolfSSL_CertManagerFree(cm);
  1485. return ret;
  1486. }
  1487. static int test_cm_load_ca_file(const char* ca_cert_file)
  1488. {
  1489. int ret = 0;
  1490. byte* cert_buf = NULL;
  1491. size_t cert_sz = 0;
  1492. #if defined(WOLFSSL_PEM_TO_DER)
  1493. DerBuffer* pDer = NULL;
  1494. #endif
  1495. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1496. if (ret == 0) {
  1497. /* normal test */
  1498. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1499. if (ret == WOLFSSL_SUCCESS) {
  1500. /* test including null terminator in length */
  1501. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1502. if (tmp == NULL) {
  1503. ret = MEMORY_E;
  1504. }
  1505. else {
  1506. cert_buf = tmp;
  1507. cert_buf[cert_sz] = '\0';
  1508. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1509. WOLFSSL_FILETYPE_PEM);
  1510. }
  1511. }
  1512. #if defined(WOLFSSL_PEM_TO_DER)
  1513. if (ret == WOLFSSL_SUCCESS) {
  1514. /* test loading DER */
  1515. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1516. if (ret == 0 && pDer != NULL) {
  1517. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1518. WOLFSSL_FILETYPE_ASN1);
  1519. wc_FreeDer(&pDer);
  1520. }
  1521. }
  1522. #endif
  1523. }
  1524. free(cert_buf);
  1525. return ret;
  1526. }
  1527. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1528. int file_type, word32 flags)
  1529. {
  1530. int ret;
  1531. WOLFSSL_CERT_MANAGER* cm;
  1532. cm = wolfSSL_CertManagerNew();
  1533. if (cm == NULL) {
  1534. fprintf(stderr, "test_cm_load_ca failed\n");
  1535. return -1;
  1536. }
  1537. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1538. 0, flags);
  1539. wolfSSL_CertManagerFree(cm);
  1540. return ret;
  1541. }
  1542. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1543. {
  1544. int ret = 0;
  1545. byte* cert_buf = NULL;
  1546. size_t cert_sz = 0;
  1547. #if defined(WOLFSSL_PEM_TO_DER)
  1548. DerBuffer* pDer = NULL;
  1549. #endif
  1550. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1551. if (ret == 0) {
  1552. /* normal test */
  1553. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1554. WOLFSSL_FILETYPE_PEM, flags);
  1555. if (ret == WOLFSSL_SUCCESS) {
  1556. /* test including null terminator in length */
  1557. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1558. if (tmp == NULL) {
  1559. ret = MEMORY_E;
  1560. }
  1561. else {
  1562. cert_buf = tmp;
  1563. cert_buf[cert_sz] = '\0';
  1564. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1565. WOLFSSL_FILETYPE_PEM, flags);
  1566. }
  1567. }
  1568. #if defined(WOLFSSL_PEM_TO_DER)
  1569. if (ret == WOLFSSL_SUCCESS) {
  1570. /* test loading DER */
  1571. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1572. if (ret == 0 && pDer != NULL) {
  1573. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1574. WOLFSSL_FILETYPE_ASN1, flags);
  1575. wc_FreeDer(&pDer);
  1576. }
  1577. }
  1578. #endif
  1579. }
  1580. free(cert_buf);
  1581. return ret;
  1582. }
  1583. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1584. static int test_wolfSSL_CertManagerAPI(void)
  1585. {
  1586. EXPECT_DECLS;
  1587. #ifndef NO_CERTS
  1588. WOLFSSL_CERT_MANAGER* cm = NULL;
  1589. unsigned char c;
  1590. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1591. wolfSSL_CertManagerFree(NULL);
  1592. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1593. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1594. #ifdef WOLFSSL_TRUST_PEER_CERT
  1595. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1596. #endif
  1597. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1598. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1599. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1600. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1601. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1602. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1603. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1604. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1605. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1606. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1607. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1608. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1609. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1610. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1611. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1612. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1613. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1614. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1615. WOLFSSL_BAD_FILETYPE);
  1616. #endif
  1617. #if !defined(NO_FILESYSTEM)
  1618. {
  1619. const char* ca_cert = "./certs/ca-cert.pem";
  1620. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1621. const char* ca_cert_der = "./certs/ca-cert.der";
  1622. #endif
  1623. const char* ca_path = "./certs";
  1624. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1625. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1626. BAD_FUNC_ARG);
  1627. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1628. BAD_FUNC_ARG);
  1629. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1630. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1631. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1632. WOLFSSL_BAD_FILETYPE);
  1633. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1634. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1635. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1636. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1637. #endif
  1638. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1639. WOLFSSL_FATAL_ERROR);
  1640. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1641. WOLFSSL_FATAL_ERROR);
  1642. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  1643. WOLFSSL_FATAL_ERROR);
  1644. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  1645. WOLFSSL_FATAL_ERROR);
  1646. }
  1647. #endif
  1648. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  1649. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  1650. #elif !defined(HAVE_CRL)
  1651. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  1652. #endif
  1653. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  1654. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  1655. #ifdef HAVE_CRL
  1656. /* Test APIs when CRL is disabled. */
  1657. #ifdef HAVE_CRL_IO
  1658. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  1659. #endif
  1660. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  1661. sizeof_server_cert_der_2048), 1);
  1662. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  1663. #endif
  1664. /* OCSP */
  1665. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  1666. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  1667. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  1668. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  1669. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1670. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1671. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  1672. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1673. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  1674. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1675. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1676. #endif
  1677. #ifdef HAVE_OCSP
  1678. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  1679. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  1680. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  1681. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  1682. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  1683. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  1684. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  1685. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  1686. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1687. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  1688. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1689. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  1690. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1691. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  1692. BAD_FUNC_ARG);
  1693. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  1694. BAD_FUNC_ARG);
  1695. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  1696. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  1697. BAD_FUNC_ARG);
  1698. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  1699. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  1700. /* Test APIs when OCSP is disabled. */
  1701. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  1702. NULL, NULL, NULL, NULL), 1);
  1703. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  1704. #endif
  1705. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  1706. wolfSSL_CertManagerFree(cm);
  1707. wolfSSL_CertManagerFree(cm);
  1708. cm = NULL;
  1709. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1710. #ifdef HAVE_OCSP
  1711. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  1712. WOLFSSL_OCSP_CHECKALL), 1);
  1713. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1714. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1715. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1716. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1717. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  1718. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1719. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  1720. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  1721. #endif
  1722. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1723. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1724. #endif
  1725. #ifdef WOLFSSL_TRUST_PEER_CERT
  1726. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  1727. #endif
  1728. wolfSSL_CertManagerFree(cm);
  1729. #endif
  1730. return EXPECT_RESULT();
  1731. }
  1732. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1733. {
  1734. EXPECT_DECLS;
  1735. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1736. const char* ca_cert = "./certs/ca-cert.pem";
  1737. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1738. int ret;
  1739. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  1740. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1741. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1742. #elif defined(NO_RSA)
  1743. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1744. #else
  1745. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1746. #endif
  1747. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  1748. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1749. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1750. #elif defined(NO_RSA)
  1751. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1752. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1753. !defined(NO_ASN_TIME)
  1754. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1755. #else
  1756. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1757. #endif
  1758. #endif
  1759. return EXPECT_RESULT();
  1760. }
  1761. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1762. {
  1763. EXPECT_DECLS;
  1764. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1765. const char* ca_cert = "./certs/ca-cert.pem";
  1766. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1767. int ret;
  1768. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  1769. 1);
  1770. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1771. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1772. #elif defined(NO_RSA)
  1773. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1774. #else
  1775. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1776. #endif
  1777. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1778. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  1779. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1780. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1781. #elif defined(NO_RSA)
  1782. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1783. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1784. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1785. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1786. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1787. #else
  1788. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1789. #endif
  1790. #endif
  1791. return EXPECT_RESULT();
  1792. }
  1793. static int test_wolfSSL_CertManagerGetCerts(void)
  1794. {
  1795. EXPECT_DECLS;
  1796. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1797. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1798. defined(WOLFSSL_SIGNER_DER_CERT)
  1799. WOLFSSL_CERT_MANAGER* cm = NULL;
  1800. WOLFSSL_STACK* sk = NULL;
  1801. X509* x509 = NULL;
  1802. X509* cert1 = NULL;
  1803. FILE* file1 = NULL;
  1804. #ifdef DEBUG_WOLFSSL_VERBOSE
  1805. WOLFSSL_BIO* bio = NULL;
  1806. #endif
  1807. int i = 0;
  1808. int ret = 0;
  1809. const byte* der;
  1810. int derSz = 0;
  1811. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  1812. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1813. if (file1 != NULL) {
  1814. fclose(file1);
  1815. }
  1816. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  1817. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1818. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1819. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1820. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1821. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1822. * and full OpenSSL compatibility */
  1823. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1824. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  1825. #else
  1826. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1827. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  1828. #endif
  1829. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1830. "./certs/ca-cert.pem", NULL));
  1831. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1832. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  1833. ExpectNotNull(x509 = sk_X509_value(sk, i));
  1834. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1835. #ifdef DEBUG_WOLFSSL_VERBOSE
  1836. bio = BIO_new(wolfSSL_BIO_s_file());
  1837. if (bio != NULL) {
  1838. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1839. X509_print(bio, x509);
  1840. BIO_free(bio);
  1841. }
  1842. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1843. }
  1844. wolfSSL_X509_free(cert1);
  1845. sk_X509_pop_free(sk, NULL);
  1846. wolfSSL_CertManagerFree(cm);
  1847. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1848. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1849. defined(WOLFSSL_SIGNER_DER_CERT) */
  1850. return EXPECT_RESULT();
  1851. }
  1852. static int test_wolfSSL_CertManagerSetVerify(void)
  1853. {
  1854. EXPECT_DECLS;
  1855. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1856. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1857. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1858. WOLFSSL_CERT_MANAGER* cm = NULL;
  1859. int tmp = myVerifyAction;
  1860. const char* ca_cert = "./certs/ca-cert.pem";
  1861. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1862. wolfSSL_CertManagerSetVerify(NULL, NULL);
  1863. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  1864. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1865. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1866. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1867. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  1868. #else
  1869. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  1870. WOLFSSL_SUCCESS);
  1871. #endif
  1872. /* Use the test CB that always accepts certs */
  1873. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1874. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  1875. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1876. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1877. {
  1878. const char* verifyCert = "./certs/server-cert.der";
  1879. /* Use the test CB that always fails certs */
  1880. myVerifyAction = VERIFY_FORCE_FAIL;
  1881. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  1882. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  1883. }
  1884. #endif
  1885. wolfSSL_CertManagerFree(cm);
  1886. myVerifyAction = tmp;
  1887. #endif
  1888. return EXPECT_RESULT();
  1889. }
  1890. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1891. defined(DEBUG_UNIT_TEST_CERTS)
  1892. /* Used when debugging name constraint tests. Not static to allow use in
  1893. * multiple locations with complex define guards. */
  1894. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1895. {
  1896. BIO* out = BIO_new_file(fileName, "wb");
  1897. if (out != NULL) {
  1898. PEM_write_bio_X509(out, x509);
  1899. BIO_free(out);
  1900. }
  1901. }
  1902. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1903. {
  1904. BIO* out = BIO_new_file(fileName, "wb");
  1905. if (out != NULL) {
  1906. BIO_write(out, der, derSz);
  1907. BIO_free(out);
  1908. }
  1909. }
  1910. #else
  1911. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  1912. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  1913. #endif
  1914. static int test_wolfSSL_CertManagerNameConstraint(void)
  1915. {
  1916. EXPECT_DECLS;
  1917. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1918. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1919. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1920. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1921. !defined(NO_SHA256)
  1922. WOLFSSL_CERT_MANAGER* cm = NULL;
  1923. WOLFSSL_EVP_PKEY *priv = NULL;
  1924. WOLFSSL_X509_NAME* name = NULL;
  1925. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1926. const char* server_cert = "./certs/test/server-goodcn.pem";
  1927. int i = 0;
  1928. static const byte extNameConsOid[] = {85, 29, 30};
  1929. RsaKey key;
  1930. WC_RNG rng;
  1931. byte *der = NULL;
  1932. int derSz = 0;
  1933. word32 idx = 0;
  1934. byte *pt;
  1935. WOLFSSL_X509 *x509 = NULL;
  1936. WOLFSSL_X509 *ca = NULL;
  1937. wc_InitRng(&rng);
  1938. /* load in CA private key for signing */
  1939. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1940. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1941. sizeof_server_key_der_2048), 0);
  1942. /* get ca certificate then alter it */
  1943. ExpectNotNull(der =
  1944. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1945. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1946. WOLFSSL_FILETYPE_ASN1));
  1947. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1948. if (EXPECT_SUCCESS() && (der != NULL)) {
  1949. XMEMCPY(der, pt, derSz);
  1950. /* find the name constraint extension and alter it */
  1951. pt = der;
  1952. for (i = 0; i < derSz - 3; i++) {
  1953. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1954. pt += 3;
  1955. break;
  1956. }
  1957. pt++;
  1958. }
  1959. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1960. /* go to the length value and set it to 0 */
  1961. while (i < derSz && *pt != 0x81) {
  1962. pt++;
  1963. i++;
  1964. }
  1965. ExpectIntNE(i, derSz); /* did not place to alter */
  1966. pt++;
  1967. *pt = 0x00;
  1968. }
  1969. /* resign the altered certificate */
  1970. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1971. FOURK_BUF, &key, NULL, &rng)), 0);
  1972. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1973. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1974. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1975. wolfSSL_CertManagerFree(cm);
  1976. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1977. wolfSSL_X509_free(x509);
  1978. wc_FreeRsaKey(&key);
  1979. wc_FreeRng(&rng);
  1980. /* add email alt name to satisfy constraint */
  1981. pt = (byte*)server_key_der_2048;
  1982. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1983. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1984. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1985. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1986. WOLFSSL_FILETYPE_ASN1));
  1987. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1988. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1989. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1990. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1991. /* Good cert test with proper alt email name */
  1992. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1993. WOLFSSL_FILETYPE_PEM));
  1994. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1995. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1996. name = NULL;
  1997. ExpectNotNull(name = X509_NAME_new());
  1998. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1999. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2000. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2001. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2002. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2003. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2004. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2005. X509_NAME_free(name);
  2006. name = NULL;
  2007. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2008. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2009. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2010. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2011. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2012. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2013. wolfSSL_X509_free(x509);
  2014. x509 = NULL;
  2015. /* Cert with bad alt name list */
  2016. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2017. WOLFSSL_FILETYPE_PEM));
  2018. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2019. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2020. name = NULL;
  2021. ExpectNotNull(name = X509_NAME_new());
  2022. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2023. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2024. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2025. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2026. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2027. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2028. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2029. X509_NAME_free(name);
  2030. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2031. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2032. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2033. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2034. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2035. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2036. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2037. wolfSSL_CertManagerFree(cm);
  2038. wolfSSL_X509_free(x509);
  2039. wolfSSL_X509_free(ca);
  2040. wolfSSL_EVP_PKEY_free(priv);
  2041. #endif
  2042. return EXPECT_RESULT();
  2043. }
  2044. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2045. {
  2046. EXPECT_DECLS;
  2047. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2048. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2049. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2050. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2051. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2052. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2053. const char* server_cert = "./certs/server-cert.pem";
  2054. WOLFSSL_CERT_MANAGER* cm = NULL;
  2055. WOLFSSL_X509 *x509 = NULL;
  2056. WOLFSSL_X509 *ca = NULL;
  2057. const unsigned char *der = NULL;
  2058. const unsigned char *pt;
  2059. WOLFSSL_EVP_PKEY *priv = NULL;
  2060. WOLFSSL_X509_NAME* name = NULL;
  2061. int derSz = 0;
  2062. /* C=US*/
  2063. char altName[] = {
  2064. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2065. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2066. };
  2067. /* C=ID */
  2068. char altNameFail[] = {
  2069. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2070. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2071. };
  2072. /* C=US ST=California*/
  2073. char altNameExc[] = {
  2074. 0x30, 0x22,
  2075. 0x31, 0x0B,
  2076. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2077. 0x31, 0x13,
  2078. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2079. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2080. };
  2081. /* load in CA private key for signing */
  2082. pt = ca_key_der_2048;
  2083. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2084. sizeof_ca_key_der_2048));
  2085. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2086. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2087. WOLFSSL_FILETYPE_ASN1));
  2088. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2089. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2090. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2091. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2092. WOLFSSL_FILETYPE_PEM));
  2093. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2094. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2095. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2096. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2097. #else
  2098. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2099. #endif
  2100. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2101. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2102. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2103. /* add in matching DIR alt name and resign */
  2104. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2105. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2106. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2107. #else
  2108. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2109. #endif
  2110. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2111. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2112. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2113. wolfSSL_X509_free(x509);
  2114. x509 = NULL;
  2115. /* check verify fail */
  2116. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2117. WOLFSSL_FILETYPE_PEM));
  2118. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2119. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2120. /* add in miss matching DIR alt name and resign */
  2121. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2122. ASN_DIR_TYPE);
  2123. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2124. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2125. #else
  2126. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2127. #endif
  2128. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2129. #ifndef WOLFSSL_NO_ASN_STRICT
  2130. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2131. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2132. #else
  2133. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2134. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2135. #endif
  2136. /* check that it still fails if one bad altname and one good altname is in
  2137. * the certificate */
  2138. wolfSSL_X509_free(x509);
  2139. x509 = NULL;
  2140. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2141. WOLFSSL_FILETYPE_PEM));
  2142. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2143. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2144. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2145. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2146. ASN_DIR_TYPE);
  2147. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2148. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2149. #else
  2150. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2151. #endif
  2152. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2153. #ifndef WOLFSSL_NO_ASN_STRICT
  2154. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2155. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2156. #else
  2157. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2158. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2159. #endif
  2160. /* check it fails with switching position of bad altname */
  2161. wolfSSL_X509_free(x509);
  2162. x509 = NULL;
  2163. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2164. WOLFSSL_FILETYPE_PEM));
  2165. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2166. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2167. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2168. ASN_DIR_TYPE);
  2169. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2170. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2171. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2172. #else
  2173. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2174. #endif
  2175. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2176. #ifndef WOLFSSL_NO_ASN_STRICT
  2177. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2178. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2179. #else
  2180. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2181. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2182. #endif
  2183. wolfSSL_CertManagerFree(cm);
  2184. wolfSSL_X509_free(x509);
  2185. x509 = NULL;
  2186. wolfSSL_X509_free(ca);
  2187. ca = NULL;
  2188. /* now test with excluded name constraint */
  2189. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2190. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2191. WOLFSSL_FILETYPE_ASN1));
  2192. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2193. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2194. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2195. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2196. WOLFSSL_FILETYPE_PEM));
  2197. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2198. ASN_DIR_TYPE);
  2199. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2200. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2201. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2202. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2203. #else
  2204. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2205. #endif
  2206. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2207. #ifndef WOLFSSL_NO_ASN_STRICT
  2208. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2209. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2210. #else
  2211. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2212. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2213. #endif
  2214. wolfSSL_CertManagerFree(cm);
  2215. wolfSSL_X509_free(x509);
  2216. wolfSSL_X509_free(ca);
  2217. wolfSSL_EVP_PKEY_free(priv);
  2218. #endif
  2219. return EXPECT_RESULT();
  2220. }
  2221. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2222. {
  2223. EXPECT_DECLS;
  2224. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2225. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2226. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2227. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2228. !defined(NO_SHA256)
  2229. WOLFSSL_CERT_MANAGER* cm = NULL;
  2230. WOLFSSL_EVP_PKEY *priv = NULL;
  2231. WOLFSSL_X509_NAME* name = NULL;
  2232. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2233. const char* server_cert = "./certs/test/server-goodcn.pem";
  2234. byte *der = NULL;
  2235. int derSz = 0;
  2236. byte *pt;
  2237. WOLFSSL_X509 *x509 = NULL;
  2238. WOLFSSL_X509 *ca = NULL;
  2239. pt = (byte*)server_key_der_2048;
  2240. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2241. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2242. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2243. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2244. WOLFSSL_FILETYPE_ASN1));
  2245. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2246. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2247. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2248. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2249. /* check satisfying .wolfssl.com constraint passes */
  2250. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2251. WOLFSSL_FILETYPE_PEM));
  2252. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2253. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2254. name = NULL;
  2255. ExpectNotNull(name = X509_NAME_new());
  2256. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2257. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2258. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2259. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2260. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2261. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2262. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2263. X509_NAME_free(name);
  2264. name = NULL;
  2265. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2266. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2267. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2268. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2269. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2270. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2271. wolfSSL_X509_free(x509);
  2272. x509 = NULL;
  2273. /* check satisfying .random.com constraint passes */
  2274. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2275. WOLFSSL_FILETYPE_PEM));
  2276. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2277. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2278. name = NULL;
  2279. ExpectNotNull(name = X509_NAME_new());
  2280. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2281. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2282. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2283. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2284. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2285. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2286. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2287. X509_NAME_free(name);
  2288. name = NULL;
  2289. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2290. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2291. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2292. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2293. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2294. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2295. wolfSSL_X509_free(x509);
  2296. x509 = NULL;
  2297. /* check fail case when neither constraint is matched */
  2298. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2299. WOLFSSL_FILETYPE_PEM));
  2300. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2301. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2302. name = NULL;
  2303. ExpectNotNull(name = X509_NAME_new());
  2304. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2305. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2306. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2307. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2308. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2309. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2310. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2311. X509_NAME_free(name);
  2312. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2313. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2314. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2315. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2316. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2317. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2318. wolfSSL_CertManagerFree(cm);
  2319. wolfSSL_X509_free(x509);
  2320. wolfSSL_X509_free(ca);
  2321. wolfSSL_EVP_PKEY_free(priv);
  2322. #endif
  2323. return EXPECT_RESULT();
  2324. }
  2325. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2326. {
  2327. EXPECT_DECLS;
  2328. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2329. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2330. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2331. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2332. !defined(NO_SHA256)
  2333. WOLFSSL_CERT_MANAGER* cm = NULL;
  2334. WOLFSSL_EVP_PKEY *priv = NULL;
  2335. WOLFSSL_X509_NAME* name = NULL;
  2336. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2337. const char* server_cert = "./certs/test/server-goodcn.pem";
  2338. byte *der = NULL;
  2339. int derSz;
  2340. byte *pt;
  2341. WOLFSSL_X509 *x509 = NULL;
  2342. WOLFSSL_X509 *ca = NULL;
  2343. pt = (byte*)server_key_der_2048;
  2344. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2345. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2346. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2347. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2348. WOLFSSL_FILETYPE_ASN1));
  2349. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2350. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2351. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2352. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2353. /* check satisfying wolfssl.com constraint passes */
  2354. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2355. WOLFSSL_FILETYPE_PEM));
  2356. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2357. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2358. name = NULL;
  2359. ExpectNotNull(name = X509_NAME_new());
  2360. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2361. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2362. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2363. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2364. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2365. X509_NAME_free(name);
  2366. name = NULL;
  2367. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2368. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2369. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2370. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2371. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2372. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2373. wolfSSL_X509_free(x509);
  2374. x509 = NULL;
  2375. /* check satisfying example.com constraint passes */
  2376. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2377. WOLFSSL_FILETYPE_PEM));
  2378. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2379. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2380. name = NULL;
  2381. ExpectNotNull(name = X509_NAME_new());
  2382. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2383. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2384. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2385. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2386. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2387. X509_NAME_free(name);
  2388. name = NULL;
  2389. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2390. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2391. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2392. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2393. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2394. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2395. wolfSSL_X509_free(x509);
  2396. x509 = NULL;
  2397. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2398. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2399. WOLFSSL_FILETYPE_PEM));
  2400. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2401. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2402. name = NULL;
  2403. ExpectNotNull(name = X509_NAME_new());
  2404. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2405. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2406. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2407. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2408. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2409. X509_NAME_free(name);
  2410. name = NULL;
  2411. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2412. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2413. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2414. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2415. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2416. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2417. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2418. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2419. wolfSSL_X509_free(x509);
  2420. x509 = NULL;
  2421. /* check fail when one DNS in the list is bad */
  2422. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2423. WOLFSSL_FILETYPE_PEM));
  2424. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2425. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2426. name = NULL;
  2427. ExpectNotNull(name = X509_NAME_new());
  2428. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2429. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2430. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2431. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2432. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2433. X509_NAME_free(name);
  2434. name = NULL;
  2435. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2436. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2437. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2438. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2439. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2440. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2441. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2442. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2443. wolfSSL_X509_free(x509);
  2444. x509 = NULL;
  2445. /* check fail case when neither constraint is matched */
  2446. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2447. WOLFSSL_FILETYPE_PEM));
  2448. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2449. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2450. name = NULL;
  2451. ExpectNotNull(name = X509_NAME_new());
  2452. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2453. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2454. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2455. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2456. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2457. X509_NAME_free(name);
  2458. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2459. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2460. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2461. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2462. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2463. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2464. wolfSSL_CertManagerFree(cm);
  2465. wolfSSL_X509_free(x509);
  2466. wolfSSL_X509_free(ca);
  2467. wolfSSL_EVP_PKEY_free(priv);
  2468. #endif
  2469. return EXPECT_RESULT();
  2470. }
  2471. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2472. {
  2473. EXPECT_DECLS;
  2474. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2475. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2476. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2477. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2478. !defined(NO_SHA256)
  2479. WOLFSSL_CERT_MANAGER* cm = NULL;
  2480. WOLFSSL_EVP_PKEY *priv = NULL;
  2481. WOLFSSL_X509_NAME* name = NULL;
  2482. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2483. const char* server_cert = "./certs/test/server-goodcn.pem";
  2484. byte *der = NULL;
  2485. int derSz;
  2486. byte *pt;
  2487. WOLFSSL_X509 *x509 = NULL;
  2488. WOLFSSL_X509 *ca = NULL;
  2489. pt = (byte*)server_key_der_2048;
  2490. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2491. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2492. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2493. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2494. WOLFSSL_FILETYPE_ASN1));
  2495. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2496. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2497. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2498. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2499. /* check satisfying wolfssl.com constraint passes */
  2500. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2501. WOLFSSL_FILETYPE_PEM));
  2502. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2503. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2504. name = NULL;
  2505. ExpectNotNull(name = X509_NAME_new());
  2506. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2507. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2508. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2509. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2510. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2511. X509_NAME_free(name);
  2512. name = NULL;
  2513. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2514. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2515. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2516. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2517. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2518. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2519. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2520. wolfSSL_X509_free(x509);
  2521. x509 = NULL;
  2522. /* fail with DNS check because of common name */
  2523. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2524. WOLFSSL_FILETYPE_PEM));
  2525. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2526. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2527. name = NULL;
  2528. ExpectNotNull(name = X509_NAME_new());
  2529. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2530. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2531. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2532. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2533. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2534. X509_NAME_free(name);
  2535. name = NULL;
  2536. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2537. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2538. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2539. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2540. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2541. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2542. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2543. wolfSSL_X509_free(x509);
  2544. x509 = NULL;
  2545. /* fail on permitted DNS name constraint */
  2546. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2547. WOLFSSL_FILETYPE_PEM));
  2548. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2549. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2550. name = NULL;
  2551. ExpectNotNull(name = X509_NAME_new());
  2552. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2553. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2554. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2555. X509_NAME_free(name);
  2556. name = NULL;
  2557. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2558. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2559. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2560. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2561. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2562. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2563. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2564. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2565. wolfSSL_X509_free(x509);
  2566. x509 = NULL;
  2567. /* fail on permitted email name constraint */
  2568. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2569. WOLFSSL_FILETYPE_PEM));
  2570. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2571. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2572. name = NULL;
  2573. ExpectNotNull(name = X509_NAME_new());
  2574. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2575. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2576. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2577. X509_NAME_free(name);
  2578. name = NULL;
  2579. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2580. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2581. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2582. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2583. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2584. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2585. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2586. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2587. wolfSSL_X509_free(x509);
  2588. x509 = NULL;
  2589. /* success with empty email name */
  2590. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2591. WOLFSSL_FILETYPE_PEM));
  2592. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2593. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2594. name = NULL;
  2595. ExpectNotNull(name = X509_NAME_new());
  2596. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2597. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2598. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2599. X509_NAME_free(name);
  2600. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2601. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2602. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2603. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2604. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2605. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2606. wolfSSL_X509_free(x509);
  2607. wolfSSL_CertManagerFree(cm);
  2608. wolfSSL_X509_free(ca);
  2609. wolfSSL_EVP_PKEY_free(priv);
  2610. #endif
  2611. return EXPECT_RESULT();
  2612. }
  2613. static int test_wolfSSL_CertManagerCRL(void)
  2614. {
  2615. EXPECT_DECLS;
  2616. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2617. !defined(NO_RSA)
  2618. const char* ca_cert = "./certs/ca-cert.pem";
  2619. const char* crl1 = "./certs/crl/crl.pem";
  2620. const char* crl2 = "./certs/crl/crl2.pem";
  2621. const unsigned char crl_buff[] = {
  2622. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2623. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2624. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2625. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2626. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2627. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2628. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2629. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2630. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2631. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2632. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2633. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2634. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2635. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2636. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2637. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  2638. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  2639. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  2640. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2641. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  2642. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2643. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  2644. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  2645. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  2646. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  2647. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  2648. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  2649. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  2650. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  2651. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  2652. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  2653. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  2654. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  2655. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  2656. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  2657. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  2658. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  2659. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  2660. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  2661. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  2662. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  2663. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  2664. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  2665. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  2666. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  2667. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  2668. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  2669. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  2670. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  2671. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  2672. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  2673. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  2674. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  2675. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  2676. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  2677. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  2678. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  2679. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  2680. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  2681. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  2682. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  2683. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  2684. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  2685. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  2686. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  2687. };
  2688. WOLFSSL_CERT_MANAGER* cm = NULL;
  2689. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2690. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  2691. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2692. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  2693. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  2694. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  2695. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  2696. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2697. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  2698. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  2699. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  2700. BAD_FUNC_ARG);
  2701. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  2702. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  2703. BAD_FUNC_ARG);
  2704. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  2705. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  2706. BAD_FUNC_ARG);
  2707. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2708. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  2709. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  2710. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  2711. #ifdef HAVE_CRL_IO
  2712. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  2713. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2714. #endif
  2715. #ifndef NO_FILESYSTEM
  2716. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  2717. 0), BAD_FUNC_ARG);
  2718. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  2719. 0), BAD_FUNC_ARG);
  2720. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2721. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  2722. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  2723. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2724. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2725. BAD_FUNC_ARG);
  2726. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2727. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  2728. ASN_PARSE_E);
  2729. #endif
  2730. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  2731. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2732. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  2733. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2734. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  2735. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2736. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  2737. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2738. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  2739. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2740. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  2741. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2742. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  2743. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2744. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  2745. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2746. ExpectIntEQ(WOLFSSL_SUCCESS,
  2747. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2748. ExpectIntEQ(WOLFSSL_SUCCESS,
  2749. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2750. ExpectIntEQ(WOLFSSL_SUCCESS,
  2751. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2752. wolfSSL_CertManagerFreeCRL(cm);
  2753. ExpectIntEQ(WOLFSSL_SUCCESS,
  2754. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2755. ExpectIntEQ(WOLFSSL_SUCCESS,
  2756. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2757. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2758. sizeof_server_cert_der_2048), CRL_MISSING);
  2759. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  2760. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  2761. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  2762. WOLFSSL_FILETYPE_ASN1), 1);
  2763. wolfSSL_CertManagerFree(cm);
  2764. #endif
  2765. return EXPECT_RESULT();
  2766. }
  2767. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  2768. {
  2769. EXPECT_DECLS;
  2770. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  2771. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  2772. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  2773. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  2774. defined(HAVE_LIGHTY)
  2775. WOLFSSL_CERT_MANAGER* cm = NULL;
  2776. /* Raw OCSP response bytes captured using the following setup:
  2777. * - Run responder with
  2778. * openssl ocsp -port 9999 -ndays 9999
  2779. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  2780. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  2781. * -rkey certs/ocsp/ocsp-responder-key.pem
  2782. * -CA certs/ocsp/intermediate1-ca-cert.pem
  2783. * - Run client with
  2784. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  2785. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  2786. * -cert certs/ocsp/server1-cert.pem
  2787. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  2788. * - Select the response packet in Wireshark, and export it using
  2789. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  2790. * packets only". After importing into the editor, remove the initial
  2791. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  2792. */
  2793. static const byte response[] = {
  2794. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  2795. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  2796. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  2797. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  2798. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  2799. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  2800. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2801. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2802. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2803. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2804. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2805. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2806. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2807. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2808. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2809. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2810. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2811. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  2812. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  2813. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  2814. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  2815. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  2816. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  2817. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  2818. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  2819. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  2820. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  2821. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  2822. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  2823. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  2824. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  2825. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  2826. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  2827. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  2828. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  2829. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  2830. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  2831. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  2832. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  2833. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  2834. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  2835. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  2836. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  2837. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  2838. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  2839. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  2840. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  2841. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  2842. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  2843. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  2844. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  2845. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  2846. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  2847. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  2848. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  2849. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  2850. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  2851. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  2852. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  2853. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  2854. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  2855. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  2856. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  2857. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  2858. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  2859. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  2860. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  2861. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  2862. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  2863. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  2864. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  2865. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  2866. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  2867. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  2868. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  2869. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  2870. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  2871. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  2872. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  2873. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  2874. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  2875. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  2876. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  2877. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  2878. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  2879. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2880. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  2881. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  2882. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  2883. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  2884. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  2885. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  2886. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  2887. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  2888. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  2889. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  2890. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  2891. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  2892. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  2893. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  2894. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  2895. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  2896. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  2897. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  2898. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  2899. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  2900. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  2901. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  2902. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  2903. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  2904. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  2905. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  2906. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  2907. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  2908. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  2909. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  2910. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2911. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  2912. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  2913. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  2914. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  2915. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  2916. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  2917. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  2918. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  2919. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  2920. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  2921. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  2922. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  2923. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  2924. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  2925. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  2926. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  2927. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  2928. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  2929. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  2930. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  2931. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  2932. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  2933. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  2934. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  2935. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  2936. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  2937. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  2938. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  2939. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  2940. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  2941. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  2942. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  2943. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  2944. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  2945. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  2946. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  2947. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  2948. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  2949. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  2950. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  2951. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  2952. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  2953. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  2954. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  2955. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  2956. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  2957. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  2958. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  2959. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  2960. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  2961. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  2962. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  2963. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  2964. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  2965. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  2966. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  2967. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  2968. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  2969. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  2970. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  2971. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  2972. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  2973. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  2974. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  2975. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  2976. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  2977. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  2978. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2979. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  2980. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  2981. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  2982. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  2983. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  2984. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  2985. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  2986. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  2987. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  2988. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  2989. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  2990. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  2991. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  2992. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  2993. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  2994. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  2995. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  2996. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  2997. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  2998. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  2999. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3000. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3001. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3002. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3003. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3004. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3005. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3006. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3007. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3008. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3009. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3010. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3011. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3012. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3013. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3014. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3015. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3016. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3017. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3018. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3019. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3020. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3021. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3022. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3023. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3024. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3025. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3026. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3027. };
  3028. OcspEntry entry[1];
  3029. CertStatus status[1];
  3030. OcspRequest* request = NULL;
  3031. #ifndef NO_FILESYSTEM
  3032. const char* ca_cert = "./certs/ca-cert.pem";
  3033. #endif
  3034. byte serial[] = {0x05};
  3035. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3036. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3037. XMEMSET(entry, 0, sizeof(OcspEntry));
  3038. XMEMSET(status, 0, sizeof(CertStatus));
  3039. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3040. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3041. DYNAMIC_TYPE_OCSP_REQUEST));
  3042. if ((request != NULL) && (request->serial != NULL)) {
  3043. request->serialSz = sizeof(serial);
  3044. XMEMCPY(request->serial, serial, sizeof(serial));
  3045. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3046. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3047. }
  3048. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3049. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3050. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3051. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3052. /* Response should be valid. */
  3053. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3054. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3055. /* Flip a byte in the request serial number, response should be invalid
  3056. * now. */
  3057. if ((request != NULL) && (request->serial != NULL))
  3058. request->serial[0] ^= request->serial[0];
  3059. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3060. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3061. #ifndef NO_FILESYSTEM
  3062. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3063. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3064. ExpectIntEQ(WOLFSSL_SUCCESS,
  3065. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3066. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3067. sizeof(server_cert_der_2048)), 1);
  3068. #endif
  3069. wolfSSL_OCSP_REQUEST_free(request);
  3070. wolfSSL_CertManagerFree(cm);
  3071. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3072. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3073. #endif /* HAVE_OCSP */
  3074. return EXPECT_RESULT();
  3075. }
  3076. static int test_wolfSSL_CheckOCSPResponse(void)
  3077. {
  3078. EXPECT_DECLS;
  3079. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  3080. const char* responseFile = "./certs/ocsp/test-response.der";
  3081. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3082. const char* responseNoInternFile =
  3083. "./certs/ocsp/test-response-nointern.der";
  3084. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3085. OcspResponse* res = NULL;
  3086. byte data[4096];
  3087. const unsigned char* pt;
  3088. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3089. * gcc sanitizer with --enable-heapmath.
  3090. */
  3091. XFILE f = XBADFILE;
  3092. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3093. WOLFSSL_X509_STORE* st = NULL;
  3094. WOLFSSL_X509* issuer = NULL;
  3095. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3096. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3097. if (f != XBADFILE) {
  3098. XFCLOSE(f);
  3099. f = XBADFILE;
  3100. }
  3101. pt = data;
  3102. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3103. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3104. SSL_FILETYPE_PEM));
  3105. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3106. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3107. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3108. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3109. wolfSSL_OCSP_BASICRESP_free(bs);
  3110. bs = NULL;
  3111. wolfSSL_OCSP_RESPONSE_free(res);
  3112. res = NULL;
  3113. wolfSSL_X509_STORE_free(st);
  3114. st = NULL;
  3115. wolfSSL_X509_free(issuer);
  3116. issuer = NULL;
  3117. /* check loading a response with optional certs */
  3118. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3119. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3120. if (f != XBADFILE)
  3121. XFCLOSE(f);
  3122. f = XBADFILE;
  3123. pt = data;
  3124. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3125. wolfSSL_OCSP_RESPONSE_free(res);
  3126. res = NULL;
  3127. /* check loading a response with multiple certs */
  3128. {
  3129. WOLFSSL_CERT_MANAGER* cm = NULL;
  3130. OcspEntry *entry = NULL;
  3131. CertStatus* status = NULL;
  3132. OcspRequest* request = NULL;
  3133. byte serial1[] = {0x01};
  3134. byte serial[] = {0x02};
  3135. byte issuerHash[] = {
  3136. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3137. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3138. 0x37, 0x15, 0x8A, 0x88
  3139. };
  3140. byte issuerKeyHash[] = {
  3141. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3142. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3143. 0x7E, 0x72, 0x15, 0x21
  3144. };
  3145. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3146. DYNAMIC_TYPE_OPENSSL));
  3147. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3148. DYNAMIC_TYPE_OPENSSL));
  3149. if (entry != NULL)
  3150. XMEMSET(entry, 0, sizeof(OcspEntry));
  3151. if (status != NULL)
  3152. XMEMSET(status, 0, sizeof(CertStatus));
  3153. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3154. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3155. DYNAMIC_TYPE_OCSP_REQUEST));
  3156. if (request != NULL && request->serial != NULL) {
  3157. request->serialSz = sizeof(serial);
  3158. XMEMCPY(request->serial, serial, sizeof(serial));
  3159. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3160. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3161. sizeof(issuerKeyHash));
  3162. }
  3163. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3164. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3165. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3166. WOLFSSL_SUCCESS);
  3167. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3168. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3169. if (f != XBADFILE)
  3170. XFCLOSE(f);
  3171. f = XBADFILE;
  3172. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3173. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3174. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3175. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3176. ExpectNotNull(entry->status);
  3177. if (request != NULL && request->serial != NULL)
  3178. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3179. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3180. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3181. /* store both status's in the entry to check that "next" is not
  3182. * overwritten */
  3183. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3184. status->next = entry->status;
  3185. entry->status = status;
  3186. }
  3187. if (request != NULL && request->serial != NULL)
  3188. XMEMCPY(request->serial, serial, sizeof(serial));
  3189. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3190. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3191. ExpectNotNull(entry->status->next);
  3192. /* compare the status found */
  3193. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3194. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3195. status->serialSz), 0);
  3196. if (status != NULL && entry != NULL && entry->status != status) {
  3197. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3198. }
  3199. wolfSSL_OCSP_CERTID_free(entry);
  3200. wolfSSL_OCSP_REQUEST_free(request);
  3201. wolfSSL_CertManagerFree(cm);
  3202. }
  3203. #if defined(WC_RSA_PSS)
  3204. {
  3205. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3206. /* check loading a response with RSA-PSS signature */
  3207. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3208. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3209. if (f != XBADFILE)
  3210. XFCLOSE(f);
  3211. pt = data;
  3212. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3213. /* try to verify the response */
  3214. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3215. SSL_FILETYPE_PEM));
  3216. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3217. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3218. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3219. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3220. WOLFSSL_SUCCESS);
  3221. wolfSSL_OCSP_BASICRESP_free(bs);
  3222. wolfSSL_OCSP_RESPONSE_free(res);
  3223. wolfSSL_X509_STORE_free(st);
  3224. wolfSSL_X509_free(issuer);
  3225. }
  3226. #endif
  3227. #endif /* HAVE_OCSP */
  3228. return EXPECT_RESULT();
  3229. }
  3230. static int test_wolfSSL_FPKI(void)
  3231. {
  3232. EXPECT_DECLS;
  3233. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3234. XFILE f = XBADFILE;
  3235. const char* fpkiCert = "./certs/fpki-cert.der";
  3236. DecodedCert cert;
  3237. byte buf[4096];
  3238. byte* uuid = NULL;
  3239. byte* fascn = NULL;
  3240. word32 fascnSz;
  3241. word32 uuidSz;
  3242. int bytes = 0;
  3243. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3244. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3245. if (f != XBADFILE)
  3246. XFCLOSE(f);
  3247. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3248. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3249. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3250. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3251. DYNAMIC_TYPE_TMP_BUFFER));
  3252. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3253. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3254. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3255. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3256. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3257. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3258. wc_FreeDecodedCert(&cert);
  3259. #endif
  3260. return EXPECT_RESULT();
  3261. }
  3262. /* use RID in confuncture with other names to test parsing of unknown other
  3263. * names */
  3264. static int test_wolfSSL_OtherName(void)
  3265. {
  3266. EXPECT_DECLS;
  3267. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3268. XFILE f = XBADFILE;
  3269. const char* ridCert = "./certs/rid-cert.der";
  3270. DecodedCert cert;
  3271. byte buf[4096];
  3272. int bytes = 0;
  3273. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3274. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3275. if (f != XBADFILE)
  3276. XFCLOSE(f);
  3277. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3278. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3279. wc_FreeDecodedCert(&cert);
  3280. #endif
  3281. return EXPECT_RESULT();
  3282. }
  3283. static int test_wolfSSL_CertRsaPss(void)
  3284. {
  3285. EXPECT_DECLS;
  3286. /* FIPS v2 and below don't support long salts. */
  3287. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3288. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3289. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3290. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3291. XFILE f = XBADFILE;
  3292. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3293. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3294. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3295. RSA_MAX_SIZE >= 3072
  3296. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3297. #endif
  3298. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3299. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3300. #endif
  3301. DecodedCert cert;
  3302. byte buf[4096];
  3303. int bytes = 0;
  3304. WOLFSSL_CERT_MANAGER* cm = NULL;
  3305. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3306. ExpectIntEQ(WOLFSSL_SUCCESS,
  3307. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3308. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3309. ExpectIntEQ(WOLFSSL_SUCCESS,
  3310. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3311. #endif
  3312. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3313. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3314. if (f != XBADFILE) {
  3315. XFCLOSE(f);
  3316. f = XBADFILE;
  3317. }
  3318. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3319. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3320. wc_FreeDecodedCert(&cert);
  3321. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3322. RSA_MAX_SIZE >= 3072
  3323. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3324. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3325. if (f != XBADFILE)
  3326. XFCLOSE(f);
  3327. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3328. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3329. wc_FreeDecodedCert(&cert);
  3330. #endif
  3331. wolfSSL_CertManagerFree(cm);
  3332. #endif
  3333. return EXPECT_RESULT();
  3334. }
  3335. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3336. {
  3337. EXPECT_DECLS;
  3338. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3339. !defined(NO_WOLFSSL_CLIENT)
  3340. WOLFSSL_CTX* ctx = NULL;
  3341. const char* ca_cert = "./certs/ca-cert.pem";
  3342. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3343. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3344. /* test good CA */
  3345. ExpectTrue(WOLFSSL_SUCCESS ==
  3346. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3347. WOLFSSL_LOAD_FLAG_NONE));
  3348. /* test expired CA */
  3349. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3350. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3351. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3352. #else
  3353. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3354. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3355. #endif
  3356. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3357. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3358. wolfSSL_CTX_free(ctx);
  3359. #endif
  3360. return EXPECT_RESULT();
  3361. }
  3362. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3363. {
  3364. EXPECT_DECLS;
  3365. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3366. defined(USE_CERT_BUFFERS_2048)
  3367. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3368. WOLFSSL_CTX* ctx;
  3369. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3370. byte ca_expired_cert[TWOK_BUF];
  3371. word32 sizeof_ca_expired_cert = 0;
  3372. XFILE fp = XBADFILE;
  3373. #ifndef NO_WOLFSSL_CLIENT
  3374. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3375. #else
  3376. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3377. #endif
  3378. ExpectNotNull(ctx);
  3379. /* test good CA */
  3380. ExpectTrue(WOLFSSL_SUCCESS ==
  3381. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3382. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3383. WOLFSSL_LOAD_FLAG_NONE));
  3384. /* load expired CA */
  3385. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3386. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3387. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3388. sizeof(ca_expired_cert), fp), 0);
  3389. if (fp != XBADFILE)
  3390. XFCLOSE(fp);
  3391. /* test expired CA failure */
  3392. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3393. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3394. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3395. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3396. #else
  3397. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3398. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3399. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3400. #endif
  3401. /* test expired CA success */
  3402. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3403. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3404. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3405. wolfSSL_CTX_free(ctx);
  3406. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3407. #endif
  3408. return EXPECT_RESULT();
  3409. }
  3410. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3411. {
  3412. EXPECT_DECLS;
  3413. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3414. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3415. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3416. WOLFSSL_CTX* ctx = NULL;
  3417. #ifndef NO_WOLFSSL_CLIENT
  3418. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3419. #else
  3420. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3421. #endif
  3422. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3423. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3424. WOLFSSL_FILETYPE_ASN1));
  3425. wolfSSL_CTX_free(ctx);
  3426. #endif
  3427. return EXPECT_RESULT();
  3428. }
  3429. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3430. {
  3431. EXPECT_DECLS;
  3432. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3433. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3434. WOLFSSL_CTX* ctx;
  3435. WOLFSSL* ssl = NULL;
  3436. const char *certChain[] = {
  3437. "./certs/intermediate/client-int-cert.pem",
  3438. "./certs/intermediate/ca-int2-cert.pem",
  3439. "./certs/intermediate/ca-int-cert.pem",
  3440. "./certs/ca-cert.pem",
  3441. NULL
  3442. };
  3443. const char** cert;
  3444. WOLFSSL_X509* x509 = NULL;
  3445. WOLF_STACK_OF(X509)* chain = NULL;
  3446. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3447. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3448. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3449. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3450. WOLFSSL_FILETYPE_PEM));
  3451. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3452. X509_free(x509);
  3453. x509 = NULL;
  3454. }
  3455. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3456. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3457. WOLFSSL_FILETYPE_PEM));
  3458. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3459. X509_free(x509);
  3460. x509 = NULL;
  3461. }
  3462. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3463. ExpectIntEQ(sk_X509_num(chain), 3);
  3464. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3465. ExpectIntEQ(sk_X509_num(chain), 3);
  3466. SSL_free(ssl);
  3467. SSL_CTX_free(ctx);
  3468. #endif
  3469. return EXPECT_RESULT();
  3470. }
  3471. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3472. {
  3473. EXPECT_DECLS;
  3474. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3475. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3476. const char* server_chain_der = "./certs/server-cert-chain.der";
  3477. const char* client_single_pem = "./certs/client-cert.pem";
  3478. WOLFSSL_CTX* ctx;
  3479. (void)server_chain_der;
  3480. (void)client_single_pem;
  3481. (void)ctx;
  3482. #ifndef NO_WOLFSSL_CLIENT
  3483. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3484. #else
  3485. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3486. #endif
  3487. ExpectNotNull(ctx);
  3488. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3489. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3490. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3491. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3492. wolfSSL_CTX_free(ctx);
  3493. #endif
  3494. return EXPECT_RESULT();
  3495. }
  3496. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3497. {
  3498. EXPECT_DECLS;
  3499. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3500. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3501. WOLFSSL_CTX *ctx = NULL;
  3502. (void)ctx;
  3503. #ifndef NO_WOLFSSL_CLIENT
  3504. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3505. #else
  3506. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3507. #endif
  3508. /* invalid context */
  3509. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3510. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3511. /* invalid dhParamFile file */
  3512. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3513. NULL, WOLFSSL_FILETYPE_PEM));
  3514. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3515. bogusFile, WOLFSSL_FILETYPE_PEM));
  3516. /* success */
  3517. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3518. WOLFSSL_FILETYPE_PEM));
  3519. wolfSSL_CTX_free(ctx);
  3520. #endif
  3521. return EXPECT_RESULT();
  3522. }
  3523. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3524. {
  3525. EXPECT_DECLS;
  3526. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3527. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3528. WOLFSSL_CTX *ctx = NULL;
  3529. #ifndef NO_WOLFSSL_CLIENT
  3530. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3531. #else
  3532. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3533. #endif
  3534. /* invalid context */
  3535. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3536. dh_key_der_2048, sizeof_dh_key_der_2048,
  3537. WOLFSSL_FILETYPE_ASN1));
  3538. /* invalid dhParamFile file */
  3539. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3540. 0, WOLFSSL_FILETYPE_ASN1));
  3541. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3542. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3543. WOLFSSL_FILETYPE_ASN1));
  3544. /* success */
  3545. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3546. dh_key_der_2048, sizeof_dh_key_der_2048,
  3547. WOLFSSL_FILETYPE_ASN1));
  3548. wolfSSL_CTX_free(ctx);
  3549. #endif
  3550. return EXPECT_RESULT();
  3551. }
  3552. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3553. {
  3554. EXPECT_DECLS;
  3555. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3556. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3557. WOLFSSL_CTX *ctx;
  3558. (void)ctx;
  3559. #ifndef NO_WOLFSSL_CLIENT
  3560. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3561. #else
  3562. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3563. #endif
  3564. ExpectNotNull(ctx);
  3565. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3566. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3567. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3568. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3569. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3570. dh_key_der_2048, sizeof_dh_key_der_2048,
  3571. WOLFSSL_FILETYPE_ASN1));
  3572. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3573. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3574. dh_key_der_2048, sizeof_dh_key_der_2048,
  3575. WOLFSSL_FILETYPE_ASN1));
  3576. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3577. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3578. dh_key_der_2048, sizeof_dh_key_der_2048,
  3579. WOLFSSL_FILETYPE_ASN1));
  3580. wolfSSL_CTX_free(ctx);
  3581. #endif
  3582. return EXPECT_RESULT();
  3583. }
  3584. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3585. {
  3586. EXPECT_DECLS;
  3587. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3588. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3589. WOLFSSL_CTX* ctx = NULL;
  3590. const char* derCert = "./certs/server-cert.der";
  3591. const char* nullPath = NULL;
  3592. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3593. const char* emptyPath = "";
  3594. /* der load Case 1 ctx NULL */
  3595. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3596. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3597. #ifndef NO_WOLFSSL_CLIENT
  3598. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3599. #else
  3600. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3601. #endif
  3602. /* Case 2 filePath NULL */
  3603. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3604. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3605. /* Case 3 invalid format */
  3606. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3607. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3608. /* Case 4 filePath not valid */
  3609. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3610. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3611. /* Case 5 filePath empty */
  3612. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3613. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3614. #ifndef NO_RSA
  3615. /* Case 6 success case */
  3616. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3617. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3618. #endif
  3619. wolfSSL_CTX_free(ctx);
  3620. #endif
  3621. return EXPECT_RESULT();
  3622. }
  3623. static int test_wolfSSL_CTX_enable_disable(void)
  3624. {
  3625. EXPECT_DECLS;
  3626. #ifndef NO_CERTS
  3627. WOLFSSL_CTX* ctx = NULL;
  3628. #ifdef HAVE_CRL
  3629. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3630. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3631. #endif
  3632. #ifdef HAVE_OCSP
  3633. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3634. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3635. #endif
  3636. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3637. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3638. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3639. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3640. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3641. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3642. #endif
  3643. #ifndef NO_WOLFSSL_CLIENT
  3644. #ifdef HAVE_EXTENDED_MASTER
  3645. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3646. #endif
  3647. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3648. #ifdef HAVE_EXTENDED_MASTER
  3649. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3650. #endif
  3651. #elif !defined(NO_WOLFSSL_SERVER)
  3652. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3653. #endif
  3654. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3655. #ifdef HAVE_CRL
  3656. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3657. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3658. #endif
  3659. #ifdef HAVE_OCSP
  3660. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3661. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3662. WOLFSSL_SUCCESS);
  3663. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3664. WOLFSSL_SUCCESS);
  3665. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3666. WOLFSSL_SUCCESS);
  3667. #endif
  3668. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3669. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3670. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3671. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3672. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3673. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3674. #endif
  3675. wolfSSL_CTX_free(ctx);
  3676. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3677. #endif /* NO_CERTS */
  3678. return EXPECT_RESULT();
  3679. }
  3680. static int test_wolfSSL_CTX_ticket_API(void)
  3681. {
  3682. EXPECT_DECLS;
  3683. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3684. WOLFSSL_CTX* ctx = NULL;
  3685. void *userCtx = (void*)"this is my ctx";
  3686. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3687. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3688. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3689. wolfSSL_CTX_free(ctx);
  3690. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3691. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3692. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3693. return EXPECT_RESULT();
  3694. }
  3695. static int test_wolfSSL_set_minmax_proto_version(void)
  3696. {
  3697. EXPECT_DECLS;
  3698. #ifdef OPENSSL_EXTRA
  3699. WOLFSSL_CTX *ctx = NULL;
  3700. WOLFSSL *ssl = NULL;
  3701. (void)ssl;
  3702. #ifndef NO_WOLFSSL_CLIENT
  3703. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3704. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3705. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3706. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3707. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3708. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3709. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3710. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3711. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3712. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3713. wolfSSL_free(ssl);
  3714. wolfSSL_CTX_free(ctx);
  3715. ctx = NULL;
  3716. #endif
  3717. #ifndef NO_WOLFSSL_SERVER
  3718. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3719. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3720. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3721. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3722. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3723. wolfSSL_CTX_free(ctx);
  3724. #endif
  3725. #endif
  3726. return EXPECT_RESULT();
  3727. }
  3728. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3729. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  3730. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3731. {
  3732. EXPECT_DECLS;
  3733. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3734. return EXPECT_RESULT();
  3735. }
  3736. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3737. {
  3738. EXPECT_DECLS;
  3739. /* Set TLS 1.2 */
  3740. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3741. WOLFSSL_SUCCESS);
  3742. return EXPECT_RESULT();
  3743. }
  3744. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3745. * what was set at ctx creation. */
  3746. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3747. {
  3748. EXPECT_DECLS;
  3749. test_ssl_cbf client_cbs;
  3750. test_ssl_cbf server_cbs;
  3751. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3752. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3753. client_cbs.method = wolfTLS_client_method;
  3754. server_cbs.method = wolfTLS_server_method;
  3755. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3756. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3757. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3758. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  3759. &server_cbs, NULL), TEST_SUCCESS);
  3760. return EXPECT_RESULT();
  3761. }
  3762. #else
  3763. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3764. {
  3765. return TEST_SKIPPED;
  3766. }
  3767. #endif
  3768. /*----------------------------------------------------------------------------*
  3769. | SSL
  3770. *----------------------------------------------------------------------------*/
  3771. static int test_server_wolfSSL_new(void)
  3772. {
  3773. EXPECT_DECLS;
  3774. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3775. !defined(NO_WOLFSSL_SERVER)
  3776. WOLFSSL_CTX *ctx = NULL;
  3777. WOLFSSL_CTX *ctx_nocert = NULL;
  3778. WOLFSSL *ssl = NULL;
  3779. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3780. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3781. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3782. WOLFSSL_FILETYPE_PEM));
  3783. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3784. WOLFSSL_FILETYPE_PEM));
  3785. /* invalid context */
  3786. ExpectNull(ssl = wolfSSL_new(NULL));
  3787. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  3788. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  3789. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  3790. #endif
  3791. /* success */
  3792. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3793. wolfSSL_free(ssl);
  3794. wolfSSL_CTX_free(ctx);
  3795. wolfSSL_CTX_free(ctx_nocert);
  3796. #endif
  3797. return EXPECT_RESULT();
  3798. }
  3799. static int test_client_wolfSSL_new(void)
  3800. {
  3801. EXPECT_DECLS;
  3802. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3803. !defined(NO_WOLFSSL_CLIENT)
  3804. WOLFSSL_CTX *ctx = NULL;
  3805. WOLFSSL_CTX *ctx_nocert = NULL;
  3806. WOLFSSL *ssl = NULL;
  3807. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3808. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3809. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3810. /* invalid context */
  3811. ExpectNull(ssl = wolfSSL_new(NULL));
  3812. /* success */
  3813. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  3814. wolfSSL_free(ssl);
  3815. ssl = NULL;
  3816. /* success */
  3817. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3818. wolfSSL_free(ssl);
  3819. wolfSSL_CTX_free(ctx);
  3820. wolfSSL_CTX_free(ctx_nocert);
  3821. #endif
  3822. return EXPECT_RESULT();
  3823. }
  3824. static int test_wolfSSL_SetTmpDH_file(void)
  3825. {
  3826. EXPECT_DECLS;
  3827. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3828. !defined(NO_WOLFSSL_SERVER)
  3829. WOLFSSL_CTX *ctx = NULL;
  3830. WOLFSSL *ssl = NULL;
  3831. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3832. #ifndef NO_RSA
  3833. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3834. WOLFSSL_FILETYPE_PEM));
  3835. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3836. WOLFSSL_FILETYPE_PEM));
  3837. #elif defined(HAVE_ECC)
  3838. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3839. WOLFSSL_FILETYPE_PEM));
  3840. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3841. WOLFSSL_FILETYPE_PEM));
  3842. #elif defined(HAVE_ED25519)
  3843. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3844. WOLFSSL_FILETYPE_PEM));
  3845. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3846. WOLFSSL_FILETYPE_PEM));
  3847. #elif defined(HAVE_ED448)
  3848. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3849. WOLFSSL_FILETYPE_PEM));
  3850. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3851. WOLFSSL_FILETYPE_PEM));
  3852. #endif
  3853. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3854. /* invalid ssl */
  3855. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3856. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3857. /* invalid dhParamFile file */
  3858. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3859. NULL, WOLFSSL_FILETYPE_PEM));
  3860. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3861. bogusFile, WOLFSSL_FILETYPE_PEM));
  3862. /* success */
  3863. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3864. WOLFSSL_FILETYPE_PEM));
  3865. wolfSSL_free(ssl);
  3866. wolfSSL_CTX_free(ctx);
  3867. #endif
  3868. return EXPECT_RESULT();
  3869. }
  3870. static int test_wolfSSL_SetTmpDH_buffer(void)
  3871. {
  3872. EXPECT_DECLS;
  3873. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3874. WOLFSSL_CTX *ctx = NULL;
  3875. WOLFSSL *ssl = NULL;
  3876. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3877. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3878. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3879. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3880. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3881. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3882. /* invalid ssl */
  3883. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3884. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3885. /* invalid dhParamFile file */
  3886. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3887. 0, WOLFSSL_FILETYPE_ASN1));
  3888. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3889. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3890. /* success */
  3891. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3892. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3893. wolfSSL_free(ssl);
  3894. wolfSSL_CTX_free(ctx);
  3895. #endif
  3896. return EXPECT_RESULT();
  3897. }
  3898. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3899. {
  3900. EXPECT_DECLS;
  3901. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3902. WOLFSSL_CTX *ctx = NULL;
  3903. WOLFSSL_CTX *ctx2 = NULL;
  3904. WOLFSSL *ssl = NULL;
  3905. WOLFSSL *ssl2 = NULL;
  3906. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3907. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3908. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3909. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3910. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3911. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3912. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3913. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3914. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3915. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3916. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3917. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3918. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3919. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  3920. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3921. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3922. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3923. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3924. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3925. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3926. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3927. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3928. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3929. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3930. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3931. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3932. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3933. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3934. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3935. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3936. wolfSSL_free(ssl2);
  3937. wolfSSL_CTX_free(ctx2);
  3938. wolfSSL_free(ssl);
  3939. wolfSSL_CTX_free(ctx);
  3940. #endif
  3941. return EXPECT_RESULT();
  3942. }
  3943. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3944. * allowed.
  3945. * POST: return 1 on success.
  3946. */
  3947. static int test_wolfSSL_SetMinVersion(void)
  3948. {
  3949. int res = TEST_SKIPPED;
  3950. #ifndef NO_WOLFSSL_CLIENT
  3951. int failFlag = WOLFSSL_SUCCESS;
  3952. WOLFSSL_CTX* ctx = NULL;
  3953. WOLFSSL* ssl = NULL;
  3954. int itr;
  3955. #ifndef NO_OLD_TLS
  3956. const int versions[] = {
  3957. #ifdef WOLFSSL_ALLOW_TLSV10
  3958. WOLFSSL_TLSV1,
  3959. #endif
  3960. WOLFSSL_TLSV1_1,
  3961. WOLFSSL_TLSV1_2};
  3962. #elif !defined(WOLFSSL_NO_TLS12)
  3963. const int versions[] = { WOLFSSL_TLSV1_2 };
  3964. #else
  3965. const int versions[] = { WOLFSSL_TLSV1_3 };
  3966. #endif
  3967. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3968. ssl = wolfSSL_new(ctx);
  3969. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3970. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3971. failFlag = WOLFSSL_FAILURE;
  3972. }
  3973. }
  3974. wolfSSL_free(ssl);
  3975. wolfSSL_CTX_free(ctx);
  3976. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3977. #endif
  3978. return res;
  3979. } /* END test_wolfSSL_SetMinVersion */
  3980. #ifdef OPENSSL_EXTRA
  3981. static int test_ED25519(void)
  3982. {
  3983. EXPECT_DECLS;
  3984. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3985. defined(WOLFSSL_KEY_GEN)
  3986. byte priv[ED25519_PRV_KEY_SIZE];
  3987. unsigned int privSz = (unsigned int)sizeof(priv);
  3988. byte pub[ED25519_PUB_KEY_SIZE];
  3989. unsigned int pubSz = (unsigned int)sizeof(pub);
  3990. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3991. const char* msg = TEST_STRING;
  3992. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3993. byte sig[ED25519_SIG_SIZE];
  3994. unsigned int sigSz = (unsigned int)sizeof(sig);
  3995. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3996. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3997. WOLFSSL_SUCCESS);
  3998. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3999. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4000. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4001. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4002. &sigSz), WOLFSSL_SUCCESS);
  4003. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4004. #ifdef HAVE_ED25519_VERIFY
  4005. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4006. sigSz), WOLFSSL_SUCCESS);
  4007. #endif /* HAVE_ED25519_VERIFY */
  4008. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4009. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4010. return EXPECT_RESULT();
  4011. }
  4012. static int test_ED448(void)
  4013. {
  4014. EXPECT_DECLS;
  4015. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4016. defined(WOLFSSL_KEY_GEN)
  4017. byte priv[ED448_PRV_KEY_SIZE];
  4018. unsigned int privSz = (unsigned int)sizeof(priv);
  4019. byte pub[ED448_PUB_KEY_SIZE];
  4020. unsigned int pubSz = (unsigned int)sizeof(pub);
  4021. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4022. const char* msg = TEST_STRING;
  4023. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4024. byte sig[ED448_SIG_SIZE];
  4025. unsigned int sigSz = (unsigned int)sizeof(sig);
  4026. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4027. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  4028. WOLFSSL_SUCCESS);
  4029. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4030. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4031. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4032. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4033. &sigSz), WOLFSSL_SUCCESS);
  4034. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4035. #ifdef HAVE_ED448_VERIFY
  4036. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4037. sigSz), WOLFSSL_SUCCESS);
  4038. #endif /* HAVE_ED448_VERIFY */
  4039. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4040. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4041. return EXPECT_RESULT();
  4042. }
  4043. #endif /* OPENSSL_EXTRA */
  4044. #include <wolfssl/openssl/pem.h>
  4045. /*----------------------------------------------------------------------------*
  4046. | EVP
  4047. *----------------------------------------------------------------------------*/
  4048. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4049. {
  4050. EXPECT_DECLS;
  4051. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4052. WOLFSSL_BIO* rbio = NULL;
  4053. WOLFSSL_BIO* wbio = NULL;
  4054. WOLFSSL_EVP_PKEY* pkey = NULL;
  4055. char line[256] = { 0 };
  4056. char line1[256] = { 0 };
  4057. int i;
  4058. /* test error cases */
  4059. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4060. /*
  4061. * test RSA public key print
  4062. * in this test, pass '3' for indent
  4063. */
  4064. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4065. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4066. sizeof_client_keypub_der_1024));
  4067. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4068. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4069. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4070. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4071. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4072. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4073. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4074. strcpy(line1, " Modulus:\n");
  4075. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4076. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4077. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4078. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4079. /* skip to the end of modulus element*/
  4080. for (i = 0; i < 8 ;i++) {
  4081. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4082. }
  4083. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4084. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4085. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4086. /* should reach EOF */
  4087. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4088. EVP_PKEY_free(pkey);
  4089. pkey = NULL;
  4090. BIO_free(rbio);
  4091. BIO_free(wbio);
  4092. rbio = NULL;
  4093. wbio = NULL;
  4094. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4095. /*
  4096. * test DSA public key print
  4097. */
  4098. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4099. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4100. sizeof_dsa_pub_key_der_2048));
  4101. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4102. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4103. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4104. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4105. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4106. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4107. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4108. strcpy(line1, "pub:\n");
  4109. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4110. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4111. strcpy(line1,
  4112. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4113. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4114. /* skip to the end of pub element*/
  4115. for (i = 0; i < 17 ;i++) {
  4116. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4117. }
  4118. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4119. strcpy(line1, "P:\n");
  4120. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4121. /* skip to the end of P element*/
  4122. for (i = 0; i < 18 ;i++) {
  4123. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4124. }
  4125. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4126. strcpy(line1, "Q:\n");
  4127. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4128. /* skip to the end of Q element*/
  4129. for (i = 0; i < 3 ;i++) {
  4130. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4131. }
  4132. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4133. strcpy(line1, "G:\n");
  4134. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4135. /* skip to the end of G element*/
  4136. for (i = 0; i < 18 ;i++) {
  4137. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4138. }
  4139. /* should reach EOF */
  4140. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4141. EVP_PKEY_free(pkey);
  4142. pkey = NULL;
  4143. BIO_free(rbio);
  4144. BIO_free(wbio);
  4145. rbio = NULL;
  4146. wbio = NULL;
  4147. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4148. /*
  4149. * test ECC public key print
  4150. */
  4151. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4152. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4153. sizeof_ecc_clikeypub_der_256));
  4154. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4155. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4156. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4157. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4158. strcpy(line1, "Public-Key: (256 bit)\n");
  4159. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4160. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4161. strcpy(line1, "pub:\n");
  4162. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4163. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4164. strcpy(line1,
  4165. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4166. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4167. /* skip to the end of pub element*/
  4168. for (i = 0; i < 4 ;i++) {
  4169. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4170. }
  4171. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4172. strcpy(line1, "ASN1 OID: prime256v1\n");
  4173. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4174. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4175. strcpy(line1, "NIST CURVE: P-256\n");
  4176. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4177. /* should reach EOF */
  4178. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4179. EVP_PKEY_free(pkey);
  4180. pkey = NULL;
  4181. BIO_free(rbio);
  4182. BIO_free(wbio);
  4183. rbio = NULL;
  4184. wbio = NULL;
  4185. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4186. /*
  4187. * test DH public key print
  4188. */
  4189. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4190. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4191. sizeof_dh_pub_key_der_2048));
  4192. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4193. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4194. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4195. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4196. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4197. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4198. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4199. strcpy(line1, "public-key:\n");
  4200. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4201. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4202. strcpy(line1,
  4203. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4204. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4205. /* skip to the end of public-key element*/
  4206. for (i = 0; i < 17 ;i++) {
  4207. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4208. }
  4209. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4210. strcpy(line1, "prime:\n");
  4211. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4212. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4213. strcpy(line1,
  4214. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4215. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4216. /* skip to the end of prime element*/
  4217. for (i = 0; i < 17 ;i++) {
  4218. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4219. }
  4220. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4221. strcpy(line1, "generator: 2 (0x02)\n");
  4222. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4223. /* should reach EOF */
  4224. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4225. EVP_PKEY_free(pkey);
  4226. pkey = NULL;
  4227. BIO_free(rbio);
  4228. BIO_free(wbio);
  4229. rbio = NULL;
  4230. wbio = NULL;
  4231. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4232. /* to prevent "unused variable" warning */
  4233. (void)pkey;
  4234. (void)wbio;
  4235. (void)rbio;
  4236. (void)line;
  4237. (void)line1;
  4238. (void)i;
  4239. #endif /* OPENSSL_EXTRA */
  4240. return EXPECT_RESULT();
  4241. }
  4242. /* Test functions for base64 encode/decode */
  4243. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4244. {
  4245. EXPECT_DECLS;
  4246. #if defined(OPENSSL_EXTRA) && \
  4247. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4248. EVP_ENCODE_CTX* ctx = NULL;
  4249. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4250. ExpectIntEQ(ctx->remaining,0);
  4251. ExpectIntEQ(ctx->data[0],0);
  4252. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4253. EVP_ENCODE_CTX_free(ctx);
  4254. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4255. return EXPECT_RESULT();
  4256. }
  4257. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4258. {
  4259. EXPECT_DECLS;
  4260. #if defined(OPENSSL_EXTRA) && \
  4261. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4262. EVP_ENCODE_CTX* ctx = NULL;
  4263. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4264. EVP_ENCODE_CTX_free(ctx);
  4265. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4266. return EXPECT_RESULT();
  4267. }
  4268. static int test_wolfSSL_EVP_EncodeInit(void)
  4269. {
  4270. EXPECT_DECLS;
  4271. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4272. EVP_ENCODE_CTX* ctx = NULL;
  4273. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4274. ExpectIntEQ(ctx->remaining, 0);
  4275. ExpectIntEQ(ctx->data[0], 0);
  4276. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4277. if (ctx != NULL) {
  4278. /* make ctx dirty */
  4279. ctx->remaining = 10;
  4280. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4281. }
  4282. EVP_EncodeInit(ctx);
  4283. ExpectIntEQ(ctx->remaining, 0);
  4284. ExpectIntEQ(ctx->data[0], 0);
  4285. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4286. EVP_ENCODE_CTX_free(ctx);
  4287. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4288. return EXPECT_RESULT();
  4289. }
  4290. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4291. {
  4292. EXPECT_DECLS;
  4293. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4294. int outl;
  4295. int total;
  4296. const unsigned char plain0[] = {"Th"};
  4297. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4298. const unsigned char plain2[] = {"This is additional data."};
  4299. const unsigned char encBlock0[] = {"VGg="};
  4300. const unsigned char enc0[] = {"VGg=\n"};
  4301. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4302. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4303. const unsigned char enc2[] =
  4304. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4305. unsigned char encOutBuff[300];
  4306. EVP_ENCODE_CTX* ctx = NULL;
  4307. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4308. EVP_EncodeInit(ctx);
  4309. /* illegal parameter test */
  4310. ExpectIntEQ(
  4311. EVP_EncodeUpdate(
  4312. NULL, /* pass NULL as ctx */
  4313. encOutBuff,
  4314. &outl,
  4315. plain1,
  4316. sizeof(plain1)-1),
  4317. 0 /* expected result code 0: fail */
  4318. );
  4319. ExpectIntEQ(
  4320. EVP_EncodeUpdate(
  4321. ctx,
  4322. NULL, /* pass NULL as out buff */
  4323. &outl,
  4324. plain1,
  4325. sizeof(plain1)-1),
  4326. 0 /* expected result code 0: fail */
  4327. );
  4328. ExpectIntEQ(
  4329. EVP_EncodeUpdate(
  4330. ctx,
  4331. encOutBuff,
  4332. NULL, /* pass NULL as outl */
  4333. plain1,
  4334. sizeof(plain1)-1),
  4335. 0 /* expected result code 0: fail */
  4336. );
  4337. ExpectIntEQ(
  4338. EVP_EncodeUpdate(
  4339. ctx,
  4340. encOutBuff,
  4341. &outl,
  4342. NULL, /* pass NULL as in */
  4343. sizeof(plain1)-1),
  4344. 0 /* expected result code 0: fail */
  4345. );
  4346. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4347. /* meaningless parameter test */
  4348. ExpectIntEQ(
  4349. EVP_EncodeUpdate(
  4350. ctx,
  4351. encOutBuff,
  4352. &outl,
  4353. plain1,
  4354. 0), /* pass zero input */
  4355. 1 /* expected result code 1: success */
  4356. );
  4357. /* very small data encoding test */
  4358. EVP_EncodeInit(ctx);
  4359. ExpectIntEQ(
  4360. EVP_EncodeUpdate(
  4361. ctx,
  4362. encOutBuff,
  4363. &outl,
  4364. plain0,
  4365. sizeof(plain0)-1),
  4366. 1 /* expected result code 1: success */
  4367. );
  4368. ExpectIntEQ(outl,0);
  4369. if (EXPECT_SUCCESS()) {
  4370. EVP_EncodeFinal(
  4371. ctx,
  4372. encOutBuff + outl,
  4373. &outl);
  4374. }
  4375. ExpectIntEQ( outl, sizeof(enc0)-1);
  4376. ExpectIntEQ(
  4377. XSTRNCMP(
  4378. (const char*)encOutBuff,
  4379. (const char*)enc0,sizeof(enc0) ),
  4380. 0);
  4381. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4382. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4383. sizeof(encBlock0)-1);
  4384. ExpectStrEQ(encOutBuff, encBlock0);
  4385. /* pass small size( < 48bytes ) input, then make sure they are not
  4386. * encoded and just stored in ctx
  4387. */
  4388. EVP_EncodeInit(ctx);
  4389. total = 0;
  4390. outl = 0;
  4391. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4392. ExpectIntEQ(
  4393. EVP_EncodeUpdate(
  4394. ctx,
  4395. encOutBuff, /* buffer for output */
  4396. &outl, /* size of output */
  4397. plain1, /* input */
  4398. sizeof(plain1)-1), /* size of input */
  4399. 1); /* expected result code 1:success */
  4400. total += outl;
  4401. ExpectIntEQ(outl, 0); /* no output expected */
  4402. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4403. ExpectTrue(
  4404. XSTRNCMP((const char*)(ctx->data),
  4405. (const char*)plain1,
  4406. ctx->remaining) ==0 );
  4407. ExpectTrue(encOutBuff[0] == 0);
  4408. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4409. * the stored data and the new input together
  4410. */
  4411. ExpectIntEQ(
  4412. EVP_EncodeUpdate(
  4413. ctx,
  4414. encOutBuff + outl, /* buffer for output */
  4415. &outl, /* size of output */
  4416. plain2, /* additional input */
  4417. sizeof(plain2) -1), /* size of additional input */
  4418. 1); /* expected result code 1:success */
  4419. total += outl;
  4420. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4421. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4422. ExpectIntEQ(
  4423. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4424. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4425. EVP_EncodeFinal(
  4426. ctx,
  4427. encOutBuff + outl,
  4428. &outl);
  4429. total += outl;
  4430. ExpectIntNE(total,0);
  4431. ExpectIntNE(outl,0);
  4432. ExpectIntEQ(XSTRNCMP(
  4433. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4434. /* test with illeagal parameters */
  4435. outl = 1;
  4436. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4437. ExpectIntEQ(outl, 0);
  4438. outl = 1;
  4439. EVP_EncodeFinal(ctx, NULL, &outl);
  4440. ExpectIntEQ(outl, 0);
  4441. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4442. EVP_EncodeFinal(NULL, NULL, NULL);
  4443. EVP_ENCODE_CTX_free(ctx);
  4444. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4445. return EXPECT_RESULT();
  4446. }
  4447. static int test_wolfSSL_EVP_EncodeFinal(void)
  4448. {
  4449. int res = TEST_SKIPPED;
  4450. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4451. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4452. * test_wolfSSL_EVP_EncodeUpdate
  4453. */
  4454. res = TEST_SUCCESS;
  4455. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4456. return res;
  4457. }
  4458. static int test_wolfSSL_EVP_DecodeInit(void)
  4459. {
  4460. EXPECT_DECLS;
  4461. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4462. EVP_ENCODE_CTX* ctx = NULL;
  4463. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4464. ExpectIntEQ( ctx->remaining,0);
  4465. ExpectIntEQ( ctx->data[0],0);
  4466. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4467. if (ctx != NULL) {
  4468. /* make ctx dirty */
  4469. ctx->remaining = 10;
  4470. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4471. }
  4472. EVP_DecodeInit(ctx);
  4473. ExpectIntEQ( ctx->remaining,0);
  4474. ExpectIntEQ( ctx->data[0],0);
  4475. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4476. EVP_ENCODE_CTX_free(ctx);
  4477. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4478. return EXPECT_RESULT();
  4479. }
  4480. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4481. {
  4482. EXPECT_DECLS;
  4483. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4484. int outl;
  4485. unsigned char decOutBuff[300];
  4486. EVP_ENCODE_CTX* ctx = NULL;
  4487. static const unsigned char enc1[] =
  4488. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4489. /* const unsigned char plain1[] =
  4490. {"This is a base64 decoding test."} */
  4491. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4492. EVP_DecodeInit(ctx);
  4493. /* illegal parameter tests */
  4494. /* pass NULL as ctx */
  4495. ExpectIntEQ(
  4496. EVP_DecodeUpdate(
  4497. NULL, /* pass NULL as ctx */
  4498. decOutBuff,
  4499. &outl,
  4500. enc1,
  4501. sizeof(enc1)-1),
  4502. -1 /* expected result code -1: fail */
  4503. );
  4504. ExpectIntEQ( outl, 0);
  4505. /* pass NULL as output */
  4506. ExpectIntEQ(
  4507. EVP_DecodeUpdate(
  4508. ctx,
  4509. NULL, /* pass NULL as out buff */
  4510. &outl,
  4511. enc1,
  4512. sizeof(enc1)-1),
  4513. -1 /* expected result code -1: fail */
  4514. );
  4515. ExpectIntEQ( outl, 0);
  4516. /* pass NULL as outl */
  4517. ExpectIntEQ(
  4518. EVP_DecodeUpdate(
  4519. ctx,
  4520. decOutBuff,
  4521. NULL, /* pass NULL as outl */
  4522. enc1,
  4523. sizeof(enc1)-1),
  4524. -1 /* expected result code -1: fail */
  4525. );
  4526. /* pass NULL as input */
  4527. ExpectIntEQ(
  4528. EVP_DecodeUpdate(
  4529. ctx,
  4530. decOutBuff,
  4531. &outl,
  4532. NULL, /* pass NULL as in */
  4533. sizeof(enc1)-1),
  4534. -1 /* expected result code -1: fail */
  4535. );
  4536. ExpectIntEQ( outl, 0);
  4537. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4538. /* pass zero length input */
  4539. ExpectIntEQ(
  4540. EVP_DecodeUpdate(
  4541. ctx,
  4542. decOutBuff,
  4543. &outl,
  4544. enc1,
  4545. 0), /* pass zero as input len */
  4546. 1 /* expected result code 1: success */
  4547. );
  4548. /* decode correct base64 string */
  4549. {
  4550. static const unsigned char enc2[] =
  4551. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4552. static const unsigned char plain2[] =
  4553. {"This is a base64 decoding test."};
  4554. EVP_EncodeInit(ctx);
  4555. ExpectIntEQ(
  4556. EVP_DecodeUpdate(
  4557. ctx,
  4558. decOutBuff,
  4559. &outl,
  4560. enc2,
  4561. sizeof(enc2)-1),
  4562. 0 /* expected result code 0: success */
  4563. );
  4564. ExpectIntEQ(outl,sizeof(plain2) -1);
  4565. ExpectIntEQ(
  4566. EVP_DecodeFinal(
  4567. ctx,
  4568. decOutBuff + outl,
  4569. &outl),
  4570. 1 /* expected result code 1: success */
  4571. );
  4572. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4573. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4574. sizeof(plain2) -1 ),0);
  4575. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4576. sizeof(plain2)-1);
  4577. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4578. sizeof(plain2) -1 ),0);
  4579. }
  4580. /* decode correct base64 string which does not have '\n' in its last*/
  4581. {
  4582. static const unsigned char enc3[] =
  4583. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4584. static const unsigned char plain3[] =
  4585. {"This is a base64 decoding test."}; /* 31 chars */
  4586. EVP_EncodeInit(ctx);
  4587. ExpectIntEQ(
  4588. EVP_DecodeUpdate(
  4589. ctx,
  4590. decOutBuff,
  4591. &outl,
  4592. enc3,
  4593. sizeof(enc3)-1),
  4594. 0 /* expected result code 0: success */
  4595. );
  4596. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4597. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4598. sizeof(plain3) -1 ),0);
  4599. ExpectIntEQ(
  4600. EVP_DecodeFinal(
  4601. ctx,
  4602. decOutBuff + outl,
  4603. &outl),
  4604. 1 /* expected result code 1: success */
  4605. );
  4606. ExpectIntEQ(outl,0 );
  4607. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4608. sizeof(plain3)-1);
  4609. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4610. sizeof(plain3) -1 ),0);
  4611. }
  4612. /* decode string which has a padding char ('=') in the illegal position*/
  4613. {
  4614. static const unsigned char enc4[] =
  4615. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4616. EVP_EncodeInit(ctx);
  4617. ExpectIntEQ(
  4618. EVP_DecodeUpdate(
  4619. ctx,
  4620. decOutBuff,
  4621. &outl,
  4622. enc4,
  4623. sizeof(enc4)-1),
  4624. -1 /* expected result code -1: error */
  4625. );
  4626. ExpectIntEQ(outl,0);
  4627. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4628. }
  4629. /* small data decode test */
  4630. {
  4631. static const unsigned char enc00[] = {"VG"};
  4632. static const unsigned char enc01[] = {"g=\n"};
  4633. static const unsigned char plain4[] = {"Th"};
  4634. EVP_EncodeInit(ctx);
  4635. ExpectIntEQ(
  4636. EVP_DecodeUpdate(
  4637. ctx,
  4638. decOutBuff,
  4639. &outl,
  4640. enc00,
  4641. sizeof(enc00)-1),
  4642. 1 /* expected result code 1: success */
  4643. );
  4644. ExpectIntEQ(outl,0);
  4645. ExpectIntEQ(
  4646. EVP_DecodeUpdate(
  4647. ctx,
  4648. decOutBuff + outl,
  4649. &outl,
  4650. enc01,
  4651. sizeof(enc01)-1),
  4652. 0 /* expected result code 0: success */
  4653. );
  4654. ExpectIntEQ(outl,sizeof(plain4)-1);
  4655. /* test with illegal parameters */
  4656. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4657. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4658. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4659. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4660. if (EXPECT_SUCCESS()) {
  4661. EVP_DecodeFinal(
  4662. ctx,
  4663. decOutBuff + outl,
  4664. &outl);
  4665. }
  4666. ExpectIntEQ( outl, 0);
  4667. ExpectIntEQ(
  4668. XSTRNCMP(
  4669. (const char*)decOutBuff,
  4670. (const char*)plain4,sizeof(plain4)-1 ),
  4671. 0);
  4672. }
  4673. EVP_ENCODE_CTX_free(ctx);
  4674. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4675. return EXPECT_RESULT();
  4676. }
  4677. static int test_wolfSSL_EVP_DecodeFinal(void)
  4678. {
  4679. int res = TEST_SKIPPED;
  4680. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4681. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4682. * test_wolfSSL_EVP_DecodeUpdate
  4683. */
  4684. res = TEST_SUCCESS;
  4685. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4686. return res;
  4687. }
  4688. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4689. */
  4690. #ifdef OPENSSL_EXTRA
  4691. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4692. {
  4693. EXPECT_DECLS;
  4694. #ifndef NO_AES
  4695. const WOLFSSL_EVP_CIPHER* c;
  4696. c = wolfSSL_EVP_get_cipherbynid(419);
  4697. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4698. defined(WOLFSSL_AES_128)
  4699. ExpectNotNull(c);
  4700. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  4701. #else
  4702. ExpectNull(c);
  4703. #endif
  4704. c = wolfSSL_EVP_get_cipherbynid(423);
  4705. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4706. defined(WOLFSSL_AES_192)
  4707. ExpectNotNull(c);
  4708. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  4709. #else
  4710. ExpectNull(c);
  4711. #endif
  4712. c = wolfSSL_EVP_get_cipherbynid(427);
  4713. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4714. defined(WOLFSSL_AES_256)
  4715. ExpectNotNull(c);
  4716. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  4717. #else
  4718. ExpectNull(c);
  4719. #endif
  4720. c = wolfSSL_EVP_get_cipherbynid(904);
  4721. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4722. ExpectNotNull(c);
  4723. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  4724. #else
  4725. ExpectNull(c);
  4726. #endif
  4727. c = wolfSSL_EVP_get_cipherbynid(905);
  4728. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4729. ExpectNotNull(c);
  4730. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  4731. #else
  4732. ExpectNull(c);
  4733. #endif
  4734. c = wolfSSL_EVP_get_cipherbynid(906);
  4735. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4736. ExpectNotNull(c);
  4737. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  4738. #else
  4739. ExpectNull(c);
  4740. #endif
  4741. c = wolfSSL_EVP_get_cipherbynid(418);
  4742. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4743. ExpectNotNull(c);
  4744. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  4745. #else
  4746. ExpectNull(c);
  4747. #endif
  4748. c = wolfSSL_EVP_get_cipherbynid(422);
  4749. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4750. ExpectNotNull(c);
  4751. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  4752. #else
  4753. ExpectNull(c);
  4754. #endif
  4755. c = wolfSSL_EVP_get_cipherbynid(426);
  4756. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4757. ExpectNotNull(c);
  4758. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  4759. #else
  4760. ExpectNull(c);
  4761. #endif
  4762. #endif /* !NO_AES */
  4763. #ifndef NO_DES3
  4764. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4765. #ifdef WOLFSSL_DES_ECB
  4766. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4767. #endif
  4768. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4769. #ifdef WOLFSSL_DES_ECB
  4770. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4771. #endif
  4772. #endif /* !NO_DES3 */
  4773. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4774. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4775. #endif
  4776. /* test for nid is out of range */
  4777. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  4778. return EXPECT_RESULT();
  4779. }
  4780. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4781. {
  4782. EXPECT_DECLS;
  4783. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4784. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4785. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4786. const EVP_CIPHER *test;
  4787. byte key[AES_BLOCK_SIZE] = {0};
  4788. byte iv[AES_BLOCK_SIZE] = {0};
  4789. ExpectNotNull(ctx);
  4790. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4791. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4792. test = EVP_CIPHER_CTX_cipher(ctx);
  4793. ExpectTrue(init == test);
  4794. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4795. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4796. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4797. EVP_CIPHER_CTX_free(ctx);
  4798. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4799. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4800. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4801. return EXPECT_RESULT();
  4802. }
  4803. #endif /* OPENSSL_EXTRA */
  4804. /*----------------------------------------------------------------------------*
  4805. | IO
  4806. *----------------------------------------------------------------------------*/
  4807. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  4808. defined(HAVE_IO_TESTS_DEPENDENCIES)
  4809. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4810. #ifdef WC_SHA512_DIGEST_SIZE
  4811. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4812. #else
  4813. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4814. #endif
  4815. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4816. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4817. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4818. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4819. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4820. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4821. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4822. defined(HAVE_AES_CBC)
  4823. typedef struct openssl_key_ctx {
  4824. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4825. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4826. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4827. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4828. } openssl_key_ctx;
  4829. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4830. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4831. static WC_INLINE int OpenSSLTicketInit(void)
  4832. {
  4833. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4834. if (ret != 0) return ret;
  4835. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4836. sizeof(myOpenSSLKey_ctx.name));
  4837. if (ret != 0) return ret;
  4838. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4839. sizeof(myOpenSSLKey_ctx.key));
  4840. if (ret != 0) return ret;
  4841. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4842. sizeof(myOpenSSLKey_ctx.hmacKey));
  4843. if (ret != 0) return ret;
  4844. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4845. sizeof(myOpenSSLKey_ctx.iv));
  4846. if (ret != 0) return ret;
  4847. return 0;
  4848. }
  4849. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4850. byte name[WOLFSSL_TICKET_NAME_SZ],
  4851. byte iv[WOLFSSL_TICKET_IV_SZ],
  4852. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4853. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4854. (void)ssl;
  4855. if (enc) {
  4856. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4857. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4858. }
  4859. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4860. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4861. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4862. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4863. return 0;
  4864. }
  4865. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4866. if (enc)
  4867. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4868. else
  4869. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4870. return 1;
  4871. }
  4872. static WC_INLINE void OpenSSLTicketCleanup(void)
  4873. {
  4874. wc_FreeRng(&myOpenSSLKey_rng);
  4875. }
  4876. #endif
  4877. #endif
  4878. /* helper functions */
  4879. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  4880. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4881. void *ctx)
  4882. {
  4883. struct test_ssl_memio_ctx *test_ctx;
  4884. byte *buf;
  4885. int *len;
  4886. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4887. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4888. buf = test_ctx->c_buff;
  4889. len = &test_ctx->c_len;
  4890. }
  4891. else {
  4892. buf = test_ctx->s_buff;
  4893. len = &test_ctx->s_len;
  4894. }
  4895. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  4896. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4897. XMEMCPY(buf + *len, data, sz);
  4898. *len += sz;
  4899. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  4900. {
  4901. /* This can be imported into Wireshark by transforming the file with
  4902. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  4903. * And then loading test_output.dump.hex into Wireshark using the
  4904. * "Import from Hex Dump..." option ion and selecting the TCP
  4905. * encapsulation option. */
  4906. char dump_file_name[64];
  4907. WOLFSSL_BIO *dump_file;
  4908. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  4909. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  4910. if (dump_file != NULL) {
  4911. (void)wolfSSL_BIO_write(dump_file, data, sz);
  4912. wolfSSL_BIO_free(dump_file);
  4913. }
  4914. }
  4915. #endif
  4916. return sz;
  4917. }
  4918. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4919. void *ctx)
  4920. {
  4921. struct test_ssl_memio_ctx *test_ctx;
  4922. int read_sz;
  4923. byte *buf;
  4924. int *len;
  4925. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4926. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4927. buf = test_ctx->s_buff;
  4928. len = &test_ctx->s_len;
  4929. }
  4930. else {
  4931. buf = test_ctx->c_buff;
  4932. len = &test_ctx->c_len;
  4933. }
  4934. if (*len == 0)
  4935. return WOLFSSL_CBIO_ERR_WANT_READ;
  4936. read_sz = sz < *len ? sz : *len;
  4937. XMEMCPY(data, buf, read_sz);
  4938. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4939. *len -= read_sz;
  4940. return read_sz;
  4941. }
  4942. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  4943. {
  4944. EXPECT_DECLS;
  4945. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4946. int c_sharedCtx = 0;
  4947. int s_sharedCtx = 0;
  4948. #endif
  4949. const char* certFile = svrCertFile;
  4950. const char* keyFile = svrKeyFile;
  4951. /********************************
  4952. * Create WOLFSSL_CTX for client.
  4953. ********************************/
  4954. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4955. if (ctx->c_ctx != NULL) {
  4956. c_sharedCtx = ctx->c_cb.isSharedCtx;
  4957. }
  4958. else
  4959. #endif
  4960. {
  4961. WOLFSSL_METHOD* method = NULL;
  4962. if (ctx->c_cb.method != NULL) {
  4963. method = ctx->c_cb.method();
  4964. }
  4965. else {
  4966. method = wolfSSLv23_client_method();
  4967. }
  4968. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  4969. }
  4970. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  4971. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  4972. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4973. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  4974. #endif
  4975. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx, caCertFile, 0),
  4976. WOLFSSL_SUCCESS);
  4977. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4978. if (!c_sharedCtx)
  4979. #endif
  4980. {
  4981. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  4982. cliCertFile), WOLFSSL_SUCCESS);
  4983. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, cliKeyFile,
  4984. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4985. }
  4986. #ifdef HAVE_CRL
  4987. if (ctx->c_cb.crlPemFile != NULL) {
  4988. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  4989. WOLFSSL_SUCCESS);
  4990. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  4991. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4992. }
  4993. #endif
  4994. if (ctx->c_ciphers != NULL) {
  4995. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  4996. WOLFSSL_SUCCESS);
  4997. }
  4998. if (ctx->c_cb.ctx_ready != NULL) {
  4999. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5000. }
  5001. /********************************
  5002. * Create WOLFSSL_CTX for server.
  5003. ********************************/
  5004. if (ctx->s_ctx != NULL) {
  5005. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5006. s_sharedCtx = 1;
  5007. #endif
  5008. ctx->s_cb.isSharedCtx = 1;
  5009. }
  5010. else
  5011. {
  5012. WOLFSSL_METHOD* method = NULL;
  5013. if (ctx->s_cb.method != NULL) {
  5014. method = ctx->s_cb.method();
  5015. }
  5016. else {
  5017. method = wolfSSLv23_server_method();
  5018. }
  5019. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5020. ctx->s_cb.isSharedCtx = 0;
  5021. }
  5022. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5023. #if defined(HAVE_SESSION_TICKET) && \
  5024. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5025. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5026. OpenSSLTicketInit();
  5027. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5028. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5029. TicketInit();
  5030. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5031. #endif
  5032. #endif
  5033. }
  5034. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5035. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5036. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5037. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5038. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx, cliCertFile, 0),
  5039. WOLFSSL_SUCCESS);
  5040. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5041. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5042. #endif
  5043. if (ctx->s_cb.certPemFile != NULL) {
  5044. certFile = ctx->s_cb.certPemFile;
  5045. }
  5046. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5047. if (!s_sharedCtx)
  5048. #endif
  5049. {
  5050. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5051. certFile), WOLFSSL_SUCCESS);
  5052. }
  5053. if (ctx->s_cb.keyPemFile != NULL) {
  5054. keyFile = ctx->s_cb.keyPemFile;
  5055. }
  5056. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5057. if (!s_sharedCtx)
  5058. #endif
  5059. {
  5060. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, keyFile,
  5061. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5062. }
  5063. if (ctx->s_ciphers != NULL) {
  5064. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5065. WOLFSSL_SUCCESS);
  5066. }
  5067. if (ctx->s_cb.ctx_ready != NULL) {
  5068. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5069. }
  5070. /****************************
  5071. * Create WOLFSSL for client.
  5072. ****************************/
  5073. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5074. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5075. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5076. if (0
  5077. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5078. || c_sharedCtx
  5079. #endif
  5080. )
  5081. {
  5082. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl, cliCertFile),
  5083. WOLFSSL_SUCCESS);
  5084. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, cliKeyFile,
  5085. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5086. }
  5087. if (ctx->c_cb.ssl_ready != NULL) {
  5088. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5089. }
  5090. /****************************
  5091. * Create WOLFSSL for server.
  5092. ****************************/
  5093. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5094. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5095. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5096. if (0
  5097. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5098. || s_sharedCtx
  5099. #endif
  5100. )
  5101. {
  5102. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl, certFile),
  5103. WOLFSSL_SUCCESS);
  5104. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, keyFile,
  5105. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5106. }
  5107. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5108. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5109. #elif !defined(NO_DH)
  5110. /* will repick suites with DHE, higher priority than PSK */
  5111. SetDH(ctx->s_ssl);
  5112. #endif
  5113. if (ctx->s_cb.ssl_ready != NULL) {
  5114. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5115. }
  5116. return EXPECT_RESULT();
  5117. }
  5118. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5119. int* rounds)
  5120. {
  5121. int handshake_complete = 0;
  5122. int hs_c = 0;
  5123. int hs_s = 0;
  5124. int failing_s = 0;
  5125. int failing_c = 0;
  5126. int ret;
  5127. int err;
  5128. if (rounds != NULL) {
  5129. *rounds = 0;
  5130. }
  5131. while ((!handshake_complete) && (max_rounds > 0)) {
  5132. if (!hs_c) {
  5133. wolfSSL_SetLoggingPrefix("client");
  5134. ret = wolfSSL_connect(ctx->c_ssl);
  5135. wolfSSL_SetLoggingPrefix(NULL);
  5136. if (ret == WOLFSSL_SUCCESS) {
  5137. hs_c = 1;
  5138. }
  5139. else {
  5140. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5141. if (err != WOLFSSL_ERROR_WANT_READ &&
  5142. err != WOLFSSL_ERROR_WANT_WRITE) {
  5143. failing_c = 1;
  5144. hs_c = 1;
  5145. if (failing_c && failing_s) {
  5146. break;
  5147. }
  5148. }
  5149. }
  5150. }
  5151. if (!hs_s) {
  5152. wolfSSL_SetLoggingPrefix("server");
  5153. ret = wolfSSL_accept(ctx->s_ssl);
  5154. wolfSSL_SetLoggingPrefix(NULL);
  5155. if (ret == WOLFSSL_SUCCESS) {
  5156. hs_s = 1;
  5157. }
  5158. else {
  5159. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5160. if (err != WOLFSSL_ERROR_WANT_READ &&
  5161. err != WOLFSSL_ERROR_WANT_WRITE) {
  5162. failing_s = 1;
  5163. hs_s = 1;
  5164. if (failing_c && failing_s) {
  5165. break;
  5166. }
  5167. }
  5168. }
  5169. }
  5170. handshake_complete = hs_c && hs_s;
  5171. max_rounds--;
  5172. if (rounds != NULL) {
  5173. *rounds += 1;
  5174. }
  5175. }
  5176. if (!handshake_complete) {
  5177. return TEST_FAIL;
  5178. }
  5179. return TEST_SUCCESS;
  5180. }
  5181. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5182. {
  5183. EXPECT_DECLS;
  5184. char input[1024];
  5185. int idx = 0;
  5186. const char* msg_c = "hello wolfssl!";
  5187. int msglen_c = (int)XSTRLEN(msg_c);
  5188. const char* msg_s = "I hear you fa shizzle!";
  5189. int msglen_s = (int)XSTRLEN(msg_s);
  5190. if (ctx->c_msg != NULL) {
  5191. msg_c = ctx->c_msg;
  5192. msglen_c = ctx->c_msglen;
  5193. }
  5194. if (ctx->s_msg != NULL) {
  5195. msg_s = ctx->s_msg;
  5196. msglen_s = ctx->s_msglen;
  5197. }
  5198. wolfSSL_SetLoggingPrefix("client");
  5199. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5200. wolfSSL_SetLoggingPrefix("server");
  5201. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5202. if (idx >= 0) {
  5203. input[idx] = '\0';
  5204. }
  5205. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5206. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5207. ctx->s_cb.return_code = EXPECT_RESULT();
  5208. wolfSSL_SetLoggingPrefix("client");
  5209. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5210. wolfSSL_SetLoggingPrefix(NULL);
  5211. if (idx >= 0) {
  5212. input[idx] = '\0';
  5213. }
  5214. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5215. ctx->c_cb.return_code = EXPECT_RESULT();
  5216. if (ctx->c_cb.on_result != NULL) {
  5217. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5218. }
  5219. if (ctx->s_cb.on_result != NULL) {
  5220. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5221. }
  5222. return EXPECT_RESULT();
  5223. }
  5224. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5225. {
  5226. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5227. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5228. if (ctx->c_cb.on_cleanup != NULL) {
  5229. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5230. }
  5231. if (ctx->s_cb.on_cleanup != NULL) {
  5232. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5233. }
  5234. wolfSSL_shutdown(ctx->s_ssl);
  5235. wolfSSL_shutdown(ctx->c_ssl);
  5236. wolfSSL_free(ctx->s_ssl);
  5237. wolfSSL_free(ctx->c_ssl);
  5238. if (!ctx->s_cb.isSharedCtx) {
  5239. wolfSSL_CTX_free(ctx->s_ctx);
  5240. ctx->s_ctx = NULL;
  5241. }
  5242. if (!ctx->c_cb.isSharedCtx) {
  5243. wolfSSL_CTX_free(ctx->c_ctx);
  5244. ctx->c_ctx = NULL;
  5245. }
  5246. if (!ctx->s_cb.ticNoInit) {
  5247. #if defined(HAVE_SESSION_TICKET) && \
  5248. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5249. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5250. OpenSSLTicketCleanup();
  5251. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5252. TicketCleanup();
  5253. #endif
  5254. #endif
  5255. }
  5256. }
  5257. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5258. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5259. {
  5260. EXPECT_DECLS;
  5261. struct test_ssl_memio_ctx test_ctx;
  5262. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5263. size_t msg_len;
  5264. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5265. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5266. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5267. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5268. test_ctx.c_ctx = client_cb->ctx;
  5269. test_ctx.s_ctx = server_cb->ctx;
  5270. test_ctx.c_cb.return_code = TEST_FAIL;
  5271. test_ctx.s_cb.return_code = TEST_FAIL;
  5272. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5273. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5274. if (client_on_handshake != NULL) {
  5275. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5276. TEST_SUCCESS);
  5277. }
  5278. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5279. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5280. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5281. MD_MAX_SIZE);
  5282. ExpectIntGE(msg_len, 0);
  5283. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5284. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5285. MD_MAX_SIZE);
  5286. ExpectIntGE(msg_len, 0);
  5287. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5288. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5289. test_ssl_memio_cleanup(&test_ctx);
  5290. client_cb->return_code = test_ctx.c_cb.return_code;
  5291. client_cb->last_err = test_ctx.c_cb.last_err;
  5292. server_cb->return_code = test_ctx.s_cb.return_code;
  5293. server_cb->last_err = test_ctx.s_cb.last_err;
  5294. return EXPECT_RESULT();
  5295. }
  5296. #endif
  5297. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5298. #ifdef WOLFSSL_SESSION_EXPORT
  5299. #ifdef WOLFSSL_DTLS
  5300. /* set up function for sending session information */
  5301. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5302. {
  5303. WOLFSSL_CTX* ctx = NULL;
  5304. WOLFSSL* ssl = NULL;
  5305. AssertNotNull(inSsl);
  5306. AssertNotNull(buf);
  5307. AssertIntNE(0, sz);
  5308. /* Set ctx to DTLS 1.2 */
  5309. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5310. AssertNotNull(ctx);
  5311. ssl = wolfSSL_new(ctx);
  5312. AssertNotNull(ssl);
  5313. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5314. wolfSSL_free(ssl);
  5315. wolfSSL_CTX_free(ctx);
  5316. (void)userCtx;
  5317. return 0;
  5318. }
  5319. #endif
  5320. /* returns negative value on fail and positive (including 0) on success */
  5321. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5322. {
  5323. int ret, err, loop_count, count, timeout = 10;
  5324. char msg[] = "I hear you fa shizzle!";
  5325. char input[1024];
  5326. loop_count = ((func_args*)args)->argc;
  5327. #ifdef WOLFSSL_ASYNC_CRYPT
  5328. err = 0; /* Reset error */
  5329. #endif
  5330. do {
  5331. #ifdef WOLFSSL_ASYNC_CRYPT
  5332. if (err == WC_PENDING_E) {
  5333. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5334. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5335. }
  5336. #endif
  5337. ret = wolfSSL_accept(ssl);
  5338. err = wolfSSL_get_error(ssl, 0);
  5339. if (err == WOLFSSL_ERROR_WANT_READ ||
  5340. err == WOLFSSL_ERROR_WANT_WRITE) {
  5341. int select_ret;
  5342. err = WC_PENDING_E;
  5343. select_ret = tcp_select(*sockfd, timeout);
  5344. if (select_ret == TEST_TIMEOUT) {
  5345. return WOLFSSL_FATAL_ERROR;
  5346. }
  5347. }
  5348. } while (err == WC_PENDING_E);
  5349. if (ret != WOLFSSL_SUCCESS) {
  5350. char buff[WOLFSSL_MAX_ERROR_SZ];
  5351. fprintf(stderr, "error = %d, %s\n", err,
  5352. wolfSSL_ERR_error_string(err, buff));
  5353. return ret;
  5354. }
  5355. for (count = 0; count < loop_count; count++) {
  5356. int select_ret;
  5357. select_ret = tcp_select(*sockfd, timeout);
  5358. if (select_ret == TEST_TIMEOUT) {
  5359. ret = WOLFSSL_FATAL_ERROR;
  5360. break;
  5361. }
  5362. do {
  5363. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5364. if (ret > 0) {
  5365. input[ret] = '\0';
  5366. fprintf(stderr, "Client message: %s\n", input);
  5367. }
  5368. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5369. do {
  5370. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5371. return WOLFSSL_FATAL_ERROR;
  5372. }
  5373. err = wolfSSL_get_error(ssl, ret);
  5374. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5375. }
  5376. return ret;
  5377. }
  5378. #endif /* WOLFSSL_SESSION_EXPORT */
  5379. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5380. {
  5381. SOCKET_T sockfd = 0;
  5382. SOCKET_T clientfd = 0;
  5383. word16 port;
  5384. callback_functions* cbf;
  5385. WOLFSSL_CTX* ctx = NULL;
  5386. WOLFSSL* ssl = NULL;
  5387. func_args* opts = (func_args*)args;
  5388. char msg[] = "I hear you fa shizzle!";
  5389. char input[1024];
  5390. int idx;
  5391. int ret, err = 0;
  5392. int sharedCtx = 0;
  5393. int doUdp = 0;
  5394. SOCKADDR_IN_T cliAddr;
  5395. socklen_t cliLen;
  5396. const char* certFile = svrCertFile;
  5397. const char* keyFile = svrKeyFile;
  5398. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5399. size_t msg_len = 0;
  5400. #endif
  5401. wolfSSL_SetLoggingPrefix("server");
  5402. #ifdef WOLFSSL_TIRTOS
  5403. fdOpenSession(Task_self());
  5404. #endif
  5405. opts->return_code = TEST_FAIL;
  5406. cbf = opts->callbacks;
  5407. if (cbf != NULL && cbf->ctx) {
  5408. ctx = cbf->ctx;
  5409. sharedCtx = 1;
  5410. }
  5411. else
  5412. {
  5413. WOLFSSL_METHOD* method = NULL;
  5414. if (cbf != NULL && cbf->method != NULL) {
  5415. method = cbf->method();
  5416. }
  5417. else {
  5418. method = wolfSSLv23_server_method();
  5419. }
  5420. ctx = wolfSSL_CTX_new(method);
  5421. }
  5422. if (ctx == NULL) {
  5423. /* Release the wait for TCP ready. */
  5424. signal_ready(opts->signal);
  5425. goto done;
  5426. }
  5427. if (cbf == NULL || !cbf->ticNoInit) {
  5428. #if defined(HAVE_SESSION_TICKET) && \
  5429. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5430. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5431. OpenSSLTicketInit();
  5432. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5433. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5434. TicketInit();
  5435. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5436. #endif
  5437. #endif
  5438. }
  5439. #if defined(USE_WINDOWS_API)
  5440. port = opts->signal->port;
  5441. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5442. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5443. /* Let tcp_listen assign port */
  5444. port = 0;
  5445. #else
  5446. /* Use default port */
  5447. port = wolfSSLPort;
  5448. #endif
  5449. if (cbf != NULL)
  5450. doUdp = cbf->doUdp;
  5451. /* do it here to detect failure */
  5452. tcp_accept(
  5453. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5454. if (doUdp) {
  5455. cliLen = sizeof(cliAddr);
  5456. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5457. (struct sockaddr*)&cliAddr, &cliLen);
  5458. AssertIntGT(idx, 0);
  5459. }
  5460. else {
  5461. CloseSocket(sockfd);
  5462. }
  5463. wolfSSL_CTX_set_verify(ctx,
  5464. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5465. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5466. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5467. #endif
  5468. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5469. != WOLFSSL_SUCCESS) {
  5470. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5471. goto done;
  5472. }
  5473. if (cbf != NULL && cbf->certPemFile != NULL)
  5474. certFile = cbf->certPemFile;
  5475. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5476. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5477. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5478. #else
  5479. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5480. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5481. #endif
  5482. /*err_sys("can't load server cert chain file, "
  5483. "Please run from wolfSSL home dir");*/
  5484. goto done;
  5485. }
  5486. if (cbf != NULL && cbf->keyPemFile != NULL)
  5487. keyFile = cbf->keyPemFile;
  5488. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5489. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5490. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5491. #else
  5492. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5493. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5494. #endif
  5495. /*err_sys("can't load server key file, "
  5496. "Please run from wolfSSL home dir");*/
  5497. goto done;
  5498. }
  5499. #ifdef HAVE_CRL
  5500. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5501. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5502. goto done;
  5503. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5504. != WOLFSSL_SUCCESS)
  5505. goto done;
  5506. }
  5507. #endif
  5508. /* call ctx setup callback */
  5509. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5510. cbf->ctx_ready(ctx);
  5511. }
  5512. ssl = wolfSSL_new(ctx);
  5513. if (ssl == NULL) {
  5514. goto done;
  5515. }
  5516. if (doUdp) {
  5517. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5518. if (err != WOLFSSL_SUCCESS)
  5519. goto done;
  5520. }
  5521. #ifdef WOLFSSL_SESSION_EXPORT
  5522. /* only add in more complex nonblocking case with session export tests */
  5523. if (args && opts->argc > 0) {
  5524. /* set as nonblock and time out for waiting on read/write */
  5525. tcp_set_nonblocking(&clientfd);
  5526. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5527. }
  5528. #endif
  5529. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5530. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5531. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5532. #else
  5533. if (wolfSSL_use_certificate_file(ssl, certFile,
  5534. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5535. #endif
  5536. /*err_sys("can't load server cert chain file, "
  5537. "Please run from wolfSSL home dir");*/
  5538. goto done;
  5539. }
  5540. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5541. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5542. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5543. #else
  5544. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5545. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5546. #endif
  5547. /*err_sys("can't load server key file, "
  5548. "Please run from wolfSSL home dir");*/
  5549. goto done;
  5550. }
  5551. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5552. /*err_sys("SSL_set_fd failed");*/
  5553. goto done;
  5554. }
  5555. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5556. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5557. #elif !defined(NO_DH)
  5558. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5559. #endif
  5560. /* call ssl setup callback */
  5561. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5562. cbf->ssl_ready(ssl);
  5563. }
  5564. #ifdef WOLFSSL_SESSION_EXPORT
  5565. /* only add in more complex nonblocking case with session export tests */
  5566. if (opts->argc > 0) {
  5567. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5568. if (ret >= 0) {
  5569. opts->return_code = TEST_SUCCESS;
  5570. }
  5571. #ifdef WOLFSSL_TIRTOS
  5572. Task_yield();
  5573. #endif
  5574. goto done;
  5575. }
  5576. #endif
  5577. #ifdef WOLFSSL_ASYNC_CRYPT
  5578. err = 0; /* Reset error */
  5579. #endif
  5580. do {
  5581. #ifdef WOLFSSL_ASYNC_CRYPT
  5582. if (err == WC_PENDING_E) {
  5583. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5584. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5585. }
  5586. #endif
  5587. ret = wolfSSL_negotiate(ssl);
  5588. err = wolfSSL_get_error(ssl, 0);
  5589. } while (err == WC_PENDING_E);
  5590. if (ret != WOLFSSL_SUCCESS) {
  5591. char buff[WOLFSSL_MAX_ERROR_SZ];
  5592. fprintf(stderr, "error = %d, %s\n", err,
  5593. wolfSSL_ERR_error_string(err, buff));
  5594. /*err_sys("SSL_accept failed");*/
  5595. goto done;
  5596. }
  5597. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5598. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5599. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5600. AssertIntGE(msg_len, 0);
  5601. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5602. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5603. AssertIntGE(msg_len, 0);
  5604. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5605. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5606. if (idx > 0) {
  5607. input[idx] = '\0';
  5608. fprintf(stderr, "Client message: %s\n", input);
  5609. }
  5610. else if (idx < 0) {
  5611. goto done;
  5612. }
  5613. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5614. /*err_sys("SSL_write failed");*/
  5615. goto done;
  5616. }
  5617. if (cbf != NULL && cbf->on_result != NULL)
  5618. cbf->on_result(ssl);
  5619. #ifdef WOLFSSL_TIRTOS
  5620. Task_yield();
  5621. #endif
  5622. opts->return_code = TEST_SUCCESS;
  5623. done:
  5624. if (cbf != NULL)
  5625. cbf->last_err = err;
  5626. if (cbf != NULL && cbf->on_cleanup != NULL)
  5627. cbf->on_cleanup(ssl);
  5628. wolfSSL_shutdown(ssl);
  5629. wolfSSL_free(ssl);
  5630. if (!sharedCtx)
  5631. wolfSSL_CTX_free(ctx);
  5632. CloseSocket(clientfd);
  5633. #ifdef WOLFSSL_TIRTOS
  5634. fdCloseSession(Task_self());
  5635. #endif
  5636. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5637. && defined(HAVE_THREAD_LS)
  5638. wc_ecc_fp_free(); /* free per thread cache */
  5639. #endif
  5640. if (cbf == NULL || !cbf->ticNoInit) {
  5641. #if defined(HAVE_SESSION_TICKET) && \
  5642. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5643. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5644. OpenSSLTicketCleanup();
  5645. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5646. TicketCleanup();
  5647. #endif
  5648. #endif
  5649. }
  5650. wolfSSL_SetLoggingPrefix(NULL);
  5651. WOLFSSL_RETURN_FROM_THREAD(0);
  5652. }
  5653. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5654. !defined(WOLFSSL_NO_TLS12)
  5655. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5656. {
  5657. SOCKET_T sockfd = 0;
  5658. SOCKET_T clientfd = 0;
  5659. word16 port;
  5660. callback_functions* cbf;
  5661. WOLFSSL_CTX* ctx = 0;
  5662. WOLFSSL* ssl = 0;
  5663. char msg[] = "I hear you fa shizzle!";
  5664. char input[1024];
  5665. int idx;
  5666. int ret, err = 0;
  5667. int sharedCtx = 0;
  5668. func_args* opts = (func_args*)args;
  5669. int loop_count = opts->argc;
  5670. int count = 0;
  5671. #ifdef WOLFSSL_TIRTOS
  5672. fdOpenSession(Task_self());
  5673. #endif
  5674. opts->return_code = TEST_FAIL;
  5675. cbf = opts->callbacks;
  5676. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5677. if (cbf != NULL && cbf->ctx) {
  5678. ctx = cbf->ctx;
  5679. sharedCtx = 1;
  5680. }
  5681. else
  5682. #endif
  5683. {
  5684. WOLFSSL_METHOD* method = NULL;
  5685. if (cbf != NULL && cbf->method != NULL) {
  5686. method = cbf->method();
  5687. }
  5688. else {
  5689. method = wolfSSLv23_server_method();
  5690. }
  5691. ctx = wolfSSL_CTX_new(method);
  5692. }
  5693. #if defined(USE_WINDOWS_API)
  5694. port = opts->signal->port;
  5695. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5696. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5697. /* Let tcp_listen assign port */
  5698. port = 0;
  5699. #else
  5700. /* Use default port */
  5701. port = wolfSSLPort;
  5702. #endif
  5703. wolfSSL_CTX_set_verify(ctx,
  5704. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5705. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5706. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5707. #endif
  5708. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5709. != WOLFSSL_SUCCESS) {
  5710. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5711. /* Release the wait for TCP ready. */
  5712. signal_ready(opts->signal);
  5713. goto done;
  5714. }
  5715. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  5716. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5717. /*err_sys("can't load server cert chain file, "
  5718. "Please run from wolfSSL home dir");*/
  5719. /* Release the wait for TCP ready. */
  5720. signal_ready(opts->signal);
  5721. goto done;
  5722. }
  5723. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5724. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5725. /*err_sys("can't load server key file, "
  5726. "Please run from wolfSSL home dir");*/
  5727. /* Release the wait for TCP ready. */
  5728. signal_ready(opts->signal);
  5729. goto done;
  5730. }
  5731. /* call ctx setup callback */
  5732. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5733. cbf->ctx_ready(ctx);
  5734. }
  5735. while (count != loop_count) {
  5736. ssl = wolfSSL_new(ctx);
  5737. if (ssl == NULL) {
  5738. signal_ready(opts->signal);
  5739. goto done;
  5740. }
  5741. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5742. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5743. /*err_sys("can't load server cert chain file, "
  5744. "Please run from wolfSSL home dir");*/
  5745. /* Release the wait for TCP ready. */
  5746. signal_ready(opts->signal);
  5747. goto done;
  5748. }
  5749. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5750. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5751. /*err_sys("can't load server key file, "
  5752. "Please run from wolfSSL home dir");*/
  5753. /* Release the wait for TCP ready. */
  5754. signal_ready(opts->signal);
  5755. goto done;
  5756. }
  5757. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5758. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5759. #elif !defined(NO_DH)
  5760. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5761. #endif
  5762. /* call ssl setup callback */
  5763. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5764. cbf->ssl_ready(ssl);
  5765. }
  5766. /* do it here to detect failure */
  5767. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  5768. 0);
  5769. CloseSocket(sockfd);
  5770. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5771. /*err_sys("SSL_set_fd failed");*/
  5772. goto done;
  5773. }
  5774. #ifdef WOLFSSL_ASYNC_CRYPT
  5775. err = 0; /* Reset error */
  5776. #endif
  5777. do {
  5778. #ifdef WOLFSSL_ASYNC_CRYPT
  5779. if (err == WC_PENDING_E) {
  5780. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5781. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5782. }
  5783. #endif
  5784. ret = wolfSSL_accept(ssl);
  5785. err = wolfSSL_get_error(ssl, 0);
  5786. } while (err == WC_PENDING_E);
  5787. if (ret != WOLFSSL_SUCCESS) {
  5788. char buff[WOLFSSL_MAX_ERROR_SZ];
  5789. fprintf(stderr, "error = %d, %s\n", err,
  5790. wolfSSL_ERR_error_string(err, buff));
  5791. /*err_sys("SSL_accept failed");*/
  5792. goto done;
  5793. }
  5794. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5795. if (idx > 0) {
  5796. input[idx] = '\0';
  5797. fprintf(stderr, "Client message: %s\n", input);
  5798. }
  5799. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5800. /*err_sys("SSL_write failed");*/
  5801. goto done;
  5802. }
  5803. /* free ssl for this connection */
  5804. wolfSSL_shutdown(ssl);
  5805. wolfSSL_free(ssl); ssl = NULL;
  5806. CloseSocket(clientfd);
  5807. count++;
  5808. }
  5809. #ifdef WOLFSSL_TIRTOS
  5810. Task_yield();
  5811. #endif
  5812. opts->return_code = TEST_SUCCESS;
  5813. done:
  5814. if (ssl != NULL) {
  5815. wolfSSL_shutdown(ssl);
  5816. wolfSSL_free(ssl);
  5817. }
  5818. if (!sharedCtx)
  5819. wolfSSL_CTX_free(ctx);
  5820. CloseSocket(clientfd);
  5821. #ifdef WOLFSSL_TIRTOS
  5822. fdCloseSession(Task_self());
  5823. #endif
  5824. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5825. && defined(HAVE_THREAD_LS)
  5826. wc_ecc_fp_free(); /* free per thread cache */
  5827. #endif
  5828. WOLFSSL_RETURN_FROM_THREAD(0);
  5829. }
  5830. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5831. static int test_client_nofail(void* args, cbType cb)
  5832. {
  5833. #if !defined(NO_WOLFSSL_CLIENT)
  5834. SOCKET_T sockfd = 0;
  5835. callback_functions* cbf;
  5836. WOLFSSL_CTX* ctx = 0;
  5837. WOLFSSL* ssl = 0;
  5838. WOLFSSL_CIPHER* cipher;
  5839. char msg[64] = "hello wolfssl!";
  5840. char reply[1024];
  5841. int input;
  5842. int msgSz = (int)XSTRLEN(msg);
  5843. int ret, err = 0;
  5844. int cipherSuite;
  5845. int sharedCtx = 0;
  5846. int doUdp = 0;
  5847. const char* cipherName1, *cipherName2;
  5848. wolfSSL_SetLoggingPrefix("client");
  5849. #ifdef WOLFSSL_TIRTOS
  5850. fdOpenSession(Task_self());
  5851. #endif
  5852. ((func_args*)args)->return_code = TEST_FAIL;
  5853. cbf = ((func_args*)args)->callbacks;
  5854. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5855. if (cbf != NULL && cbf->ctx) {
  5856. ctx = cbf->ctx;
  5857. sharedCtx = cbf->isSharedCtx;
  5858. }
  5859. else
  5860. #endif
  5861. {
  5862. WOLFSSL_METHOD* method = NULL;
  5863. if (cbf != NULL && cbf->method != NULL) {
  5864. method = cbf->method();
  5865. }
  5866. else {
  5867. method = wolfSSLv23_client_method();
  5868. }
  5869. ctx = wolfSSL_CTX_new(method);
  5870. }
  5871. if (cbf != NULL)
  5872. doUdp = cbf->doUdp;
  5873. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5874. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5875. #endif
  5876. /* Do connect here so server detects failures */
  5877. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5878. doUdp, 0, NULL);
  5879. /* Connect the socket so that we don't have to set the peer later on */
  5880. if (doUdp)
  5881. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5882. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5883. {
  5884. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5885. goto done;
  5886. }
  5887. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5888. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5889. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5890. #else
  5891. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5892. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5893. #endif
  5894. /*err_sys("can't load client cert file, "
  5895. "Please run from wolfSSL home dir");*/
  5896. goto done;
  5897. }
  5898. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5899. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5900. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5901. #else
  5902. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5903. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5904. #endif
  5905. /*err_sys("can't load client key file, "
  5906. "Please run from wolfSSL home dir");*/
  5907. goto done;
  5908. }
  5909. #ifdef HAVE_CRL
  5910. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5911. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5912. goto done;
  5913. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5914. != WOLFSSL_SUCCESS)
  5915. goto done;
  5916. }
  5917. #endif
  5918. /* call ctx setup callback */
  5919. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5920. cbf->ctx_ready(ctx);
  5921. }
  5922. ssl = wolfSSL_new(ctx);
  5923. if (ssl == NULL) {
  5924. goto done;
  5925. }
  5926. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5927. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5928. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5929. #else
  5930. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5931. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5932. #endif
  5933. /*err_sys("can't load client cert file, "
  5934. "Please run from wolfSSL home dir");*/
  5935. goto done;
  5936. }
  5937. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5938. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5939. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5940. #else
  5941. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5942. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5943. #endif
  5944. /*err_sys("can't load client key file, "
  5945. "Please run from wolfSSL home dir");*/
  5946. goto done;
  5947. }
  5948. if (!doUdp) {
  5949. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5950. /*err_sys("SSL_set_fd failed");*/
  5951. goto done;
  5952. }
  5953. }
  5954. else {
  5955. #ifdef WOLFSSL_DTLS
  5956. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5957. /*err_sys("SSL_set_fd failed");*/
  5958. goto done;
  5959. }
  5960. #else
  5961. goto done;
  5962. #endif
  5963. }
  5964. /* call ssl setup callback */
  5965. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5966. cbf->ssl_ready(ssl);
  5967. }
  5968. #ifdef WOLFSSL_ASYNC_CRYPT
  5969. err = 0; /* Reset error */
  5970. #endif
  5971. do {
  5972. #ifdef WOLFSSL_ASYNC_CRYPT
  5973. if (err == WC_PENDING_E) {
  5974. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5975. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5976. }
  5977. #endif
  5978. ret = wolfSSL_negotiate(ssl);
  5979. err = wolfSSL_get_error(ssl, 0);
  5980. } while (err == WC_PENDING_E);
  5981. if (ret != WOLFSSL_SUCCESS) {
  5982. char buff[WOLFSSL_MAX_ERROR_SZ];
  5983. fprintf(stderr, "error = %d, %s\n", err,
  5984. wolfSSL_ERR_error_string(err, buff));
  5985. /*err_sys("SSL_connect failed");*/
  5986. goto done;
  5987. }
  5988. /* test the various get cipher methods */
  5989. /* Internal cipher suite names */
  5990. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  5991. cipherName1 = wolfSSL_get_cipher_name(ssl);
  5992. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  5993. (cipherSuite >> 8), cipherSuite & 0xFF);
  5994. AssertStrEQ(cipherName1, cipherName2);
  5995. /* IANA Cipher Suites Names */
  5996. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  5997. then it's the internal cipher suite name */
  5998. cipher = wolfSSL_get_current_cipher(ssl);
  5999. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6000. cipherName2 = wolfSSL_get_cipher(ssl);
  6001. AssertStrEQ(cipherName1, cipherName2);
  6002. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6003. !defined(WOLFSSL_QT)
  6004. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6005. (cipherSuite >> 8), cipherSuite & 0xFF);
  6006. AssertStrEQ(cipherName1, cipherName2);
  6007. #endif
  6008. if (cb != NULL)
  6009. (cb)(ctx, ssl);
  6010. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6011. /*err_sys("SSL_write failed");*/
  6012. goto done;
  6013. }
  6014. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6015. if (input > 0) {
  6016. reply[input] = '\0';
  6017. fprintf(stderr, "Server response: %s\n", reply);
  6018. }
  6019. if (cbf != NULL && cbf->on_result != NULL)
  6020. cbf->on_result(ssl);
  6021. ((func_args*)args)->return_code = TEST_SUCCESS;
  6022. done:
  6023. if (cbf != NULL)
  6024. cbf->last_err = err;
  6025. if (cbf != NULL && cbf->on_cleanup != NULL)
  6026. cbf->on_cleanup(ssl);
  6027. wolfSSL_free(ssl);
  6028. if (!sharedCtx)
  6029. wolfSSL_CTX_free(ctx);
  6030. CloseSocket(sockfd);
  6031. #ifdef WOLFSSL_TIRTOS
  6032. fdCloseSession(Task_self());
  6033. #endif
  6034. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6035. && defined(HAVE_THREAD_LS)
  6036. wc_ecc_fp_free(); /* free per thread cache */
  6037. #endif
  6038. #else
  6039. (void)args;
  6040. (void)cb;
  6041. #endif /* !NO_WOLFSSL_CLIENT */
  6042. wolfSSL_SetLoggingPrefix(NULL);
  6043. return 0;
  6044. }
  6045. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6046. callback_functions* server_cb, cbType client_on_handshake)
  6047. {
  6048. func_args client_args;
  6049. func_args server_args;
  6050. tcp_ready ready;
  6051. THREAD_TYPE serverThread;
  6052. XMEMSET(&client_args, 0, sizeof(func_args));
  6053. XMEMSET(&server_args, 0, sizeof(func_args));
  6054. #ifdef WOLFSSL_TIRTOS
  6055. fdOpenSession(Task_self());
  6056. #endif
  6057. StartTCP();
  6058. InitTcpReady(&ready);
  6059. #if defined(USE_WINDOWS_API)
  6060. /* use RNG to get random port if using windows */
  6061. ready.port = GetRandomPort();
  6062. #endif
  6063. server_args.signal = &ready;
  6064. server_args.callbacks = server_cb;
  6065. client_args.signal = &ready;
  6066. client_args.callbacks = client_cb;
  6067. start_thread(test_server_nofail, &server_args, &serverThread);
  6068. wait_tcp_ready(&server_args);
  6069. test_client_nofail(&client_args, client_on_handshake);
  6070. join_thread(serverThread);
  6071. client_cb->return_code = client_args.return_code;
  6072. server_cb->return_code = server_args.return_code;
  6073. FreeTcpReady(&ready);
  6074. #ifdef WOLFSSL_TIRTOS
  6075. fdOpenSession(Task_self());
  6076. #endif
  6077. }
  6078. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6079. callback_functions* server_cb)
  6080. {
  6081. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6082. }
  6083. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6084. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6085. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6086. void* server_args)
  6087. {
  6088. SOCKET_T sockfd = 0;
  6089. callback_functions* cbf;
  6090. WOLFSSL_CTX* ctx = 0;
  6091. WOLFSSL* ssl = 0;
  6092. WOLFSSL_SESSION* session = NULL;
  6093. char msg[64] = "hello wolfssl!";
  6094. char reply[1024];
  6095. int input;
  6096. int msgSz = (int)XSTRLEN(msg);
  6097. int ret, err = 0;
  6098. int sharedCtx = 0;
  6099. #ifdef WOLFSSL_TIRTOS
  6100. fdOpenSession(Task_self());
  6101. #endif
  6102. ((func_args*)args)->return_code = TEST_FAIL;
  6103. cbf = ((func_args*)args)->callbacks;
  6104. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6105. if (cbf != NULL && cbf->ctx) {
  6106. ctx = cbf->ctx;
  6107. sharedCtx = 1;
  6108. }
  6109. else
  6110. #endif
  6111. {
  6112. WOLFSSL_METHOD* method = NULL;
  6113. if (cbf != NULL && cbf->method != NULL) {
  6114. method = cbf->method();
  6115. }
  6116. else {
  6117. method = wolfSSLv23_client_method();
  6118. }
  6119. ctx = wolfSSL_CTX_new(method);
  6120. }
  6121. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6122. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6123. #endif
  6124. /* Do connect here so server detects failures */
  6125. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6126. 0, 0, NULL);
  6127. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6128. WOLFSSL_SUCCESS) {
  6129. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6130. goto done;
  6131. }
  6132. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6133. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6134. /*err_sys("can't load client cert file, "
  6135. "Please run from wolfSSL home dir");*/
  6136. goto done;
  6137. }
  6138. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6139. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6140. /*err_sys("can't load client key file, "
  6141. "Please run from wolfSSL home dir");*/
  6142. goto done;
  6143. }
  6144. /* call ctx setup callback */
  6145. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6146. cbf->ctx_ready(ctx);
  6147. }
  6148. ssl = wolfSSL_new(ctx);
  6149. if (ssl == NULL) {
  6150. goto done;
  6151. }
  6152. /* keep handshake resources for re-using WOLFSSL obj */
  6153. wolfSSL_KeepArrays(ssl);
  6154. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6155. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6156. goto done;
  6157. }
  6158. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6159. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6160. /*err_sys("can't load client cert file, "
  6161. "Please run from wolfSSL home dir");*/
  6162. goto done;
  6163. }
  6164. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6165. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6166. /*err_sys("can't load client key file, "
  6167. "Please run from wolfSSL home dir");*/
  6168. goto done;
  6169. }
  6170. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6171. /*err_sys("SSL_set_fd failed");*/
  6172. goto done;
  6173. }
  6174. /* call ssl setup callback */
  6175. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6176. cbf->ssl_ready(ssl);
  6177. }
  6178. #ifdef WOLFSSL_ASYNC_CRYPT
  6179. err = 0; /* Reset error */
  6180. #endif
  6181. do {
  6182. #ifdef WOLFSSL_ASYNC_CRYPT
  6183. if (err == WC_PENDING_E) {
  6184. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6185. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6186. }
  6187. #endif
  6188. ret = wolfSSL_connect(ssl);
  6189. err = wolfSSL_get_error(ssl, 0);
  6190. } while (err == WC_PENDING_E);
  6191. if (ret != WOLFSSL_SUCCESS) {
  6192. char buff[WOLFSSL_MAX_ERROR_SZ];
  6193. fprintf(stderr, "error = %d, %s\n", err,
  6194. wolfSSL_ERR_error_string(err, buff));
  6195. /*err_sys("SSL_connect failed");*/
  6196. goto done;
  6197. }
  6198. /* Build first session */
  6199. if (cb != NULL)
  6200. cb(ctx, ssl);
  6201. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6202. /*err_sys("SSL_write failed");*/
  6203. goto done;
  6204. }
  6205. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6206. if (input > 0) {
  6207. reply[input] = '\0';
  6208. fprintf(stderr, "Server response: %s\n", reply);
  6209. }
  6210. /* Session Resumption by re-using WOLFSSL object */
  6211. wolfSSL_set_quiet_shutdown(ssl, 1);
  6212. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6213. /* err_sys ("SSL shutdown failed"); */
  6214. goto done;
  6215. }
  6216. session = wolfSSL_get1_session(ssl);
  6217. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6218. wolfSSL_SESSION_free(session);
  6219. /* err_sys ("SSL_clear failed"); */
  6220. goto done;
  6221. }
  6222. wolfSSL_set_session(ssl, session);
  6223. wolfSSL_SESSION_free(session);
  6224. session = NULL;
  6225. /* close socket once */
  6226. CloseSocket(sockfd);
  6227. sockfd = 0;
  6228. /* wait until server ready */
  6229. wait_tcp_ready((func_args*)server_args);
  6230. fprintf(stderr, "session resumption\n");
  6231. /* Do re-connect */
  6232. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6233. 0, 0, NULL);
  6234. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6235. /*err_sys("SSL_set_fd failed");*/
  6236. goto done;
  6237. }
  6238. #ifdef WOLFSSL_ASYNC_CRYPT
  6239. err = 0; /* Reset error */
  6240. #endif
  6241. do {
  6242. #ifdef WOLFSSL_ASYNC_CRYPT
  6243. if (err == WC_PENDING_E) {
  6244. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6245. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6246. }
  6247. #endif
  6248. ret = wolfSSL_connect(ssl);
  6249. err = wolfSSL_get_error(ssl, 0);
  6250. } while (err == WC_PENDING_E);
  6251. if (ret != WOLFSSL_SUCCESS) {
  6252. char buff[WOLFSSL_MAX_ERROR_SZ];
  6253. fprintf(stderr, "error = %d, %s\n", err,
  6254. wolfSSL_ERR_error_string(err, buff));
  6255. /*err_sys("SSL_connect failed");*/
  6256. goto done;
  6257. }
  6258. /* Build first session */
  6259. if (cb != NULL)
  6260. cb(ctx, ssl);
  6261. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6262. /*err_sys("SSL_write failed");*/
  6263. goto done;
  6264. }
  6265. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6266. if (input > 0) {
  6267. reply[input] = '\0';
  6268. fprintf(stderr, "Server response: %s\n", reply);
  6269. }
  6270. ((func_args*)args)->return_code = TEST_SUCCESS;
  6271. done:
  6272. wolfSSL_free(ssl);
  6273. if (!sharedCtx)
  6274. wolfSSL_CTX_free(ctx);
  6275. CloseSocket(sockfd);
  6276. #ifdef WOLFSSL_TIRTOS
  6277. fdCloseSession(Task_self());
  6278. #endif
  6279. return;
  6280. }
  6281. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6282. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6283. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6284. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6285. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6286. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6287. #define HAVE_ALPN_PROTOS_SUPPORT
  6288. #endif
  6289. /* Generic TLS client / server with callbacks for API unit tests
  6290. * Used by SNI / ALPN / crypto callback helper functions */
  6291. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6292. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6293. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6294. #define ENABLE_TLS_CALLBACK_TEST
  6295. #endif
  6296. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6297. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6298. /* TLS server for API unit testing - generic */
  6299. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6300. {
  6301. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6302. WOLFSSL_CTX* ctx = NULL;
  6303. WOLFSSL* ssl = NULL;
  6304. SOCKET_T sfd = 0;
  6305. SOCKET_T cfd = 0;
  6306. word16 port;
  6307. char msg[] = "I hear you fa shizzle!";
  6308. int len = (int) XSTRLEN(msg);
  6309. char input[1024];
  6310. int idx;
  6311. int ret, err = 0;
  6312. ((func_args*)args)->return_code = TEST_FAIL;
  6313. #if defined(USE_WINDOWS_API)
  6314. port = ((func_args*)args)->signal->port;
  6315. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6316. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6317. /* Let tcp_listen assign port */
  6318. port = 0;
  6319. #else
  6320. /* Use default port */
  6321. port = wolfSSLPort;
  6322. #endif
  6323. #ifdef WOLFSSL_DTLS
  6324. if (callbacks->method == wolfDTLS_server_method
  6325. #ifdef WOLFSSL_STATIC_MEMORY
  6326. || callbacks->method_ex == wolfDTLS_server_method_ex
  6327. #endif
  6328. #ifndef NO_OLD_TLS
  6329. || callbacks->method == wolfDTLSv1_server_method
  6330. #ifdef WOLFSSL_STATIC_MEMORY
  6331. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6332. #endif
  6333. #endif
  6334. #ifndef WOLFSSL_NO_TLS12
  6335. || callbacks->method == wolfDTLSv1_2_server_method
  6336. #ifdef WOLFSSL_STATIC_MEMORY
  6337. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6338. #endif
  6339. #endif
  6340. #ifdef WOLFSSL_DTLS13
  6341. || callbacks->method == wolfDTLSv1_3_server_method
  6342. #ifdef WOLFSSL_STATIC_MEMORY
  6343. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6344. #endif
  6345. #endif
  6346. ) {
  6347. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6348. }
  6349. else
  6350. #endif
  6351. {
  6352. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6353. }
  6354. #ifdef WOLFSSL_STATIC_MEMORY
  6355. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6356. callbacks->memSz > 0) {
  6357. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6358. callbacks->mem, callbacks->memSz, 0, 1);
  6359. if (ret != WOLFSSL_SUCCESS) {
  6360. fprintf(stderr, "CTX static new failed %d\n", ret);
  6361. goto cleanup;
  6362. }
  6363. }
  6364. #else
  6365. ctx = wolfSSL_CTX_new(callbacks->method());
  6366. #endif
  6367. if (ctx == NULL) {
  6368. fprintf(stderr, "CTX new failed\n");
  6369. goto cleanup;
  6370. }
  6371. /* set defaults */
  6372. if (callbacks->caPemFile == NULL)
  6373. callbacks->caPemFile = cliCertFile;
  6374. if (callbacks->certPemFile == NULL)
  6375. callbacks->certPemFile = svrCertFile;
  6376. if (callbacks->keyPemFile == NULL)
  6377. callbacks->keyPemFile = svrKeyFile;
  6378. #ifdef WOLFSSL_TIRTOS
  6379. fdOpenSession(Task_self());
  6380. #endif
  6381. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6382. wolfSSL_CTX_set_verify(ctx,
  6383. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6384. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6385. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6386. #endif
  6387. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6388. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6389. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6390. goto cleanup;
  6391. }
  6392. #endif
  6393. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6394. WOLFSSL_SUCCESS) {
  6395. goto cleanup;
  6396. }
  6397. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6398. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6399. goto cleanup;
  6400. }
  6401. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6402. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6403. goto cleanup;
  6404. }
  6405. #ifdef HAVE_CRL
  6406. if (callbacks->crlPemFile != NULL) {
  6407. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6408. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6409. goto cleanup;
  6410. }
  6411. }
  6412. #endif
  6413. if (callbacks->ctx_ready)
  6414. callbacks->ctx_ready(ctx);
  6415. ssl = wolfSSL_new(ctx);
  6416. if (ssl == NULL) {
  6417. fprintf(stderr, "SSL new failed\n");
  6418. goto cleanup;
  6419. }
  6420. if (wolfSSL_dtls(ssl)) {
  6421. SOCKADDR_IN_T cliAddr;
  6422. socklen_t cliLen;
  6423. cliLen = sizeof(cliAddr);
  6424. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6425. (struct sockaddr*)&cliAddr, &cliLen);
  6426. if (idx <= 0) {
  6427. goto cleanup;
  6428. }
  6429. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6430. }
  6431. else {
  6432. CloseSocket(sfd);
  6433. }
  6434. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6435. goto cleanup;
  6436. }
  6437. if (callbacks->loadToSSL) {
  6438. wolfSSL_SetDevId(ssl, callbacks->devId);
  6439. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6440. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6441. goto cleanup;
  6442. }
  6443. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6444. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6445. goto cleanup;
  6446. }
  6447. }
  6448. #ifdef NO_PSK
  6449. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6450. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6451. #elif !defined(NO_DH)
  6452. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6453. #endif
  6454. #endif
  6455. if (callbacks->ssl_ready)
  6456. callbacks->ssl_ready(ssl);
  6457. #ifdef WOLFSSL_ASYNC_CRYPT
  6458. err = 0; /* Reset error */
  6459. #endif
  6460. do {
  6461. #ifdef WOLFSSL_ASYNC_CRYPT
  6462. if (err == WC_PENDING_E) {
  6463. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6464. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6465. }
  6466. #endif
  6467. ret = wolfSSL_accept(ssl);
  6468. err = wolfSSL_get_error(ssl, ret);
  6469. } while (err == WC_PENDING_E);
  6470. if (ret != WOLFSSL_SUCCESS) {
  6471. char buff[WOLFSSL_MAX_ERROR_SZ];
  6472. fprintf(stderr, "accept error = %d, %s\n", err,
  6473. wolfSSL_ERR_error_string(err, buff));
  6474. /*err_sys("SSL_accept failed");*/
  6475. }
  6476. else {
  6477. #ifdef WOLFSSL_ASYNC_CRYPT
  6478. err = 0; /* Reset error */
  6479. #endif
  6480. do {
  6481. #ifdef WOLFSSL_ASYNC_CRYPT
  6482. if (err == WC_PENDING_E) {
  6483. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6484. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6485. }
  6486. #endif
  6487. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6488. err = wolfSSL_get_error(ssl, idx);
  6489. } while (err == WC_PENDING_E);
  6490. if (idx > 0) {
  6491. input[idx] = 0;
  6492. fprintf(stderr, "Client message: %s\n", input);
  6493. }
  6494. #ifdef WOLFSSL_ASYNC_CRYPT
  6495. err = 0; /* Reset error */
  6496. #endif
  6497. do {
  6498. #ifdef WOLFSSL_ASYNC_CRYPT
  6499. if (err == WC_PENDING_E) {
  6500. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6501. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6502. }
  6503. #endif
  6504. ret = wolfSSL_write(ssl, msg, len);
  6505. err = wolfSSL_get_error(ssl, ret);
  6506. } while (err == WC_PENDING_E);
  6507. if (len != ret) {
  6508. goto cleanup;
  6509. }
  6510. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6511. defined(WOLFSSL_DTLS)
  6512. if (wolfSSL_dtls(ssl)) {
  6513. byte* import;
  6514. word32 sz;
  6515. wolfSSL_dtls_export(ssl, NULL, &sz);
  6516. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6517. if (import == NULL) {
  6518. goto cleanup;
  6519. }
  6520. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6521. if (idx < 0) {
  6522. goto cleanup;
  6523. }
  6524. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6525. goto cleanup;
  6526. }
  6527. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6528. }
  6529. #endif
  6530. #ifdef WOLFSSL_TIRTOS
  6531. Task_yield();
  6532. #endif
  6533. ((func_args*)args)->return_code = TEST_SUCCESS;
  6534. }
  6535. if (callbacks->on_result)
  6536. callbacks->on_result(ssl);
  6537. wolfSSL_shutdown(ssl);
  6538. cleanup:
  6539. wolfSSL_free(ssl);
  6540. wolfSSL_CTX_free(ctx);
  6541. CloseSocket(cfd);
  6542. #ifdef WOLFSSL_TIRTOS
  6543. fdCloseSession(Task_self());
  6544. #endif
  6545. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6546. && defined(HAVE_THREAD_LS)
  6547. wc_ecc_fp_free(); /* free per thread cache */
  6548. #endif
  6549. WOLFSSL_RETURN_FROM_THREAD(0);
  6550. }
  6551. /* TLS Client for API unit testing - generic */
  6552. static void run_wolfssl_client(void* args)
  6553. {
  6554. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6555. WOLFSSL_CTX* ctx = NULL;
  6556. WOLFSSL* ssl = NULL;
  6557. SOCKET_T sfd = 0;
  6558. char msg[] = "hello wolfssl server!";
  6559. int len = (int) XSTRLEN(msg);
  6560. char input[1024];
  6561. int ret, err = 0;
  6562. ((func_args*)args)->return_code = TEST_FAIL;
  6563. /* set defaults */
  6564. if (callbacks->caPemFile == NULL)
  6565. callbacks->caPemFile = caCertFile;
  6566. if (callbacks->certPemFile == NULL)
  6567. callbacks->certPemFile = cliCertFile;
  6568. if (callbacks->keyPemFile == NULL)
  6569. callbacks->keyPemFile = cliKeyFile;
  6570. #ifdef WOLFSSL_STATIC_MEMORY
  6571. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6572. callbacks->memSz > 0) {
  6573. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6574. callbacks->mem, callbacks->memSz, 0, 1);
  6575. if (ret != WOLFSSL_SUCCESS) {
  6576. fprintf(stderr, "CTX static new failed %d\n", ret);
  6577. goto cleanup;
  6578. }
  6579. }
  6580. #else
  6581. ctx = wolfSSL_CTX_new(callbacks->method());
  6582. #endif
  6583. if (ctx == NULL) {
  6584. fprintf(stderr, "CTX new failed\n");
  6585. goto cleanup;
  6586. }
  6587. #ifdef WOLFSSL_TIRTOS
  6588. fdOpenSession(Task_self());
  6589. #endif
  6590. if (!callbacks->loadToSSL) {
  6591. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6592. }
  6593. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6594. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6595. #endif
  6596. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6597. WOLFSSL_SUCCESS) {
  6598. goto cleanup;
  6599. }
  6600. if (!callbacks->loadToSSL) {
  6601. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6602. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6603. goto cleanup;
  6604. }
  6605. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6606. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6607. goto cleanup;
  6608. }
  6609. }
  6610. #ifdef HAVE_CRL
  6611. if (callbacks->crlPemFile != NULL) {
  6612. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6613. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6614. goto cleanup;
  6615. }
  6616. }
  6617. #endif
  6618. if (callbacks->ctx_ready)
  6619. callbacks->ctx_ready(ctx);
  6620. ssl = wolfSSL_new(ctx);
  6621. if (wolfSSL_dtls(ssl)) {
  6622. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6623. 1, 0, ssl);
  6624. }
  6625. else {
  6626. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6627. 0, 0, ssl);
  6628. }
  6629. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6630. goto cleanup;
  6631. }
  6632. if (callbacks->loadToSSL) {
  6633. wolfSSL_SetDevId(ssl, callbacks->devId);
  6634. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6635. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6636. goto cleanup;
  6637. }
  6638. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6639. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6640. goto cleanup;
  6641. }
  6642. }
  6643. if (callbacks->ssl_ready)
  6644. callbacks->ssl_ready(ssl);
  6645. #ifdef WOLFSSL_ASYNC_CRYPT
  6646. err = 0; /* Reset error */
  6647. #endif
  6648. do {
  6649. #ifdef WOLFSSL_ASYNC_CRYPT
  6650. if (err == WC_PENDING_E) {
  6651. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6652. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6653. }
  6654. #endif
  6655. ret = wolfSSL_connect(ssl);
  6656. err = wolfSSL_get_error(ssl, ret);
  6657. } while (err == WC_PENDING_E);
  6658. if (ret != WOLFSSL_SUCCESS) {
  6659. char buff[WOLFSSL_MAX_ERROR_SZ];
  6660. fprintf(stderr, "error = %d, %s\n", err,
  6661. wolfSSL_ERR_error_string(err, buff));
  6662. /*err_sys("SSL_connect failed");*/
  6663. }
  6664. else {
  6665. #ifdef WOLFSSL_ASYNC_CRYPT
  6666. err = 0; /* Reset error */
  6667. #endif
  6668. do {
  6669. #ifdef WOLFSSL_ASYNC_CRYPT
  6670. if (err == WC_PENDING_E) {
  6671. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6672. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6673. }
  6674. #endif
  6675. ret = wolfSSL_write(ssl, msg, len);
  6676. err = wolfSSL_get_error(ssl, ret);
  6677. } while (err == WC_PENDING_E);
  6678. if (len != ret)
  6679. goto cleanup;
  6680. #ifdef WOLFSSL_ASYNC_CRYPT
  6681. err = 0; /* Reset error */
  6682. #endif
  6683. do {
  6684. #ifdef WOLFSSL_ASYNC_CRYPT
  6685. if (err == WC_PENDING_E) {
  6686. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6687. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6688. }
  6689. #endif
  6690. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6691. err = wolfSSL_get_error(ssl, ret);
  6692. } while (err == WC_PENDING_E);
  6693. if (ret > 0) {
  6694. input[ret] = '\0'; /* null term */
  6695. fprintf(stderr, "Server response: %s\n", input);
  6696. }
  6697. ((func_args*)args)->return_code = TEST_SUCCESS;
  6698. }
  6699. if (callbacks->on_result)
  6700. callbacks->on_result(ssl);
  6701. cleanup:
  6702. wolfSSL_free(ssl);
  6703. wolfSSL_CTX_free(ctx);
  6704. CloseSocket(sfd);
  6705. #ifdef WOLFSSL_TIRTOS
  6706. fdCloseSession(Task_self());
  6707. #endif
  6708. }
  6709. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6710. static int test_wolfSSL_read_write(void)
  6711. {
  6712. /* The unit testing for read and write shall happen simultaneously, since
  6713. * one can't do anything with one without the other. (Except for a failure
  6714. * test case.) This function will call all the others that will set up,
  6715. * execute, and report their test findings.
  6716. *
  6717. * Set up the success case first. This function will become the template
  6718. * for the other tests. This should eventually be renamed
  6719. *
  6720. * The success case isn't interesting, how can this fail?
  6721. * - Do not give the client context a CA certificate. The connect should
  6722. * fail. Do not need server for this?
  6723. * - Using NULL for the ssl object on server. Do not need client for this.
  6724. * - Using NULL for the ssl object on client. Do not need server for this.
  6725. * - Good ssl objects for client and server. Client write() without server
  6726. * read().
  6727. * - Good ssl objects for client and server. Server write() without client
  6728. * read().
  6729. * - Forgetting the password callback?
  6730. */
  6731. tcp_ready ready;
  6732. func_args client_args;
  6733. func_args server_args;
  6734. THREAD_TYPE serverThread;
  6735. EXPECT_DECLS;
  6736. XMEMSET(&client_args, 0, sizeof(func_args));
  6737. XMEMSET(&server_args, 0, sizeof(func_args));
  6738. #ifdef WOLFSSL_TIRTOS
  6739. fdOpenSession(Task_self());
  6740. #endif
  6741. StartTCP();
  6742. InitTcpReady(&ready);
  6743. #if defined(USE_WINDOWS_API)
  6744. /* use RNG to get random port if using windows */
  6745. ready.port = GetRandomPort();
  6746. #endif
  6747. server_args.signal = &ready;
  6748. client_args.signal = &ready;
  6749. start_thread(test_server_nofail, &server_args, &serverThread);
  6750. wait_tcp_ready(&server_args);
  6751. test_client_nofail(&client_args, NULL);
  6752. join_thread(serverThread);
  6753. ExpectTrue(client_args.return_code);
  6754. ExpectTrue(server_args.return_code);
  6755. FreeTcpReady(&ready);
  6756. #ifdef WOLFSSL_TIRTOS
  6757. fdOpenSession(Task_self());
  6758. #endif
  6759. return EXPECT_RESULT();
  6760. }
  6761. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6762. {
  6763. EXPECT_DECLS;
  6764. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6765. !defined(WOLFSSL_NO_TLS12)
  6766. /* The unit test for session resumption by re-using WOLFSSL object.
  6767. * WOLFSSL object is not cleared after first session. It reuse the object
  6768. * for second connection.
  6769. */
  6770. tcp_ready ready;
  6771. func_args client_args;
  6772. func_args server_args;
  6773. THREAD_TYPE serverThread;
  6774. callback_functions client_cbf;
  6775. callback_functions server_cbf;
  6776. XMEMSET(&client_args, 0, sizeof(func_args));
  6777. XMEMSET(&server_args, 0, sizeof(func_args));
  6778. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6779. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6780. #ifdef WOLFSSL_TIRTOS
  6781. fdOpenSession(Task_self());
  6782. #endif
  6783. StartTCP();
  6784. InitTcpReady(&ready);
  6785. #if defined(USE_WINDOWS_API)
  6786. /* use RNG to get random port if using windows */
  6787. ready.port = GetRandomPort();
  6788. #endif
  6789. client_cbf.method = wolfTLSv1_2_client_method;
  6790. server_cbf.method = wolfTLSv1_2_server_method;
  6791. client_args.callbacks = &client_cbf;
  6792. server_args.callbacks = &server_cbf;
  6793. server_args.signal = &ready;
  6794. client_args.signal = &ready;
  6795. /* the var is used for loop number */
  6796. server_args.argc = 2;
  6797. start_thread(test_server_loop, &server_args, &serverThread);
  6798. wait_tcp_ready(&server_args);
  6799. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6800. join_thread(serverThread);
  6801. ExpectTrue(client_args.return_code);
  6802. ExpectTrue(server_args.return_code);
  6803. FreeTcpReady(&ready);
  6804. #ifdef WOLFSSL_TIRTOS
  6805. fdOpenSession(Task_self());
  6806. #endif
  6807. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6808. * !defined(WOLFSSL_TLS13) */
  6809. return EXPECT_RESULT();
  6810. }
  6811. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6812. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6813. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6814. WOLFSSL_CTX* ctx)
  6815. {
  6816. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6817. myVerifyAction = VERIFY_USE_PREVERFIY;
  6818. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6819. return TEST_SUCCESS;
  6820. }
  6821. #endif
  6822. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6823. {
  6824. EXPECT_DECLS;
  6825. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6826. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6827. test_ssl_cbf client_cbf;
  6828. test_ssl_cbf server_cbf;
  6829. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6830. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6831. #ifdef WOLFSSL_TLS13
  6832. client_cbf.method = wolfTLSv1_3_client_method;
  6833. #endif /* WOLFSSL_TLS13 */
  6834. client_cbf.ctx_ready =
  6835. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6836. /* test case 1 verify depth is equal to peer chain */
  6837. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6838. &server_cbf, NULL), TEST_SUCCESS);
  6839. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6840. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6841. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6842. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6843. return EXPECT_RESULT();
  6844. }
  6845. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6846. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6847. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6848. WOLFSSL_CTX* ctx)
  6849. {
  6850. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6851. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  6852. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6853. return TEST_SUCCESS;
  6854. }
  6855. #endif
  6856. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6857. {
  6858. EXPECT_DECLS;
  6859. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6860. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6861. test_ssl_cbf client_cbf;
  6862. test_ssl_cbf server_cbf;
  6863. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6864. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6865. #ifdef WOLFSSL_TLS13
  6866. client_cbf.method = wolfTLSv1_3_client_method;
  6867. #endif /* WOLFSSL_TLS13 */
  6868. client_cbf.ctx_ready =
  6869. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6870. /* test case 2
  6871. * verify depth is zero, number of peer's chain is 2.
  6872. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6873. * callback.
  6874. */
  6875. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6876. &server_cbf, NULL), TEST_SUCCESS);
  6877. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6878. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6879. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6880. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6881. return EXPECT_RESULT();
  6882. }
  6883. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6884. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6885. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6886. WOLFSSL_CTX* ctx)
  6887. {
  6888. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6889. myVerifyAction = VERIFY_USE_PREVERFIY;
  6890. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6891. return TEST_SUCCESS;
  6892. }
  6893. #endif
  6894. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6895. {
  6896. EXPECT_DECLS;
  6897. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6898. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6899. test_ssl_cbf client_cbf;
  6900. test_ssl_cbf server_cbf;
  6901. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6902. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6903. #ifdef WOLFSSL_TLS13
  6904. client_cbf.method = wolfTLSv1_3_client_method;
  6905. #endif /* WOLFSSL_TLS13 */
  6906. client_cbf.ctx_ready =
  6907. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6908. /* test case 3
  6909. * verify depth is zero, number of peer's chain is 2
  6910. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6911. * therefore, handshake becomes failure.
  6912. */
  6913. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6914. &server_cbf, NULL), TEST_FAIL);
  6915. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  6916. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  6917. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  6918. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  6919. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6920. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6921. return EXPECT_RESULT();
  6922. }
  6923. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6924. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6925. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6926. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6927. {
  6928. EXPECT_DECLS;
  6929. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6930. return EXPECT_RESULT();
  6931. }
  6932. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6933. {
  6934. EXPECT_DECLS;
  6935. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6936. return EXPECT_RESULT();
  6937. }
  6938. #endif
  6939. static int test_wolfSSL_CTX_set_cipher_list(void)
  6940. {
  6941. EXPECT_DECLS;
  6942. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6943. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6944. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6945. WOLFSSL_CTX* ctxClient = NULL;
  6946. WOLFSSL* sslClient = NULL;
  6947. test_ssl_cbf client_cbf;
  6948. test_ssl_cbf server_cbf;
  6949. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6950. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6951. server_cbf.method = wolfTLSv1_2_server_method;
  6952. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6953. client_cbf.method = wolfTLSv1_2_client_method;
  6954. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6955. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6956. &server_cbf, NULL), TEST_SUCCESS);
  6957. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6958. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6959. /* check with cipher string that has '+' */
  6960. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6961. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6962. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6963. /* check for the existence of an ECDHE ECDSA cipher suite */
  6964. if (EXPECT_SUCCESS()) {
  6965. int i = 0;
  6966. int found = 0;
  6967. const char* suite;
  6968. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6969. WOLFSSL_CIPHER* current;
  6970. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6971. do {
  6972. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  6973. if (current) {
  6974. suite = wolfSSL_CIPHER_get_name(current);
  6975. if (suite && XSTRSTR(suite, "ECDSA")) {
  6976. found = 1;
  6977. break;
  6978. }
  6979. }
  6980. } while (current);
  6981. ExpectIntEQ(found, 1);
  6982. }
  6983. wolfSSL_free(sslClient);
  6984. wolfSSL_CTX_free(ctxClient);
  6985. #endif
  6986. return EXPECT_RESULT();
  6987. }
  6988. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6989. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6990. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  6991. WOLFSSL* ssl)
  6992. {
  6993. EXPECT_DECLS;
  6994. size_t msg_len;
  6995. (void)ctx;
  6996. /* get_finished test */
  6997. /* 1. get own sent message */
  6998. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6999. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7000. ExpectIntGE(msg_len, 0);
  7001. /* 2. get peer message */
  7002. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7003. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7004. ExpectIntGE(msg_len, 0);
  7005. return EXPECT_RESULT();
  7006. }
  7007. #endif
  7008. static int test_wolfSSL_get_finished(void)
  7009. {
  7010. EXPECT_DECLS;
  7011. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7012. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7013. test_ssl_cbf client_cbf;
  7014. test_ssl_cbf server_cbf;
  7015. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7016. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7017. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7018. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7019. TEST_SUCCESS);
  7020. /* test received msg vs sent msg */
  7021. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7022. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7023. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7024. return EXPECT_RESULT();
  7025. }
  7026. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7027. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7028. !defined(NO_SESSION_CACHE)
  7029. /* Sessions to restore/store */
  7030. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7031. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7032. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7033. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7034. {
  7035. /* Don't store sessions. Lookup is still enabled. */
  7036. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7037. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7038. #ifdef OPENSSL_EXTRA
  7039. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7040. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7041. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7042. #endif
  7043. /* Require both peers to provide certs */
  7044. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7045. }
  7046. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7047. {
  7048. WOLFSSL_SESSION** sess;
  7049. if (wolfSSL_is_server(ssl))
  7050. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7051. else
  7052. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7053. if (*sess == NULL) {
  7054. #ifdef NO_SESSION_CACHE_REF
  7055. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7056. #else
  7057. /* Test for backwards compatibility */
  7058. if (wolfSSL_is_server(ssl)) {
  7059. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7060. }
  7061. else {
  7062. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7063. }
  7064. #endif
  7065. /* Now save the session in the internal store to make it available
  7066. * for lookup. For TLS 1.3, we can't save the session without
  7067. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7068. * session from cache. */
  7069. if (wolfSSL_is_server(ssl)
  7070. #ifndef WOLFSSL_TICKET_HAVE_ID
  7071. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7072. #endif
  7073. )
  7074. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7075. *sess), WOLFSSL_SUCCESS);
  7076. }
  7077. else {
  7078. /* If we have a session retrieved then remaining connections should be
  7079. * resuming on that session */
  7080. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7081. }
  7082. /* Save CTX to be able to decrypt tickets */
  7083. if (wolfSSL_is_server(ssl) &&
  7084. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7085. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7086. = wolfSSL_get_SSL_CTX(ssl));
  7087. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7088. WOLFSSL_SUCCESS);
  7089. }
  7090. #ifdef SESSION_CERTS
  7091. #ifndef WOLFSSL_TICKET_HAVE_ID
  7092. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7093. wolfSSL_session_reused(ssl))
  7094. #endif
  7095. {
  7096. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7097. * for all connections. TLS 1.3 only has tickets so if we don't
  7098. * include the session id in the ticket then the certificates
  7099. * will not be available on resumption. */
  7100. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7101. AssertNotNull(peer);
  7102. wolfSSL_X509_free(peer);
  7103. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7104. #ifdef OPENSSL_EXTRA
  7105. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7106. #endif
  7107. }
  7108. #endif /* SESSION_CERTS */
  7109. }
  7110. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7111. {
  7112. /* Set the session to reuse for the client */
  7113. AssertIntEQ(wolfSSL_set_session(ssl,
  7114. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7115. }
  7116. #endif
  7117. static int test_wolfSSL_CTX_add_session(void)
  7118. {
  7119. EXPECT_DECLS;
  7120. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7121. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7122. !defined(NO_SESSION_CACHE)
  7123. tcp_ready ready;
  7124. func_args client_args;
  7125. func_args server_args;
  7126. THREAD_TYPE serverThread;
  7127. callback_functions client_cb;
  7128. callback_functions server_cb;
  7129. method_provider methods[][2] = {
  7130. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7131. !defined(NO_DES3))
  7132. /* Without AES there are almost no ciphersuites available. This leads
  7133. * to no ciphersuites being available and an error. */
  7134. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7135. #endif
  7136. #ifndef WOLFSSL_NO_TLS12
  7137. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7138. #endif
  7139. /* Needs the default ticket callback since it is tied to the
  7140. * connection context and this makes it easy to carry over the ticket
  7141. * crypto context between connections */
  7142. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7143. defined(HAVE_SESSION_TICKET)
  7144. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7145. #endif
  7146. };
  7147. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7148. size_t i, j;
  7149. for (i = 0; i < methodsLen; i++) {
  7150. /* First run creates a connection while the second+ run will attempt
  7151. * to resume the connection. The trick is that the internal cache
  7152. * is turned off. wolfSSL_CTX_add_session should put the session in
  7153. * the cache anyway. */
  7154. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7155. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7156. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7157. #ifdef NO_SESSION_CACHE_REF
  7158. for (j = 0; j < 4; j++) {
  7159. #else
  7160. /* The session may be overwritten in this case. Do only one resumption
  7161. * to stop this test from failing intermittently. */
  7162. for (j = 0; j < 2; j++) {
  7163. #endif
  7164. #ifdef WOLFSSL_TIRTOS
  7165. fdOpenSession(Task_self());
  7166. #endif
  7167. StartTCP();
  7168. InitTcpReady(&ready);
  7169. XMEMSET(&client_args, 0, sizeof(func_args));
  7170. XMEMSET(&server_args, 0, sizeof(func_args));
  7171. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7172. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7173. client_cb.method = methods[i][0];
  7174. server_cb.method = methods[i][1];
  7175. server_args.signal = &ready;
  7176. server_args.callbacks = &server_cb;
  7177. client_args.signal = &ready;
  7178. client_args.callbacks = &client_cb;
  7179. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7180. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7181. server_cb.isSharedCtx = 1;
  7182. }
  7183. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7184. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7185. if (j != 0)
  7186. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7187. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7188. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7189. server_cb.ticNoInit = 1; /* Use default builtin */
  7190. start_thread(test_server_nofail, &server_args, &serverThread);
  7191. wait_tcp_ready(&server_args);
  7192. test_client_nofail(&client_args, NULL);
  7193. join_thread(serverThread);
  7194. ExpectTrue(client_args.return_code);
  7195. ExpectTrue(server_args.return_code);
  7196. FreeTcpReady(&ready);
  7197. if (EXPECT_FAIL())
  7198. break;
  7199. }
  7200. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7201. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7202. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7203. if (EXPECT_FAIL())
  7204. break;
  7205. }
  7206. #endif
  7207. return EXPECT_RESULT();
  7208. }
  7209. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7210. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7211. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7212. defined(HAVE_SESSION_TICKET) && \
  7213. !defined(TITAN_SESSION_CACHE) && \
  7214. !defined(HUGE_SESSION_CACHE) && \
  7215. !defined(BIG_SESSION_CACHE) && \
  7216. !defined(MEDIUM_SESSION_CACHE)
  7217. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7218. /* Sessions to restore/store */
  7219. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7220. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7221. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7222. static int twcase_new_session_called = 0;
  7223. static int twcase_remove_session_called = 0;
  7224. static int twcase_get_session_called = 0;
  7225. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7226. #define SESSION_CACHE_SIZE 33
  7227. typedef struct {
  7228. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7229. WOLFSSL_SESSION* value;
  7230. } hashTable_entry;
  7231. typedef struct {
  7232. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7233. size_t capacity; /* size of entries */
  7234. size_t length; /* number of items in the hash table */
  7235. wolfSSL_Mutex htLock; /* lock */
  7236. }hashTable;
  7237. static hashTable server_sessionCache;
  7238. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7239. {
  7240. int i;
  7241. unsigned int len;
  7242. (void)ssl;
  7243. /*
  7244. * This example uses a hash table.
  7245. * Steps you should take for a non-demo code:
  7246. * - acquire a lock for the file named according to the session id
  7247. * - open the file
  7248. * - encrypt and write the SSL_SESSION object to the file
  7249. * - release the lock
  7250. *
  7251. * Return:
  7252. * 0: The callback does not wish to hold a reference of the sess
  7253. * 1: The callback wants to hold a reference of the sess. The callback is
  7254. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7255. */
  7256. if (sess == NULL)
  7257. return 0;
  7258. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7259. return 0;
  7260. }
  7261. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7262. if (server_sessionCache.entries[i].value == NULL) {
  7263. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7264. server_sessionCache.entries[i].value = sess;
  7265. server_sessionCache.length++;
  7266. break;
  7267. }
  7268. }
  7269. ++twcase_new_session_called;
  7270. wc_UnLockMutex(&server_sessionCache.htLock);
  7271. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7272. twcase_new_session_called);
  7273. return 1;
  7274. }
  7275. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7276. {
  7277. int i;
  7278. (void)ctx;
  7279. (void)sess;
  7280. if (sess == NULL)
  7281. return;
  7282. /*
  7283. * This example uses a hash table.
  7284. * Steps you should take for a non-demo code:
  7285. * - acquire a lock for the file named according to the session id
  7286. * - remove the file
  7287. * - release the lock
  7288. */
  7289. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7290. return;
  7291. }
  7292. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7293. if (server_sessionCache.entries[i].key != NULL &&
  7294. XMEMCMP(server_sessionCache.entries[i].key,
  7295. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7296. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7297. server_sessionCache.entries[i].value = NULL;
  7298. server_sessionCache.entries[i].key = NULL;
  7299. server_sessionCache.length--;
  7300. break;
  7301. }
  7302. }
  7303. ++twcase_remove_session_called;
  7304. wc_UnLockMutex(&server_sessionCache.htLock);
  7305. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7306. twcase_remove_session_called);
  7307. }
  7308. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7309. const unsigned char *id, int len, int *ref)
  7310. {
  7311. int i;
  7312. (void)ssl;
  7313. (void)id;
  7314. (void)len;
  7315. /*
  7316. * This example uses a hash table.
  7317. * Steps you should take for a non-demo code:
  7318. * - acquire a lock for the file named according to the session id in the
  7319. * 2nd arg
  7320. * - read and decrypt contents of file and create a new SSL_SESSION
  7321. * - object release the lock
  7322. * - return the new session object
  7323. */
  7324. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7325. ++twcase_get_session_called);
  7326. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7327. * be responsible for the pointer then set to 0. */
  7328. *ref = 1;
  7329. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7330. if (server_sessionCache.entries[i].key != NULL &&
  7331. XMEMCMP(server_sessionCache.entries[i].key, id,
  7332. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7333. return server_sessionCache.entries[i].value;
  7334. }
  7335. }
  7336. return NULL;
  7337. }
  7338. static int twcase_get_sessionCb_cleanup(void)
  7339. {
  7340. int i;
  7341. int cnt = 0;
  7342. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7343. * for freeing sessions */
  7344. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7345. if (server_sessionCache.entries[i].value != NULL) {
  7346. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7347. cnt++;
  7348. }
  7349. }
  7350. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7351. cnt);
  7352. return TEST_SUCCESS;
  7353. }
  7354. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7355. {
  7356. EXPECT_DECLS;
  7357. /* off - Disable internal cache */
  7358. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7359. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7360. #ifdef OPENSSL_EXTRA
  7361. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7362. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7363. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7364. #endif
  7365. /* off - Do not setup external cache */
  7366. /* Require both peers to provide certs */
  7367. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7368. return EXPECT_RESULT();
  7369. }
  7370. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7371. {
  7372. /* on - internal cache is on by default */
  7373. /* off - Do not setup external cache */
  7374. /* Require both peers to provide certs */
  7375. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7376. return TEST_SUCCESS;
  7377. }
  7378. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7379. {
  7380. EXPECT_DECLS;
  7381. /* off - Disable internal cache */
  7382. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7383. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7384. #ifdef OPENSSL_EXTRA
  7385. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7386. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7387. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7388. #endif
  7389. /* on - Enable external cache */
  7390. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7391. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7392. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7393. /* Require both peers to provide certs */
  7394. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7395. return EXPECT_RESULT();
  7396. }
  7397. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7398. {
  7399. /* on - internal cache is on by default */
  7400. /* on - Enable external cache */
  7401. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7402. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7403. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7404. /* Require both peers to provide certs */
  7405. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7406. return TEST_SUCCESS;
  7407. }
  7408. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7409. {
  7410. /* on - internal cache is on by default */
  7411. /* on - Enable external cache */
  7412. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7413. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7414. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7415. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7416. /* Require both peers to provide certs */
  7417. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7418. return TEST_SUCCESS;
  7419. }
  7420. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7421. {
  7422. EXPECT_DECLS;
  7423. WOLFSSL_SESSION** sess;
  7424. if (wolfSSL_is_server(ssl))
  7425. sess = &twcase_server_first_session_ptr;
  7426. else
  7427. return TEST_SUCCESS;
  7428. if (*sess == NULL) {
  7429. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7430. /* Now save the session in the internal store to make it available
  7431. * for lookup. For TLS 1.3, we can't save the session without
  7432. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7433. * session from cache. */
  7434. if (wolfSSL_is_server(ssl)
  7435. #ifndef WOLFSSL_TICKET_HAVE_ID
  7436. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7437. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7438. #endif
  7439. ) {
  7440. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7441. *sess), WOLFSSL_SUCCESS);
  7442. }
  7443. }
  7444. /* Save CTX to be able to decrypt tickets */
  7445. if (twcase_server_current_ctx_ptr == NULL) {
  7446. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7447. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7448. WOLFSSL_SUCCESS);
  7449. }
  7450. #ifdef SESSION_CERTS
  7451. #ifndef WOLFSSL_TICKET_HAVE_ID
  7452. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7453. wolfSSL_session_reused(ssl))
  7454. #endif
  7455. {
  7456. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7457. * for all connections. TLS 1.3 only has tickets so if we don't
  7458. * include the session id in the ticket then the certificates
  7459. * will not be available on resumption. */
  7460. WOLFSSL_X509* peer = NULL;
  7461. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7462. wolfSSL_X509_free(peer);
  7463. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7464. }
  7465. #endif
  7466. return EXPECT_RESULT();
  7467. }
  7468. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7469. {
  7470. EXPECT_DECLS;
  7471. WOLFSSL_SESSION** sess;
  7472. sess = &twcase_client_first_session_ptr;
  7473. if (*sess == NULL) {
  7474. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7475. }
  7476. else {
  7477. /* If we have a session retrieved then remaining connections should be
  7478. * resuming on that session */
  7479. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7480. }
  7481. #ifdef SESSION_CERTS
  7482. #ifndef WOLFSSL_TICKET_HAVE_ID
  7483. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7484. wolfSSL_session_reused(ssl))
  7485. #endif
  7486. {
  7487. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7488. ExpectNotNull(peer);
  7489. wolfSSL_X509_free(peer);
  7490. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7491. #ifdef OPENSSL_EXTRA
  7492. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7493. #endif
  7494. }
  7495. #endif
  7496. return EXPECT_RESULT();
  7497. }
  7498. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7499. {
  7500. EXPECT_DECLS;
  7501. /* Set the session to reuse for the client */
  7502. ExpectNotNull(ssl);
  7503. ExpectNotNull(twcase_client_first_session_ptr);
  7504. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7505. WOLFSSL_SUCCESS);
  7506. return EXPECT_RESULT();
  7507. }
  7508. struct test_add_session_ext_params {
  7509. method_provider client_meth;
  7510. method_provider server_meth;
  7511. const char* tls_version;
  7512. };
  7513. static int test_wolfSSL_CTX_add_session_ext(
  7514. struct test_add_session_ext_params* param)
  7515. {
  7516. EXPECT_DECLS;
  7517. /* Test the default 33 sessions */
  7518. int j;
  7519. /* Clear cache before starting */
  7520. wolfSSL_CTX_flush_sessions(NULL, -1);
  7521. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7522. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7523. return BAD_MUTEX_E;
  7524. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7525. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7526. for (j = 0; j < 5; j++) {
  7527. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7528. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7529. test_ssl_cbf client_cb;
  7530. test_ssl_cbf server_cb;
  7531. (void)dtls;
  7532. /* Test five cache configurations */
  7533. twcase_client_first_session_ptr = NULL;
  7534. twcase_server_first_session_ptr = NULL;
  7535. twcase_server_current_ctx_ptr = NULL;
  7536. twcase_new_session_called = 0;
  7537. twcase_remove_session_called = 0;
  7538. twcase_get_session_called = 0;
  7539. /* connection 1 - first connection */
  7540. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7541. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7542. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7543. client_cb.method = param->client_meth;
  7544. server_cb.method = param->server_meth;
  7545. if (dtls)
  7546. client_cb.doUdp = server_cb.doUdp = 1;
  7547. /* Setup internal and external cache */
  7548. switch (j) {
  7549. case 0:
  7550. /* SSL_OP_NO_TICKET stateful ticket case */
  7551. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7552. break;
  7553. case 1:
  7554. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7555. break;
  7556. case 2:
  7557. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7558. break;
  7559. case 3:
  7560. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7561. break;
  7562. case 4:
  7563. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7564. break;
  7565. }
  7566. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7567. /* Add session to internal cache and save SSL session for testing */
  7568. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7569. /* Save client SSL session for testing */
  7570. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7571. server_cb.ticNoInit = 1; /* Use default builtin */
  7572. /* Don't free/release ctx */
  7573. server_cb.ctx = twcase_server_current_ctx_ptr;
  7574. server_cb.isSharedCtx = 1;
  7575. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7576. &server_cb, NULL), TEST_SUCCESS);
  7577. ExpectIntEQ(twcase_get_session_called, 0);
  7578. if (EXPECT_FAIL()) {
  7579. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7580. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7581. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7582. break;
  7583. }
  7584. switch (j) {
  7585. case 0:
  7586. case 1:
  7587. case 2:
  7588. /* cache cannot be searched with out a connection */
  7589. /* Add a new session */
  7590. ExpectIntEQ(twcase_new_session_called, 1);
  7591. /* In twcase_server_sess_ctx_pre_shutdown
  7592. * wolfSSL_CTX_add_session which evicts the existing session
  7593. * in cache and adds it back in */
  7594. ExpectIntLE(twcase_remove_session_called, 1);
  7595. break;
  7596. case 3:
  7597. case 4:
  7598. /* no external cache */
  7599. ExpectIntEQ(twcase_new_session_called, 0);
  7600. ExpectIntEQ(twcase_remove_session_called, 0);
  7601. break;
  7602. }
  7603. /* connection 2 - session resume */
  7604. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7605. twcase_new_session_called = 0;
  7606. twcase_remove_session_called = 0;
  7607. twcase_get_session_called = 0;
  7608. server_cb.on_result = 0;
  7609. client_cb.on_result = 0;
  7610. server_cb.ticNoInit = 1; /* Use default builtin */
  7611. server_cb.ctx = twcase_server_current_ctx_ptr;
  7612. /* try session resumption */
  7613. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7614. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7615. &server_cb, NULL), TEST_SUCCESS);
  7616. /* Clear cache before checking */
  7617. wolfSSL_CTX_flush_sessions(NULL, -1);
  7618. switch (j) {
  7619. case 0:
  7620. if (tls13) {
  7621. /* (D)TLSv1.3 stateful case */
  7622. /* cache hit */
  7623. /* DTLS accesses cache once for stateless parsing and
  7624. * once for stateful parsing */
  7625. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7626. /* (D)TLSv1.3 creates a new ticket,
  7627. * updates both internal and external cache */
  7628. ExpectIntEQ(twcase_new_session_called, 1);
  7629. /* A new session ID is created for a new ticket */
  7630. ExpectIntEQ(twcase_remove_session_called, 2);
  7631. }
  7632. else {
  7633. /* non (D)TLSv1.3 case, no update */
  7634. /* DTLS accesses cache once for stateless parsing and
  7635. * once for stateful parsing */
  7636. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7637. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7638. #else
  7639. ExpectIntEQ(twcase_get_session_called, 1);
  7640. #endif
  7641. ExpectIntEQ(twcase_new_session_called, 0);
  7642. /* Called on session added in
  7643. * twcase_server_sess_ctx_pre_shutdown */
  7644. ExpectIntEQ(twcase_remove_session_called, 1);
  7645. }
  7646. break;
  7647. case 1:
  7648. if (tls13) {
  7649. /* (D)TLSv1.3 case */
  7650. /* cache hit */
  7651. ExpectIntEQ(twcase_get_session_called, 1);
  7652. /* (D)TLSv1.3 creates a new ticket,
  7653. * updates both internal and external cache */
  7654. ExpectIntEQ(twcase_new_session_called, 1);
  7655. /* Called on session added in
  7656. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7657. ExpectIntEQ(twcase_remove_session_called, 1);
  7658. }
  7659. else {
  7660. /* non (D)TLSv1.3 case */
  7661. /* cache hit */
  7662. /* DTLS accesses cache once for stateless parsing and
  7663. * once for stateful parsing */
  7664. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7665. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7666. #else
  7667. ExpectIntEQ(twcase_get_session_called, 1);
  7668. #endif
  7669. ExpectIntEQ(twcase_new_session_called, 0);
  7670. /* Called on session added in
  7671. * twcase_server_sess_ctx_pre_shutdown */
  7672. ExpectIntEQ(twcase_remove_session_called, 1);
  7673. }
  7674. break;
  7675. case 2:
  7676. if (tls13) {
  7677. /* (D)TLSv1.3 case */
  7678. /* cache hit */
  7679. ExpectIntEQ(twcase_get_session_called, 1);
  7680. /* (D)TLSv1.3 creates a new ticket,
  7681. * updates both internal and external cache */
  7682. ExpectIntEQ(twcase_new_session_called, 1);
  7683. /* Called on session added in
  7684. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7685. ExpectIntEQ(twcase_remove_session_called, 1);
  7686. }
  7687. else {
  7688. /* non (D)TLSv1.3 case */
  7689. /* cache hit */
  7690. /* DTLS accesses cache once for stateless parsing and
  7691. * once for stateful parsing */
  7692. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7693. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7694. #else
  7695. ExpectIntEQ(twcase_get_session_called, 1);
  7696. #endif
  7697. ExpectIntEQ(twcase_new_session_called, 0);
  7698. /* Called on session added in
  7699. * twcase_server_sess_ctx_pre_shutdown */
  7700. ExpectIntEQ(twcase_remove_session_called, 1);
  7701. }
  7702. break;
  7703. case 3:
  7704. case 4:
  7705. /* no external cache */
  7706. ExpectIntEQ(twcase_get_session_called, 0);
  7707. ExpectIntEQ(twcase_new_session_called, 0);
  7708. ExpectIntEQ(twcase_remove_session_called, 0);
  7709. break;
  7710. }
  7711. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7712. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7713. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7714. if (EXPECT_FAIL())
  7715. break;
  7716. }
  7717. twcase_get_sessionCb_cleanup();
  7718. XMEMSET(&server_sessionCache.entries, 0,
  7719. sizeof(server_sessionCache.entries));
  7720. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  7721. wc_FreeMutex(&server_sessionCache.htLock);
  7722. return EXPECT_RESULT();
  7723. }
  7724. #endif
  7725. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  7726. {
  7727. EXPECT_DECLS;
  7728. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7729. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7730. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7731. defined(HAVE_SESSION_TICKET) && \
  7732. !defined(TITAN_SESSION_CACHE) && \
  7733. !defined(HUGE_SESSION_CACHE) && \
  7734. !defined(BIG_SESSION_CACHE) && \
  7735. !defined(MEDIUM_SESSION_CACHE)
  7736. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7737. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7738. struct test_add_session_ext_params param[1] = {
  7739. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  7740. };
  7741. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7742. #endif
  7743. #endif
  7744. return EXPECT_RESULT();
  7745. }
  7746. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  7747. {
  7748. EXPECT_DECLS;
  7749. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7750. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7751. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7752. defined(HAVE_SESSION_TICKET) && \
  7753. !defined(TITAN_SESSION_CACHE) && \
  7754. !defined(HUGE_SESSION_CACHE) && \
  7755. !defined(BIG_SESSION_CACHE) && \
  7756. !defined(MEDIUM_SESSION_CACHE)
  7757. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7758. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7759. #ifdef WOLFSSL_DTLS13
  7760. struct test_add_session_ext_params param[1] = {
  7761. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  7762. };
  7763. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7764. #endif
  7765. #endif
  7766. #endif
  7767. return EXPECT_RESULT();
  7768. }
  7769. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  7770. {
  7771. EXPECT_DECLS;
  7772. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7773. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7774. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7775. defined(HAVE_SESSION_TICKET) && \
  7776. !defined(TITAN_SESSION_CACHE) && \
  7777. !defined(HUGE_SESSION_CACHE) && \
  7778. !defined(BIG_SESSION_CACHE) && \
  7779. !defined(MEDIUM_SESSION_CACHE)
  7780. #ifndef WOLFSSL_NO_TLS12
  7781. struct test_add_session_ext_params param[1] = {
  7782. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  7783. };
  7784. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7785. #endif
  7786. #endif
  7787. return EXPECT_RESULT();
  7788. }
  7789. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  7790. {
  7791. EXPECT_DECLS;
  7792. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7793. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7794. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7795. defined(HAVE_SESSION_TICKET) && \
  7796. !defined(TITAN_SESSION_CACHE) && \
  7797. !defined(HUGE_SESSION_CACHE) && \
  7798. !defined(BIG_SESSION_CACHE) && \
  7799. !defined(MEDIUM_SESSION_CACHE)
  7800. #ifndef WOLFSSL_NO_TLS12
  7801. #ifdef WOLFSSL_DTLS
  7802. struct test_add_session_ext_params param[1] = {
  7803. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  7804. };
  7805. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7806. #endif
  7807. #endif
  7808. #endif
  7809. return EXPECT_RESULT();
  7810. }
  7811. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  7812. {
  7813. EXPECT_DECLS;
  7814. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7815. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7816. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7817. defined(HAVE_SESSION_TICKET) && \
  7818. !defined(TITAN_SESSION_CACHE) && \
  7819. !defined(HUGE_SESSION_CACHE) && \
  7820. !defined(BIG_SESSION_CACHE) && \
  7821. !defined(MEDIUM_SESSION_CACHE)
  7822. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7823. !defined(NO_DES3))
  7824. struct test_add_session_ext_params param[1] = {
  7825. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  7826. };
  7827. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7828. #endif
  7829. #endif
  7830. return EXPECT_RESULT();
  7831. }
  7832. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  7833. {
  7834. EXPECT_DECLS;
  7835. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7836. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7837. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7838. defined(HAVE_SESSION_TICKET) && \
  7839. !defined(TITAN_SESSION_CACHE) && \
  7840. !defined(HUGE_SESSION_CACHE) && \
  7841. !defined(BIG_SESSION_CACHE) && \
  7842. !defined(MEDIUM_SESSION_CACHE)
  7843. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7844. !defined(NO_DES3))
  7845. #ifdef WOLFSSL_DTLS
  7846. struct test_add_session_ext_params param[1] = {
  7847. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  7848. };
  7849. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7850. #endif
  7851. #endif
  7852. #endif
  7853. return EXPECT_RESULT();
  7854. }
  7855. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7856. /* canned export of a session using older version 3 */
  7857. static unsigned char version_3[] = {
  7858. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7859. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7860. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7861. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7862. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7863. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7864. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7865. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7866. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7867. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7868. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7869. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7870. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7871. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7872. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7873. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7874. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7875. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7876. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7877. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7878. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7879. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7880. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7881. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7882. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7883. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7884. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7885. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7886. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7887. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7888. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7889. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7890. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7891. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7892. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7893. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7894. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7895. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7896. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7897. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7898. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7900. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7901. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7902. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7903. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7904. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7905. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7906. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7907. 0x2E, 0x31, 0xED, 0x4F
  7908. };
  7909. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7910. static int test_wolfSSL_dtls_export(void)
  7911. {
  7912. EXPECT_DECLS;
  7913. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7914. tcp_ready ready;
  7915. func_args client_args;
  7916. func_args server_args;
  7917. THREAD_TYPE serverThread;
  7918. callback_functions server_cbf;
  7919. callback_functions client_cbf;
  7920. #ifdef WOLFSSL_TIRTOS
  7921. fdOpenSession(Task_self());
  7922. #endif
  7923. InitTcpReady(&ready);
  7924. #if defined(USE_WINDOWS_API)
  7925. /* use RNG to get random port if using windows */
  7926. ready.port = GetRandomPort();
  7927. #endif
  7928. /* set using dtls */
  7929. XMEMSET(&client_args, 0, sizeof(func_args));
  7930. XMEMSET(&server_args, 0, sizeof(func_args));
  7931. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7932. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7933. server_cbf.method = wolfDTLSv1_2_server_method;
  7934. client_cbf.method = wolfDTLSv1_2_client_method;
  7935. server_args.callbacks = &server_cbf;
  7936. client_args.callbacks = &client_cbf;
  7937. server_args.signal = &ready;
  7938. client_args.signal = &ready;
  7939. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7940. wait_tcp_ready(&server_args);
  7941. run_wolfssl_client(&client_args);
  7942. join_thread(serverThread);
  7943. ExpectTrue(client_args.return_code);
  7944. ExpectTrue(server_args.return_code);
  7945. FreeTcpReady(&ready);
  7946. #ifdef WOLFSSL_TIRTOS
  7947. fdOpenSession(Task_self());
  7948. #endif
  7949. if (EXPECT_SUCCESS()) {
  7950. SOCKET_T sockfd = 0;
  7951. WOLFSSL_CTX* ctx = NULL;
  7952. WOLFSSL* ssl = NULL;
  7953. char msg[64] = "hello wolfssl!";
  7954. char reply[1024];
  7955. int msgSz = (int)XSTRLEN(msg);
  7956. byte *session, *window;
  7957. unsigned int sessionSz = 0;
  7958. unsigned int windowSz = 0;
  7959. #ifndef TEST_IPV6
  7960. struct sockaddr_in peerAddr;
  7961. #else
  7962. struct sockaddr_in6 peerAddr;
  7963. #endif /* TEST_IPV6 */
  7964. int i;
  7965. /* Set ctx to DTLS 1.2 */
  7966. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7967. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7968. /* test importing version 3 */
  7969. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7970. /* test importing bad length and bad version */
  7971. version_3[2] += 1;
  7972. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7973. version_3[2] -= 1; version_3[1] = 0XA0;
  7974. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7975. wolfSSL_free(ssl);
  7976. wolfSSL_CTX_free(ctx);
  7977. /* check storing client state after connection and storing window only */
  7978. #ifdef WOLFSSL_TIRTOS
  7979. fdOpenSession(Task_self());
  7980. #endif
  7981. InitTcpReady(&ready);
  7982. #if defined(USE_WINDOWS_API)
  7983. /* use RNG to get random port if using windows */
  7984. ready.port = GetRandomPort();
  7985. #endif
  7986. /* set using dtls */
  7987. XMEMSET(&server_args, 0, sizeof(func_args));
  7988. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7989. server_cbf.method = wolfDTLSv1_2_server_method;
  7990. server_cbf.doUdp = 1;
  7991. server_args.callbacks = &server_cbf;
  7992. server_args.argc = 3; /* set loop_count to 3 */
  7993. server_args.signal = &ready;
  7994. start_thread(test_server_nofail, &server_args, &serverThread);
  7995. wait_tcp_ready(&server_args);
  7996. /* create and connect with client */
  7997. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  7998. ExpectIntEQ(WOLFSSL_SUCCESS,
  7999. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8000. ExpectIntEQ(WOLFSSL_SUCCESS,
  8001. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8002. ExpectIntEQ(WOLFSSL_SUCCESS,
  8003. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8004. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8005. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8006. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8007. /* store server information connected too */
  8008. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8009. #ifndef TEST_IPV6
  8010. peerAddr.sin_family = AF_INET;
  8011. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8012. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8013. #else
  8014. peerAddr.sin6_family = AF_INET6;
  8015. ExpectIntEQ(
  8016. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8017. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8018. #endif
  8019. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8020. WOLFSSL_SUCCESS);
  8021. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8022. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8023. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8024. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8025. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8026. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8027. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8028. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8029. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8030. wolfSSL_free(ssl);
  8031. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8032. /* restore state */
  8033. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8034. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8035. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8036. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8037. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8038. WOLFSSL_SUCCESS);
  8039. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8040. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8041. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8042. wolfSSL_free(ssl);
  8043. }
  8044. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8045. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8046. wolfSSL_CTX_free(ctx);
  8047. fprintf(stderr, "done and waiting for server\n");
  8048. join_thread(serverThread);
  8049. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8050. FreeTcpReady(&ready);
  8051. #ifdef WOLFSSL_TIRTOS
  8052. fdOpenSession(Task_self());
  8053. #endif
  8054. }
  8055. #endif
  8056. return EXPECT_RESULT();
  8057. }
  8058. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8059. #ifdef WOLFSSL_TLS13
  8060. static const byte canned_client_tls13_session[] = {
  8061. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8062. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8063. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8064. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8065. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8066. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8067. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8068. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8069. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8070. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8071. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8072. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8073. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8074. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8075. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8076. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8077. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8078. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8079. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8080. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8081. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8082. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8083. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8084. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8085. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8086. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8087. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8088. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8089. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8090. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8091. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8092. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8093. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8094. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8095. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8096. 0x00, 0x03
  8097. };
  8098. static const byte canned_server_tls13_session[] = {
  8099. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8100. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8101. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8102. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8105. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8106. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8107. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8108. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8109. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8110. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8111. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8112. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8113. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8114. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8115. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8116. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8117. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8118. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8119. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8120. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8121. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8122. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8123. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8124. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8125. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8126. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8128. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8129. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8130. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8131. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8132. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8133. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8134. 0x00, 0x04
  8135. };
  8136. #endif /* WOLFSSL_TLS13 */
  8137. static const byte canned_client_session[] = {
  8138. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8139. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8140. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8141. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8142. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8143. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8145. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8146. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8147. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8148. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8149. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8150. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8151. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8152. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8153. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8154. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8155. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8156. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8157. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8158. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8159. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8160. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8161. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8162. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8163. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8164. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8165. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8166. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8169. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8170. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8171. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8172. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8173. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8174. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8175. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8176. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8177. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8178. 0x00, 0x03
  8179. };
  8180. static const byte canned_server_session[] = {
  8181. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8182. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8183. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8184. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8185. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8188. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8189. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8190. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8191. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8192. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8193. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8194. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8195. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8196. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8197. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8198. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8199. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8200. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8201. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8202. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8203. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8204. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8205. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8206. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8207. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8208. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8209. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8214. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8215. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8216. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8217. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8218. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8219. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8220. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8221. 0x00, 0x04
  8222. };
  8223. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8224. {
  8225. SOCKET_T sockfd = 0;
  8226. SOCKET_T clientfd = 0;
  8227. word16 port;
  8228. callback_functions* cbf;
  8229. WOLFSSL_CTX* ctx = 0;
  8230. WOLFSSL* ssl = 0;
  8231. char msg[] = "I hear you fa shizzle!";
  8232. char input[1024];
  8233. int idx;
  8234. #ifdef WOLFSSL_TIRTOS
  8235. fdOpenSession(Task_self());
  8236. #endif
  8237. ((func_args*)args)->return_code = TEST_FAIL;
  8238. cbf = ((func_args*)args)->callbacks;
  8239. #if defined(USE_WINDOWS_API)
  8240. port = ((func_args*)args)->signal->port;
  8241. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8242. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8243. /* Let tcp_listen assign port */
  8244. port = 0;
  8245. #else
  8246. /* Use default port */
  8247. port = wolfSSLPort;
  8248. #endif
  8249. /* do it here to detect failure */
  8250. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8251. CloseSocket(sockfd);
  8252. {
  8253. WOLFSSL_METHOD* method = NULL;
  8254. if (cbf != NULL && cbf->method != NULL) {
  8255. method = cbf->method();
  8256. }
  8257. else {
  8258. method = wolfTLSv1_2_server_method();
  8259. }
  8260. ctx = wolfSSL_CTX_new(method);
  8261. }
  8262. if (ctx == NULL) {
  8263. goto done;
  8264. }
  8265. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8266. /* call ctx setup callback */
  8267. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8268. cbf->ctx_ready(ctx);
  8269. }
  8270. ssl = wolfSSL_new(ctx);
  8271. if (ssl == NULL) {
  8272. goto done;
  8273. }
  8274. wolfSSL_set_fd(ssl, clientfd);
  8275. /* call ssl setup callback */
  8276. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8277. cbf->ssl_ready(ssl);
  8278. }
  8279. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8280. if (idx > 0) {
  8281. input[idx] = '\0';
  8282. fprintf(stderr, "Client message export/import: %s\n", input);
  8283. }
  8284. else {
  8285. fprintf(stderr, "ret = %d error = %d\n", idx,
  8286. wolfSSL_get_error(ssl, idx));
  8287. goto done;
  8288. }
  8289. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8290. /*err_sys("SSL_write failed");*/
  8291. WOLFSSL_RETURN_FROM_THREAD(0);
  8292. }
  8293. #ifdef WOLFSSL_TIRTOS
  8294. Task_yield();
  8295. #endif
  8296. ((func_args*)args)->return_code = TEST_SUCCESS;
  8297. done:
  8298. wolfSSL_shutdown(ssl);
  8299. wolfSSL_free(ssl);
  8300. wolfSSL_CTX_free(ctx);
  8301. CloseSocket(clientfd);
  8302. #ifdef WOLFSSL_TIRTOS
  8303. fdCloseSession(Task_self());
  8304. #endif
  8305. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8306. && defined(HAVE_THREAD_LS)
  8307. wc_ecc_fp_free(); /* free per thread cache */
  8308. #endif
  8309. #if defined(HAVE_SESSION_TICKET) && \
  8310. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8311. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8312. OpenSSLTicketCleanup();
  8313. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8314. TicketCleanup();
  8315. #endif
  8316. #endif
  8317. WOLFSSL_RETURN_FROM_THREAD(0);
  8318. }
  8319. static void load_tls12_canned_server(WOLFSSL* ssl)
  8320. {
  8321. int clientfd = wolfSSL_get_fd(ssl);
  8322. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8323. sizeof(canned_server_session)), sizeof(canned_server_session));
  8324. wolfSSL_set_fd(ssl, clientfd);
  8325. }
  8326. #ifdef WOLFSSL_TLS13
  8327. static void load_tls13_canned_server(WOLFSSL* ssl)
  8328. {
  8329. int clientfd = wolfSSL_get_fd(ssl);
  8330. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8331. sizeof(canned_server_tls13_session)),
  8332. sizeof(canned_server_tls13_session));
  8333. wolfSSL_set_fd(ssl, clientfd);
  8334. }
  8335. #endif
  8336. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8337. static int test_wolfSSL_tls_export_run(int v)
  8338. {
  8339. EXPECT_DECLS;
  8340. SOCKET_T sockfd = 0;
  8341. WOLFSSL_CTX* ctx = 0;
  8342. WOLFSSL* ssl = 0;
  8343. char msg[64] = "hello wolfssl!";
  8344. char reply[1024];
  8345. word32 replySz;
  8346. int msgSz = (int)XSTRLEN(msg);
  8347. const byte* clientSession = NULL;
  8348. int clientSessionSz = 0;
  8349. tcp_ready ready;
  8350. func_args server_args;
  8351. THREAD_TYPE serverThread;
  8352. callback_functions server_cbf;
  8353. #ifdef WOLFSSL_TIRTOS
  8354. fdOpenSession(Task_self());
  8355. #endif
  8356. InitTcpReady(&ready);
  8357. #if defined(USE_WINDOWS_API)
  8358. /* use RNG to get random port if using windows */
  8359. ready.port = GetRandomPort();
  8360. #endif
  8361. XMEMSET(&server_args, 0, sizeof(func_args));
  8362. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8363. switch (v) {
  8364. case WOLFSSL_TLSV1_2:
  8365. server_cbf.method = wolfTLSv1_2_server_method;
  8366. server_cbf.ssl_ready = load_tls12_canned_server;
  8367. /* setup the client side */
  8368. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8369. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8370. clientSession = canned_client_session;
  8371. clientSessionSz = sizeof(canned_client_session);
  8372. break;
  8373. #ifdef WOLFSSL_TLS13
  8374. case WOLFSSL_TLSV1_3:
  8375. server_cbf.method = wolfTLSv1_3_server_method;
  8376. server_cbf.ssl_ready = load_tls13_canned_server;
  8377. /* setup the client side */
  8378. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8379. clientSession = canned_client_tls13_session;
  8380. clientSessionSz = sizeof(canned_client_tls13_session);
  8381. break;
  8382. #endif
  8383. }
  8384. server_args.callbacks = &server_cbf;
  8385. server_args.signal = &ready;
  8386. start_thread(tls_export_server, &server_args, &serverThread);
  8387. wait_tcp_ready(&server_args);
  8388. #ifdef WOLFSSL_TIRTOS
  8389. fdOpenSession(Task_self());
  8390. #endif
  8391. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8392. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8393. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8394. clientSessionSz);
  8395. replySz = sizeof(reply);
  8396. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8397. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8398. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8399. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8400. #endif
  8401. wolfSSL_set_fd(ssl, sockfd);
  8402. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8403. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8404. wolfSSL_free(ssl);
  8405. wolfSSL_CTX_free(ctx);
  8406. CloseSocket(sockfd);
  8407. #ifdef WOLFSSL_TIRTOS
  8408. fdCloseSession(Task_self());
  8409. #endif
  8410. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8411. && defined(HAVE_THREAD_LS)
  8412. wc_ecc_fp_free(); /* free per thread cache */
  8413. #endif
  8414. join_thread(serverThread);
  8415. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8416. FreeTcpReady(&ready);
  8417. #ifdef WOLFSSL_TIRTOS
  8418. fdOpenSession(Task_self());
  8419. #endif
  8420. return EXPECT_RESULT();
  8421. }
  8422. #endif
  8423. static int test_wolfSSL_tls_export(void)
  8424. {
  8425. int res = TEST_SKIPPED;
  8426. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8427. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8428. #ifdef WOLFSSL_TLS13
  8429. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8430. #endif
  8431. res = TEST_RES_CHECK(1);
  8432. #endif
  8433. return res;
  8434. }
  8435. /*----------------------------------------------------------------------------*
  8436. | TLS extensions tests
  8437. *----------------------------------------------------------------------------*/
  8438. #ifdef ENABLE_TLS_CALLBACK_TEST
  8439. /* Connection test runner - generic */
  8440. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8441. callback_functions* server_callbacks)
  8442. {
  8443. tcp_ready ready;
  8444. func_args client_args;
  8445. func_args server_args;
  8446. THREAD_TYPE serverThread;
  8447. XMEMSET(&client_args, 0, sizeof(func_args));
  8448. XMEMSET(&server_args, 0, sizeof(func_args));
  8449. StartTCP();
  8450. client_args.callbacks = client_callbacks;
  8451. server_args.callbacks = server_callbacks;
  8452. #ifdef WOLFSSL_TIRTOS
  8453. fdOpenSession(Task_self());
  8454. #endif
  8455. /* RUN Server side */
  8456. InitTcpReady(&ready);
  8457. #if defined(USE_WINDOWS_API)
  8458. /* use RNG to get random port if using windows */
  8459. ready.port = GetRandomPort();
  8460. #endif
  8461. server_args.signal = &ready;
  8462. client_args.signal = &ready;
  8463. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8464. wait_tcp_ready(&server_args);
  8465. /* RUN Client side */
  8466. run_wolfssl_client(&client_args);
  8467. join_thread(serverThread);
  8468. FreeTcpReady(&ready);
  8469. #ifdef WOLFSSL_TIRTOS
  8470. fdCloseSession(Task_self());
  8471. #endif
  8472. client_callbacks->return_code = client_args.return_code;
  8473. server_callbacks->return_code = server_args.return_code;
  8474. }
  8475. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8476. #ifdef HAVE_SNI
  8477. static int test_wolfSSL_UseSNI_params(void)
  8478. {
  8479. EXPECT_DECLS;
  8480. #if !defined(NO_WOLFSSL_CLIENT)
  8481. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8482. WOLFSSL *ssl = wolfSSL_new(ctx);
  8483. ExpectNotNull(ctx);
  8484. ExpectNotNull(ssl);
  8485. /* invalid [ctx|ssl] */
  8486. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8487. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8488. /* invalid type */
  8489. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8490. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8491. /* invalid data */
  8492. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8493. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8494. /* success case */
  8495. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8496. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8497. wolfSSL_free(ssl);
  8498. wolfSSL_CTX_free(ctx);
  8499. #endif /* !NO_WOLFSSL_CLIENT */
  8500. return EXPECT_RESULT();
  8501. }
  8502. /* BEGIN of connection tests callbacks */
  8503. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8504. {
  8505. AssertIntEQ(WOLFSSL_SUCCESS,
  8506. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8507. }
  8508. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8509. {
  8510. AssertIntEQ(WOLFSSL_SUCCESS,
  8511. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8512. }
  8513. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8514. {
  8515. AssertIntEQ(WOLFSSL_SUCCESS,
  8516. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8517. }
  8518. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8519. {
  8520. use_SNI_at_ssl(ssl);
  8521. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8522. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8523. }
  8524. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8525. {
  8526. use_SNI_at_ssl(ssl);
  8527. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8528. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8529. }
  8530. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8531. {
  8532. use_SNI_at_ctx(ctx);
  8533. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8534. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8535. }
  8536. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8537. {
  8538. use_SNI_at_ssl(ssl);
  8539. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8540. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8541. }
  8542. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8543. {
  8544. use_SNI_at_ctx(ctx);
  8545. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8546. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8547. }
  8548. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8549. {
  8550. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8551. }
  8552. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8553. {
  8554. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8555. }
  8556. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8557. {
  8558. byte type = WOLFSSL_SNI_HOST_NAME;
  8559. void* request = (void*) &type; /* to be overwritten */
  8560. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8561. AssertNotNull(request);
  8562. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8563. AssertNull(request);
  8564. }
  8565. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8566. {
  8567. byte type = WOLFSSL_SNI_HOST_NAME;
  8568. void* request = NULL;
  8569. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8570. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8571. AssertNotNull(request);
  8572. AssertStrEQ("www.wolfssl.com", (char*)request);
  8573. }
  8574. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8575. {
  8576. byte type = WOLFSSL_SNI_HOST_NAME;
  8577. void* request = NULL;
  8578. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8579. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8580. AssertNotNull(request);
  8581. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8582. }
  8583. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8584. {
  8585. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8586. }
  8587. /* END of connection tests callbacks */
  8588. static int test_wolfSSL_UseSNI_connection(void)
  8589. {
  8590. int res = TEST_SKIPPED;
  8591. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8592. callback_functions client_cb;
  8593. callback_functions server_cb;
  8594. size_t i;
  8595. #ifdef WOLFSSL_STATIC_MEMORY
  8596. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8597. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8598. #endif
  8599. struct {
  8600. method_provider client_meth;
  8601. method_provider server_meth;
  8602. #ifdef WOLFSSL_STATIC_MEMORY
  8603. wolfSSL_method_func client_meth_ex;
  8604. wolfSSL_method_func server_meth_ex;
  8605. #endif
  8606. } methods[] = {
  8607. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8608. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8609. #ifdef WOLFSSL_STATIC_MEMORY
  8610. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8611. #endif
  8612. },
  8613. #endif
  8614. #ifndef WOLFSSL_NO_TLS12
  8615. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8616. #ifdef WOLFSSL_STATIC_MEMORY
  8617. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8618. #endif
  8619. },
  8620. #endif
  8621. #ifdef WOLFSSL_TLS13
  8622. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  8623. #ifdef WOLFSSL_STATIC_MEMORY
  8624. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  8625. #endif
  8626. },
  8627. #endif
  8628. };
  8629. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  8630. for (i = 0; i < methodsSz; i++) {
  8631. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8632. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8633. client_cb.method = methods[i].client_meth;
  8634. server_cb.method = methods[i].server_meth;
  8635. client_cb.devId = testDevId;
  8636. server_cb.devId = testDevId;
  8637. #ifdef WOLFSSL_STATIC_MEMORY
  8638. client_cb.method_ex = methods[i].client_meth_ex;
  8639. server_cb.method_ex = methods[i].server_meth_ex;
  8640. client_cb.mem = cliMem;
  8641. client_cb.memSz = (word32)sizeof(cliMem);
  8642. server_cb.mem = svrMem;
  8643. server_cb.memSz = (word32)sizeof(svrMem);;
  8644. #endif
  8645. /* success case at ctx */
  8646. fprintf(stderr, "\n\tsuccess case at ctx\n");
  8647. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8648. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8649. test_wolfSSL_client_server(&client_cb, &server_cb);
  8650. /* success case at ssl */
  8651. fprintf(stderr, "\tsuccess case at ssl\n");
  8652. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8653. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8654. test_wolfSSL_client_server(&client_cb, &server_cb);
  8655. /* default mismatch behavior */
  8656. fprintf(stderr, "\tdefault mismatch behavior\n");
  8657. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8658. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8659. test_wolfSSL_client_server(&client_cb, &server_cb);
  8660. /* continue on mismatch */
  8661. fprintf(stderr, "\tcontinue on mismatch\n");
  8662. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8663. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8664. test_wolfSSL_client_server(&client_cb, &server_cb);
  8665. /* fake answer on mismatch */
  8666. fprintf(stderr, "\tfake answer on mismatch\n");
  8667. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8668. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8669. test_wolfSSL_client_server(&client_cb, &server_cb);
  8670. /* sni abort - success */
  8671. fprintf(stderr, "\tsni abort - success\n");
  8672. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8673. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8674. test_wolfSSL_client_server(&client_cb, &server_cb);
  8675. /* sni abort - abort when absent (ctx) */
  8676. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  8677. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8678. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8679. test_wolfSSL_client_server(&client_cb, &server_cb);
  8680. /* sni abort - abort when absent (ssl) */
  8681. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  8682. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8683. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8684. test_wolfSSL_client_server(&client_cb, &server_cb);
  8685. /* sni abort - success when overwritten */
  8686. fprintf(stderr, "\tsni abort - success when overwritten\n");
  8687. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8688. 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;
  8689. test_wolfSSL_client_server(&client_cb, &server_cb);
  8690. /* sni abort - success when allowing mismatches */
  8691. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  8692. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8693. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8694. test_wolfSSL_client_server(&client_cb, &server_cb);
  8695. }
  8696. res = TEST_RES_CHECK(1);
  8697. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8698. return res;
  8699. }
  8700. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8701. {
  8702. EXPECT_DECLS;
  8703. byte buff[] = { /* www.paypal.com */
  8704. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8705. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8706. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8707. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8708. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8709. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8710. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8711. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8712. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8713. };
  8714. byte buff2[] = { /* api.textmate.org */
  8715. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8716. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8717. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8718. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8719. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8720. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8721. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8722. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8723. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8724. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8725. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8726. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8727. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8728. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8729. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8730. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8731. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8732. };
  8733. byte buff3[] = { /* no sni extension */
  8734. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8735. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8736. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8737. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8738. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8739. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8740. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8741. };
  8742. byte buff4[] = { /* last extension has zero size */
  8743. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8744. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8745. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8746. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8747. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8748. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8749. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8750. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8751. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8752. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8753. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8754. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8755. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8756. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8757. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8758. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8759. 0x12, 0x00, 0x00
  8760. };
  8761. byte buff5[] = { /* SSL v2.0 client hello */
  8762. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8763. /* dummy bytes below, just to pass size check */
  8764. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8765. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8766. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8767. };
  8768. byte result[32] = {0};
  8769. word32 length = 32;
  8770. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8771. 0, result, &length));
  8772. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8773. 0, result, &length));
  8774. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8775. 1, result, &length));
  8776. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8777. 0, result, &length));
  8778. buff[0] = 0x16;
  8779. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8780. 0, result, &length));
  8781. buff[1] = 0x03;
  8782. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8783. sizeof(buff), 0, result, &length));
  8784. buff[2] = 0x03;
  8785. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8786. sizeof(buff), 0, result, &length));
  8787. buff[4] = 0x64;
  8788. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8789. 0, result, &length));
  8790. if (EXPECT_SUCCESS())
  8791. result[length] = 0;
  8792. ExpectStrEQ("www.paypal.com", (const char*) result);
  8793. length = 32;
  8794. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8795. 0, result, &length));
  8796. if (EXPECT_SUCCESS())
  8797. result[length] = 0;
  8798. ExpectStrEQ("api.textmate.org", (const char*) result);
  8799. /* SSL v2.0 tests */
  8800. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8801. sizeof(buff5), 0, result, &length));
  8802. buff5[2] = 0x02;
  8803. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8804. sizeof(buff5), 0, result, &length));
  8805. buff5[2] = 0x01; buff5[6] = 0x08;
  8806. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8807. sizeof(buff5), 0, result, &length));
  8808. buff5[6] = 0x09; buff5[8] = 0x01;
  8809. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8810. sizeof(buff5), 0, result, &length));
  8811. return EXPECT_RESULT();
  8812. }
  8813. #endif /* HAVE_SNI */
  8814. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8815. static int test_wolfSSL_UseTrustedCA(void)
  8816. {
  8817. EXPECT_DECLS;
  8818. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8819. && !defined(NO_RSA)
  8820. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8821. WOLFSSL_CTX *ctx = NULL;
  8822. WOLFSSL *ssl = NULL;
  8823. byte id[20];
  8824. #ifndef NO_WOLFSSL_SERVER
  8825. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8826. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8827. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8828. #else
  8829. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8830. #endif
  8831. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8832. XMEMSET(id, 0, sizeof(id));
  8833. /* error cases */
  8834. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8835. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8836. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8837. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8838. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8839. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8840. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8841. #ifdef NO_SHA
  8842. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8843. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8844. #endif
  8845. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8846. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8847. /* success cases */
  8848. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8849. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8850. #ifndef NO_SHA
  8851. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8852. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8853. #endif
  8854. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8855. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8856. wolfSSL_free(ssl);
  8857. wolfSSL_CTX_free(ctx);
  8858. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8859. #endif /* HAVE_TRUSTED_CA */
  8860. return EXPECT_RESULT();
  8861. }
  8862. static int test_wolfSSL_UseMaxFragment(void)
  8863. {
  8864. EXPECT_DECLS;
  8865. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8866. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8867. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8868. #ifndef NO_WOLFSSL_SERVER
  8869. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8870. #else
  8871. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8872. #endif
  8873. WOLFSSL *ssl = NULL;
  8874. #ifdef OPENSSL_EXTRA
  8875. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8876. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  8877. #else
  8878. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  8879. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  8880. #endif
  8881. #ifndef NO_WOLFSSL_SERVER
  8882. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8883. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8884. #endif
  8885. ExpectNotNull(ctx);
  8886. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8887. #ifdef OPENSSL_EXTRA
  8888. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  8889. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  8890. #else
  8891. UseMaxFragment = wolfSSL_UseMaxFragment;
  8892. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  8893. #endif
  8894. /* error cases */
  8895. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  8896. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  8897. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  8898. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  8899. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  8900. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  8901. /* success case */
  8902. #ifdef OPENSSL_EXTRA
  8903. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8904. #else
  8905. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8906. #endif
  8907. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  8908. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  8909. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  8910. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  8911. #ifdef OPENSSL_EXTRA
  8912. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8913. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8914. #else
  8915. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8916. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8917. #endif
  8918. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  8919. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  8920. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  8921. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  8922. #ifdef OPENSSL_EXTRA
  8923. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8924. #else
  8925. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8926. #endif
  8927. wolfSSL_free(ssl);
  8928. wolfSSL_CTX_free(ctx);
  8929. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8930. #endif
  8931. return EXPECT_RESULT();
  8932. }
  8933. static int test_wolfSSL_UseTruncatedHMAC(void)
  8934. {
  8935. EXPECT_DECLS;
  8936. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  8937. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8938. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8939. #ifndef NO_WOLFSSL_SERVER
  8940. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8941. #else
  8942. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8943. #endif
  8944. WOLFSSL *ssl = NULL;
  8945. ExpectNotNull(ctx);
  8946. #ifndef NO_WOLFSSL_SERVER
  8947. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8948. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8949. #endif
  8950. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8951. /* error cases */
  8952. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  8953. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  8954. /* success case */
  8955. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  8956. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  8957. wolfSSL_free(ssl);
  8958. wolfSSL_CTX_free(ctx);
  8959. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8960. #endif
  8961. return EXPECT_RESULT();
  8962. }
  8963. static int test_wolfSSL_UseSupportedCurve(void)
  8964. {
  8965. EXPECT_DECLS;
  8966. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  8967. !defined(NO_TLS)
  8968. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8969. WOLFSSL *ssl = wolfSSL_new(ctx);
  8970. ExpectNotNull(ctx);
  8971. ExpectNotNull(ssl);
  8972. /* error cases */
  8973. ExpectIntNE(WOLFSSL_SUCCESS,
  8974. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8975. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  8976. ExpectIntNE(WOLFSSL_SUCCESS,
  8977. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8978. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  8979. /* success case */
  8980. ExpectIntEQ(WOLFSSL_SUCCESS,
  8981. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  8982. ExpectIntEQ(WOLFSSL_SUCCESS,
  8983. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  8984. wolfSSL_free(ssl);
  8985. wolfSSL_CTX_free(ctx);
  8986. #endif
  8987. return EXPECT_RESULT();
  8988. }
  8989. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  8990. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8991. {
  8992. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  8993. }
  8994. static void use_ALPN_all(WOLFSSL* ssl)
  8995. {
  8996. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8997. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8998. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8999. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9000. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9001. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9002. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9003. }
  9004. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9005. {
  9006. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9007. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9008. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9009. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9010. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9011. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9012. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9013. }
  9014. static void use_ALPN_one(WOLFSSL* ssl)
  9015. {
  9016. /* spdy/2 */
  9017. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9018. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9019. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9020. }
  9021. static void use_ALPN_unknown(WOLFSSL* ssl)
  9022. {
  9023. /* http/2.0 */
  9024. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9025. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9026. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9027. }
  9028. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9029. {
  9030. /* http/2.0 */
  9031. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9032. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9033. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9034. }
  9035. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9036. {
  9037. /* spdy/3 */
  9038. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9039. char *proto = NULL;
  9040. word16 protoSz = 0;
  9041. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9042. /* check value */
  9043. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9044. if (proto) {
  9045. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9046. }
  9047. }
  9048. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9049. {
  9050. char *proto = NULL;
  9051. word16 protoSz = 0;
  9052. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9053. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9054. /* check value */
  9055. AssertIntEQ(1, (0 == protoSz));
  9056. AssertIntEQ(1, (NULL == proto));
  9057. }
  9058. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9059. {
  9060. /* http/1.1 */
  9061. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9062. char *proto;
  9063. word16 protoSz = 0;
  9064. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9065. /* check value */
  9066. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9067. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9068. }
  9069. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9070. {
  9071. /* spdy/2 */
  9072. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9073. char *proto;
  9074. word16 protoSz = 0;
  9075. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9076. /* check value */
  9077. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9078. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9079. }
  9080. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9081. {
  9082. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9083. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9084. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9085. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9086. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9087. char *clist = NULL;
  9088. word16 clistSz = 0;
  9089. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9090. &clistSz));
  9091. /* check value */
  9092. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9093. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9094. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9095. }
  9096. static int test_wolfSSL_UseALPN_connection(void)
  9097. {
  9098. int res = TEST_SKIPPED;
  9099. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9100. callback_functions client_cb;
  9101. callback_functions server_cb;
  9102. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9103. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9104. client_cb.method = wolfSSLv23_client_method;
  9105. server_cb.method = wolfSSLv23_server_method;
  9106. client_cb.devId = testDevId;
  9107. server_cb.devId = testDevId;
  9108. /* success case same list */
  9109. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9110. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9111. test_wolfSSL_client_server(&client_cb, &server_cb);
  9112. /* success case only one for server */
  9113. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9114. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9115. test_wolfSSL_client_server(&client_cb, &server_cb);
  9116. /* success case only one for client */
  9117. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9118. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9119. test_wolfSSL_client_server(&client_cb, &server_cb);
  9120. /* success case none for client */
  9121. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9122. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9123. test_wolfSSL_client_server(&client_cb, &server_cb);
  9124. /* success case mismatch behavior but option 'continue' set */
  9125. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9126. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9127. test_wolfSSL_client_server(&client_cb, &server_cb);
  9128. /* success case read protocol send by client */
  9129. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9130. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9131. test_wolfSSL_client_server(&client_cb, &server_cb);
  9132. /* mismatch behavior with same list
  9133. * the first and only this one must be taken */
  9134. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9135. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9136. test_wolfSSL_client_server(&client_cb, &server_cb);
  9137. /* default mismatch behavior */
  9138. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9139. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9140. test_wolfSSL_client_server(&client_cb, &server_cb);
  9141. res = TEST_RES_CHECK(1);
  9142. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9143. return res;
  9144. }
  9145. static int test_wolfSSL_UseALPN_params(void)
  9146. {
  9147. EXPECT_DECLS;
  9148. #ifndef NO_WOLFSSL_CLIENT
  9149. /* "http/1.1" */
  9150. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9151. /* "spdy/1" */
  9152. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9153. /* "spdy/2" */
  9154. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9155. /* "spdy/3" */
  9156. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9157. char buff[256];
  9158. word32 idx;
  9159. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9160. WOLFSSL *ssl = wolfSSL_new(ctx);
  9161. ExpectNotNull(ctx);
  9162. ExpectNotNull(ssl);
  9163. /* error cases */
  9164. ExpectIntNE(WOLFSSL_SUCCESS,
  9165. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9166. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9167. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9168. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9169. /* success case */
  9170. /* http1 only */
  9171. ExpectIntEQ(WOLFSSL_SUCCESS,
  9172. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9173. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9174. /* http1, spdy1 */
  9175. XMEMCPY(buff, http1, sizeof(http1));
  9176. idx = sizeof(http1);
  9177. buff[idx++] = ',';
  9178. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9179. idx += sizeof(spdy1);
  9180. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9181. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9182. /* http1, spdy2, spdy1 */
  9183. XMEMCPY(buff, http1, sizeof(http1));
  9184. idx = sizeof(http1);
  9185. buff[idx++] = ',';
  9186. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9187. idx += sizeof(spdy2);
  9188. buff[idx++] = ',';
  9189. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9190. idx += sizeof(spdy1);
  9191. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9192. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9193. /* spdy3, http1, spdy2, spdy1 */
  9194. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9195. idx = sizeof(spdy3);
  9196. buff[idx++] = ',';
  9197. XMEMCPY(buff+idx, http1, sizeof(http1));
  9198. idx += sizeof(http1);
  9199. buff[idx++] = ',';
  9200. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9201. idx += sizeof(spdy2);
  9202. buff[idx++] = ',';
  9203. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9204. idx += sizeof(spdy1);
  9205. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9206. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9207. wolfSSL_free(ssl);
  9208. wolfSSL_CTX_free(ctx);
  9209. #endif
  9210. return EXPECT_RESULT();
  9211. }
  9212. #endif /* HAVE_ALPN */
  9213. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9214. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9215. {
  9216. unsigned char p[] = {
  9217. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9218. 6, 's', 'p', 'd', 'y', '/', '2',
  9219. 6, 's', 'p', 'd', 'y', '/', '1',
  9220. };
  9221. unsigned char p_len = sizeof(p);
  9222. int ret;
  9223. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9224. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9225. AssertIntEQ(ret, 0);
  9226. #else
  9227. AssertIntEQ(ret, SSL_SUCCESS);
  9228. #endif
  9229. }
  9230. static void set_alpn_protos(SSL* ssl)
  9231. {
  9232. unsigned char p[] = {
  9233. 6, 's', 'p', 'd', 'y', '/', '3',
  9234. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9235. 6, 's', 'p', 'd', 'y', '/', '2',
  9236. 6, 's', 'p', 'd', 'y', '/', '1',
  9237. };
  9238. unsigned char p_len = sizeof(p);
  9239. int ret;
  9240. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9241. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9242. AssertIntEQ(ret, 0);
  9243. #else
  9244. AssertIntEQ(ret, SSL_SUCCESS);
  9245. #endif
  9246. }
  9247. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9248. {
  9249. /* "spdy/3" */
  9250. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9251. const unsigned char *proto;
  9252. unsigned int protoSz = 0;
  9253. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9254. /* check value */
  9255. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9256. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9257. }
  9258. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9259. {
  9260. /* "http/1.1" */
  9261. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9262. const unsigned char *proto;
  9263. unsigned int protoSz = 0;
  9264. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9265. /* check value */
  9266. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9267. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9268. }
  9269. static int test_wolfSSL_set_alpn_protos(void)
  9270. {
  9271. int res = TEST_SKIPPED;
  9272. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9273. callback_functions client_cb;
  9274. callback_functions server_cb;
  9275. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9276. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9277. client_cb.method = wolfSSLv23_client_method;
  9278. server_cb.method = wolfSSLv23_server_method;
  9279. client_cb.devId = testDevId;
  9280. server_cb.devId = testDevId;
  9281. /* use CTX_alpn_protos */
  9282. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9283. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9284. test_wolfSSL_client_server(&client_cb, &server_cb);
  9285. /* use set_alpn_protos */
  9286. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9287. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9288. test_wolfSSL_client_server(&client_cb, &server_cb);
  9289. res = TEST_SUCCESS;
  9290. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9291. return res;
  9292. }
  9293. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9294. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9295. {
  9296. EXPECT_DECLS;
  9297. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9298. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9299. WOLFSSL *ssl = wolfSSL_new(ctx);
  9300. ExpectNotNull(ctx);
  9301. ExpectNotNull(ssl);
  9302. /* error cases */
  9303. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9304. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9305. /* success cases */
  9306. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9307. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9308. wolfSSL_free(ssl);
  9309. wolfSSL_CTX_free(ctx);
  9310. #endif
  9311. return EXPECT_RESULT();
  9312. }
  9313. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9314. {
  9315. EXPECT_DECLS;
  9316. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9317. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9318. WOLFSSL *ssl = wolfSSL_new(ctx);
  9319. ExpectNotNull(ctx);
  9320. ExpectNotNull(ssl);
  9321. /* error cases */
  9322. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9323. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9324. /* success cases */
  9325. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9326. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9327. wolfSSL_free(ssl);
  9328. wolfSSL_CTX_free(ctx);
  9329. #endif
  9330. return EXPECT_RESULT();
  9331. }
  9332. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9333. static int test_wolfSSL_SCR_Reconnect(void)
  9334. {
  9335. EXPECT_DECLS;
  9336. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9337. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9338. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9339. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9340. struct test_memio_ctx test_ctx;
  9341. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9342. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9343. byte data;
  9344. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9345. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9346. test_ctx.s_ciphers =
  9347. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9348. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9349. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9350. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9351. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9352. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9353. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9354. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9355. /* WOLFSSL_FATAL_ERROR since it will block */
  9356. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9357. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9358. WOLFSSL_ERROR_WANT_READ);
  9359. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9360. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9361. WOLFSSL_ERROR_WANT_READ);
  9362. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9363. wolfSSL_free(ssl_c);
  9364. ssl_c = NULL;
  9365. wolfSSL_free(ssl_s);
  9366. ssl_s = NULL;
  9367. wolfSSL_CTX_free(ctx_c);
  9368. ctx_c = NULL;
  9369. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9370. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9371. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9372. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9373. wolfSSL_free(ssl_s);
  9374. wolfSSL_free(ssl_c);
  9375. wolfSSL_CTX_free(ctx_s);
  9376. wolfSSL_CTX_free(ctx_c);
  9377. #endif
  9378. return EXPECT_RESULT();
  9379. }
  9380. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9381. (!defined(NO_RSA) || defined(HAVE_ECC))
  9382. /* Called when writing. */
  9383. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9384. {
  9385. (void)ssl;
  9386. (void)buf;
  9387. (void)sz;
  9388. (void)ctx;
  9389. /* Force error return from wolfSSL_accept_TLSv13(). */
  9390. return WANT_WRITE;
  9391. }
  9392. /* Called when reading. */
  9393. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9394. {
  9395. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9396. int len = (int)msg->length;
  9397. (void)ssl;
  9398. (void)sz;
  9399. /* Pass back as much of message as will fit in buffer. */
  9400. if (len > sz)
  9401. len = sz;
  9402. XMEMCPY(buf, msg->buffer, len);
  9403. /* Move over returned data. */
  9404. msg->buffer += len;
  9405. msg->length -= len;
  9406. /* Amount actually copied. */
  9407. return len;
  9408. }
  9409. #endif
  9410. /* Test the detection of duplicate known TLS extensions.
  9411. * Specifically in a ClientHello.
  9412. */
  9413. static int test_tls_ext_duplicate(void)
  9414. {
  9415. EXPECT_DECLS;
  9416. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9417. !defined(NO_FILESYSTEM)
  9418. const unsigned char clientHelloDupTlsExt[] = {
  9419. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9420. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9421. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9422. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9423. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9424. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9425. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9426. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9427. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9428. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9429. 0x00, 0x9e, 0x01, 0x00,
  9430. /* Extensions - duplicate signature algorithms. */
  9431. 0x00, 0x19, 0x00, 0x0d,
  9432. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9433. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9434. /* Supported Versions extension for TLS 1.3. */
  9435. 0x00, 0x2b,
  9436. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9437. };
  9438. WOLFSSL_BUFFER_INFO msg;
  9439. const char* testCertFile;
  9440. const char* testKeyFile;
  9441. WOLFSSL_CTX *ctx = NULL;
  9442. WOLFSSL *ssl = NULL;
  9443. #ifndef NO_RSA
  9444. testCertFile = svrCertFile;
  9445. testKeyFile = svrKeyFile;
  9446. #elif defined(HAVE_ECC)
  9447. testCertFile = eccCertFile;
  9448. testKeyFile = eccKeyFile;
  9449. #endif
  9450. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9451. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9452. WOLFSSL_FILETYPE_PEM));
  9453. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9454. WOLFSSL_FILETYPE_PEM));
  9455. /* Read from 'msg'. */
  9456. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9457. /* No where to send to - dummy sender. */
  9458. wolfSSL_SetIOSend(ctx, DummySend);
  9459. ssl = wolfSSL_new(ctx);
  9460. ExpectNotNull(ssl);
  9461. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9462. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  9463. wolfSSL_SetIOReadCtx(ssl, &msg);
  9464. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  9465. /* can return duplicate ext error or socket error if the peer closed down
  9466. * while sending alert */
  9467. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  9468. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  9469. }
  9470. wolfSSL_free(ssl);
  9471. wolfSSL_CTX_free(ctx);
  9472. #endif
  9473. return EXPECT_RESULT();
  9474. }
  9475. /*----------------------------------------------------------------------------*
  9476. | X509 Tests
  9477. *----------------------------------------------------------------------------*/
  9478. static int test_wolfSSL_X509_NAME_get_entry(void)
  9479. {
  9480. EXPECT_DECLS;
  9481. #if !defined(NO_CERTS) && !defined(NO_RSA)
  9482. #if defined(OPENSSL_ALL) || \
  9483. (defined(OPENSSL_EXTRA) && \
  9484. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  9485. /* use openssl like name to test mapping */
  9486. X509_NAME_ENTRY* ne;
  9487. X509_NAME* name;
  9488. X509* x509 = NULL;
  9489. #ifndef NO_FILESYSTEM
  9490. ASN1_STRING* asn;
  9491. char* subCN = NULL;
  9492. #endif
  9493. int idx;
  9494. ASN1_OBJECT *object = NULL;
  9495. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9496. defined(WOLFSSL_NGINX)
  9497. #ifndef NO_BIO
  9498. BIO* bio = NULL;
  9499. #endif
  9500. #endif
  9501. #ifndef NO_FILESYSTEM
  9502. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9503. WOLFSSL_FILETYPE_PEM));
  9504. ExpectNotNull(name = X509_get_subject_name(x509));
  9505. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9506. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9507. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  9508. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  9509. wolfSSL_FreeX509(x509);
  9510. x509 = NULL;
  9511. #endif
  9512. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9513. WOLFSSL_FILETYPE_PEM));
  9514. ExpectNotNull(name = X509_get_subject_name(x509));
  9515. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9516. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9517. defined(WOLFSSL_NGINX)
  9518. #ifndef NO_BIO
  9519. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9520. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  9521. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9522. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9523. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9524. BIO_free(bio);
  9525. #endif
  9526. #endif
  9527. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9528. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9529. wolfSSL_FreeX509(x509);
  9530. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9531. #endif /* !NO_CERTS && !NO_RSA */
  9532. return EXPECT_RESULT();
  9533. }
  9534. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9535. static int test_wolfSSL_PKCS12(void)
  9536. {
  9537. EXPECT_DECLS;
  9538. /* .p12 file is encrypted with DES3 */
  9539. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9540. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9541. * Password Key
  9542. */
  9543. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9544. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9545. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9546. byte buf[6000];
  9547. char file[] = "./certs/test-servercert.p12";
  9548. char order[] = "./certs/ecc-rsa-server.p12";
  9549. #ifdef WC_RC2
  9550. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9551. #endif
  9552. char pass[] = "a password";
  9553. const char goodPsw[] = "wolfSSL test";
  9554. const char badPsw[] = "bad";
  9555. #ifdef HAVE_ECC
  9556. WOLFSSL_X509_NAME *subject = NULL;
  9557. WOLFSSL_X509 *x509 = NULL;
  9558. #endif
  9559. XFILE f = XBADFILE;
  9560. int bytes, ret, goodPswLen, badPswLen;
  9561. WOLFSSL_BIO *bio = NULL;
  9562. WOLFSSL_EVP_PKEY *pkey = NULL;
  9563. WC_PKCS12 *pkcs12 = NULL;
  9564. WC_PKCS12 *pkcs12_2 = NULL;
  9565. WOLFSSL_X509 *cert = NULL;
  9566. WOLFSSL_X509 *tmp = NULL;
  9567. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9568. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9569. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9570. WOLFSSL_CTX *ctx = NULL;
  9571. WOLFSSL *ssl = NULL;
  9572. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9573. #endif
  9574. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9575. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9576. if (f != XBADFILE) {
  9577. XFCLOSE(f);
  9578. f = XBADFILE;
  9579. }
  9580. goodPswLen = (int)XSTRLEN(goodPsw);
  9581. badPswLen = (int)XSTRLEN(badPsw);
  9582. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9583. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9584. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9585. ExpectNotNull(pkcs12);
  9586. BIO_free(bio);
  9587. bio = NULL;
  9588. /* check verify MAC directly */
  9589. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9590. /* check verify MAC fail case directly */
  9591. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9592. /* check verify MAC fail case */
  9593. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9594. ExpectNull(pkey);
  9595. ExpectNull(cert);
  9596. /* check parse with no extra certs kept */
  9597. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9598. 1);
  9599. ExpectNotNull(pkey);
  9600. ExpectNotNull(cert);
  9601. wolfSSL_EVP_PKEY_free(pkey);
  9602. pkey = NULL;
  9603. wolfSSL_X509_free(cert);
  9604. cert = NULL;
  9605. /* check parse with extra certs kept */
  9606. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9607. 1);
  9608. ExpectNotNull(pkey);
  9609. ExpectNotNull(cert);
  9610. ExpectNotNull(ca);
  9611. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9612. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9613. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9614. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9615. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9616. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9617. #else
  9618. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9619. #endif
  9620. /* Copy stack structure */
  9621. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9622. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9623. /* CTX now owns the tmp_ca stack structure */
  9624. tmp_ca = NULL;
  9625. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9626. ExpectNotNull(tmp_ca);
  9627. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9628. /* Check that the main cert is also set */
  9629. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9630. ExpectNotNull(ssl = SSL_new(ctx));
  9631. ExpectNotNull(SSL_get_certificate(ssl));
  9632. SSL_free(ssl);
  9633. SSL_CTX_free(ctx);
  9634. ctx = NULL;
  9635. #endif
  9636. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9637. /* should be 2 other certs on stack */
  9638. ExpectNotNull(tmp = sk_X509_pop(ca));
  9639. X509_free(tmp);
  9640. ExpectNotNull(tmp = sk_X509_pop(ca));
  9641. X509_free(tmp);
  9642. ExpectNull(sk_X509_pop(ca));
  9643. EVP_PKEY_free(pkey);
  9644. pkey = NULL;
  9645. X509_free(cert);
  9646. cert = NULL;
  9647. sk_X509_pop_free(ca, X509_free);
  9648. ca = NULL;
  9649. /* check PKCS12_create */
  9650. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9651. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9652. SSL_SUCCESS);
  9653. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9654. -1, -1, 100, -1, 0)));
  9655. EVP_PKEY_free(pkey);
  9656. pkey = NULL;
  9657. X509_free(cert);
  9658. cert = NULL;
  9659. sk_X509_pop_free(ca, NULL);
  9660. ca = NULL;
  9661. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9662. SSL_SUCCESS);
  9663. PKCS12_free(pkcs12_2);
  9664. pkcs12_2 = NULL;
  9665. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9666. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9667. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9668. 2000, 1, 0)));
  9669. EVP_PKEY_free(pkey);
  9670. pkey = NULL;
  9671. X509_free(cert);
  9672. cert = NULL;
  9673. sk_X509_pop_free(ca, NULL);
  9674. ca = NULL;
  9675. /* convert to DER then back and parse */
  9676. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9677. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9678. PKCS12_free(pkcs12_2);
  9679. pkcs12_2 = NULL;
  9680. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9681. BIO_free(bio);
  9682. bio = NULL;
  9683. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9684. SSL_SUCCESS);
  9685. /* should be 2 other certs on stack */
  9686. ExpectNotNull(tmp = sk_X509_pop(ca));
  9687. X509_free(tmp);
  9688. ExpectNotNull(tmp = sk_X509_pop(ca));
  9689. X509_free(tmp);
  9690. ExpectNull(sk_X509_pop(ca));
  9691. #ifndef NO_RC4
  9692. PKCS12_free(pkcs12_2);
  9693. pkcs12_2 = NULL;
  9694. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9695. NID_pbe_WithSHA1And128BitRC4,
  9696. NID_pbe_WithSHA1And128BitRC4,
  9697. 2000, 1, 0)));
  9698. EVP_PKEY_free(pkey);
  9699. pkey = NULL;
  9700. X509_free(cert);
  9701. cert = NULL;
  9702. sk_X509_pop_free(ca, NULL);
  9703. ca = NULL;
  9704. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9705. SSL_SUCCESS);
  9706. #endif /* NO_RC4 */
  9707. EVP_PKEY_free(pkey);
  9708. pkey = NULL;
  9709. X509_free(cert);
  9710. cert = NULL;
  9711. PKCS12_free(pkcs12);
  9712. pkcs12 = NULL;
  9713. PKCS12_free(pkcs12_2);
  9714. pkcs12_2 = NULL;
  9715. sk_X509_pop_free(ca, NULL);
  9716. ca = NULL;
  9717. #ifdef HAVE_ECC
  9718. /* test order of parsing */
  9719. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9720. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9721. if (f != XBADFILE) {
  9722. XFCLOSE(f);
  9723. f = XBADFILE;
  9724. }
  9725. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9726. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9727. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9728. WOLFSSL_SUCCESS);
  9729. /* check use of pkey after parse */
  9730. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9731. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9732. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9733. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9734. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9735. #else
  9736. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9737. #endif
  9738. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9739. SSL_CTX_free(ctx);
  9740. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9741. #endif
  9742. ExpectNotNull(pkey);
  9743. ExpectNotNull(cert);
  9744. ExpectNotNull(ca);
  9745. /* compare subject lines of certificates */
  9746. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9747. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9748. SSL_FILETYPE_PEM));
  9749. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9750. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9751. X509_free(x509);
  9752. x509 = NULL;
  9753. /* test expected fail case */
  9754. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9755. SSL_FILETYPE_PEM));
  9756. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9757. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9758. X509_free(x509);
  9759. x509 = NULL;
  9760. X509_free(cert);
  9761. cert = NULL;
  9762. /* get subject line from ca stack */
  9763. ExpectNotNull(cert = sk_X509_pop(ca));
  9764. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9765. /* compare subject from certificate in ca to expected */
  9766. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9767. SSL_FILETYPE_PEM));
  9768. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9769. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9770. EVP_PKEY_free(pkey);
  9771. pkey = NULL;
  9772. X509_free(x509);
  9773. x509 = NULL;
  9774. X509_free(cert);
  9775. cert = NULL;
  9776. BIO_free(bio);
  9777. bio = NULL;
  9778. PKCS12_free(pkcs12);
  9779. pkcs12 = NULL;
  9780. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9781. ca = NULL;
  9782. /* test order of parsing */
  9783. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9784. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9785. if (f != XBADFILE) {
  9786. XFCLOSE(f);
  9787. f = XBADFILE;
  9788. }
  9789. /* check verify MAC fail case */
  9790. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9791. ExpectNull(pkey);
  9792. ExpectNull(cert);
  9793. /* check parse with no extra certs kept */
  9794. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9795. 1);
  9796. ExpectNotNull(pkey);
  9797. ExpectNotNull(cert);
  9798. wolfSSL_EVP_PKEY_free(pkey);
  9799. pkey = NULL;
  9800. wolfSSL_X509_free(cert);
  9801. cert = NULL;
  9802. /* check parse with extra certs kept */
  9803. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9804. 1);
  9805. ExpectNotNull(pkey);
  9806. ExpectNotNull(cert);
  9807. ExpectNotNull(ca);
  9808. wolfSSL_EVP_PKEY_free(pkey);
  9809. pkey = NULL;
  9810. wolfSSL_X509_free(cert);
  9811. cert = NULL;
  9812. sk_X509_pop_free(ca, NULL);
  9813. ca = NULL;
  9814. PKCS12_free(pkcs12);
  9815. pkcs12 = NULL;
  9816. #endif /* HAVE_ECC */
  9817. #ifdef WC_RC2
  9818. /* test PKCS#12 with RC2 encryption */
  9819. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  9820. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9821. if (f != XBADFILE) {
  9822. XFCLOSE(f);
  9823. f = XBADFILE;
  9824. }
  9825. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9826. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9827. /* check verify MAC fail case */
  9828. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9829. ExpectNull(pkey);
  9830. ExpectNull(cert);
  9831. /* check parse with not extra certs kept */
  9832. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9833. WOLFSSL_SUCCESS);
  9834. ExpectNotNull(pkey);
  9835. ExpectNotNull(cert);
  9836. wolfSSL_EVP_PKEY_free(pkey);
  9837. pkey = NULL;
  9838. wolfSSL_X509_free(cert);
  9839. cert = NULL;
  9840. /* check parse with extra certs kept */
  9841. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9842. WOLFSSL_SUCCESS);
  9843. ExpectNotNull(pkey);
  9844. ExpectNotNull(cert);
  9845. ExpectNotNull(ca);
  9846. wolfSSL_EVP_PKEY_free(pkey);
  9847. wolfSSL_X509_free(cert);
  9848. sk_X509_pop_free(ca, NULL);
  9849. BIO_free(bio);
  9850. bio = NULL;
  9851. PKCS12_free(pkcs12);
  9852. pkcs12 = NULL;
  9853. #endif /* WC_RC2 */
  9854. /* Test i2d_PKCS12_bio */
  9855. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9856. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9857. if (f != XBADFILE)
  9858. XFCLOSE(f);
  9859. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9860. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  9861. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  9862. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  9863. PKCS12_free(pkcs12);
  9864. BIO_free(bio);
  9865. (void)order;
  9866. #endif /* OPENSSL_EXTRA */
  9867. #endif /* HAVE_FIPS */
  9868. return EXPECT_RESULT();
  9869. }
  9870. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9871. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  9872. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  9873. #define TEST_PKCS8_ENC
  9874. #endif
  9875. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9876. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9877. /* used to keep track if FailTestCallback was called */
  9878. static int failTestCallbackCalled = 0;
  9879. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  9880. {
  9881. (void)passwd;
  9882. (void)sz;
  9883. (void)rw;
  9884. (void)userdata;
  9885. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  9886. failTestCallbackCalled = 1;
  9887. return -1;
  9888. }
  9889. #endif
  9890. static int test_wolfSSL_no_password_cb(void)
  9891. {
  9892. EXPECT_DECLS;
  9893. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9894. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9895. WOLFSSL_CTX* ctx = NULL;
  9896. byte buff[FOURK_BUF];
  9897. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9898. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9899. XFILE f = XBADFILE;
  9900. int bytes = 0;
  9901. #ifndef NO_WOLFSSL_CLIENT
  9902. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  9903. #else
  9904. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  9905. #endif
  9906. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  9907. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9908. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9909. if (f != XBADFILE) {
  9910. XFCLOSE(f);
  9911. f = XBADFILE;
  9912. }
  9913. ExpectIntLE(bytes, sizeof(buff));
  9914. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9915. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9916. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9917. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9918. if (f != XBADFILE)
  9919. XFCLOSE(f);
  9920. ExpectIntLE(bytes, sizeof(buff));
  9921. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9922. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9923. wolfSSL_CTX_free(ctx);
  9924. /* Password callback should not be called by default */
  9925. ExpectIntEQ(failTestCallbackCalled, 0);
  9926. #endif
  9927. return EXPECT_RESULT();
  9928. }
  9929. #ifdef TEST_PKCS8_ENC
  9930. /* for PKCS8 test case */
  9931. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  9932. {
  9933. int flag = 0;
  9934. (void)rw;
  9935. if (userdata != NULL) {
  9936. flag = *((int*)userdata); /* user set data */
  9937. }
  9938. switch (flag) {
  9939. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  9940. * can be anything the user wishes to be passed to the callback
  9941. * associated with the WOLFSSL_CTX */
  9942. XSTRNCPY(passwd, "yassl123", sz);
  9943. return 8;
  9944. default:
  9945. return BAD_FUNC_ARG;
  9946. }
  9947. }
  9948. #endif /* TEST_PKCS8_ENC */
  9949. /* Testing functions dealing with PKCS8 */
  9950. static int test_wolfSSL_PKCS8(void)
  9951. {
  9952. EXPECT_DECLS;
  9953. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9954. !defined(WOLFCRYPT_ONLY)
  9955. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9956. byte buff[FOURK_BUF];
  9957. byte der[FOURK_BUF];
  9958. #ifndef NO_RSA
  9959. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  9960. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  9961. #endif
  9962. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9963. #ifdef HAVE_ECC
  9964. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9965. #endif
  9966. XFILE f = XBADFILE;
  9967. int bytes = 0;
  9968. WOLFSSL_CTX* ctx = NULL;
  9969. #if defined(HAVE_ECC) && !defined(NO_CODING)
  9970. int ret;
  9971. ecc_key key;
  9972. word32 x = 0;
  9973. #endif
  9974. #ifdef TEST_PKCS8_ENC
  9975. #if !defined(NO_RSA) && !defined(NO_SHA)
  9976. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  9977. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  9978. #endif
  9979. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9980. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  9981. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  9982. #endif
  9983. int flag;
  9984. #endif
  9985. (void)der;
  9986. #ifndef NO_WOLFSSL_CLIENT
  9987. #ifndef WOLFSSL_NO_TLS12
  9988. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9989. #else
  9990. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9991. #endif
  9992. #else
  9993. #ifndef WOLFSSL_NO_TLS12
  9994. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  9995. #else
  9996. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  9997. #endif
  9998. #endif
  9999. #ifdef TEST_PKCS8_ENC
  10000. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10001. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10002. flag = 1; /* used by password callback as return code */
  10003. #if !defined(NO_RSA) && !defined(NO_SHA)
  10004. /* test loading PEM PKCS8 encrypted file */
  10005. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10006. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10007. if (f != XBADFILE) {
  10008. XFCLOSE(f);
  10009. f = XBADFILE;
  10010. }
  10011. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10012. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10013. /* this next case should fail because of password callback return code */
  10014. flag = 0; /* used by password callback as return code */
  10015. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10016. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10017. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10018. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10019. "yassl123"), 0);
  10020. /* test that error value is returned with a bad password */
  10021. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10022. "bad"), 0);
  10023. /* test loading PEM PKCS8 encrypted file */
  10024. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10025. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10026. if (f != XBADFILE) {
  10027. XFCLOSE(f);
  10028. f = XBADFILE;
  10029. }
  10030. flag = 1; /* used by password callback as return code */
  10031. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10032. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10033. /* this next case should fail because of password callback return code */
  10034. flag = 0; /* used by password callback as return code */
  10035. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10036. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10037. #endif /* !NO_RSA && !NO_SHA */
  10038. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10039. /* test loading PEM PKCS8 encrypted ECC Key file */
  10040. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10041. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10042. if (f != XBADFILE) {
  10043. XFCLOSE(f);
  10044. f = XBADFILE;
  10045. }
  10046. flag = 1; /* used by password callback as return code */
  10047. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10048. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10049. /* this next case should fail because of password callback return code */
  10050. flag = 0; /* used by password callback as return code */
  10051. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10052. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10053. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10054. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10055. "yassl123"), 0);
  10056. /* test that error value is returned with a bad password */
  10057. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10058. "bad"), 0);
  10059. /* test loading DER PKCS8 encrypted ECC Key file */
  10060. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10061. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10062. if (f != XBADFILE) {
  10063. XFCLOSE(f);
  10064. f = XBADFILE;
  10065. }
  10066. flag = 1; /* used by password callback as return code */
  10067. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10068. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10069. /* this next case should fail because of password callback return code */
  10070. flag = 0; /* used by password callback as return code */
  10071. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10072. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10073. /* leave flag as "okay" */
  10074. flag = 1;
  10075. #endif /* HAVE_ECC && !NO_SHA */
  10076. #endif /* TEST_PKCS8_ENC */
  10077. #ifndef NO_RSA
  10078. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10079. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10080. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10081. if (f != XBADFILE) {
  10082. XFCLOSE(f);
  10083. f = XBADFILE;
  10084. }
  10085. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10086. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10087. /* test loading PEM PKCS8 private key file (not encrypted) */
  10088. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10089. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10090. if (f != XBADFILE) {
  10091. XFCLOSE(f);
  10092. f = XBADFILE;
  10093. }
  10094. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10095. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10096. #endif /* !NO_RSA */
  10097. /* Test PKCS8 PEM ECC key no crypt */
  10098. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10099. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10100. if (f != XBADFILE) {
  10101. XFCLOSE(f);
  10102. f = XBADFILE;
  10103. }
  10104. #ifdef HAVE_ECC
  10105. /* Test PKCS8 PEM ECC key no crypt */
  10106. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10107. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10108. #ifndef NO_CODING
  10109. /* decrypt PKCS8 PEM to key in DER format */
  10110. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10111. (word32)sizeof(der), NULL)), 0);
  10112. ret = wc_ecc_init(&key);
  10113. if (ret == 0) {
  10114. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10115. wc_ecc_free(&key);
  10116. }
  10117. ExpectIntEQ(ret, 0);
  10118. #endif
  10119. /* Test PKCS8 DER ECC key no crypt */
  10120. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10121. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10122. if (f != XBADFILE)
  10123. XFCLOSE(f);
  10124. /* Test using a PKCS8 ECC PEM */
  10125. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10126. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10127. #else
  10128. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10129. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10130. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10131. #endif /* HAVE_ECC */
  10132. wolfSSL_CTX_free(ctx);
  10133. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10134. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10135. return EXPECT_RESULT();
  10136. }
  10137. static int test_wolfSSL_PKCS8_ED25519(void)
  10138. {
  10139. EXPECT_DECLS;
  10140. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10141. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10142. defined(HAVE_ED25519_KEY_IMPORT)
  10143. const byte encPrivKey[] = \
  10144. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10145. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10146. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10147. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10148. "2L9W4v7swXkV+X+a1ww=\n"
  10149. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10150. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10151. byte der[FOURK_BUF];
  10152. WOLFSSL_CTX* ctx = NULL;
  10153. int bytes;
  10154. XMEMSET(der, 0, sizeof(der));
  10155. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10156. (word32)sizeof(der), password)), 0);
  10157. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10158. #ifndef NO_WOLFSSL_SERVER
  10159. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10160. #else
  10161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10162. #endif
  10163. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10164. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10165. wolfSSL_CTX_free(ctx);
  10166. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10167. #endif
  10168. return EXPECT_RESULT();
  10169. }
  10170. static int test_wolfSSL_PKCS8_ED448(void)
  10171. {
  10172. EXPECT_DECLS;
  10173. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10174. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10175. defined(HAVE_ED448_KEY_IMPORT)
  10176. const byte encPrivKey[] = \
  10177. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10178. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10179. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10180. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10181. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10182. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10183. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10184. byte der[FOURK_BUF];
  10185. WOLFSSL_CTX* ctx = NULL;
  10186. int bytes;
  10187. XMEMSET(der, 0, sizeof(der));
  10188. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10189. (word32)sizeof(der), password)), 0);
  10190. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10191. #ifndef NO_WOLFSSL_SERVER
  10192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10193. #else
  10194. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10195. #endif
  10196. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10197. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10198. wolfSSL_CTX_free(ctx);
  10199. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10200. #endif
  10201. return EXPECT_RESULT();
  10202. }
  10203. /* Testing functions dealing with PKCS5 */
  10204. static int test_wolfSSL_PKCS5(void)
  10205. {
  10206. EXPECT_DECLS;
  10207. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10208. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10209. const char* passwd = "myfipsPa$$W0rd";
  10210. #else
  10211. const char *passwd = "pass1234";
  10212. #endif
  10213. const unsigned char *salt = (unsigned char *)"salt1234";
  10214. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10215. DYNAMIC_TYPE_TMP_BUFFER);
  10216. int ret = 0;
  10217. ExpectNotNull(out);
  10218. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10219. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10220. WOLFSSL_SUCCESS);
  10221. #ifdef WOLFSSL_SHA512
  10222. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10223. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10224. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10225. #endif
  10226. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10227. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10228. return EXPECT_RESULT();
  10229. }
  10230. /* test parsing URI from certificate */
  10231. static int test_wolfSSL_URI(void)
  10232. {
  10233. EXPECT_DECLS;
  10234. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10235. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10236. defined(OPENSSL_EXTRA))
  10237. WOLFSSL_X509* x509 = NULL;
  10238. const char uri[] = "./certs/client-uri-cert.pem";
  10239. const char urn[] = "./certs/client-absolute-urn.pem";
  10240. const char badUri[] = "./certs/client-relative-uri.pem";
  10241. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10242. WOLFSSL_FILETYPE_PEM));
  10243. wolfSSL_FreeX509(x509);
  10244. x509 = NULL;
  10245. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10246. WOLFSSL_FILETYPE_PEM));
  10247. wolfSSL_FreeX509(x509);
  10248. x509 = NULL;
  10249. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10250. && !defined(WOLFSSL_FPKI)
  10251. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10252. WOLFSSL_FILETYPE_PEM));
  10253. #else
  10254. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10255. WOLFSSL_FILETYPE_PEM));
  10256. #endif
  10257. wolfSSL_FreeX509(x509);
  10258. #endif
  10259. return EXPECT_RESULT();
  10260. }
  10261. static int test_wolfSSL_TBS(void)
  10262. {
  10263. EXPECT_DECLS;
  10264. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10265. && defined(OPENSSL_EXTRA)
  10266. WOLFSSL_X509* x509 = NULL;
  10267. const unsigned char* tbs;
  10268. int tbsSz;
  10269. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10270. WOLFSSL_FILETYPE_PEM));
  10271. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10272. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10273. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10274. ExpectIntEQ(tbsSz, 1003);
  10275. wolfSSL_FreeX509(x509);
  10276. #endif
  10277. return EXPECT_RESULT();
  10278. }
  10279. static int test_wolfSSL_X509_verify(void)
  10280. {
  10281. EXPECT_DECLS;
  10282. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10283. defined(OPENSSL_EXTRA)
  10284. WOLFSSL_X509* ca = NULL;
  10285. WOLFSSL_X509* serv = NULL;
  10286. WOLFSSL_EVP_PKEY* pkey = NULL;
  10287. unsigned char buf[2048];
  10288. const unsigned char* pt = NULL;
  10289. int bufSz;
  10290. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10291. WOLFSSL_FILETYPE_PEM));
  10292. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10293. WOLFSSL_SUCCESS);
  10294. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10295. WOLFSSL_SUCCESS);
  10296. ExpectIntEQ(bufSz, 294);
  10297. bufSz = 2048;
  10298. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10299. WOLFSSL_SUCCESS);
  10300. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10301. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10302. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10303. WOLFSSL_FILETYPE_PEM));
  10304. /* success case */
  10305. pt = buf;
  10306. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10307. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10308. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10309. wolfSSL_EVP_PKEY_free(pkey);
  10310. pkey = NULL;
  10311. /* fail case */
  10312. bufSz = 2048;
  10313. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10314. WOLFSSL_SUCCESS);
  10315. pt = buf;
  10316. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10317. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10318. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10319. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10320. wolfSSL_EVP_PKEY_free(pkey);
  10321. wolfSSL_FreeX509(ca);
  10322. wolfSSL_FreeX509(serv);
  10323. #endif
  10324. return EXPECT_RESULT();
  10325. }
  10326. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10327. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10328. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10329. /* create certificate with version 2 */
  10330. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10331. {
  10332. EXPECT_DECLS;
  10333. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10334. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10335. unsigned char *der = NULL, *key = NULL, *pt;
  10336. char *header = NULL, *name = NULL;
  10337. int derSz;
  10338. long keySz;
  10339. XFILE fp = XBADFILE;
  10340. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10341. time_t t;
  10342. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10343. WOLFSSL_FILETYPE_PEM));
  10344. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10345. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10346. WOLFSSL_SUCCESS);
  10347. if (fp != XBADFILE)
  10348. XFCLOSE(fp);
  10349. pt = key;
  10350. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10351. (const unsigned char**)&pt, keySz));
  10352. /* create the version 2 certificate */
  10353. ExpectNotNull(x509v2 = X509_new());
  10354. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10355. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10356. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10357. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10358. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10359. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10360. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10361. t = time(NULL);
  10362. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10363. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10364. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10365. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10366. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10367. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10368. ExpectIntGT(derSz, 0);
  10369. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10370. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10371. /* TODO: Replace with API call */
  10372. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10373. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10374. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10375. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10376. wolfSSL_X509_free(x509);
  10377. wolfSSL_X509_free(x509v2);
  10378. wolfSSL_EVP_PKEY_free(priv);
  10379. wolfSSL_EVP_PKEY_free(pub);
  10380. wolfSSL_ASN1_TIME_free(notBefore);
  10381. wolfSSL_ASN1_TIME_free(notAfter);
  10382. return EXPECT_RESULT();
  10383. }
  10384. /* override certificate version error */
  10385. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10386. {
  10387. EXPECT_DECLS;
  10388. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10389. ExpectIntEQ(store->error, ASN_VERSION_E);
  10390. #else
  10391. ExpectIntEQ(store->error, 0);
  10392. #endif
  10393. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10394. (void)preverify;
  10395. return EXPECT_RESULT() == TEST_SUCCESS;
  10396. }
  10397. /* set verify callback that will override bad certificate version */
  10398. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10399. {
  10400. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10401. return TEST_SUCCESS;
  10402. }
  10403. #endif
  10404. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10405. {
  10406. EXPECT_DECLS;
  10407. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10408. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10409. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10410. test_ssl_cbf func_cb_client;
  10411. test_ssl_cbf func_cb_server;
  10412. /* test server rejects a client certificate that is not version 3 */
  10413. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10414. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10415. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10416. #ifndef WOLFSSL_NO_TLS12
  10417. func_cb_client.method = wolfTLSv1_2_client_method;
  10418. #else
  10419. func_cb_client.method = wolfTLSv1_3_client_method;
  10420. #endif
  10421. #ifndef WOLFSSL_NO_TLS12
  10422. func_cb_server.method = wolfTLSv1_2_server_method;
  10423. #else
  10424. func_cb_server.method = wolfTLSv1_3_server_method;
  10425. #endif
  10426. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10427. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10428. &func_cb_server, NULL), TEST_FAIL);
  10429. #else
  10430. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10431. &func_cb_server, NULL), TEST_SUCCESS);
  10432. #endif
  10433. #endif
  10434. return EXPECT_RESULT();
  10435. }
  10436. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10437. {
  10438. EXPECT_DECLS;
  10439. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10440. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10441. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10442. test_ssl_cbf func_cb_client;
  10443. test_ssl_cbf func_cb_server;
  10444. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10445. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10446. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10447. func_cb_server.ctx_ready = &test_set_override_x509;
  10448. #ifndef WOLFSSL_NO_TLS12
  10449. func_cb_client.method = wolfTLSv1_2_client_method;
  10450. #else
  10451. func_cb_client.method = wolfTLSv1_3_client_method;
  10452. #endif
  10453. #ifndef WOLFSSL_NO_TLS12
  10454. func_cb_server.method = wolfTLSv1_2_server_method;
  10455. #else
  10456. func_cb_server.method = wolfTLSv1_3_server_method;
  10457. #endif
  10458. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10459. &func_cb_server, NULL), TEST_SUCCESS);
  10460. #endif
  10461. return EXPECT_RESULT();
  10462. }
  10463. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  10464. * version allowed.
  10465. * POST: 1 on success.
  10466. */
  10467. static int test_wolfSSL_CTX_SetMinVersion(void)
  10468. {
  10469. int res = TEST_SKIPPED;
  10470. #ifndef NO_WOLFSSL_CLIENT
  10471. int failFlag = WOLFSSL_SUCCESS;
  10472. WOLFSSL_CTX* ctx;
  10473. int itr;
  10474. #ifndef NO_OLD_TLS
  10475. const int versions[] = {
  10476. #ifdef WOLFSSL_ALLOW_TLSV10
  10477. WOLFSSL_TLSV1,
  10478. #endif
  10479. WOLFSSL_TLSV1_1,
  10480. WOLFSSL_TLSV1_2 };
  10481. #elif !defined(WOLFSSL_NO_TLS12)
  10482. const int versions[] = { WOLFSSL_TLSV1_2 };
  10483. #elif defined(WOLFSSL_TLS13)
  10484. const int versions[] = { WOLFSSL_TLSV1_3 };
  10485. #else
  10486. const int versions[0];
  10487. #endif
  10488. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10489. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  10490. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  10491. != WOLFSSL_SUCCESS) {
  10492. failFlag = WOLFSSL_FAILURE;
  10493. }
  10494. }
  10495. wolfSSL_CTX_free(ctx);
  10496. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  10497. #endif
  10498. return res;
  10499. } /* END test_wolfSSL_CTX_SetMinVersion */
  10500. /*----------------------------------------------------------------------------*
  10501. | OCSP Stapling
  10502. *----------------------------------------------------------------------------*/
  10503. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10504. * need to contact the CA, lowering the cost of cert revocation checking.
  10505. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10506. * POST: 1 returned for success.
  10507. */
  10508. static int test_wolfSSL_UseOCSPStapling(void)
  10509. {
  10510. EXPECT_DECLS;
  10511. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10512. !defined(NO_WOLFSSL_CLIENT)
  10513. WOLFSSL_CTX* ctx = NULL;
  10514. WOLFSSL* ssl = NULL;
  10515. #ifndef NO_WOLFSSL_CLIENT
  10516. #ifndef WOLFSSL_NO_TLS12
  10517. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10518. #else
  10519. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10520. #endif
  10521. #else
  10522. #ifndef WOLFSSL_NO_TLS12
  10523. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10524. #else
  10525. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10526. #endif
  10527. #endif
  10528. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10529. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10530. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10531. #ifndef NO_WOLFSSL_CLIENT
  10532. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10533. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10534. #else
  10535. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10536. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10537. #endif
  10538. wolfSSL_free(ssl);
  10539. wolfSSL_CTX_free(ctx);
  10540. #endif
  10541. return EXPECT_RESULT();
  10542. } /* END test_wolfSSL_UseOCSPStapling */
  10543. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10544. * stapling eliminates the need to contact the CA and lowers cert revocation
  10545. * check.
  10546. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10547. */
  10548. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10549. {
  10550. EXPECT_DECLS;
  10551. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10552. !defined(NO_WOLFSSL_CLIENT)
  10553. WOLFSSL_CTX* ctx = NULL;
  10554. WOLFSSL* ssl = NULL;
  10555. #ifndef NO_WOLFSSL_CLIENT
  10556. #ifndef WOLFSSL_NO_TLS12
  10557. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10558. #else
  10559. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10560. #endif
  10561. #else
  10562. #ifndef WOLFSSL_NO_TLS12
  10563. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10564. #else
  10565. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10566. #endif
  10567. #endif
  10568. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10569. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10570. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10571. #ifndef NO_WOLFSSL_CLIENT
  10572. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10573. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10574. #else
  10575. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10576. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10577. #endif
  10578. wolfSSL_free(ssl);
  10579. wolfSSL_CTX_free(ctx);
  10580. #endif
  10581. return EXPECT_RESULT();
  10582. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10583. /*----------------------------------------------------------------------------*
  10584. | Multicast Tests
  10585. *----------------------------------------------------------------------------*/
  10586. static int test_wolfSSL_mcast(void)
  10587. {
  10588. EXPECT_DECLS;
  10589. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10590. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10591. WOLFSSL_CTX* ctx = NULL;
  10592. WOLFSSL* ssl = NULL;
  10593. byte preMasterSecret[512];
  10594. byte clientRandom[32];
  10595. byte serverRandom[32];
  10596. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10597. byte buf[256];
  10598. word16 newId;
  10599. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10600. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10601. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10602. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10603. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10604. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10605. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10606. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10607. WOLFSSL_SUCCESS);
  10608. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10609. ExpectIntLE(newId, 100);
  10610. wolfSSL_free(ssl);
  10611. wolfSSL_CTX_free(ctx);
  10612. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10613. * WOLFSSL_SNIFFER) */
  10614. return EXPECT_RESULT();
  10615. }
  10616. /*----------------------------------------------------------------------------*
  10617. | Wolfcrypt
  10618. *----------------------------------------------------------------------------*/
  10619. /*
  10620. * Unit test for the wc_InitBlake2b()
  10621. */
  10622. static int test_wc_InitBlake2b(void)
  10623. {
  10624. EXPECT_DECLS;
  10625. #ifdef HAVE_BLAKE2
  10626. Blake2b blake;
  10627. /* Test good arg. */
  10628. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  10629. /* Test bad arg. */
  10630. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  10631. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  10632. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  10633. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  10634. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  10635. #endif
  10636. return EXPECT_RESULT();
  10637. } /* END test_wc_InitBlake2b*/
  10638. /*
  10639. * Unit test for the wc_InitBlake2b_WithKey()
  10640. */
  10641. static int test_wc_InitBlake2b_WithKey(void)
  10642. {
  10643. EXPECT_DECLS;
  10644. #ifdef HAVE_BLAKE2
  10645. Blake2b blake;
  10646. word32 digestSz = BLAKE2B_KEYBYTES;
  10647. byte key[BLAKE2B_KEYBYTES];
  10648. word32 keylen = BLAKE2B_KEYBYTES;
  10649. XMEMSET(key, 0, sizeof(key));
  10650. /* Test good arg. */
  10651. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  10652. /* Test bad args. */
  10653. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  10654. BAD_FUNC_ARG);
  10655. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  10656. BAD_FUNC_ARG);
  10657. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  10658. #endif
  10659. return EXPECT_RESULT();
  10660. } /* END wc_InitBlake2b_WithKey*/
  10661. /*
  10662. * Unit test for the wc_InitBlake2s_WithKey()
  10663. */
  10664. static int test_wc_InitBlake2s_WithKey(void)
  10665. {
  10666. EXPECT_DECLS;
  10667. #ifdef HAVE_BLAKE2S
  10668. Blake2s blake;
  10669. word32 digestSz = BLAKE2S_KEYBYTES;
  10670. byte *key = (byte*)"01234567890123456789012345678901";
  10671. word32 keylen = BLAKE2S_KEYBYTES;
  10672. /* Test good arg. */
  10673. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  10674. /* Test bad args. */
  10675. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  10676. BAD_FUNC_ARG);
  10677. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  10678. BAD_FUNC_ARG);
  10679. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  10680. #endif
  10681. return EXPECT_RESULT();
  10682. } /* END wc_InitBlake2s_WithKey*/
  10683. /*
  10684. * Unit test for the wc_InitMd5()
  10685. */
  10686. static int test_wc_InitMd5(void)
  10687. {
  10688. EXPECT_DECLS;
  10689. #ifndef NO_MD5
  10690. wc_Md5 md5;
  10691. /* Test good arg. */
  10692. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10693. /* Test bad arg. */
  10694. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  10695. wc_Md5Free(&md5);
  10696. #endif
  10697. return EXPECT_RESULT();
  10698. } /* END test_wc_InitMd5 */
  10699. /*
  10700. * Testing wc_UpdateMd5()
  10701. */
  10702. static int test_wc_Md5Update(void)
  10703. {
  10704. EXPECT_DECLS;
  10705. #ifndef NO_MD5
  10706. wc_Md5 md5;
  10707. byte hash[WC_MD5_DIGEST_SIZE];
  10708. testVector a, b, c;
  10709. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10710. /* Input */
  10711. a.input = "a";
  10712. a.inLen = XSTRLEN(a.input);
  10713. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  10714. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10715. /* Update input. */
  10716. a.input = "abc";
  10717. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10718. "\x72";
  10719. a.inLen = XSTRLEN(a.input);
  10720. a.outLen = XSTRLEN(a.output);
  10721. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  10722. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10723. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  10724. /* Pass in bad values. */
  10725. b.input = NULL;
  10726. b.inLen = 0;
  10727. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  10728. c.input = NULL;
  10729. c.inLen = WC_MD5_DIGEST_SIZE;
  10730. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  10731. BAD_FUNC_ARG);
  10732. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  10733. BAD_FUNC_ARG);
  10734. wc_Md5Free(&md5);
  10735. #endif
  10736. return EXPECT_RESULT();
  10737. } /* END test_wc_Md5Update() */
  10738. /*
  10739. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10740. */
  10741. static int test_wc_Md5Final(void)
  10742. {
  10743. EXPECT_DECLS;
  10744. #ifndef NO_MD5
  10745. /* Instantiate */
  10746. wc_Md5 md5;
  10747. byte* hash_test[3];
  10748. byte hash1[WC_MD5_DIGEST_SIZE];
  10749. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10750. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10751. int times, i;
  10752. /* Initialize */
  10753. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10754. hash_test[0] = hash1;
  10755. hash_test[1] = hash2;
  10756. hash_test[2] = hash3;
  10757. times = sizeof(hash_test)/sizeof(byte*);
  10758. for (i = 0; i < times; i++) {
  10759. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  10760. }
  10761. /* Test bad args. */
  10762. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  10763. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  10764. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  10765. wc_Md5Free(&md5);
  10766. #endif
  10767. return EXPECT_RESULT();
  10768. }
  10769. /*
  10770. * Unit test for the wc_InitSha()
  10771. */
  10772. static int test_wc_InitSha(void)
  10773. {
  10774. EXPECT_DECLS;
  10775. #ifndef NO_SHA
  10776. wc_Sha sha;
  10777. /* Test good arg. */
  10778. ExpectIntEQ(wc_InitSha(&sha), 0);
  10779. /* Test bad arg. */
  10780. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  10781. wc_ShaFree(&sha);
  10782. #endif
  10783. return EXPECT_RESULT();
  10784. } /* END test_wc_InitSha */
  10785. /*
  10786. * Tesing wc_ShaUpdate()
  10787. */
  10788. static int test_wc_ShaUpdate(void)
  10789. {
  10790. EXPECT_DECLS;
  10791. #ifndef NO_SHA
  10792. wc_Sha sha;
  10793. byte hash[WC_SHA_DIGEST_SIZE];
  10794. testVector a, b, c;
  10795. ExpectIntEQ(wc_InitSha(&sha), 0);
  10796. /* Input. */
  10797. a.input = "a";
  10798. a.inLen = XSTRLEN(a.input);
  10799. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  10800. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  10801. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10802. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10803. /* Update input. */
  10804. a.input = "abc";
  10805. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10806. "\x6C\x9C\xD0\xD8\x9D";
  10807. a.inLen = XSTRLEN(a.input);
  10808. a.outLen = XSTRLEN(a.output);
  10809. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10810. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10811. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  10812. /* Try passing in bad values. */
  10813. b.input = NULL;
  10814. b.inLen = 0;
  10815. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  10816. c.input = NULL;
  10817. c.inLen = WC_SHA_DIGEST_SIZE;
  10818. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  10819. BAD_FUNC_ARG);
  10820. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  10821. BAD_FUNC_ARG);
  10822. wc_ShaFree(&sha);
  10823. #endif
  10824. return EXPECT_RESULT();
  10825. } /* END test_wc_ShaUpdate() */
  10826. /*
  10827. * Unit test on wc_ShaFinal
  10828. */
  10829. static int test_wc_ShaFinal(void)
  10830. {
  10831. EXPECT_DECLS;
  10832. #ifndef NO_SHA
  10833. wc_Sha sha;
  10834. byte* hash_test[3];
  10835. byte hash1[WC_SHA_DIGEST_SIZE];
  10836. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10837. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10838. int times, i;
  10839. /* Initialize*/
  10840. ExpectIntEQ(wc_InitSha(&sha), 0);
  10841. hash_test[0] = hash1;
  10842. hash_test[1] = hash2;
  10843. hash_test[2] = hash3;
  10844. times = sizeof(hash_test)/sizeof(byte*);
  10845. for (i = 0; i < times; i++) {
  10846. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  10847. }
  10848. /* Test bad args. */
  10849. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  10850. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  10851. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  10852. wc_ShaFree(&sha);
  10853. #endif
  10854. return EXPECT_RESULT();
  10855. } /* END test_wc_ShaFinal */
  10856. /*
  10857. * Unit test for wc_InitSha256()
  10858. */
  10859. static int test_wc_InitSha256(void)
  10860. {
  10861. EXPECT_DECLS;
  10862. #ifndef NO_SHA256
  10863. wc_Sha256 sha256;
  10864. /* Test good arg. */
  10865. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10866. /* Test bad arg. */
  10867. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  10868. wc_Sha256Free(&sha256);
  10869. #endif
  10870. return EXPECT_RESULT();
  10871. } /* END test_wc_InitSha256 */
  10872. /*
  10873. * Unit test for wc_Sha256Update()
  10874. */
  10875. static int test_wc_Sha256Update(void)
  10876. {
  10877. EXPECT_DECLS;
  10878. #ifndef NO_SHA256
  10879. wc_Sha256 sha256;
  10880. byte hash[WC_SHA256_DIGEST_SIZE];
  10881. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  10882. testVector a, b, c;
  10883. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10884. /* Input. */
  10885. a.input = "a";
  10886. a.inLen = XSTRLEN(a.input);
  10887. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  10888. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  10889. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10890. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10891. /* Update input. */
  10892. a.input = "abc";
  10893. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10894. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10895. "\x15\xAD";
  10896. a.inLen = XSTRLEN(a.input);
  10897. a.outLen = XSTRLEN(a.output);
  10898. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10899. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10900. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  10901. /* Unaligned check. */
  10902. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  10903. 0);
  10904. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  10905. /* Try passing in bad values */
  10906. b.input = NULL;
  10907. b.inLen = 0;
  10908. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  10909. c.input = NULL;
  10910. c.inLen = WC_SHA256_DIGEST_SIZE;
  10911. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  10912. BAD_FUNC_ARG);
  10913. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  10914. BAD_FUNC_ARG);
  10915. wc_Sha256Free(&sha256);
  10916. #endif
  10917. return EXPECT_RESULT();
  10918. } /* END test_wc_Sha256Update */
  10919. /*
  10920. * Unit test function for wc_Sha256Final()
  10921. */
  10922. static int test_wc_Sha256Final(void)
  10923. {
  10924. EXPECT_DECLS;
  10925. #ifndef NO_SHA256
  10926. wc_Sha256 sha256;
  10927. byte* hash_test[3];
  10928. byte hash1[WC_SHA256_DIGEST_SIZE];
  10929. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10930. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10931. int times, i;
  10932. /* Initialize */
  10933. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10934. hash_test[0] = hash1;
  10935. hash_test[1] = hash2;
  10936. hash_test[2] = hash3;
  10937. times = sizeof(hash_test) / sizeof(byte*);
  10938. for (i = 0; i < times; i++) {
  10939. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  10940. }
  10941. /* Test bad args. */
  10942. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  10943. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  10944. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  10945. wc_Sha256Free(&sha256);
  10946. #endif
  10947. return EXPECT_RESULT();
  10948. } /* END test_wc_Sha256Final */
  10949. /*
  10950. * Unit test function for wc_Sha256FinalRaw()
  10951. */
  10952. static int test_wc_Sha256FinalRaw(void)
  10953. {
  10954. EXPECT_DECLS;
  10955. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10956. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10957. !defined(WOLFSSL_NO_HASH_RAW)
  10958. wc_Sha256 sha256;
  10959. byte* hash_test[3];
  10960. byte hash1[WC_SHA256_DIGEST_SIZE];
  10961. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10962. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10963. int times, i;
  10964. /* Initialize */
  10965. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10966. hash_test[0] = hash1;
  10967. hash_test[1] = hash2;
  10968. hash_test[2] = hash3;
  10969. times = sizeof(hash_test) / sizeof(byte*);
  10970. for (i = 0; i < times; i++) {
  10971. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  10972. }
  10973. /* Test bad args. */
  10974. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  10975. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  10976. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  10977. wc_Sha256Free(&sha256);
  10978. #endif
  10979. return EXPECT_RESULT();
  10980. } /* END test_wc_Sha256FinalRaw */
  10981. /*
  10982. * Unit test function for wc_Sha256GetFlags()
  10983. */
  10984. static int test_wc_Sha256GetFlags(void)
  10985. {
  10986. EXPECT_DECLS;
  10987. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10988. wc_Sha256 sha256;
  10989. word32 flags = 0;
  10990. /* Initialize */
  10991. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10992. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  10993. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  10994. wc_Sha256Free(&sha256);
  10995. #endif
  10996. return EXPECT_RESULT();
  10997. } /* END test_wc_Sha256GetFlags */
  10998. /*
  10999. * Unit test function for wc_Sha256Free()
  11000. */
  11001. static int test_wc_Sha256Free(void)
  11002. {
  11003. EXPECT_DECLS;
  11004. #ifndef NO_SHA256
  11005. wc_Sha256Free(NULL);
  11006. /* Set result to SUCCESS. */
  11007. ExpectTrue(1);
  11008. #endif
  11009. return EXPECT_RESULT();
  11010. } /* END test_wc_Sha256Free */
  11011. /*
  11012. * Unit test function for wc_Sha256GetHash()
  11013. */
  11014. static int test_wc_Sha256GetHash(void)
  11015. {
  11016. EXPECT_DECLS;
  11017. #ifndef NO_SHA256
  11018. wc_Sha256 sha256;
  11019. byte hash1[WC_SHA256_DIGEST_SIZE];
  11020. /* Initialize */
  11021. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11022. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11023. /* test bad arguments*/
  11024. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11025. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11026. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11027. wc_Sha256Free(&sha256);
  11028. #endif
  11029. return EXPECT_RESULT();
  11030. } /* END test_wc_Sha256GetHash */
  11031. /*
  11032. * Unit test function for wc_Sha256Copy()
  11033. */
  11034. static int test_wc_Sha256Copy(void)
  11035. {
  11036. EXPECT_DECLS;
  11037. #ifndef NO_SHA256
  11038. wc_Sha256 sha256;
  11039. wc_Sha256 temp;
  11040. XMEMSET(&sha256, 0, sizeof(sha256));
  11041. XMEMSET(&temp, 0, sizeof(temp));
  11042. /* Initialize */
  11043. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11044. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11045. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11046. /* test bad arguments*/
  11047. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11048. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11049. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11050. wc_Sha256Free(&sha256);
  11051. wc_Sha256Free(&temp);
  11052. #endif
  11053. return EXPECT_RESULT();
  11054. } /* END test_wc_Sha256Copy */
  11055. /*
  11056. * Testing wc_InitSha512()
  11057. */
  11058. static int test_wc_InitSha512(void)
  11059. {
  11060. EXPECT_DECLS;
  11061. #ifdef WOLFSSL_SHA512
  11062. wc_Sha512 sha512;
  11063. /* Test good arg. */
  11064. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11065. /* Test bad arg. */
  11066. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11067. wc_Sha512Free(&sha512);
  11068. #endif
  11069. return EXPECT_RESULT();
  11070. } /* END test_wc_InitSha512 */
  11071. /*
  11072. * wc_Sha512Update() test.
  11073. */
  11074. static int test_wc_Sha512Update(void)
  11075. {
  11076. EXPECT_DECLS;
  11077. #ifdef WOLFSSL_SHA512
  11078. wc_Sha512 sha512;
  11079. byte hash[WC_SHA512_DIGEST_SIZE];
  11080. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11081. testVector a, b, c;
  11082. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11083. /* Input. */
  11084. a.input = "a";
  11085. a.inLen = XSTRLEN(a.input);
  11086. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11087. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11088. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11089. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11090. /* Update input. */
  11091. a.input = "abc";
  11092. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11093. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11094. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11095. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11096. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11097. a.inLen = XSTRLEN(a.input);
  11098. a.outLen = XSTRLEN(a.output);
  11099. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11100. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11101. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11102. /* Unaligned check. */
  11103. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11104. 0);
  11105. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11106. /* Try passing in bad values */
  11107. b.input = NULL;
  11108. b.inLen = 0;
  11109. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11110. c.input = NULL;
  11111. c.inLen = WC_SHA512_DIGEST_SIZE;
  11112. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11113. BAD_FUNC_ARG);
  11114. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11115. BAD_FUNC_ARG);
  11116. wc_Sha512Free(&sha512);
  11117. #endif
  11118. return EXPECT_RESULT();
  11119. } /* END test_wc_Sha512Update */
  11120. #ifdef WOLFSSL_SHA512
  11121. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11122. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11123. /* Performs test for
  11124. * - wc_Sha512Final/wc_Sha512FinalRaw
  11125. * - wc_Sha512_224Final/wc_Sha512_224Final
  11126. * - wc_Sha512_256Final/wc_Sha512_256Final
  11127. * parameter:
  11128. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11129. * WC_HASH_TYPE_SHA512_256
  11130. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11131. *return 0 on success
  11132. */
  11133. static int test_Sha512_Family_Final(int type, int isRaw)
  11134. {
  11135. EXPECT_DECLS;
  11136. wc_Sha512 sha512;
  11137. byte* hash_test[3];
  11138. byte hash1[WC_SHA512_DIGEST_SIZE];
  11139. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11140. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11141. int times, i;
  11142. int(*initFp)(wc_Sha512*);
  11143. int(*finalFp)(wc_Sha512*, byte*);
  11144. void(*freeFp)(wc_Sha512*);
  11145. if (type == WC_HASH_TYPE_SHA512) {
  11146. initFp = wc_InitSha512;
  11147. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11148. !defined(WOLFSSL_NO_HASH_RAW)
  11149. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11150. #else
  11151. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11152. #endif
  11153. freeFp = wc_Sha512Free;
  11154. }
  11155. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11156. #if !defined(WOLFSSL_NOSHA512_224)
  11157. else if (type == WC_HASH_TYPE_SHA512_224) {
  11158. initFp = wc_InitSha512_224;
  11159. #if !defined(WOLFSSL_NO_HASH_RAW)
  11160. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11161. #else
  11162. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11163. #endif
  11164. freeFp = wc_Sha512_224Free;
  11165. }
  11166. #endif
  11167. #if !defined(WOLFSSL_NOSHA512_256)
  11168. else if (type == WC_HASH_TYPE_SHA512_256) {
  11169. initFp = wc_InitSha512_256;
  11170. #if !defined(WOLFSSL_NO_HASH_RAW)
  11171. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11172. #else
  11173. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11174. #endif
  11175. freeFp = wc_Sha512_256Free;
  11176. }
  11177. #endif
  11178. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11179. else
  11180. return TEST_FAIL;
  11181. /* Initialize */
  11182. ExpectIntEQ(initFp(&sha512), 0);
  11183. hash_test[0] = hash1;
  11184. hash_test[1] = hash2;
  11185. hash_test[2] = hash3;
  11186. times = sizeof(hash_test) / sizeof(byte *);
  11187. /* Good test args. */
  11188. for (i = 0; i < times; i++) {
  11189. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11190. }
  11191. /* Test bad args. */
  11192. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11193. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11194. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11195. freeFp(&sha512);
  11196. return EXPECT_RESULT();
  11197. }
  11198. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11199. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11200. #endif /* WOLFSSL_SHA512 */
  11201. /*
  11202. * Unit test function for wc_Sha512Final()
  11203. */
  11204. static int test_wc_Sha512Final(void)
  11205. {
  11206. EXPECT_DECLS;
  11207. #ifdef WOLFSSL_SHA512
  11208. wc_Sha512 sha512;
  11209. byte* hash_test[3];
  11210. byte hash1[WC_SHA512_DIGEST_SIZE];
  11211. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11212. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11213. int times, i;
  11214. /* Initialize */
  11215. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11216. hash_test[0] = hash1;
  11217. hash_test[1] = hash2;
  11218. hash_test[2] = hash3;
  11219. times = sizeof(hash_test) / sizeof(byte *);
  11220. for (i = 0; i < times; i++) {
  11221. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11222. }
  11223. /* Test bad args. */
  11224. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11225. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11226. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11227. wc_Sha512Free(&sha512);
  11228. #endif
  11229. return EXPECT_RESULT();
  11230. } /* END test_wc_Sha512Final */
  11231. /*
  11232. * Unit test function for wc_Sha512GetFlags()
  11233. */
  11234. static int test_wc_Sha512GetFlags(void)
  11235. {
  11236. EXPECT_DECLS;
  11237. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11238. wc_Sha512 sha512;
  11239. word32 flags = 0;
  11240. /* Initialize */
  11241. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11242. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11243. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11244. wc_Sha512Free(&sha512);
  11245. #endif
  11246. return EXPECT_RESULT();
  11247. } /* END test_wc_Sha512GetFlags */
  11248. /*
  11249. * Unit test function for wc_Sha512FinalRaw()
  11250. */
  11251. static int test_wc_Sha512FinalRaw(void)
  11252. {
  11253. EXPECT_DECLS;
  11254. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11255. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11256. !defined(WOLFSSL_NO_HASH_RAW)
  11257. wc_Sha512 sha512;
  11258. byte* hash_test[3];
  11259. byte hash1[WC_SHA512_DIGEST_SIZE];
  11260. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11261. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11262. int times, i;
  11263. /* Initialize */
  11264. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11265. hash_test[0] = hash1;
  11266. hash_test[1] = hash2;
  11267. hash_test[2] = hash3;
  11268. times = sizeof(hash_test) / sizeof(byte*);
  11269. /* Good test args. */
  11270. for (i = 0; i < times; i++) {
  11271. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11272. }
  11273. /* Test bad args. */
  11274. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11275. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11276. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11277. wc_Sha512Free(&sha512);
  11278. #endif
  11279. return EXPECT_RESULT();
  11280. } /* END test_wc_Sha512FinalRaw */
  11281. /*
  11282. * Unit test function for wc_Sha512Free()
  11283. */
  11284. static int test_wc_Sha512Free(void)
  11285. {
  11286. EXPECT_DECLS;
  11287. #ifdef WOLFSSL_SHA512
  11288. wc_Sha512Free(NULL);
  11289. /* Set result to SUCCESS. */
  11290. ExpectTrue(1);
  11291. #endif
  11292. return EXPECT_RESULT();
  11293. } /* END test_wc_Sha512Free */
  11294. #ifdef WOLFSSL_SHA512
  11295. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11296. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11297. static int test_Sha512_Family_GetHash(int type )
  11298. {
  11299. EXPECT_DECLS;
  11300. int(*initFp)(wc_Sha512*);
  11301. int(*ghashFp)(wc_Sha512*, byte*);
  11302. wc_Sha512 sha512;
  11303. byte hash1[WC_SHA512_DIGEST_SIZE];
  11304. if (type == WC_HASH_TYPE_SHA512) {
  11305. initFp = wc_InitSha512;
  11306. ghashFp = wc_Sha512GetHash;
  11307. }
  11308. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11309. #if !defined(WOLFSSL_NOSHA512_224)
  11310. else if (type == WC_HASH_TYPE_SHA512_224) {
  11311. initFp = wc_InitSha512_224;
  11312. ghashFp = wc_Sha512_224GetHash;
  11313. }
  11314. #endif
  11315. #if !defined(WOLFSSL_NOSHA512_256)
  11316. else if (type == WC_HASH_TYPE_SHA512_256) {
  11317. initFp = wc_InitSha512_256;
  11318. ghashFp = wc_Sha512_256GetHash;
  11319. }
  11320. #endif
  11321. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11322. else {
  11323. initFp = NULL;
  11324. ghashFp = NULL;
  11325. }
  11326. if (initFp == NULL || ghashFp == NULL)
  11327. return TEST_FAIL;
  11328. ExpectIntEQ(initFp(&sha512), 0);
  11329. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11330. /* test bad arguments*/
  11331. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11332. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11333. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11334. wc_Sha512Free(&sha512);
  11335. return EXPECT_RESULT();
  11336. }
  11337. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11338. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11339. #endif /* WOLFSSL_SHA512 */
  11340. /*
  11341. * Unit test function for wc_Sha512GetHash()
  11342. */
  11343. static int test_wc_Sha512GetHash(void)
  11344. {
  11345. EXPECT_DECLS;
  11346. #ifdef WOLFSSL_SHA512
  11347. wc_Sha512 sha512;
  11348. byte hash1[WC_SHA512_DIGEST_SIZE];
  11349. /* Initialize */
  11350. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11351. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11352. /* test bad arguments*/
  11353. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11354. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11355. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11356. wc_Sha512Free(&sha512);
  11357. #endif
  11358. return EXPECT_RESULT();
  11359. } /* END test_wc_Sha512GetHash */
  11360. /*
  11361. * Unit test function for wc_Sha512Copy()
  11362. */
  11363. static int test_wc_Sha512Copy(void)
  11364. {
  11365. EXPECT_DECLS;
  11366. #ifdef WOLFSSL_SHA512
  11367. wc_Sha512 sha512;
  11368. wc_Sha512 temp;
  11369. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11370. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11371. /* Initialize */
  11372. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11373. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11374. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11375. /* test bad arguments*/
  11376. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11377. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11378. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11379. wc_Sha512Free(&sha512);
  11380. wc_Sha512Free(&temp);
  11381. #endif
  11382. return EXPECT_RESULT();
  11383. } /* END test_wc_Sha512Copy */
  11384. static int test_wc_InitSha512_224(void)
  11385. {
  11386. EXPECT_DECLS;
  11387. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11388. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11389. wc_Sha512 sha512;
  11390. /* Test good arg. */
  11391. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11392. /* Test bad arg. */
  11393. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11394. wc_Sha512_224Free(&sha512);
  11395. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11396. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11397. return EXPECT_RESULT();
  11398. }
  11399. static int test_wc_Sha512_224Update(void)
  11400. {
  11401. EXPECT_DECLS;
  11402. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11403. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11404. wc_Sha512 sha512;
  11405. byte hash[WC_SHA512_DIGEST_SIZE];
  11406. testVector a, c;
  11407. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11408. /* Input. */
  11409. a.input = "a";
  11410. a.inLen = XSTRLEN(a.input);
  11411. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11412. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11413. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11414. 0);
  11415. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11416. /* Update input. */
  11417. a.input = "abc";
  11418. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11419. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11420. a.inLen = XSTRLEN(a.input);
  11421. a.outLen = XSTRLEN(a.output);
  11422. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11423. 0);
  11424. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11425. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11426. c.input = NULL;
  11427. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11428. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11429. BAD_FUNC_ARG);
  11430. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11431. BAD_FUNC_ARG);
  11432. wc_Sha512_224Free(&sha512);
  11433. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11434. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11435. return EXPECT_RESULT();
  11436. }
  11437. static int test_wc_Sha512_224Final(void)
  11438. {
  11439. EXPECT_DECLS;
  11440. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11441. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11442. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11443. TEST_SUCCESS);
  11444. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11445. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11446. return EXPECT_RESULT();
  11447. }
  11448. static int test_wc_Sha512_224GetFlags(void)
  11449. {
  11450. EXPECT_DECLS;
  11451. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11452. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11453. wc_Sha512 sha512;
  11454. wc_Sha512 copy;
  11455. word32 flags = 0;
  11456. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11457. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11458. /* Initialize */
  11459. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11460. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11461. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  11462. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11463. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  11464. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  11465. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11466. wc_Sha512_224Free(&copy);
  11467. wc_Sha512_224Free(&sha512);
  11468. #endif
  11469. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11470. return EXPECT_RESULT();
  11471. }
  11472. static int test_wc_Sha512_224FinalRaw(void)
  11473. {
  11474. EXPECT_DECLS;
  11475. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11476. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11477. !defined(WOLFSSL_NO_HASH_RAW)
  11478. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  11479. TEST_SUCCESS);
  11480. #endif
  11481. return EXPECT_RESULT();
  11482. }
  11483. static int test_wc_Sha512_224Free(void)
  11484. {
  11485. EXPECT_DECLS;
  11486. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11487. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11488. wc_Sha512_224Free(NULL);
  11489. /* Set result to SUCCESS. */
  11490. ExpectTrue(1);
  11491. #endif
  11492. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11493. return EXPECT_RESULT();
  11494. }
  11495. static int test_wc_Sha512_224GetHash(void)
  11496. {
  11497. EXPECT_DECLS;
  11498. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11499. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11500. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  11501. TEST_SUCCESS);
  11502. #endif
  11503. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11504. return EXPECT_RESULT();
  11505. }
  11506. static int test_wc_Sha512_224Copy(void)
  11507. {
  11508. EXPECT_DECLS;
  11509. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11510. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11511. wc_Sha512 sha512;
  11512. wc_Sha512 temp;
  11513. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11514. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11515. /* Initialize */
  11516. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11517. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  11518. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  11519. /* test bad arguments*/
  11520. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  11521. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  11522. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  11523. wc_Sha512_224Free(&sha512);
  11524. wc_Sha512_224Free(&temp);
  11525. #endif
  11526. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11527. return EXPECT_RESULT();
  11528. }
  11529. static int test_wc_InitSha512_256(void)
  11530. {
  11531. EXPECT_DECLS;
  11532. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11533. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11534. wc_Sha512 sha512;
  11535. /* Test good arg. */
  11536. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11537. /* Test bad arg. */
  11538. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  11539. wc_Sha512_256Free(&sha512);
  11540. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11541. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11542. return EXPECT_RESULT();
  11543. }
  11544. static int test_wc_Sha512_256Update(void)
  11545. {
  11546. EXPECT_DECLS;
  11547. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11548. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11549. wc_Sha512 sha512;
  11550. byte hash[WC_SHA512_DIGEST_SIZE];
  11551. testVector a, c;
  11552. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11553. /* Input. */
  11554. a.input = "a";
  11555. a.inLen = XSTRLEN(a.input);
  11556. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  11557. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  11558. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11559. 0);
  11560. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11561. /* Update input. */
  11562. a.input = "abc";
  11563. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11564. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11565. "\x07\xe7\xaf\x23";
  11566. a.inLen = XSTRLEN(a.input);
  11567. a.outLen = XSTRLEN(a.output);
  11568. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11569. 0);
  11570. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11571. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  11572. c.input = NULL;
  11573. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11574. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11575. BAD_FUNC_ARG);
  11576. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11577. BAD_FUNC_ARG);
  11578. wc_Sha512_256Free(&sha512);
  11579. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11580. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11581. return EXPECT_RESULT();
  11582. }
  11583. static int test_wc_Sha512_256Final(void)
  11584. {
  11585. EXPECT_DECLS;
  11586. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11587. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11588. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  11589. TEST_SUCCESS);
  11590. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11591. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11592. return EXPECT_RESULT();
  11593. }
  11594. static int test_wc_Sha512_256GetFlags(void)
  11595. {
  11596. EXPECT_DECLS;
  11597. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11598. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11599. wc_Sha512 sha512, copy;
  11600. word32 flags = 0;
  11601. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11602. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11603. /* Initialize */
  11604. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11605. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  11606. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  11607. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11608. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  11609. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  11610. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11611. wc_Sha512_256Free(&sha512);
  11612. #endif
  11613. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11614. return EXPECT_RESULT();
  11615. }
  11616. static int test_wc_Sha512_256FinalRaw(void)
  11617. {
  11618. EXPECT_DECLS;
  11619. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11620. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11621. !defined(WOLFSSL_NO_HASH_RAW)
  11622. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  11623. TEST_SUCCESS);
  11624. #endif
  11625. return EXPECT_RESULT();
  11626. }
  11627. static int test_wc_Sha512_256Free(void)
  11628. {
  11629. EXPECT_DECLS;
  11630. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11631. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11632. wc_Sha512_256Free(NULL);
  11633. /* Set result to SUCCESS. */
  11634. ExpectTrue(1);
  11635. #endif
  11636. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11637. return EXPECT_RESULT();
  11638. }
  11639. static int test_wc_Sha512_256GetHash(void)
  11640. {
  11641. EXPECT_DECLS;
  11642. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11643. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11644. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  11645. TEST_SUCCESS);
  11646. #endif
  11647. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11648. return EXPECT_RESULT();
  11649. }
  11650. static int test_wc_Sha512_256Copy(void)
  11651. {
  11652. EXPECT_DECLS;
  11653. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11654. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11655. wc_Sha512 sha512;
  11656. wc_Sha512 temp;
  11657. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11658. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11659. /* Initialize */
  11660. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11661. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  11662. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  11663. /* test bad arguments*/
  11664. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  11665. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  11666. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  11667. wc_Sha512_256Free(&sha512);
  11668. wc_Sha512_256Free(&temp);
  11669. #endif
  11670. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11671. return EXPECT_RESULT();
  11672. }
  11673. /*
  11674. * Testing wc_InitSha384()
  11675. */
  11676. static int test_wc_InitSha384(void)
  11677. {
  11678. EXPECT_DECLS;
  11679. #ifdef WOLFSSL_SHA384
  11680. wc_Sha384 sha384;
  11681. /* Test good arg. */
  11682. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11683. /* Test bad arg. */
  11684. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  11685. wc_Sha384Free(&sha384);
  11686. #endif
  11687. return EXPECT_RESULT();
  11688. } /* END test_wc_InitSha384 */
  11689. /*
  11690. * test wc_Sha384Update()
  11691. */
  11692. static int test_wc_Sha384Update(void)
  11693. {
  11694. EXPECT_DECLS;
  11695. #ifdef WOLFSSL_SHA384
  11696. wc_Sha384 sha384;
  11697. byte hash[WC_SHA384_DIGEST_SIZE];
  11698. testVector a, b, c;
  11699. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11700. /* Input */
  11701. a.input = "a";
  11702. a.inLen = XSTRLEN(a.input);
  11703. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  11704. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  11705. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11706. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11707. /* Update input. */
  11708. a.input = "abc";
  11709. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11710. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11711. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11712. "\xc8\x25\xa7";
  11713. a.inLen = XSTRLEN(a.input);
  11714. a.outLen = XSTRLEN(a.output);
  11715. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11716. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11717. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  11718. /* Pass in bad values. */
  11719. b.input = NULL;
  11720. b.inLen = 0;
  11721. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  11722. c.input = NULL;
  11723. c.inLen = WC_SHA384_DIGEST_SIZE;
  11724. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  11725. BAD_FUNC_ARG);
  11726. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  11727. BAD_FUNC_ARG);
  11728. wc_Sha384Free(&sha384);
  11729. #endif
  11730. return EXPECT_RESULT();
  11731. } /* END test_wc_Sha384Update */
  11732. /*
  11733. * Unit test function for wc_Sha384Final();
  11734. */
  11735. static int test_wc_Sha384Final(void)
  11736. {
  11737. EXPECT_DECLS;
  11738. #ifdef WOLFSSL_SHA384
  11739. wc_Sha384 sha384;
  11740. byte* hash_test[3];
  11741. byte hash1[WC_SHA384_DIGEST_SIZE];
  11742. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11743. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11744. int times, i;
  11745. /* Initialize */
  11746. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11747. hash_test[0] = hash1;
  11748. hash_test[1] = hash2;
  11749. hash_test[2] = hash3;
  11750. times = sizeof(hash_test) / sizeof(byte*);
  11751. /* Good test args. */
  11752. for (i = 0; i < times; i++) {
  11753. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  11754. }
  11755. /* Test bad args. */
  11756. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  11757. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  11758. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  11759. wc_Sha384Free(&sha384);
  11760. #endif
  11761. return EXPECT_RESULT();
  11762. } /* END test_wc_Sha384Final */
  11763. /*
  11764. * Unit test function for wc_Sha384GetFlags()
  11765. */
  11766. static int test_wc_Sha384GetFlags(void)
  11767. {
  11768. EXPECT_DECLS;
  11769. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11770. wc_Sha384 sha384;
  11771. word32 flags = 0;
  11772. /* Initialize */
  11773. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11774. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  11775. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11776. wc_Sha384Free(&sha384);
  11777. #endif
  11778. return EXPECT_RESULT();
  11779. } /* END test_wc_Sha384GetFlags */
  11780. /*
  11781. * Unit test function for wc_Sha384FinalRaw()
  11782. */
  11783. static int test_wc_Sha384FinalRaw(void)
  11784. {
  11785. EXPECT_DECLS;
  11786. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11787. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11788. !defined(WOLFSSL_NO_HASH_RAW)
  11789. wc_Sha384 sha384;
  11790. byte* hash_test[3];
  11791. byte hash1[WC_SHA384_DIGEST_SIZE];
  11792. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11793. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11794. int times, i;
  11795. /* Initialize */
  11796. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11797. hash_test[0] = hash1;
  11798. hash_test[1] = hash2;
  11799. hash_test[2] = hash3;
  11800. times = sizeof(hash_test) / sizeof(byte*);
  11801. /* Good test args. */
  11802. for (i = 0; i < times; i++) {
  11803. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  11804. }
  11805. /* Test bad args. */
  11806. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11807. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11808. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  11809. wc_Sha384Free(&sha384);
  11810. #endif
  11811. return EXPECT_RESULT();
  11812. } /* END test_wc_Sha384FinalRaw */
  11813. /*
  11814. * Unit test function for wc_Sha384Free()
  11815. */
  11816. static int test_wc_Sha384Free(void)
  11817. {
  11818. EXPECT_DECLS;
  11819. #ifdef WOLFSSL_SHA384
  11820. wc_Sha384Free(NULL);
  11821. /* Set result to SUCCESS. */
  11822. ExpectTrue(1);
  11823. #endif
  11824. return EXPECT_RESULT();
  11825. } /* END test_wc_Sha384Free */
  11826. /*
  11827. * Unit test function for wc_Sha384GetHash()
  11828. */
  11829. static int test_wc_Sha384GetHash(void)
  11830. {
  11831. EXPECT_DECLS;
  11832. #ifdef WOLFSSL_SHA384
  11833. wc_Sha384 sha384;
  11834. byte hash1[WC_SHA384_DIGEST_SIZE];
  11835. /* Initialize */
  11836. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11837. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  11838. /* test bad arguments*/
  11839. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  11840. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  11841. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  11842. wc_Sha384Free(&sha384);
  11843. #endif
  11844. return EXPECT_RESULT();
  11845. } /* END test_wc_Sha384GetHash */
  11846. /*
  11847. * Unit test function for wc_Sha384Copy()
  11848. */
  11849. static int test_wc_Sha384Copy(void)
  11850. {
  11851. EXPECT_DECLS;
  11852. #ifdef WOLFSSL_SHA384
  11853. wc_Sha384 sha384;
  11854. wc_Sha384 temp;
  11855. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  11856. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  11857. /* Initialize */
  11858. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11859. ExpectIntEQ(wc_InitSha384(&temp), 0);
  11860. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  11861. /* test bad arguments*/
  11862. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  11863. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  11864. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  11865. wc_Sha384Free(&sha384);
  11866. wc_Sha384Free(&temp);
  11867. #endif
  11868. return EXPECT_RESULT();
  11869. } /* END test_wc_Sha384Copy */
  11870. /*
  11871. * Testing wc_InitSha224();
  11872. */
  11873. static int test_wc_InitSha224(void)
  11874. {
  11875. EXPECT_DECLS;
  11876. #ifdef WOLFSSL_SHA224
  11877. wc_Sha224 sha224;
  11878. /* Test good arg. */
  11879. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11880. /* Test bad arg. */
  11881. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  11882. wc_Sha224Free(&sha224);
  11883. #endif
  11884. return EXPECT_RESULT();
  11885. } /* END test_wc_InitSha224 */
  11886. /*
  11887. * Unit test on wc_Sha224Update
  11888. */
  11889. static int test_wc_Sha224Update(void)
  11890. {
  11891. EXPECT_DECLS;
  11892. #ifdef WOLFSSL_SHA224
  11893. wc_Sha224 sha224;
  11894. byte hash[WC_SHA224_DIGEST_SIZE];
  11895. testVector a, b, c;
  11896. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11897. /* Input. */
  11898. a.input = "a";
  11899. a.inLen = XSTRLEN(a.input);
  11900. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  11901. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  11902. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11903. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11904. /* Update input. */
  11905. a.input = "abc";
  11906. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11907. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11908. a.inLen = XSTRLEN(a.input);
  11909. a.outLen = XSTRLEN(a.output);
  11910. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11911. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11912. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  11913. /* Pass in bad values. */
  11914. b.input = NULL;
  11915. b.inLen = 0;
  11916. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  11917. c.input = NULL;
  11918. c.inLen = WC_SHA224_DIGEST_SIZE;
  11919. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  11920. BAD_FUNC_ARG);
  11921. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11922. BAD_FUNC_ARG);
  11923. wc_Sha224Free(&sha224);
  11924. #endif
  11925. return EXPECT_RESULT();
  11926. } /* END test_wc_Sha224Update */
  11927. /*
  11928. * Unit test for wc_Sha224Final();
  11929. */
  11930. static int test_wc_Sha224Final(void)
  11931. {
  11932. EXPECT_DECLS;
  11933. #ifdef WOLFSSL_SHA224
  11934. wc_Sha224 sha224;
  11935. byte* hash_test[3];
  11936. byte hash1[WC_SHA224_DIGEST_SIZE];
  11937. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11938. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11939. int times, i;
  11940. /* Initialize */
  11941. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11942. hash_test[0] = hash1;
  11943. hash_test[1] = hash2;
  11944. hash_test[2] = hash3;
  11945. times = sizeof(hash_test) / sizeof(byte*);
  11946. /* Good test args. */
  11947. /* Testing oversized buffers. */
  11948. for (i = 0; i < times; i++) {
  11949. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  11950. }
  11951. /* Test bad args. */
  11952. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  11953. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  11954. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  11955. wc_Sha224Free(&sha224);
  11956. #endif
  11957. return EXPECT_RESULT();
  11958. } /* END test_wc_Sha224Final */
  11959. /*
  11960. * Unit test function for wc_Sha224SetFlags()
  11961. */
  11962. static int test_wc_Sha224SetFlags(void)
  11963. {
  11964. EXPECT_DECLS;
  11965. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11966. wc_Sha224 sha224;
  11967. word32 flags = WC_HASH_FLAG_WILLCOPY;
  11968. /* Initialize */
  11969. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11970. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  11971. flags = 0;
  11972. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11973. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  11974. wc_Sha224Free(&sha224);
  11975. #endif
  11976. return EXPECT_RESULT();
  11977. } /* END test_wc_Sha224SetFlags */
  11978. /*
  11979. * Unit test function for wc_Sha224GetFlags()
  11980. */
  11981. static int test_wc_Sha224GetFlags(void)
  11982. {
  11983. EXPECT_DECLS;
  11984. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11985. wc_Sha224 sha224;
  11986. word32 flags = 0;
  11987. /* Initialize */
  11988. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11989. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11990. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11991. wc_Sha224Free(&sha224);
  11992. #endif
  11993. return EXPECT_RESULT();
  11994. } /* END test_wc_Sha224GetFlags */
  11995. /*
  11996. * Unit test function for wc_Sha224Free()
  11997. */
  11998. static int test_wc_Sha224Free(void)
  11999. {
  12000. EXPECT_DECLS;
  12001. #ifdef WOLFSSL_SHA224
  12002. wc_Sha224Free(NULL);
  12003. /* Set result to SUCCESS. */
  12004. ExpectTrue(1);
  12005. #endif
  12006. return EXPECT_RESULT();
  12007. } /* END test_wc_Sha224Free */
  12008. /*
  12009. * Unit test function for wc_Sha224GetHash()
  12010. */
  12011. static int test_wc_Sha224GetHash(void)
  12012. {
  12013. EXPECT_DECLS;
  12014. #ifdef WOLFSSL_SHA224
  12015. wc_Sha224 sha224;
  12016. byte hash1[WC_SHA224_DIGEST_SIZE];
  12017. /* Initialize */
  12018. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12019. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12020. /* test bad arguments*/
  12021. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12022. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12023. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12024. wc_Sha224Free(&sha224);
  12025. #endif
  12026. return EXPECT_RESULT();
  12027. } /* END test_wc_Sha224GetHash */
  12028. /*
  12029. * Unit test function for wc_Sha224Copy()
  12030. */
  12031. static int test_wc_Sha224Copy(void)
  12032. {
  12033. EXPECT_DECLS;
  12034. #ifdef WOLFSSL_SHA224
  12035. wc_Sha224 sha224;
  12036. wc_Sha224 temp;
  12037. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12038. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12039. /* Initialize */
  12040. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12041. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12042. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12043. /* test bad arguments*/
  12044. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12045. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12046. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12047. wc_Sha224Free(&sha224);
  12048. wc_Sha224Free(&temp);
  12049. #endif
  12050. return EXPECT_RESULT();
  12051. } /* END test_wc_Sha224Copy */
  12052. /*
  12053. * Testing wc_InitRipeMd()
  12054. */
  12055. static int test_wc_InitRipeMd(void)
  12056. {
  12057. EXPECT_DECLS;
  12058. #ifdef WOLFSSL_RIPEMD
  12059. RipeMd ripemd;
  12060. /* Test good arg. */
  12061. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12062. /* Test bad arg. */
  12063. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12064. #endif
  12065. return EXPECT_RESULT();
  12066. } /* END test_wc_InitRipeMd */
  12067. /*
  12068. * Testing wc_RipeMdUpdate()
  12069. */
  12070. static int test_wc_RipeMdUpdate(void)
  12071. {
  12072. EXPECT_DECLS;
  12073. #ifdef WOLFSSL_RIPEMD
  12074. RipeMd ripemd;
  12075. byte hash[RIPEMD_DIGEST_SIZE];
  12076. testVector a, b, c;
  12077. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12078. /* Input */
  12079. a.input = "a";
  12080. a.inLen = XSTRLEN(a.input);
  12081. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12082. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12083. /* Update input. */
  12084. a.input = "abc";
  12085. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12086. "\xb0\x87\xf1\x5a\x0b\xfc";
  12087. a.inLen = XSTRLEN(a.input);
  12088. a.outLen = XSTRLEN(a.output);
  12089. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12090. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12091. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12092. /* Pass in bad values. */
  12093. b.input = NULL;
  12094. b.inLen = 0;
  12095. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12096. c.input = NULL;
  12097. c.inLen = RIPEMD_DIGEST_SIZE;
  12098. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12099. BAD_FUNC_ARG);
  12100. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12101. BAD_FUNC_ARG);
  12102. #endif
  12103. return EXPECT_RESULT();
  12104. } /* END test_wc_RipeMdUdpate */
  12105. /*
  12106. * Unit test function for wc_RipeMdFinal()
  12107. */
  12108. static int test_wc_RipeMdFinal(void)
  12109. {
  12110. EXPECT_DECLS;
  12111. #ifdef WOLFSSL_RIPEMD
  12112. RipeMd ripemd;
  12113. byte* hash_test[3];
  12114. byte hash1[RIPEMD_DIGEST_SIZE];
  12115. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12116. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12117. int times, i;
  12118. /* Initialize */
  12119. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12120. hash_test[0] = hash1;
  12121. hash_test[1] = hash2;
  12122. hash_test[2] = hash3;
  12123. times = sizeof(hash_test) / sizeof(byte*);
  12124. /* Testing oversized buffers. */
  12125. for (i = 0; i < times; i++) {
  12126. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12127. }
  12128. /* Test bad args. */
  12129. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12130. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12131. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12132. #endif
  12133. return EXPECT_RESULT();
  12134. } /* END test_wc_RipeMdFinal */
  12135. /*
  12136. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12137. * wc_InitSha3_512
  12138. */
  12139. static int test_wc_InitSha3(void)
  12140. {
  12141. EXPECT_DECLS;
  12142. #if defined(WOLFSSL_SHA3)
  12143. wc_Sha3 sha3;
  12144. (void)sha3;
  12145. #if !defined(WOLFSSL_NOSHA3_224)
  12146. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12147. /* Test bad args. */
  12148. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12149. wc_Sha3_224_Free(&sha3);
  12150. #endif /* NOSHA3_224 */
  12151. #if !defined(WOLFSSL_NOSHA3_256)
  12152. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12153. /* Test bad args. */
  12154. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12155. wc_Sha3_256_Free(&sha3);
  12156. #endif /* NOSHA3_256 */
  12157. #if !defined(WOLFSSL_NOSHA3_384)
  12158. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12159. /* Test bad args. */
  12160. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12161. wc_Sha3_384_Free(&sha3);
  12162. #endif /* NOSHA3_384 */
  12163. #if !defined(WOLFSSL_NOSHA3_512)
  12164. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12165. /* Test bad args. */
  12166. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12167. wc_Sha3_512_Free(&sha3);
  12168. #endif /* NOSHA3_512 */
  12169. #endif
  12170. return EXPECT_RESULT();
  12171. } /* END test_wc_InitSha3 */
  12172. /*
  12173. * Testing wc_Sha3_Update()
  12174. */
  12175. static int testing_wc_Sha3_Update(void)
  12176. {
  12177. EXPECT_DECLS;
  12178. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12179. !defined(WOLFSSL_AFALG_XILINX)
  12180. wc_Sha3 sha3;
  12181. byte msg[] = "Everybody's working for the weekend.";
  12182. byte msg2[] = "Everybody gets Friday off.";
  12183. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12184. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12185. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12186. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12187. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12188. word32 msglen = sizeof(msg) - 1;
  12189. word32 msg2len = sizeof(msg2);
  12190. word32 msgCmplen = sizeof(msgCmp);
  12191. #if !defined(WOLFSSL_NOSHA3_224)
  12192. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12193. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12194. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12195. ExpectTrue(sha3.i == msglen);
  12196. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12197. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12198. /* Pass bad args. */
  12199. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12200. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12201. wc_Sha3_224_Free(&sha3);
  12202. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12203. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12204. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12205. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12206. wc_Sha3_224_Free(&sha3);
  12207. #endif /* SHA3_224 */
  12208. #if !defined(WOLFSSL_NOSHA3_256)
  12209. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12210. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12211. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12212. ExpectTrue(sha3.i == msglen);
  12213. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12214. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12215. /* Pass bad args. */
  12216. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12217. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12218. wc_Sha3_256_Free(&sha3);
  12219. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12220. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12221. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12222. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12223. wc_Sha3_256_Free(&sha3);
  12224. #endif /* SHA3_256 */
  12225. #if !defined(WOLFSSL_NOSHA3_384)
  12226. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12227. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12228. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12229. ExpectTrue(sha3.i == msglen);
  12230. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12231. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12232. /* Pass bad args. */
  12233. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12234. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12235. wc_Sha3_384_Free(&sha3);
  12236. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12237. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12238. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12239. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12240. wc_Sha3_384_Free(&sha3);
  12241. #endif /* SHA3_384 */
  12242. #if !defined(WOLFSSL_NOSHA3_512)
  12243. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12244. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12245. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12246. ExpectTrue(sha3.i == msglen);
  12247. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12248. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12249. /* Pass bad args. */
  12250. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12251. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12252. wc_Sha3_512_Free(&sha3);
  12253. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12254. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12255. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12256. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12257. wc_Sha3_512_Free(&sha3);
  12258. #endif /* SHA3_512 */
  12259. #endif /* WOLFSSL_SHA3 */
  12260. return EXPECT_RESULT();
  12261. } /* END testing_wc_Sha3_Update */
  12262. /*
  12263. * Testing wc_Sha3_224_Final()
  12264. */
  12265. static int test_wc_Sha3_224_Final(void)
  12266. {
  12267. EXPECT_DECLS;
  12268. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12269. wc_Sha3 sha3;
  12270. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12271. "nopnopq";
  12272. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12273. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12274. "\x64\xea\xd0\xfc\xce\x33";
  12275. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12276. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12277. /* Init stack variables. */
  12278. XMEMSET(hash, 0, sizeof(hash));
  12279. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12280. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12281. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12282. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12283. /* Test bad args. */
  12284. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12285. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12286. wc_Sha3_224_Free(&sha3);
  12287. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12288. /* Init stack variables. */
  12289. XMEMSET(hash, 0, sizeof(hash));
  12290. XMEMSET(hashRet, 0, sizeof(hashRet));
  12291. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12292. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12293. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12294. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12295. /* Test bad args. */
  12296. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12297. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12298. wc_Sha3_224_Free(&sha3);
  12299. #endif
  12300. return EXPECT_RESULT();
  12301. } /* END test_wc_Sha3_224_Final */
  12302. /*
  12303. * Testing wc_Sha3_256_Final()
  12304. */
  12305. static int test_wc_Sha3_256_Final(void)
  12306. {
  12307. EXPECT_DECLS;
  12308. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12309. wc_Sha3 sha3;
  12310. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12311. "nopnopq";
  12312. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12313. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12314. "\xdd\x97\x49\x6d\x33\x76";
  12315. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12316. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12317. /* Init stack variables. */
  12318. XMEMSET(hash, 0, sizeof(hash));
  12319. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12320. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12321. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12322. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12323. /* Test bad args. */
  12324. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12325. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12326. wc_Sha3_256_Free(&sha3);
  12327. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12328. /* Init stack variables. */
  12329. XMEMSET(hash, 0, sizeof(hash));
  12330. XMEMSET(hashRet, 0, sizeof(hashRet));
  12331. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12332. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12333. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12334. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12335. /* Test bad args. */
  12336. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12337. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12338. wc_Sha3_256_Free(&sha3);
  12339. #endif
  12340. return EXPECT_RESULT();
  12341. } /* END test_wc_Sha3_256_Final */
  12342. /*
  12343. * Testing wc_Sha3_384_Final()
  12344. */
  12345. static int test_wc_Sha3_384_Final(void)
  12346. {
  12347. EXPECT_DECLS;
  12348. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12349. wc_Sha3 sha3;
  12350. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12351. "nopnopq";
  12352. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12353. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12354. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12355. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12356. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12357. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12358. /* Init stack variables. */
  12359. XMEMSET(hash, 0, sizeof(hash));
  12360. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12361. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12362. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12363. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12364. /* Test bad args. */
  12365. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12366. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12367. wc_Sha3_384_Free(&sha3);
  12368. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12369. /* Init stack variables. */
  12370. XMEMSET(hash, 0, sizeof(hash));
  12371. XMEMSET(hashRet, 0, sizeof(hashRet));
  12372. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12373. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12374. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12375. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12376. /* Test bad args. */
  12377. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12378. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12379. wc_Sha3_384_Free(&sha3);
  12380. #endif
  12381. return EXPECT_RESULT();
  12382. } /* END test_wc_Sha3_384_Final */
  12383. /*
  12384. * Testing wc_Sha3_512_Final()
  12385. */
  12386. static int test_wc_Sha3_512_Final(void)
  12387. {
  12388. EXPECT_DECLS;
  12389. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12390. !defined(WOLFSSL_NOSHA3_384)
  12391. wc_Sha3 sha3;
  12392. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12393. "nopnopq";
  12394. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12395. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12396. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12397. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12398. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12399. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12400. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12401. /* Init stack variables. */
  12402. XMEMSET(hash, 0, sizeof(hash));
  12403. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12404. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12405. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12406. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12407. /* Test bad args. */
  12408. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12409. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12410. wc_Sha3_512_Free(&sha3);
  12411. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12412. /* Init stack variables. */
  12413. XMEMSET(hash, 0, sizeof(hash));
  12414. XMEMSET(hashRet, 0, sizeof(hashRet));
  12415. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12416. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12417. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12418. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12419. /* Test bad args. */
  12420. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12421. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12422. wc_Sha3_512_Free(&sha3);
  12423. #endif
  12424. return EXPECT_RESULT();
  12425. } /* END test_wc_Sha3_512_Final */
  12426. /*
  12427. * Testing wc_Sha3_224_Copy()
  12428. */
  12429. static int test_wc_Sha3_224_Copy(void)
  12430. {
  12431. EXPECT_DECLS;
  12432. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12433. wc_Sha3 sha3, sha3Cpy;
  12434. const char* msg = TEST_STRING;
  12435. word32 msglen = (word32)TEST_STRING_SZ;
  12436. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12437. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12438. XMEMSET(hash, 0, sizeof(hash));
  12439. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12440. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12441. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12442. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12443. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12444. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12445. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12446. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12447. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12448. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12449. /* Test bad args. */
  12450. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12451. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12452. wc_Sha3_224_Free(&sha3);
  12453. wc_Sha3_224_Free(&sha3Cpy);
  12454. #endif
  12455. return EXPECT_RESULT();
  12456. } /* END test_wc_Sha3_224_Copy */
  12457. /*
  12458. * Testing wc_Sha3_256_Copy()
  12459. */
  12460. static int test_wc_Sha3_256_Copy(void)
  12461. {
  12462. EXPECT_DECLS;
  12463. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12464. wc_Sha3 sha3, sha3Cpy;
  12465. const char* msg = TEST_STRING;
  12466. word32 msglen = (word32)TEST_STRING_SZ;
  12467. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12468. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12469. XMEMSET(hash, 0, sizeof(hash));
  12470. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12471. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12472. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12473. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12474. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12475. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  12476. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  12477. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12478. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  12479. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12480. /* Test bad args. */
  12481. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12482. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12483. wc_Sha3_256_Free(&sha3);
  12484. wc_Sha3_256_Free(&sha3Cpy);
  12485. #endif
  12486. return EXPECT_RESULT();
  12487. } /* END test_wc_Sha3_256_Copy */
  12488. /*
  12489. * Testing wc_Sha3_384_Copy()
  12490. */
  12491. static int test_wc_Sha3_384_Copy(void)
  12492. {
  12493. EXPECT_DECLS;
  12494. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12495. wc_Sha3 sha3, sha3Cpy;
  12496. const char* msg = TEST_STRING;
  12497. word32 msglen = (word32)TEST_STRING_SZ;
  12498. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12499. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12500. XMEMSET(hash, 0, sizeof(hash));
  12501. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12502. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12503. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12504. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12505. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12506. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  12507. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  12508. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12509. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  12510. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12511. /* Test bad args. */
  12512. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12513. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12514. wc_Sha3_384_Free(&sha3);
  12515. wc_Sha3_384_Free(&sha3Cpy);
  12516. #endif
  12517. return EXPECT_RESULT();
  12518. } /* END test_wc_Sha3_384_Copy */
  12519. /*
  12520. * Testing wc_Sha3_512_Copy()
  12521. */
  12522. static int test_wc_Sha3_512_Copy(void)
  12523. {
  12524. EXPECT_DECLS;
  12525. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12526. wc_Sha3 sha3, sha3Cpy;
  12527. const char* msg = TEST_STRING;
  12528. word32 msglen = (word32)TEST_STRING_SZ;
  12529. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12530. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12531. XMEMSET(hash, 0, sizeof(hash));
  12532. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12533. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12534. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12535. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12536. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12537. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  12538. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  12539. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12540. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  12541. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12542. /* Test bad args. */
  12543. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12544. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12545. wc_Sha3_512_Free(&sha3);
  12546. wc_Sha3_512_Free(&sha3Cpy);
  12547. #endif
  12548. return EXPECT_RESULT();
  12549. } /* END test_wc_Sha3_512_Copy */
  12550. /*
  12551. * Unit test function for wc_Sha3_GetFlags()
  12552. */
  12553. static int test_wc_Sha3_GetFlags(void)
  12554. {
  12555. EXPECT_DECLS;
  12556. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12557. wc_Sha3 sha3;
  12558. word32 flags = 0;
  12559. /* Initialize */
  12560. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12561. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  12562. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12563. wc_Sha3_224_Free(&sha3);
  12564. #endif
  12565. return EXPECT_RESULT();
  12566. } /* END test_wc_Sha3_GetFlags */
  12567. static int test_wc_InitShake256(void)
  12568. {
  12569. EXPECT_DECLS;
  12570. #ifdef WOLFSSL_SHAKE256
  12571. wc_Shake shake;
  12572. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12573. /* Test bad args. */
  12574. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12575. wc_Shake256_Free(&shake);
  12576. #endif
  12577. return EXPECT_RESULT();
  12578. }
  12579. static int testing_wc_Shake256_Update(void)
  12580. {
  12581. EXPECT_DECLS;
  12582. #ifdef WOLFSSL_SHAKE256
  12583. wc_Shake shake;
  12584. byte msg[] = "Everybody's working for the weekend.";
  12585. byte msg2[] = "Everybody gets Friday off.";
  12586. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12587. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12588. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12589. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12590. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12591. word32 msglen = sizeof(msg) - 1;
  12592. word32 msg2len = sizeof(msg2);
  12593. word32 msgCmplen = sizeof(msgCmp);
  12594. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12595. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  12596. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  12597. ExpectTrue(shake.i == msglen);
  12598. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12599. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  12600. /* Pass bad args. */
  12601. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12602. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  12603. wc_Shake256_Free(&shake);
  12604. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12605. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  12606. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12607. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  12608. wc_Shake256_Free(&shake);
  12609. #endif /* WOLFSSL_SHAKE256 */
  12610. return EXPECT_RESULT();
  12611. }
  12612. static int test_wc_Shake256_Final(void)
  12613. {
  12614. EXPECT_DECLS;
  12615. #ifdef WOLFSSL_SHAKE256
  12616. wc_Shake shake;
  12617. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12618. "nopnopq";
  12619. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12620. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12621. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12622. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12623. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12624. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12625. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12626. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12627. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12628. byte hash[114];
  12629. /* Init stack variables. */
  12630. XMEMSET(hash, 0, sizeof(hash));
  12631. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12632. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  12633. 0);
  12634. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  12635. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  12636. /* Test bad args. */
  12637. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  12638. BAD_FUNC_ARG);
  12639. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  12640. BAD_FUNC_ARG);
  12641. wc_Shake256_Free(&shake);
  12642. #endif
  12643. return EXPECT_RESULT();
  12644. }
  12645. /*
  12646. * Testing wc_Shake256_Copy()
  12647. */
  12648. static int test_wc_Shake256_Copy(void)
  12649. {
  12650. EXPECT_DECLS;
  12651. #ifdef WOLFSSL_SHAKE256
  12652. wc_Shake shake, shakeCpy;
  12653. const char* msg = TEST_STRING;
  12654. word32 msglen = (word32)TEST_STRING_SZ;
  12655. byte hash[144];
  12656. byte hashCpy[144];
  12657. word32 hashLen = sizeof(hash);
  12658. word32 hashLenCpy = sizeof(hashCpy);
  12659. XMEMSET(hash, 0, sizeof(hash));
  12660. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12661. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12662. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  12663. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  12664. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  12665. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  12666. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  12667. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12668. /* Test bad args. */
  12669. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  12670. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  12671. wc_Shake256_Free(&shake);
  12672. wc_Shake256_Free(&shakeCpy);
  12673. #endif
  12674. return EXPECT_RESULT();
  12675. } /* END test_wc_Shake256_Copy */
  12676. /*
  12677. * Unit test function for wc_Shake256Hash()
  12678. */
  12679. static int test_wc_Shake256Hash(void)
  12680. {
  12681. EXPECT_DECLS;
  12682. #ifdef WOLFSSL_SHAKE256
  12683. const byte data[] = { /* Hello World */
  12684. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12685. 0x72,0x6c,0x64
  12686. };
  12687. word32 len = sizeof(data);
  12688. byte hash[144];
  12689. word32 hashLen = sizeof(hash);
  12690. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  12691. #endif
  12692. return EXPECT_RESULT();
  12693. } /* END test_wc_Shake256Hash */
  12694. /*
  12695. * Testing wc_InitSm3(), wc_Sm3Free()
  12696. */
  12697. static int test_wc_InitSm3Free(void)
  12698. {
  12699. EXPECT_DECLS;
  12700. #ifdef WOLFSSL_SM3
  12701. wc_Sm3 sm3;
  12702. /* Invalid Parameters */
  12703. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  12704. /* Valid Parameters */
  12705. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12706. wc_Sm3Free(NULL);
  12707. wc_Sm3Free(&sm3);
  12708. #endif
  12709. return EXPECT_RESULT();
  12710. } /* END test_wc_InitSm3 */
  12711. /*
  12712. * Testing wc_Sm3Update(), wc_Sm3Final()
  12713. */
  12714. static int test_wc_Sm3UpdateFinal(void)
  12715. {
  12716. EXPECT_DECLS;
  12717. #ifdef WOLFSSL_SM3
  12718. wc_Sm3 sm3;
  12719. byte data[WC_SM3_BLOCK_SIZE * 4];
  12720. byte hash[WC_SM3_DIGEST_SIZE];
  12721. byte calcHash[WC_SM3_DIGEST_SIZE];
  12722. byte expHash[WC_SM3_DIGEST_SIZE] = {
  12723. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  12724. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  12725. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  12726. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  12727. };
  12728. word32 chunk;
  12729. word32 i;
  12730. XMEMSET(data, 0, sizeof(data));
  12731. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12732. /* Invalid Parameters */
  12733. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  12734. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  12735. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  12736. /* Valid Parameters */
  12737. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  12738. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12739. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12740. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  12741. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  12742. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  12743. /* Ensure too many bytes for lengths. */
  12744. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  12745. /* Invalid Parameters */
  12746. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  12747. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  12748. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  12749. /* Valid Parameters */
  12750. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12751. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  12752. /* Chunk tests. */
  12753. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12754. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12755. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  12756. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  12757. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  12758. }
  12759. if (i < (word32)sizeof(data)) {
  12760. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  12761. 0);
  12762. }
  12763. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12764. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12765. }
  12766. /* Not testing when the low 32-bit length overflows. */
  12767. wc_Sm3Free(&sm3);
  12768. #endif
  12769. return EXPECT_RESULT();
  12770. } /* END test_wc_Sm3Update */
  12771. /*
  12772. * Testing wc_Sm3GetHash()
  12773. */
  12774. static int test_wc_Sm3GetHash(void)
  12775. {
  12776. EXPECT_DECLS;
  12777. #ifdef WOLFSSL_SM3
  12778. wc_Sm3 sm3;
  12779. byte hash[WC_SM3_DIGEST_SIZE];
  12780. byte calcHash[WC_SM3_DIGEST_SIZE];
  12781. byte data[WC_SM3_BLOCK_SIZE];
  12782. XMEMSET(data, 0, sizeof(data));
  12783. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12784. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12785. /* Invalid Parameters */
  12786. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  12787. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  12788. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  12789. /* Valid Parameters */
  12790. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12791. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12792. /* With update. */
  12793. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12794. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12795. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12796. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12797. wc_Sm3Free(&sm3);
  12798. #endif
  12799. return EXPECT_RESULT();
  12800. } /* END test_wc_Sm3Update */
  12801. /*
  12802. * Testing wc_Sm3Copy()
  12803. */
  12804. static int test_wc_Sm3Copy(void)
  12805. {
  12806. EXPECT_DECLS;
  12807. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12808. wc_Sm3 sm3;
  12809. wc_Sm3 sm3Copy;
  12810. byte hash[WC_SM3_DIGEST_SIZE];
  12811. byte hashCopy[WC_SM3_DIGEST_SIZE];
  12812. byte data[WC_SM3_BLOCK_SIZE + 1];
  12813. int i;
  12814. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12815. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12816. /* Invalid Parameters */
  12817. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  12818. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  12819. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  12820. /* Valid Parameters */
  12821. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12822. /* Ensure all parts of data updated during hashing are copied. */
  12823. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  12824. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  12825. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12826. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12827. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  12828. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12829. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  12830. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  12831. }
  12832. wc_Sm3Free(&sm3Copy);
  12833. wc_Sm3Free(&sm3);
  12834. #endif
  12835. return EXPECT_RESULT();
  12836. } /* END test_wc_Sm3Copy */
  12837. /*
  12838. * Testing wc_Sm3FinalRaw()
  12839. */
  12840. static int test_wc_Sm3FinalRaw(void)
  12841. {
  12842. EXPECT_DECLS;
  12843. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  12844. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  12845. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  12846. !defined(WOLFSSL_NO_HASH_RAW)
  12847. wc_Sm3 sm3;
  12848. byte hash1[WC_SM3_DIGEST_SIZE];
  12849. byte hash2[WC_SM3_DIGEST_SIZE];
  12850. byte hash3[WC_SM3_DIGEST_SIZE];
  12851. byte* hash_test[3] = { hash1, hash2, hash3 };
  12852. int times;
  12853. int i;
  12854. XMEMSET(&sm3, 0, sizeof(sm3));
  12855. /* Initialize */
  12856. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12857. /* Invalid Parameters */
  12858. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12859. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  12860. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12861. times = sizeof(hash_test) / sizeof(byte*);
  12862. for (i = 0; i < times; i++) {
  12863. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  12864. }
  12865. wc_Sm3Free(&sm3);
  12866. #endif
  12867. return EXPECT_RESULT();
  12868. } /* END test_wc_Sm3FinalRaw */
  12869. /*
  12870. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  12871. */
  12872. static int test_wc_Sm3GetSetFlags(void)
  12873. {
  12874. EXPECT_DECLS;
  12875. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12876. wc_Sm3 sm3;
  12877. wc_Sm3 sm3Copy;
  12878. word32 flags = 0;
  12879. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12880. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12881. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12882. ExpectIntEQ(flags, 0);
  12883. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  12884. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12885. ExpectIntEQ(flags, 0);
  12886. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12887. ExpectIntEQ(flags, 0);
  12888. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  12889. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12890. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  12891. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12892. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  12893. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  12894. wc_Sm3Free(&sm3Copy);
  12895. wc_Sm3Free(&sm3);
  12896. #endif
  12897. return EXPECT_RESULT();
  12898. } /* END test_wc_Sm3Update */
  12899. /*
  12900. * Testing wc_Sm3Hash()
  12901. */
  12902. static int test_wc_Sm3Hash(void)
  12903. {
  12904. EXPECT_DECLS;
  12905. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12906. byte data[WC_SM3_BLOCK_SIZE];
  12907. byte hash[WC_SM3_DIGEST_SIZE];
  12908. /* Invalid parameters. */
  12909. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  12910. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  12911. /* Valid parameters. */
  12912. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  12913. #endif
  12914. return EXPECT_RESULT();
  12915. } /* END test_wc_Sm3Hash */
  12916. /*
  12917. * Test function for wc_HmacSetKey
  12918. */
  12919. static int test_wc_Md5HmacSetKey(void)
  12920. {
  12921. EXPECT_DECLS;
  12922. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12923. Hmac hmac;
  12924. int ret, times, itr;
  12925. const char* keys[]=
  12926. {
  12927. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12928. #ifndef HAVE_FIPS
  12929. "Jefe", /* smaller than minimum FIPS key size */
  12930. #endif
  12931. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12932. };
  12933. times = sizeof(keys) / sizeof(char*);
  12934. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12935. for (itr = 0; itr < times; itr++) {
  12936. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12937. (word32)XSTRLEN(keys[itr]));
  12938. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12939. wc_HmacFree(&hmac);
  12940. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12941. #else
  12942. ExpectIntEQ(ret, 0);
  12943. #endif
  12944. }
  12945. /* Bad args. */
  12946. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12947. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12948. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  12949. BAD_FUNC_ARG);
  12950. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12951. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12952. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12953. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12954. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12955. #elif defined(HAVE_FIPS)
  12956. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12957. #else
  12958. ExpectIntEQ(ret, 0);
  12959. #endif
  12960. wc_HmacFree(&hmac);
  12961. #endif
  12962. return EXPECT_RESULT();
  12963. } /* END test_wc_Md5HmacSetKey */
  12964. /*
  12965. * testing wc_HmacSetKey() on wc_Sha hash.
  12966. */
  12967. static int test_wc_ShaHmacSetKey(void)
  12968. {
  12969. EXPECT_DECLS;
  12970. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12971. Hmac hmac;
  12972. int ret, times, itr;
  12973. const char* keys[]=
  12974. {
  12975. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12976. "\x0b\x0b\x0b",
  12977. #ifndef HAVE_FIPS
  12978. "Jefe", /* smaller than minimum FIPS key size */
  12979. #endif
  12980. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12981. "\xAA\xAA\xAA"
  12982. };
  12983. times = sizeof(keys) / sizeof(char*);
  12984. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12985. for (itr = 0; itr < times; itr++) {
  12986. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12987. (word32)XSTRLEN(keys[itr])), 0);
  12988. }
  12989. /* Bad args. */
  12990. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12991. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12992. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  12993. BAD_FUNC_ARG);
  12994. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12995. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12996. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12997. #ifdef HAVE_FIPS
  12998. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12999. #else
  13000. ExpectIntEQ(ret, 0);
  13001. #endif
  13002. wc_HmacFree(&hmac);
  13003. #endif
  13004. return EXPECT_RESULT();
  13005. } /* END test_wc_ShaHmacSetKey() */
  13006. /*
  13007. * testing wc_HmacSetKey() on Sha224 hash.
  13008. */
  13009. static int test_wc_Sha224HmacSetKey(void)
  13010. {
  13011. EXPECT_DECLS;
  13012. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13013. Hmac hmac;
  13014. int ret, times, itr;
  13015. const char* keys[]=
  13016. {
  13017. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13018. "\x0b\x0b\x0b",
  13019. #ifndef HAVE_FIPS
  13020. "Jefe", /* smaller than minimum FIPS key size */
  13021. #endif
  13022. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13023. "\xAA\xAA\xAA"
  13024. };
  13025. times = sizeof(keys) / sizeof(char*);
  13026. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13027. for (itr = 0; itr < times; itr++) {
  13028. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13029. (word32)XSTRLEN(keys[itr])), 0);
  13030. }
  13031. /* Bad args. */
  13032. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13033. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13034. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13035. BAD_FUNC_ARG);
  13036. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13037. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13038. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13039. #ifdef HAVE_FIPS
  13040. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13041. #else
  13042. ExpectIntEQ(ret, 0);
  13043. #endif
  13044. wc_HmacFree(&hmac);
  13045. #endif
  13046. return EXPECT_RESULT();
  13047. } /* END test_wc_Sha224HmacSetKey() */
  13048. /*
  13049. * testing wc_HmacSetKey() on Sha256 hash
  13050. */
  13051. static int test_wc_Sha256HmacSetKey(void)
  13052. {
  13053. EXPECT_DECLS;
  13054. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13055. Hmac hmac;
  13056. int ret, times, itr;
  13057. const char* keys[]=
  13058. {
  13059. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13060. "\x0b\x0b\x0b",
  13061. #ifndef HAVE_FIPS
  13062. "Jefe", /* smaller than minimum FIPS key size */
  13063. #endif
  13064. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13065. "\xAA\xAA\xAA"
  13066. };
  13067. times = sizeof(keys) / sizeof(char*);
  13068. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13069. for (itr = 0; itr < times; itr++) {
  13070. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13071. (word32)XSTRLEN(keys[itr])), 0);
  13072. }
  13073. /* Bad args. */
  13074. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13075. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13076. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13077. BAD_FUNC_ARG);
  13078. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13079. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13080. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13081. #ifdef HAVE_FIPS
  13082. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13083. #else
  13084. ExpectIntEQ(ret, 0);
  13085. #endif
  13086. wc_HmacFree(&hmac);
  13087. #endif
  13088. return EXPECT_RESULT();
  13089. } /* END test_wc_Sha256HmacSetKey() */
  13090. /*
  13091. * testing wc_HmacSetKey on Sha384 hash.
  13092. */
  13093. static int test_wc_Sha384HmacSetKey(void)
  13094. {
  13095. EXPECT_DECLS;
  13096. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13097. Hmac hmac;
  13098. int ret, times, itr;
  13099. const char* keys[]=
  13100. {
  13101. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13102. "\x0b\x0b\x0b",
  13103. #ifndef HAVE_FIPS
  13104. "Jefe", /* smaller than minimum FIPS key size */
  13105. #endif
  13106. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13107. "\xAA\xAA\xAA"
  13108. };
  13109. times = sizeof(keys) / sizeof(char*);
  13110. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13111. for (itr = 0; itr < times; itr++) {
  13112. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13113. (word32)XSTRLEN(keys[itr])), 0);
  13114. }
  13115. /* Bad args. */
  13116. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13117. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13118. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13119. BAD_FUNC_ARG);
  13120. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13121. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13122. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13123. #ifdef HAVE_FIPS
  13124. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13125. #else
  13126. ExpectIntEQ(ret, 0);
  13127. #endif
  13128. wc_HmacFree(&hmac);
  13129. #endif
  13130. return EXPECT_RESULT();
  13131. } /* END test_wc_Sha384HmacSetKey() */
  13132. /*
  13133. * testing wc_HmacUpdate on wc_Md5 hash.
  13134. */
  13135. static int test_wc_Md5HmacUpdate(void)
  13136. {
  13137. EXPECT_DECLS;
  13138. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13139. Hmac hmac;
  13140. testVector a, b;
  13141. #ifdef HAVE_FIPS
  13142. const char* keys =
  13143. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13144. #else
  13145. const char* keys = "Jefe";
  13146. #endif
  13147. a.input = "what do ya want for nothing?";
  13148. a.inLen = XSTRLEN(a.input);
  13149. b.input = "Hi There";
  13150. b.inLen = XSTRLEN(b.input);
  13151. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13152. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13153. (word32)XSTRLEN(keys)), 0);
  13154. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13155. /* Update Hmac. */
  13156. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13157. /* Test bad args. */
  13158. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13159. BAD_FUNC_ARG);
  13160. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13161. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13162. wc_HmacFree(&hmac);
  13163. #endif
  13164. return EXPECT_RESULT();
  13165. } /* END test_wc_Md5HmacUpdate */
  13166. /*
  13167. * testing wc_HmacUpdate on SHA hash.
  13168. */
  13169. static int test_wc_ShaHmacUpdate(void)
  13170. {
  13171. EXPECT_DECLS;
  13172. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13173. Hmac hmac;
  13174. testVector a, b;
  13175. #ifdef HAVE_FIPS
  13176. const char* keys =
  13177. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13178. #else
  13179. const char* keys = "Jefe";
  13180. #endif
  13181. a.input = "what do ya want for nothing?";
  13182. a.inLen = XSTRLEN(a.input);
  13183. b.input = "Hi There";
  13184. b.inLen = XSTRLEN(b.input);
  13185. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13186. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13187. (word32)XSTRLEN(keys)), 0);
  13188. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13189. /* Update Hmac. */
  13190. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13191. /* Test bad args. */
  13192. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13193. BAD_FUNC_ARG);
  13194. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13195. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13196. wc_HmacFree(&hmac);
  13197. #endif
  13198. return EXPECT_RESULT();
  13199. } /* END test_wc_ShaHmacUpdate */
  13200. /*
  13201. * testing wc_HmacUpdate on SHA224 hash.
  13202. */
  13203. static int test_wc_Sha224HmacUpdate(void)
  13204. {
  13205. EXPECT_DECLS;
  13206. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13207. Hmac hmac;
  13208. testVector a, b;
  13209. #ifdef HAVE_FIPS
  13210. const char* keys =
  13211. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13212. #else
  13213. const char* keys = "Jefe";
  13214. #endif
  13215. a.input = "what do ya want for nothing?";
  13216. a.inLen = XSTRLEN(a.input);
  13217. b.input = "Hi There";
  13218. b.inLen = XSTRLEN(b.input);
  13219. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13220. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13221. (word32)XSTRLEN(keys)), 0);
  13222. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13223. /* Update Hmac. */
  13224. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13225. /* Test bad args. */
  13226. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13227. BAD_FUNC_ARG);
  13228. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13229. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13230. wc_HmacFree(&hmac);
  13231. #endif
  13232. return EXPECT_RESULT();
  13233. } /* END test_wc_Sha224HmacUpdate */
  13234. /*
  13235. * testing wc_HmacUpdate on SHA256 hash.
  13236. */
  13237. static int test_wc_Sha256HmacUpdate(void)
  13238. {
  13239. EXPECT_DECLS;
  13240. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13241. Hmac hmac;
  13242. testVector a, b;
  13243. #ifdef HAVE_FIPS
  13244. const char* keys =
  13245. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13246. #else
  13247. const char* keys = "Jefe";
  13248. #endif
  13249. a.input = "what do ya want for nothing?";
  13250. a.inLen = XSTRLEN(a.input);
  13251. b.input = "Hi There";
  13252. b.inLen = XSTRLEN(b.input);
  13253. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13254. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13255. (word32)XSTRLEN(keys)), 0);
  13256. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13257. /* Update Hmac. */
  13258. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13259. /* Test bad args. */
  13260. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13261. BAD_FUNC_ARG);
  13262. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13263. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13264. wc_HmacFree(&hmac);
  13265. #endif
  13266. return EXPECT_RESULT();
  13267. } /* END test_wc_Sha256HmacUpdate */
  13268. /*
  13269. * testing wc_HmacUpdate on SHA384 hash.
  13270. */
  13271. static int test_wc_Sha384HmacUpdate(void)
  13272. {
  13273. EXPECT_DECLS;
  13274. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13275. Hmac hmac;
  13276. testVector a, b;
  13277. #ifdef HAVE_FIPS
  13278. const char* keys =
  13279. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13280. #else
  13281. const char* keys = "Jefe";
  13282. #endif
  13283. a.input = "what do ya want for nothing?";
  13284. a.inLen = XSTRLEN(a.input);
  13285. b.input = "Hi There";
  13286. b.inLen = XSTRLEN(b.input);
  13287. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13288. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13289. (word32)XSTRLEN(keys)), 0);
  13290. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13291. /* Update Hmac. */
  13292. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13293. /* Test bad args. */
  13294. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13295. BAD_FUNC_ARG);
  13296. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13297. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13298. wc_HmacFree(&hmac);
  13299. #endif
  13300. return EXPECT_RESULT();
  13301. } /* END test_wc_Sha384HmacUpdate */
  13302. /*
  13303. * Testing wc_HmacFinal() with MD5
  13304. */
  13305. static int test_wc_Md5HmacFinal(void)
  13306. {
  13307. EXPECT_DECLS;
  13308. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13309. Hmac hmac;
  13310. byte hash[WC_MD5_DIGEST_SIZE];
  13311. testVector a;
  13312. const char* key;
  13313. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13314. a.input = "Hi There";
  13315. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13316. "\x9d";
  13317. a.inLen = XSTRLEN(a.input);
  13318. a.outLen = XSTRLEN(a.output);
  13319. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13320. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13321. 0);
  13322. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13323. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13324. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13325. /* Try bad parameters. */
  13326. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13327. #ifndef HAVE_FIPS
  13328. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13329. #endif
  13330. wc_HmacFree(&hmac);
  13331. #endif
  13332. return EXPECT_RESULT();
  13333. } /* END test_wc_Md5HmacFinal */
  13334. /*
  13335. * Testing wc_HmacFinal() with SHA
  13336. */
  13337. static int test_wc_ShaHmacFinal(void)
  13338. {
  13339. EXPECT_DECLS;
  13340. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13341. Hmac hmac;
  13342. byte hash[WC_SHA_DIGEST_SIZE];
  13343. testVector a;
  13344. const char* key;
  13345. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13346. "\x0b\x0b\x0b";
  13347. a.input = "Hi There";
  13348. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13349. "\x8e\xf1\x46\xbe\x00";
  13350. a.inLen = XSTRLEN(a.input);
  13351. a.outLen = XSTRLEN(a.output);
  13352. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13353. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13354. 0);
  13355. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13356. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13357. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13358. /* Try bad parameters. */
  13359. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13360. #ifndef HAVE_FIPS
  13361. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13362. #endif
  13363. wc_HmacFree(&hmac);
  13364. #endif
  13365. return EXPECT_RESULT();
  13366. } /* END test_wc_ShaHmacFinal */
  13367. /*
  13368. * Testing wc_HmacFinal() with SHA224
  13369. */
  13370. static int test_wc_Sha224HmacFinal(void)
  13371. {
  13372. EXPECT_DECLS;
  13373. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13374. Hmac hmac;
  13375. byte hash[WC_SHA224_DIGEST_SIZE];
  13376. testVector a;
  13377. const char* key;
  13378. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13379. "\x0b\x0b\x0b";
  13380. a.input = "Hi There";
  13381. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13382. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13383. a.inLen = XSTRLEN(a.input);
  13384. a.outLen = XSTRLEN(a.output);
  13385. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13386. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13387. (word32)XSTRLEN(key)), 0);
  13388. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13389. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13390. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13391. /* Try bad parameters. */
  13392. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13393. #ifndef HAVE_FIPS
  13394. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13395. #endif
  13396. wc_HmacFree(&hmac);
  13397. #endif
  13398. return EXPECT_RESULT();
  13399. } /* END test_wc_Sha224HmacFinal */
  13400. /*
  13401. * Testing wc_HmacFinal() with SHA256
  13402. */
  13403. static int test_wc_Sha256HmacFinal(void)
  13404. {
  13405. EXPECT_DECLS;
  13406. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13407. Hmac hmac;
  13408. byte hash[WC_SHA256_DIGEST_SIZE];
  13409. testVector a;
  13410. const char* key;
  13411. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13412. "\x0b\x0b\x0b";
  13413. a.input = "Hi There";
  13414. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13415. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13416. "\xcf\xf7";
  13417. a.inLen = XSTRLEN(a.input);
  13418. a.outLen = XSTRLEN(a.output);
  13419. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13420. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13421. (word32)XSTRLEN(key)), 0);
  13422. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13423. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13424. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13425. /* Try bad parameters. */
  13426. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13427. #ifndef HAVE_FIPS
  13428. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13429. #endif
  13430. wc_HmacFree(&hmac);
  13431. #endif
  13432. return EXPECT_RESULT();
  13433. } /* END test_wc_Sha256HmacFinal */
  13434. /*
  13435. * Testing wc_HmacFinal() with SHA384
  13436. */
  13437. static int test_wc_Sha384HmacFinal(void)
  13438. {
  13439. EXPECT_DECLS;
  13440. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13441. Hmac hmac;
  13442. byte hash[WC_SHA384_DIGEST_SIZE];
  13443. testVector a;
  13444. const char* key;
  13445. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13446. "\x0b\x0b\x0b";
  13447. a.input = "Hi There";
  13448. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13449. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13450. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13451. "\xfa\x9c\xb6";
  13452. a.inLen = XSTRLEN(a.input);
  13453. a.outLen = XSTRLEN(a.output);
  13454. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13455. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13456. (word32)XSTRLEN(key)), 0);
  13457. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13458. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13459. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13460. /* Try bad parameters. */
  13461. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13462. #ifndef HAVE_FIPS
  13463. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13464. #endif
  13465. wc_HmacFree(&hmac);
  13466. #endif
  13467. return EXPECT_RESULT();
  13468. } /* END test_wc_Sha384HmacFinal */
  13469. /*
  13470. * Testing wc_InitCmac()
  13471. */
  13472. static int test_wc_InitCmac(void)
  13473. {
  13474. EXPECT_DECLS;
  13475. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13476. Cmac cmac1;
  13477. Cmac cmac2;
  13478. Cmac cmac3;
  13479. /* AES 128 key. */
  13480. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13481. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13482. /* AES 192 key. */
  13483. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13484. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13485. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13486. /* AES 256 key. */
  13487. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13488. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13489. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13490. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13491. word32 key1Sz = (word32)sizeof(key1) - 1;
  13492. word32 key2Sz = (word32)sizeof(key2) - 1;
  13493. word32 key3Sz = (word32)sizeof(key3) - 1;
  13494. int type = WC_CMAC_AES;
  13495. (void)key1;
  13496. (void)key1Sz;
  13497. (void)key2;
  13498. (void)key2Sz;
  13499. XMEMSET(&cmac1, 0, sizeof(Cmac));
  13500. XMEMSET(&cmac2, 0, sizeof(Cmac));
  13501. XMEMSET(&cmac3, 0, sizeof(Cmac));
  13502. #ifdef WOLFSSL_AES_128
  13503. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  13504. #endif
  13505. #ifdef WOLFSSL_AES_192
  13506. wc_AesFree(&cmac1.aes);
  13507. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  13508. #endif
  13509. #ifdef WOLFSSL_AES_256
  13510. wc_AesFree(&cmac2.aes);
  13511. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  13512. #endif
  13513. wc_AesFree(&cmac3.aes);
  13514. /* Test bad args. */
  13515. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  13516. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  13517. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  13518. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  13519. #endif
  13520. return EXPECT_RESULT();
  13521. } /* END test_wc_InitCmac */
  13522. /*
  13523. * Testing wc_CmacUpdate()
  13524. */
  13525. static int test_wc_CmacUpdate(void)
  13526. {
  13527. EXPECT_DECLS;
  13528. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13529. Cmac cmac;
  13530. byte key[] = {
  13531. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13532. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13533. };
  13534. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13535. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13536. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13537. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13538. "\xf4";
  13539. word32 inSz = (word32)sizeof(in) - 1;
  13540. word32 keySz = (word32)sizeof(key);
  13541. int type = WC_CMAC_AES;
  13542. XMEMSET(&cmac, 0, sizeof(Cmac));
  13543. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13544. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  13545. /* Test bad args. */
  13546. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  13547. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  13548. wc_AesFree(&cmac.aes);
  13549. #endif
  13550. return EXPECT_RESULT();
  13551. } /* END test_wc_CmacUpdate */
  13552. /*
  13553. * Testing wc_CmacFinal()
  13554. */
  13555. static int test_wc_CmacFinal(void)
  13556. {
  13557. EXPECT_DECLS;
  13558. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13559. Cmac cmac;
  13560. byte key[] = {
  13561. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13562. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13563. };
  13564. byte msg[] = {
  13565. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13566. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13567. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13568. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13569. 0xf4
  13570. };
  13571. /* Test vectors from CMACGenAES128.rsp from
  13572. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13573. * Per RFC4493 truncation of lsb is possible.
  13574. */
  13575. byte expMac[] = {
  13576. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13577. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13578. };
  13579. byte mac[AES_BLOCK_SIZE];
  13580. word32 msgSz = (word32)sizeof(msg);
  13581. word32 keySz = (word32)sizeof(key);
  13582. word32 macSz = sizeof(mac);
  13583. word32 badMacSz = 17;
  13584. int expMacSz = sizeof(expMac);
  13585. int type = WC_CMAC_AES;
  13586. XMEMSET(&cmac, 0, sizeof(Cmac));
  13587. XMEMSET(mac, 0, macSz);
  13588. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13589. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  13590. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13591. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13592. /* Pass in bad args. */
  13593. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  13594. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13595. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  13596. #endif
  13597. return EXPECT_RESULT();
  13598. } /* END test_wc_CmacFinal */
  13599. /*
  13600. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13601. */
  13602. static int test_wc_AesCmacGenerate(void)
  13603. {
  13604. EXPECT_DECLS;
  13605. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13606. byte key[] = {
  13607. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13608. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13609. };
  13610. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13611. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13612. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13613. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13614. byte mac[AES_BLOCK_SIZE];
  13615. word32 keySz = sizeof(key);
  13616. word32 macSz = sizeof(mac);
  13617. word32 msgSz = sizeof(msg) - 1;
  13618. word32 expMacSz = sizeof(expMac) - 1;
  13619. XMEMSET(mac, 0, macSz);
  13620. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  13621. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13622. /* Pass in bad args. */
  13623. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  13624. BAD_FUNC_ARG);
  13625. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  13626. BAD_FUNC_ARG);
  13627. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  13628. BAD_FUNC_ARG);
  13629. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  13630. BAD_FUNC_ARG);
  13631. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  13632. /* Test bad args. */
  13633. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  13634. BAD_FUNC_ARG);
  13635. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  13636. BAD_FUNC_ARG);
  13637. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  13638. BAD_FUNC_ARG);
  13639. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  13640. BAD_FUNC_ARG);
  13641. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  13642. BAD_FUNC_ARG);
  13643. #endif
  13644. return EXPECT_RESULT();
  13645. } /* END test_wc_AesCmacGenerate */
  13646. /*
  13647. * Testing streaming AES-GCM API.
  13648. */
  13649. static int test_wc_AesGcmStream(void)
  13650. {
  13651. EXPECT_DECLS;
  13652. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13653. defined(WOLFSSL_AESGCM_STREAM)
  13654. int i;
  13655. WC_RNG rng[1];
  13656. Aes aesEnc[1];
  13657. Aes aesDec[1];
  13658. byte tag[AES_BLOCK_SIZE];
  13659. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13660. byte out[AES_BLOCK_SIZE * 3 + 2];
  13661. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13662. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13663. byte key[AES_128_KEY_SIZE] = { 0, };
  13664. byte iv[AES_IV_SIZE] = { 1, };
  13665. byte ivOut[AES_IV_SIZE];
  13666. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13667. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13668. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13669. };
  13670. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13671. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13672. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13673. };
  13674. static const byte expTag[AES_BLOCK_SIZE] = {
  13675. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13676. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13677. };
  13678. XMEMSET(&rng, 0, sizeof(WC_RNG));
  13679. XMEMSET(&aesEnc, 0, sizeof(Aes));
  13680. XMEMSET(&aesDec, 0, sizeof(Aes));
  13681. /* Create a random for generating IV/nonce. */
  13682. ExpectIntEQ(wc_InitRng(rng), 0);
  13683. /* Initialize data structures. */
  13684. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13685. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13686. /* BadParameters to streaming init. */
  13687. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13688. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13689. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13690. BAD_FUNC_ARG);
  13691. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13692. BAD_FUNC_ARG);
  13693. /* Bad parameters to encrypt update. */
  13694. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13695. BAD_FUNC_ARG);
  13696. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13697. BAD_FUNC_ARG);
  13698. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13699. BAD_FUNC_ARG);
  13700. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13701. BAD_FUNC_ARG);
  13702. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13703. BAD_FUNC_ARG);
  13704. /* Bad parameters to decrypt update. */
  13705. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13706. BAD_FUNC_ARG);
  13707. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13708. BAD_FUNC_ARG);
  13709. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13710. BAD_FUNC_ARG);
  13711. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13712. BAD_FUNC_ARG);
  13713. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13714. BAD_FUNC_ARG);
  13715. /* Bad parameters to encrypt final. */
  13716. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13717. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13718. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13719. BAD_FUNC_ARG);
  13720. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13721. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13722. BAD_FUNC_ARG);
  13723. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13724. BAD_FUNC_ARG);
  13725. /* Bad parameters to decrypt final. */
  13726. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13727. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13728. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13729. BAD_FUNC_ARG);
  13730. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13731. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13732. BAD_FUNC_ARG);
  13733. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13734. BAD_FUNC_ARG);
  13735. /* Check calling final before setting key fails. */
  13736. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13737. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13738. /* Check calling update before setting key else fails. */
  13739. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13740. MISSING_KEY);
  13741. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13742. MISSING_KEY);
  13743. /* Set key but not IV. */
  13744. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13745. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13746. /* Check calling final before setting IV fails. */
  13747. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13748. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13749. /* Check calling update before setting IV else fails. */
  13750. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13751. MISSING_IV);
  13752. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13753. MISSING_IV);
  13754. /* Set IV using fixed part IV and external IV APIs. */
  13755. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13756. rng), 0);
  13757. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13758. GCM_NONCE_MID_SZ), 0);
  13759. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13760. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13761. /* Encrypt and decrypt data. */
  13762. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13763. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13764. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13765. /* Finalize and check tag matches. */
  13766. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13767. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13768. /* Set key and IV through streaming init API. */
  13769. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13770. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13771. /* Encrypt/decrypt one block and AAD of one block. */
  13772. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13773. AES_BLOCK_SIZE), 0);
  13774. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13775. AES_BLOCK_SIZE), 0);
  13776. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13777. /* Finalize and check tag matches. */
  13778. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13779. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13780. /* Set key and IV through streaming init API. */
  13781. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13782. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13783. /* No data to encrypt/decrypt one byte of AAD. */
  13784. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13785. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13786. /* Finalize and check tag matches. */
  13787. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13788. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13789. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13790. /* Set key and IV through streaming init API. */
  13791. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13792. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13793. /* Encrypt/decrypt one byte and no AAD. */
  13794. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13795. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13796. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13797. /* Finalize and check tag matches. */
  13798. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13799. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13800. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13801. /* Set key and IV through streaming init API. */
  13802. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13803. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13804. /* Encryption AES is one byte at a time */
  13805. for (i = 0; i < (int)sizeof(aad); i++) {
  13806. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13807. 0);
  13808. }
  13809. for (i = 0; i < (int)sizeof(in); i++) {
  13810. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13811. 0);
  13812. }
  13813. /* Decryption AES is two bytes at a time */
  13814. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13815. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13816. 0);
  13817. }
  13818. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13819. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13820. 0), 0);
  13821. }
  13822. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13823. /* Finalize and check tag matches. */
  13824. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13825. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13826. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13827. /* Check streaming encryption can be decrypted with one shot. */
  13828. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13829. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13830. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13831. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13832. wc_AesFree(aesEnc);
  13833. wc_AesFree(aesDec);
  13834. wc_FreeRng(rng);
  13835. #endif
  13836. return EXPECT_RESULT();
  13837. } /* END test_wc_AesGcmStream */
  13838. /*
  13839. * Testing streaming SM4 API.
  13840. */
  13841. static int test_wc_Sm4(void)
  13842. {
  13843. int res = TEST_SKIPPED;
  13844. #ifdef WOLFSSL_SM4
  13845. EXPECT_DECLS;
  13846. wc_Sm4 sm4;
  13847. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13848. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13849. unsigned char key[SM4_KEY_SIZE];
  13850. #endif
  13851. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13852. unsigned char iv[SM4_IV_SIZE];
  13853. #endif
  13854. /* Invalid parameters - wc_Sm4Init */
  13855. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13856. /* Valid cases - wc_Sm4Init */
  13857. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13858. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13859. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13860. XMEMSET(key, 0, sizeof(key));
  13861. /* Invalid parameters - wc_Sm4SetKey. */
  13862. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  13863. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  13864. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  13865. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13866. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  13867. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13868. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13869. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  13870. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  13871. /* Valid cases - wc_Sm4SetKey. */
  13872. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13873. #endif
  13874. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13875. XMEMSET(iv, 0, sizeof(iv));
  13876. /* Invalid parameters - wc_Sm4SetIV. */
  13877. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  13878. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  13879. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  13880. /* Valid cases - wc_Sm4SetIV. */
  13881. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13882. #endif
  13883. /* Valid cases - wc_Sm4Free */
  13884. wc_Sm4Free(NULL);
  13885. wc_Sm4Free(&sm4);
  13886. res = EXPECT_RESULT();
  13887. #endif
  13888. return res;
  13889. } /* END test_wc_Sm4 */
  13890. /*
  13891. * Testing block based SM4-ECB API.
  13892. */
  13893. static int test_wc_Sm4Ecb(void)
  13894. {
  13895. int res = TEST_SKIPPED;
  13896. #ifdef WOLFSSL_SM4_ECB
  13897. EXPECT_DECLS;
  13898. wc_Sm4 sm4;
  13899. unsigned char key[SM4_KEY_SIZE];
  13900. unsigned char in[SM4_BLOCK_SIZE * 2];
  13901. unsigned char out[SM4_BLOCK_SIZE * 2];
  13902. unsigned char out2[SM4_BLOCK_SIZE];
  13903. XMEMSET(key, 0, sizeof(key));
  13904. XMEMSET(in, 0, sizeof(in));
  13905. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13906. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13907. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13908. /* Tested in test_wc_Sm4. */
  13909. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13910. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  13911. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13912. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13913. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13914. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13915. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13916. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13917. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13918. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13919. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13920. /* Valid cases - wc_Sm4EcbEncrypt. */
  13921. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  13922. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13923. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13924. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13925. /* In and out are same pointer. */
  13926. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13927. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13928. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  13929. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13930. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13931. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13932. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13933. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13934. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13935. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13936. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13937. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13938. /* Valid cases - wc_Sm4EcbDecrypt. */
  13939. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  13940. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13941. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13942. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13943. /* In and out are same pointer. */
  13944. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13945. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13946. wc_Sm4Free(&sm4);
  13947. res = EXPECT_RESULT();
  13948. #endif
  13949. return res;
  13950. } /* END test_wc_Sm4Ecb */
  13951. /*
  13952. * Testing block based SM4-CBC API.
  13953. */
  13954. static int test_wc_Sm4Cbc(void)
  13955. {
  13956. int res = TEST_SKIPPED;
  13957. #ifdef WOLFSSL_SM4_CBC
  13958. EXPECT_DECLS;
  13959. wc_Sm4 sm4;
  13960. unsigned char key[SM4_KEY_SIZE];
  13961. unsigned char iv[SM4_IV_SIZE];
  13962. unsigned char in[SM4_BLOCK_SIZE * 2];
  13963. unsigned char out[SM4_BLOCK_SIZE * 2];
  13964. unsigned char out2[SM4_BLOCK_SIZE];
  13965. XMEMSET(key, 0, sizeof(key));
  13966. XMEMSET(iv, 0, sizeof(iv));
  13967. XMEMSET(in, 0, sizeof(in));
  13968. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13969. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13970. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13971. /* Tested in test_wc_Sm4. */
  13972. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13973. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  13974. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  13975. /* Tested in test_wc_Sm4. */
  13976. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13977. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  13978. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13979. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13980. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13981. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13982. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13983. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13984. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13985. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13986. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13987. /* Valid cases - wc_Sm4CbcEncrypt. */
  13988. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  13989. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13990. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13991. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13992. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13993. /* In and out are same pointer. */
  13994. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13995. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13996. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13997. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  13998. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13999. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14000. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14001. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14002. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14003. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14004. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14005. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14006. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14007. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14008. /* Valid cases - wc_Sm4CbcDecrypt. */
  14009. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14010. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14011. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14012. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14013. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14014. /* In and out are same pointer. */
  14015. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14016. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14017. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14018. wc_Sm4Free(&sm4);
  14019. res = EXPECT_RESULT();
  14020. #endif
  14021. return res;
  14022. } /* END test_wc_Sm4Cbc */
  14023. /*
  14024. * Testing streaming SM4-CTR API.
  14025. */
  14026. static int test_wc_Sm4Ctr(void)
  14027. {
  14028. int res = TEST_SKIPPED;
  14029. #ifdef WOLFSSL_SM4_CTR
  14030. EXPECT_DECLS;
  14031. wc_Sm4 sm4;
  14032. unsigned char key[SM4_KEY_SIZE];
  14033. unsigned char iv[SM4_IV_SIZE];
  14034. unsigned char in[SM4_BLOCK_SIZE * 4];
  14035. unsigned char out[SM4_BLOCK_SIZE * 4];
  14036. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14037. word32 chunk;
  14038. word32 i;
  14039. XMEMSET(key, 0, sizeof(key));
  14040. XMEMSET(iv, 0, sizeof(iv));
  14041. XMEMSET(in, 0, sizeof(in));
  14042. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14043. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14044. /* Tested in test_wc_Sm4. */
  14045. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14046. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14047. /* Tested in test_wc_Sm4. */
  14048. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14049. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14050. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14051. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14052. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14053. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14054. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14055. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14056. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14057. /* Valid cases - wc_Sm4CtrEncrypt. */
  14058. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14059. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14060. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14061. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14062. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14063. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14064. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14065. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14066. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14067. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14068. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14069. /* In and out are same pointer. Also check encrypt of cipher text produces
  14070. * plaintext.
  14071. */
  14072. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14073. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14074. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14075. /* Chunking tests. */
  14076. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14077. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14078. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14079. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14080. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14081. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  14082. }
  14083. if (i < (word32)sizeof(in)) {
  14084. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  14085. (word32)sizeof(in) - i), 0);
  14086. }
  14087. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  14088. }
  14089. for (i = 0; i < (word32)sizeof(iv); i++) {
  14090. iv[i] = 0xff;
  14091. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14092. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14093. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14094. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  14095. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  14096. }
  14097. wc_Sm4Free(&sm4);
  14098. res = EXPECT_RESULT();
  14099. #endif
  14100. return res;
  14101. } /* END test_wc_Sm4Ctr */
  14102. /*
  14103. * Testing stream SM4-GCM API.
  14104. */
  14105. static int test_wc_Sm4Gcm(void)
  14106. {
  14107. int res = TEST_SKIPPED;
  14108. #ifdef WOLFSSL_SM4_GCM
  14109. EXPECT_DECLS;
  14110. wc_Sm4 sm4;
  14111. unsigned char key[SM4_KEY_SIZE];
  14112. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14113. unsigned char in[SM4_BLOCK_SIZE * 2];
  14114. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14115. unsigned char out[SM4_BLOCK_SIZE * 2];
  14116. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14117. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14118. unsigned char tag[SM4_BLOCK_SIZE];
  14119. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14120. word32 i;
  14121. XMEMSET(key, 0, sizeof(key));
  14122. XMEMSET(nonce, 0, sizeof(nonce));
  14123. XMEMSET(in, 0, sizeof(in));
  14124. XMEMSET(in2, 0, sizeof(in2));
  14125. XMEMSET(aad, 0, sizeof(aad));
  14126. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14127. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14128. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14129. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14130. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14131. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14132. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14133. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14134. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14135. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14136. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14137. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14138. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14139. /* Valid parameters - wc_Sm4GcmSetKey. */
  14140. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14141. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14142. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14143. 0), BAD_FUNC_ARG);
  14144. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14145. 0), BAD_FUNC_ARG);
  14146. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14147. 0), BAD_FUNC_ARG);
  14148. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14149. 0), BAD_FUNC_ARG);
  14150. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14151. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14152. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14153. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14154. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14155. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14156. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14157. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14158. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14159. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14160. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14161. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14162. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14163. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14164. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14165. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14167. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14168. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14169. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14170. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14171. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14172. 0), BAD_FUNC_ARG);
  14173. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14174. 0), BAD_FUNC_ARG);
  14175. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14176. 0), BAD_FUNC_ARG);
  14177. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14178. 0), BAD_FUNC_ARG);
  14179. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14180. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14181. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14182. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14183. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14184. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14185. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14186. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14188. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14189. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14190. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14191. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14192. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14193. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14194. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14195. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14196. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14197. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14198. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14199. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14200. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14201. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14202. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14203. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14204. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14205. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14206. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14207. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14208. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14209. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14210. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14211. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14212. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14213. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14214. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14215. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14216. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14217. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14218. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14219. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14220. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14221. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14222. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14223. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14224. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14225. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14226. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14227. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14228. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14229. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14230. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14231. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14232. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14233. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14234. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14235. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14236. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14237. SM4_GCM_AUTH_E);
  14238. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14239. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14240. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14241. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14242. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14243. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14244. }
  14245. /* Check different in/out sizes. */
  14246. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14247. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14248. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14249. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14250. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14251. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14252. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14253. XMEMCPY(out2, out, i - 1);
  14254. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14255. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14256. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14258. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14259. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14260. }
  14261. /* Force the counter to roll over in first byte. */
  14262. {
  14263. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14264. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14265. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14266. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14267. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14268. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14269. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14270. }
  14271. wc_Sm4Free(&sm4);
  14272. res = EXPECT_RESULT();
  14273. #endif
  14274. return res;
  14275. } /* END test_wc_Sm4Gcm */
  14276. /*
  14277. * Testing stream SM4-CCM API.
  14278. */
  14279. static int test_wc_Sm4Ccm(void)
  14280. {
  14281. int res = TEST_SKIPPED;
  14282. #ifdef WOLFSSL_SM4_CCM
  14283. EXPECT_DECLS;
  14284. wc_Sm4 sm4;
  14285. unsigned char key[SM4_KEY_SIZE];
  14286. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14287. unsigned char in[SM4_BLOCK_SIZE * 2];
  14288. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14289. unsigned char out[SM4_BLOCK_SIZE * 2];
  14290. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14291. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14292. unsigned char tag[SM4_BLOCK_SIZE];
  14293. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14294. word32 i;
  14295. XMEMSET(key, 0, sizeof(key));
  14296. XMEMSET(nonce, 0, sizeof(nonce));
  14297. XMEMSET(in, 0, sizeof(in));
  14298. XMEMSET(in2, 0, sizeof(in2));
  14299. XMEMSET(aad, 0, sizeof(aad));
  14300. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14301. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14302. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14303. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14304. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14305. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14306. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14307. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14308. 0), BAD_FUNC_ARG);
  14309. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14310. 0), BAD_FUNC_ARG);
  14311. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14312. 0), BAD_FUNC_ARG);
  14313. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14314. 0), BAD_FUNC_ARG);
  14315. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14316. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14317. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14318. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14319. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14320. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14321. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14322. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14323. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14324. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14325. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14326. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14327. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14328. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14329. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14330. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14331. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14332. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14333. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14334. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14335. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14336. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14337. 0), BAD_FUNC_ARG);
  14338. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14339. 0), BAD_FUNC_ARG);
  14340. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14341. 0), BAD_FUNC_ARG);
  14342. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14343. 0), BAD_FUNC_ARG);
  14344. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14345. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14346. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14347. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14348. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14349. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14350. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14351. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14352. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14353. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14354. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14355. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14356. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14357. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14358. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14359. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14360. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14361. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14362. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14363. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14364. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14365. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14366. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14367. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14368. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14369. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14370. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14371. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14372. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14373. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14374. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14375. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14376. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14377. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14378. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14379. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14380. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14381. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14382. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14383. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14384. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14385. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14386. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14387. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14388. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14389. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14390. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14391. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14392. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14393. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14394. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14395. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14396. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14397. }
  14398. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14399. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14400. SM4_CCM_AUTH_E);
  14401. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14402. for (i = 0; i < 4; i++) {
  14403. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14404. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14405. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14406. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14407. }
  14408. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14409. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14410. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14411. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14412. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14413. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14414. }
  14415. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14416. * Even values in range 4..SM4_BLOCK_SIZE.
  14417. */
  14418. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14419. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14420. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14421. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14422. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14423. }
  14424. /* Check different in/out sizes. */
  14425. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14426. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14427. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14428. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14429. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14430. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14431. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14432. XMEMCPY(out2, out, i - 1);
  14433. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  14434. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14435. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14436. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  14437. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14438. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14439. }
  14440. /* Force the counter to roll over in first byte. */
  14441. {
  14442. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14443. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14444. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14445. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14446. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14447. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14448. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14449. }
  14450. wc_Sm4Free(&sm4);
  14451. res = EXPECT_RESULT();
  14452. #endif
  14453. return res;
  14454. } /* END test_wc_Sm4Ccm */
  14455. /*
  14456. * unit test for wc_Des3_SetIV()
  14457. */
  14458. static int test_wc_Des3_SetIV(void)
  14459. {
  14460. EXPECT_DECLS;
  14461. #ifndef NO_DES3
  14462. Des3 des;
  14463. const byte key[] = {
  14464. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14465. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14466. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14467. };
  14468. const byte iv[] = {
  14469. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14470. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14471. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14472. };
  14473. XMEMSET(&des, 0, sizeof(Des3));
  14474. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14475. /* DES_ENCRYPTION or DES_DECRYPTION */
  14476. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14477. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14478. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14479. /* Test explicitly wc_Des3_SetIV() */
  14480. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  14481. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  14482. #endif
  14483. wc_Des3Free(&des);
  14484. #endif
  14485. return EXPECT_RESULT();
  14486. } /* END test_wc_Des3_SetIV */
  14487. /*
  14488. * unit test for wc_Des3_SetKey()
  14489. */
  14490. static int test_wc_Des3_SetKey(void)
  14491. {
  14492. EXPECT_DECLS;
  14493. #ifndef NO_DES3
  14494. Des3 des;
  14495. const byte key[] = {
  14496. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14497. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14498. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14499. };
  14500. const byte iv[] = {
  14501. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14502. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14503. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14504. };
  14505. XMEMSET(&des, 0, sizeof(Des3));
  14506. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14507. /* DES_ENCRYPTION or DES_DECRYPTION */
  14508. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14509. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14510. /* Test bad args. */
  14511. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14512. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14513. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  14514. /* Default case. Should return 0. */
  14515. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  14516. wc_Des3Free(&des);
  14517. #endif
  14518. return EXPECT_RESULT();
  14519. } /* END test_wc_Des3_SetKey */
  14520. /*
  14521. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14522. */
  14523. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14524. {
  14525. EXPECT_DECLS;
  14526. #ifndef NO_DES3
  14527. Des3 des;
  14528. byte cipher[24];
  14529. byte plain[24];
  14530. const byte key[] = {
  14531. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14532. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14533. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14534. };
  14535. const byte iv[] = {
  14536. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14537. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14538. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14539. };
  14540. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14541. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14542. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14543. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14544. };
  14545. XMEMSET(&des, 0, sizeof(Des3));
  14546. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14547. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14548. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  14549. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  14550. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  14551. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14552. /* Pass in bad args. */
  14553. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  14554. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  14555. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  14556. BAD_FUNC_ARG);
  14557. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  14558. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  14559. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  14560. wc_Des3Free(&des);
  14561. #endif
  14562. return EXPECT_RESULT();
  14563. } /* END wc_Des3_CbcEncrypt */
  14564. /*
  14565. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14566. */
  14567. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14568. {
  14569. EXPECT_DECLS;
  14570. #ifndef NO_DES3
  14571. word32 vectorSz, cipherSz;
  14572. byte cipher[24];
  14573. byte plain[24];
  14574. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  14575. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14576. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14577. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14578. };
  14579. byte key[] = {
  14580. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14581. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14582. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14583. };
  14584. byte iv[] = {
  14585. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14586. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14587. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14588. };
  14589. vectorSz = sizeof(byte) * 24;
  14590. cipherSz = sizeof(byte) * 24;
  14591. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  14592. 0);
  14593. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  14594. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14595. /* pass in bad args. */
  14596. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  14597. BAD_FUNC_ARG);
  14598. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  14599. BAD_FUNC_ARG);
  14600. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  14601. BAD_FUNC_ARG);
  14602. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  14603. 0);
  14604. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  14605. BAD_FUNC_ARG);
  14606. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  14607. BAD_FUNC_ARG);
  14608. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  14609. BAD_FUNC_ARG);
  14610. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  14611. 0);
  14612. #endif
  14613. return EXPECT_RESULT();
  14614. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14615. /*
  14616. * Unit test for wc_Des3_EcbEncrypt
  14617. */
  14618. static int test_wc_Des3_EcbEncrypt(void)
  14619. {
  14620. EXPECT_DECLS;
  14621. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14622. Des3 des;
  14623. byte cipher[24];
  14624. word32 cipherSz = sizeof(cipher);
  14625. const byte key[] = {
  14626. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14627. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14628. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14629. };
  14630. const byte iv[] = {
  14631. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14632. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14633. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14634. };
  14635. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14636. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14637. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14638. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14639. };
  14640. XMEMSET(&des, 0, sizeof(Des3));
  14641. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14642. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14643. /* Bad Cases */
  14644. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14645. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  14646. BAD_FUNC_ARG);
  14647. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  14648. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  14649. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  14650. /* Good Cases */
  14651. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  14652. wc_Des3Free(&des);
  14653. #endif
  14654. return EXPECT_RESULT();
  14655. } /* END test_wc_Des3_EcbEncrypt */
  14656. /*
  14657. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14658. */
  14659. static int test_wc_Chacha_SetKey(void)
  14660. {
  14661. EXPECT_DECLS;
  14662. #ifdef HAVE_CHACHA
  14663. ChaCha ctx;
  14664. const byte key[] = {
  14665. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14666. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14667. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14668. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14669. };
  14670. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14671. byte cipher[128];
  14672. XMEMSET(cipher, 0, sizeof(cipher));
  14673. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  14674. /* Test bad args. */
  14675. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  14676. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14677. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  14678. /* Test bad args. */
  14679. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  14680. #endif
  14681. return EXPECT_RESULT();
  14682. } /* END test_wc_Chacha_SetKey */
  14683. /*
  14684. * unit test for wc_Poly1305SetKey()
  14685. */
  14686. static int test_wc_Poly1305SetKey(void)
  14687. {
  14688. EXPECT_DECLS;
  14689. #ifdef HAVE_POLY1305
  14690. Poly1305 ctx;
  14691. const byte key[] =
  14692. {
  14693. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14694. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14695. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14696. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14697. };
  14698. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14699. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  14700. /* Test bad args. */
  14701. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  14702. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  14703. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14704. #endif
  14705. return EXPECT_RESULT();
  14706. } /* END test_wc_Poly1305_SetKey() */
  14707. /*
  14708. * Testing wc_Chacha_Process()
  14709. */
  14710. static int test_wc_Chacha_Process(void)
  14711. {
  14712. EXPECT_DECLS;
  14713. #ifdef HAVE_CHACHA
  14714. ChaCha enc, dec;
  14715. byte cipher[128];
  14716. byte plain[128];
  14717. const byte key[] =
  14718. {
  14719. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14720. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14721. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14722. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14723. };
  14724. const char* input = "Everybody gets Friday off.";
  14725. word32 keySz = sizeof(key)/sizeof(byte);
  14726. unsigned long int inlen = XSTRLEN(input);
  14727. /* Initialize stack variables. */
  14728. XMEMSET(cipher, 0, 128);
  14729. XMEMSET(plain, 0, 128);
  14730. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  14731. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  14732. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14733. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14734. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  14735. 0);
  14736. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  14737. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14738. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14739. /* test checking and using leftovers, currently just in C code */
  14740. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14741. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14742. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  14743. (word32)inlen - 2), 0);
  14744. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14745. (byte*)input + (inlen - 2), 2), 0);
  14746. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  14747. (word32)inlen - 2), 0);
  14748. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14749. (byte*)input + (inlen - 2), 2), 0);
  14750. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14751. /* check edge cases with counter increment */
  14752. {
  14753. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14754. const byte expected[] = {
  14755. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14756. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14757. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14758. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14759. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14760. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14761. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14762. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14763. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14764. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14765. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14766. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14767. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14768. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14769. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14770. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14771. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14772. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14773. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14774. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14775. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14776. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14777. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14778. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14779. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14780. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14781. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14782. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14783. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14784. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14785. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14786. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14787. };
  14788. const byte iv2[] = {
  14789. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14790. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14791. };
  14792. byte input2[256];
  14793. int i;
  14794. for (i = 0; i < 256; i++)
  14795. input2[i] = i;
  14796. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  14797. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  14798. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14799. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  14800. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14801. /* partial */
  14802. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  14803. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14804. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  14805. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14806. }
  14807. #endif
  14808. /* Test bad args. */
  14809. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  14810. BAD_FUNC_ARG);
  14811. #endif
  14812. return EXPECT_RESULT();
  14813. } /* END test_wc_Chacha_Process */
  14814. /*
  14815. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14816. */
  14817. static int test_wc_ChaCha20Poly1305_aead(void)
  14818. {
  14819. EXPECT_DECLS;
  14820. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14821. const byte key[] = {
  14822. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14823. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14824. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14825. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14826. };
  14827. const byte plaintext[] = {
  14828. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14829. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14830. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14831. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14832. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14833. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14834. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14835. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14836. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14837. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14838. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14839. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14840. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14841. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14842. 0x74, 0x2e
  14843. };
  14844. const byte iv[] = {
  14845. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14846. 0x44, 0x45, 0x46, 0x47
  14847. };
  14848. const byte aad[] = { /* additional data */
  14849. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14850. 0xc4, 0xc5, 0xc6, 0xc7
  14851. };
  14852. const byte cipher[] = { /* expected output from operation */
  14853. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14854. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14855. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14856. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14857. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14858. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14859. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14860. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14861. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14862. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14863. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14864. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14865. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14866. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14867. 0x61, 0x16
  14868. };
  14869. const byte authTag[] = { /* expected output from operation */
  14870. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14871. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14872. };
  14873. byte generatedCiphertext[272];
  14874. byte generatedPlaintext[272];
  14875. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14876. /* Initialize stack variables. */
  14877. XMEMSET(generatedCiphertext, 0, 272);
  14878. XMEMSET(generatedPlaintext, 0, 272);
  14879. /* Test Encrypt */
  14880. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14881. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14882. 0);
  14883. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  14884. sizeof(cipher)/sizeof(byte)), 0);
  14885. /* Test bad args. */
  14886. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  14887. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14888. BAD_FUNC_ARG);
  14889. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14890. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14891. BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14893. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14894. BAD_FUNC_ARG);
  14895. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14896. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14897. BAD_FUNC_ARG);
  14898. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14899. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14901. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  14902. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14903. sizeof(cipher), authTag, generatedPlaintext), 0);
  14904. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  14905. sizeof(plaintext)/sizeof(byte)), 0);
  14906. /* Test bad args. */
  14907. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14908. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14909. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14910. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14911. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14912. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14913. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14914. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  14915. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14916. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14918. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14919. #endif
  14920. return EXPECT_RESULT();
  14921. } /* END test_wc_ChaCha20Poly1305_aead */
  14922. /*
  14923. * Testing function for wc_Rc2SetKey().
  14924. */
  14925. static int test_wc_Rc2SetKey(void)
  14926. {
  14927. EXPECT_DECLS;
  14928. #ifdef WC_RC2
  14929. Rc2 rc2;
  14930. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14931. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14932. /* valid key and IV */
  14933. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14934. iv, 40), 0);
  14935. /* valid key, no IV */
  14936. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14937. NULL, 40), 0);
  14938. /* bad arguments */
  14939. /* null Rc2 struct */
  14940. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14941. iv, 40), BAD_FUNC_ARG);
  14942. /* null key */
  14943. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14944. iv, 40), BAD_FUNC_ARG);
  14945. /* key size == 0 */
  14946. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  14947. /* key size > 128 */
  14948. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  14949. /* effective bits == 0 */
  14950. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14951. iv, 0), WC_KEY_SIZE_E);
  14952. /* effective bits > 1024 */
  14953. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14954. iv, 1025), WC_KEY_SIZE_E);
  14955. #endif
  14956. return EXPECT_RESULT();
  14957. } /* END test_wc_Rc2SetKey */
  14958. /*
  14959. * Testing function for wc_Rc2SetIV().
  14960. */
  14961. static int test_wc_Rc2SetIV(void)
  14962. {
  14963. EXPECT_DECLS;
  14964. #ifdef WC_RC2
  14965. Rc2 rc2;
  14966. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14967. /* valid IV */
  14968. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  14969. /* valid NULL IV */
  14970. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  14971. /* bad arguments */
  14972. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  14973. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  14974. #endif
  14975. return EXPECT_RESULT();
  14976. } /* END test_wc_Rc2SetIV */
  14977. /*
  14978. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  14979. */
  14980. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14981. {
  14982. EXPECT_DECLS;
  14983. #ifdef WC_RC2
  14984. Rc2 rc2;
  14985. int effectiveKeyBits = 63;
  14986. byte cipher[RC2_BLOCK_SIZE];
  14987. byte plain[RC2_BLOCK_SIZE];
  14988. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14989. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14990. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14991. XMEMSET(cipher, 0, sizeof(cipher));
  14992. XMEMSET(plain, 0, sizeof(plain));
  14993. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14994. NULL, effectiveKeyBits), 0);
  14995. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  14996. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  14997. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  14998. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  14999. /* Rc2EcbEncrypt bad arguments */
  15000. /* null Rc2 struct */
  15001. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15002. BAD_FUNC_ARG);
  15003. /* null out buffer */
  15004. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15005. BAD_FUNC_ARG);
  15006. /* null input buffer */
  15007. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15008. BAD_FUNC_ARG);
  15009. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15010. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15011. /* Rc2EcbDecrypt bad arguments */
  15012. /* null Rc2 struct */
  15013. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15014. BAD_FUNC_ARG);
  15015. /* null out buffer */
  15016. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15017. BAD_FUNC_ARG);
  15018. /* null input buffer */
  15019. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15020. BAD_FUNC_ARG);
  15021. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15022. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15023. #endif
  15024. return EXPECT_RESULT();
  15025. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15026. /*
  15027. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15028. */
  15029. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15030. {
  15031. EXPECT_DECLS;
  15032. #ifdef WC_RC2
  15033. Rc2 rc2;
  15034. int effectiveKeyBits = 63;
  15035. byte cipher[RC2_BLOCK_SIZE*2];
  15036. byte plain[RC2_BLOCK_SIZE*2];
  15037. /* vector taken from test.c */
  15038. byte key[] = {
  15039. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15040. };
  15041. byte iv[] = {
  15042. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15043. };
  15044. byte input[] = {
  15045. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15046. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15047. };
  15048. byte output[] = {
  15049. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15050. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15051. };
  15052. XMEMSET(cipher, 0, sizeof(cipher));
  15053. XMEMSET(plain, 0, sizeof(plain));
  15054. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15055. iv, effectiveKeyBits), 0);
  15056. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15057. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15058. /* reset IV for decrypt */
  15059. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15060. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15061. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15062. /* Rc2CbcEncrypt bad arguments */
  15063. /* null Rc2 struct */
  15064. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15065. BAD_FUNC_ARG);
  15066. /* null out buffer */
  15067. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15068. BAD_FUNC_ARG);
  15069. /* null input buffer */
  15070. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15071. BAD_FUNC_ARG);
  15072. /* Rc2CbcDecrypt bad arguments */
  15073. /* in size is 0 */
  15074. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15075. /* null Rc2 struct */
  15076. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15077. BAD_FUNC_ARG);
  15078. /* null out buffer */
  15079. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15080. BAD_FUNC_ARG);
  15081. /* null input buffer */
  15082. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  15083. BAD_FUNC_ARG);
  15084. #endif
  15085. return EXPECT_RESULT();
  15086. } /* END test_wc_Rc2CbcEncryptDecrypt */
  15087. /*
  15088. * Testing function for wc_AesSetIV
  15089. */
  15090. static int test_wc_AesSetIV(void)
  15091. {
  15092. int res = TEST_SKIPPED;
  15093. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  15094. Aes aes;
  15095. int ret = 0;
  15096. byte key16[] =
  15097. {
  15098. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15099. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15100. };
  15101. byte iv1[] = "1234567890abcdef";
  15102. byte iv2[] = "0987654321fedcba";
  15103. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15104. if (ret != 0)
  15105. return ret;
  15106. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  15107. iv1, AES_ENCRYPTION);
  15108. if (ret == 0) {
  15109. ret = wc_AesSetIV(&aes, iv2);
  15110. }
  15111. /* Test bad args. */
  15112. if (ret == 0) {
  15113. ret = wc_AesSetIV(NULL, iv1);
  15114. if (ret == BAD_FUNC_ARG) {
  15115. /* NULL iv should return 0. */
  15116. ret = wc_AesSetIV(&aes, NULL);
  15117. }
  15118. else {
  15119. ret = WOLFSSL_FATAL_ERROR;
  15120. }
  15121. }
  15122. wc_AesFree(&aes);
  15123. res = TEST_RES_CHECK(ret == 0);
  15124. #endif
  15125. return res;
  15126. } /* test_wc_AesSetIV */
  15127. /*
  15128. * Testing function for wc_AesSetKey().
  15129. */
  15130. static int test_wc_AesSetKey(void)
  15131. {
  15132. EXPECT_DECLS;
  15133. #ifndef NO_AES
  15134. Aes aes;
  15135. byte key16[] = {
  15136. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15137. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15138. };
  15139. #ifdef WOLFSSL_AES_192
  15140. byte key24[] = {
  15141. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15142. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15143. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15144. };
  15145. #endif
  15146. #ifdef WOLFSSL_AES_256
  15147. byte key32[] = {
  15148. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15149. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15150. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15151. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15152. };
  15153. #endif
  15154. byte badKey16[] = {
  15155. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15156. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15157. };
  15158. byte iv[] = "1234567890abcdef";
  15159. XMEMSET(&aes, 0, sizeof(Aes));
  15160. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15161. #ifdef WOLFSSL_AES_128
  15162. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15163. iv, AES_ENCRYPTION), 0);
  15164. #endif
  15165. #ifdef WOLFSSL_AES_192
  15166. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15167. iv, AES_ENCRYPTION), 0);
  15168. #endif
  15169. #ifdef WOLFSSL_AES_256
  15170. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15171. iv, AES_ENCRYPTION), 0);
  15172. #endif
  15173. /* Pass in bad args. */
  15174. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15175. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15176. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15177. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15178. BAD_FUNC_ARG);
  15179. wc_AesFree(&aes);
  15180. #endif
  15181. return EXPECT_RESULT();
  15182. } /* END test_wc_AesSetKey */
  15183. /*
  15184. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15185. * and wc_AesCbcDecryptWithKey()
  15186. */
  15187. static int test_wc_AesCbcEncryptDecrypt(void)
  15188. {
  15189. EXPECT_DECLS;
  15190. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15191. defined(WOLFSSL_AES_256)
  15192. Aes aes;
  15193. byte key32[] = {
  15194. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15195. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15196. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15197. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15198. };
  15199. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15200. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15201. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15202. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15203. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15204. };
  15205. byte iv[] = "1234567890abcdef";
  15206. byte enc[sizeof(vector)];
  15207. byte dec[sizeof(vector)];
  15208. byte dec2[sizeof(vector)];
  15209. /* Init stack variables. */
  15210. XMEMSET(&aes, 0, sizeof(Aes));
  15211. XMEMSET(enc, 0, sizeof(enc));
  15212. XMEMSET(dec, 0, sizeof(vector));
  15213. XMEMSET(dec2, 0, sizeof(vector));
  15214. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15215. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15216. AES_ENCRYPTION), 0);
  15217. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15218. wc_AesFree(&aes);
  15219. /* Re init for decrypt and set flag. */
  15220. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15221. AES_DECRYPTION), 0);
  15222. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15223. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15224. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15225. sizeof(key32)/sizeof(byte), iv), 0);
  15226. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15227. /* Pass in bad args */
  15228. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15229. BAD_FUNC_ARG);
  15230. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15231. BAD_FUNC_ARG);
  15232. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15233. BAD_FUNC_ARG);
  15234. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15235. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15236. BAD_LENGTH_E);
  15237. #endif
  15238. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15239. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15240. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15241. "mode (v2), skip test");
  15242. #else
  15243. /* Test passing in size of 0 */
  15244. XMEMSET(enc, 0, sizeof(enc));
  15245. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15246. /* Check enc was not modified */
  15247. {
  15248. int i;
  15249. for (i = 0; i < (int)sizeof(enc); i++)
  15250. ExpectIntEQ(enc[i], 0);
  15251. }
  15252. #endif
  15253. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15254. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15255. BAD_FUNC_ARG);
  15256. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15257. BAD_FUNC_ARG);
  15258. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15259. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15260. BAD_LENGTH_E);
  15261. #else
  15262. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15263. BAD_FUNC_ARG);
  15264. #endif
  15265. /* Test passing in size of 0 */
  15266. XMEMSET(dec, 0, sizeof(dec));
  15267. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15268. /* Check dec was not modified */
  15269. {
  15270. int i;
  15271. for (i = 0; i < (int)sizeof(dec); i++)
  15272. ExpectIntEQ(dec[i], 0);
  15273. }
  15274. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15275. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15276. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15277. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15278. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15279. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15280. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15281. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15282. wc_AesFree(&aes);
  15283. #endif
  15284. return EXPECT_RESULT();
  15285. } /* END test_wc_AesCbcEncryptDecrypt */
  15286. /*
  15287. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15288. */
  15289. static int test_wc_AesCtrEncryptDecrypt(void)
  15290. {
  15291. EXPECT_DECLS;
  15292. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15293. Aes aesEnc;
  15294. Aes aesDec;
  15295. byte key32[] = {
  15296. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15297. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15298. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15299. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15300. };
  15301. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15302. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15303. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15304. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15305. };
  15306. byte iv[] = "1234567890abcdef";
  15307. byte enc[AES_BLOCK_SIZE * 2];
  15308. byte dec[AES_BLOCK_SIZE * 2];
  15309. /* Init stack variables. */
  15310. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15311. XMEMSET(&aesDec, 0, sizeof(Aes));
  15312. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15313. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15314. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15315. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15316. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15317. AES_ENCRYPTION), 0);
  15318. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15319. sizeof(vector)/sizeof(byte)), 0);
  15320. /* Decrypt with wc_AesCtrEncrypt() */
  15321. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15322. AES_ENCRYPTION), 0);
  15323. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15324. 0);
  15325. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15326. /* Test bad args. */
  15327. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15328. BAD_FUNC_ARG);
  15329. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15330. BAD_FUNC_ARG);
  15331. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15332. BAD_FUNC_ARG);
  15333. wc_AesFree(&aesEnc);
  15334. wc_AesFree(&aesDec);
  15335. #endif
  15336. return EXPECT_RESULT();
  15337. } /* END test_wc_AesCtrEncryptDecrypt */
  15338. /*
  15339. * test function for wc_AesGcmSetKey()
  15340. */
  15341. static int test_wc_AesGcmSetKey(void)
  15342. {
  15343. EXPECT_DECLS;
  15344. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15345. Aes aes;
  15346. #ifdef WOLFSSL_AES_128
  15347. byte key16[] = {
  15348. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15349. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15350. };
  15351. #endif
  15352. #ifdef WOLFSSL_AES_192
  15353. byte key24[] = {
  15354. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15355. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15356. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15357. };
  15358. #endif
  15359. #ifdef WOLFSSL_AES_256
  15360. byte key32[] = {
  15361. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15362. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15363. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15364. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15365. };
  15366. #endif
  15367. byte badKey16[] = {
  15368. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15369. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15370. };
  15371. byte badKey24[] = {
  15372. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15373. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15374. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15375. };
  15376. byte badKey32[] = {
  15377. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15378. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15379. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15380. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15381. };
  15382. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15383. #ifdef WOLFSSL_AES_128
  15384. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15385. #endif
  15386. #ifdef WOLFSSL_AES_192
  15387. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15388. #endif
  15389. #ifdef WOLFSSL_AES_256
  15390. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15391. #endif
  15392. /* Pass in bad args. */
  15393. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15394. BAD_FUNC_ARG);
  15395. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15396. BAD_FUNC_ARG);
  15397. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15398. BAD_FUNC_ARG);
  15399. wc_AesFree(&aes);
  15400. #endif
  15401. return EXPECT_RESULT();
  15402. } /* END test_wc_AesGcmSetKey */
  15403. /*
  15404. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15405. */
  15406. static int test_wc_AesGcmEncryptDecrypt(void)
  15407. {
  15408. EXPECT_DECLS;
  15409. /* WOLFSSL_AFALG requires 12 byte IV */
  15410. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15411. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15412. Aes aes;
  15413. byte key32[] = {
  15414. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15415. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15416. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15417. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15418. };
  15419. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15420. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15421. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15422. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15423. };
  15424. const byte a[] = {
  15425. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15426. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15427. 0xab, 0xad, 0xda, 0xd2
  15428. };
  15429. byte iv[] = "1234567890a";
  15430. byte longIV[] = "1234567890abcdefghij";
  15431. byte enc[sizeof(vector)];
  15432. byte resultT[AES_BLOCK_SIZE];
  15433. byte dec[sizeof(vector)];
  15434. /* Init stack variables. */
  15435. XMEMSET(&aes, 0, sizeof(Aes));
  15436. XMEMSET(enc, 0, sizeof(vector));
  15437. XMEMSET(dec, 0, sizeof(vector));
  15438. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15439. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15440. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15441. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15442. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15443. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  15444. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15445. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15446. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15447. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  15448. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15449. BAD_FUNC_ARG);
  15450. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15451. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15452. BAD_FUNC_ARG);
  15453. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15454. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  15455. BAD_FUNC_ARG);
  15456. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15457. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15458. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15459. /* FIPS does not check the lower bound of ivSz */
  15460. #else
  15461. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  15462. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15463. #endif
  15464. /* This case is now considered good. Long IVs are now allowed.
  15465. * Except for the original FIPS release, it still has an upper
  15466. * bound on the IV length. */
  15467. #if (!defined(HAVE_FIPS) || \
  15468. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15469. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15470. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15471. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15472. 0);
  15473. #else
  15474. (void)longIV;
  15475. #endif /* Old FIPS */
  15476. /* END wc_AesGcmEncrypt */
  15477. #ifdef HAVE_AES_DECRYPT
  15478. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15479. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15480. BAD_FUNC_ARG);
  15481. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  15482. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15483. BAD_FUNC_ARG);
  15484. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  15485. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15486. BAD_FUNC_ARG);
  15487. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  15488. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15489. BAD_FUNC_ARG);
  15490. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15491. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  15492. BAD_FUNC_ARG);
  15493. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15494. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15495. BAD_FUNC_ARG);
  15496. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15497. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15498. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15499. /* FIPS does not check the lower bound of ivSz */
  15500. #else
  15501. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15502. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15503. #endif
  15504. #endif /* HAVE_AES_DECRYPT */
  15505. wc_AesFree(&aes);
  15506. #endif
  15507. return EXPECT_RESULT();
  15508. } /* END test_wc_AesGcmEncryptDecrypt */
  15509. /*
  15510. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15511. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15512. */
  15513. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15514. {
  15515. EXPECT_DECLS;
  15516. #if (!defined(HAVE_FIPS) || \
  15517. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15518. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15519. const byte key[] = {
  15520. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15521. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15522. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15523. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15524. };
  15525. const byte in[] = {
  15526. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15527. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15528. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15529. };
  15530. const byte aad[] = {
  15531. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15532. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15533. 0xab, 0xad, 0xda, 0xd2
  15534. };
  15535. Aes aesEnc;
  15536. Aes aesDec;
  15537. byte iv[] = "1234567890abcdefghij";
  15538. byte out[sizeof(in)];
  15539. byte plain[sizeof(in)];
  15540. byte tag[AES_BLOCK_SIZE];
  15541. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15542. XMEMSET(&aesDec, 0, sizeof(Aes));
  15543. XMEMSET(out, 0, sizeof(out));
  15544. XMEMSET(plain, 0, sizeof(plain));
  15545. XMEMSET(tag, 0, sizeof(tag));
  15546. /* Perform one-shot encryption using long IV */
  15547. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15548. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15549. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15550. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15551. /* Perform streaming decryption using long IV */
  15552. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15553. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15554. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15555. sizeof(aad)), 0);
  15556. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15557. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15558. /* Free resources */
  15559. wc_AesFree(&aesEnc);
  15560. wc_AesFree(&aesDec);
  15561. #endif
  15562. return EXPECT_RESULT();
  15563. } /* END wc_AesGcmMixedEncDecLongIV */
  15564. /*
  15565. * unit test for wc_GmacSetKey()
  15566. */
  15567. static int test_wc_GmacSetKey(void)
  15568. {
  15569. EXPECT_DECLS;
  15570. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15571. Gmac gmac;
  15572. byte key16[] = {
  15573. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15574. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15575. };
  15576. #ifdef WOLFSSL_AES_192
  15577. byte key24[] = {
  15578. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15579. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15580. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15581. };
  15582. #endif
  15583. #ifdef WOLFSSL_AES_256
  15584. byte key32[] = {
  15585. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15586. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15587. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15588. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15589. };
  15590. #endif
  15591. byte badKey16[] = {
  15592. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15593. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15594. };
  15595. byte badKey24[] = {
  15596. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15597. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15598. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15599. };
  15600. byte badKey32[] = {
  15601. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15602. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15603. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15604. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15605. };
  15606. XMEMSET(&gmac, 0, sizeof(Gmac));
  15607. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15608. #ifdef WOLFSSL_AES_128
  15609. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  15610. #endif
  15611. #ifdef WOLFSSL_AES_192
  15612. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15613. #endif
  15614. #ifdef WOLFSSL_AES_256
  15615. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15616. #endif
  15617. /* Pass in bad args. */
  15618. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  15619. BAD_FUNC_ARG);
  15620. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  15621. BAD_FUNC_ARG);
  15622. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  15623. BAD_FUNC_ARG);
  15624. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  15625. BAD_FUNC_ARG);
  15626. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  15627. BAD_FUNC_ARG);
  15628. wc_AesFree(&gmac.aes);
  15629. #endif
  15630. return EXPECT_RESULT();
  15631. } /* END test_wc_GmacSetKey */
  15632. /*
  15633. * unit test for wc_GmacUpdate
  15634. */
  15635. static int test_wc_GmacUpdate(void)
  15636. {
  15637. EXPECT_DECLS;
  15638. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15639. Gmac gmac;
  15640. #ifdef WOLFSSL_AES_128
  15641. const byte key16[] = {
  15642. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15643. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15644. };
  15645. #endif
  15646. #ifdef WOLFSSL_AES_192
  15647. byte key24[] = {
  15648. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15649. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15650. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15651. };
  15652. #endif
  15653. #ifdef WOLFSSL_AES_256
  15654. byte key32[] = {
  15655. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15656. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15657. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15658. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15659. };
  15660. #endif
  15661. #ifdef WOLFSSL_AES_128
  15662. const byte authIn[] = {
  15663. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15664. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15665. };
  15666. #endif
  15667. #ifdef WOLFSSL_AES_192
  15668. const byte authIn2[] = {
  15669. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15670. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15671. };
  15672. #endif
  15673. const byte authIn3[] = {
  15674. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15675. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15676. };
  15677. #ifdef WOLFSSL_AES_128
  15678. const byte tag1[] = { /* Known. */
  15679. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15680. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15681. };
  15682. #endif
  15683. #ifdef WOLFSSL_AES_192
  15684. const byte tag2[] = { /* Known */
  15685. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15686. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15687. };
  15688. #endif
  15689. const byte tag3[] = { /* Known */
  15690. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15691. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15692. };
  15693. #ifdef WOLFSSL_AES_128
  15694. const byte iv[] = {
  15695. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15696. 0xe2, 0x8c, 0x8f, 0x16
  15697. };
  15698. #endif
  15699. #ifdef WOLFSSL_AES_192
  15700. const byte iv2[] = {
  15701. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15702. 0x7e, 0x1a, 0x6f, 0xbc
  15703. };
  15704. #endif
  15705. const byte iv3[] = {
  15706. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15707. 0xc3, 0xfb, 0x6c, 0x8a
  15708. };
  15709. byte tagOut[16];
  15710. byte tagOut2[24];
  15711. byte tagOut3[32];
  15712. /* Init stack variables. */
  15713. XMEMSET(&gmac, 0, sizeof(Gmac));
  15714. XMEMSET(tagOut, 0, sizeof(tagOut));
  15715. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15716. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15717. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15718. #ifdef WOLFSSL_AES_128
  15719. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  15720. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15721. tagOut, sizeof(tag1)), 0);
  15722. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  15723. #endif
  15724. #ifdef WOLFSSL_AES_192
  15725. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15726. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15727. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  15728. tagOut2, sizeof(tag2)), 0);
  15729. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  15730. #endif
  15731. #ifdef WOLFSSL_AES_256
  15732. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15733. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15734. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15735. tagOut3, sizeof(tag3)), 0);
  15736. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  15737. #endif
  15738. /* Pass bad args. */
  15739. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15740. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  15741. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15742. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  15743. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15744. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  15745. wc_AesFree(&gmac.aes);
  15746. #endif
  15747. return EXPECT_RESULT();
  15748. } /* END test_wc_GmacUpdate */
  15749. /*
  15750. * testing wc_CamelliaSetKey
  15751. */
  15752. static int test_wc_CamelliaSetKey(void)
  15753. {
  15754. EXPECT_DECLS;
  15755. #ifdef HAVE_CAMELLIA
  15756. Camellia camellia;
  15757. /*128-bit key*/
  15758. static const byte key16[] = {
  15759. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15760. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15761. };
  15762. /* 192-bit key */
  15763. static const byte key24[] = {
  15764. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15765. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15766. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15767. };
  15768. /* 256-bit key */
  15769. static const byte key32[] = {
  15770. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15771. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15772. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15773. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15774. };
  15775. static const byte iv[] = {
  15776. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15777. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15778. };
  15779. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  15780. 0);
  15781. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  15782. NULL), 0);
  15783. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15784. 0);
  15785. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15786. NULL), 0);
  15787. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  15788. 0);
  15789. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  15790. NULL), 0);
  15791. /* Bad args. */
  15792. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  15793. BAD_FUNC_ARG);
  15794. #endif
  15795. return EXPECT_RESULT();
  15796. } /* END test_wc_CammeliaSetKey */
  15797. /*
  15798. * Testing wc_CamelliaSetIV()
  15799. */
  15800. static int test_wc_CamelliaSetIV(void)
  15801. {
  15802. EXPECT_DECLS;
  15803. #ifdef HAVE_CAMELLIA
  15804. Camellia camellia;
  15805. static const byte iv[] = {
  15806. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15807. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15808. };
  15809. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  15810. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  15811. /* Bad args. */
  15812. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  15813. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  15814. #endif
  15815. return EXPECT_RESULT();
  15816. } /* END test_wc_CamelliaSetIV*/
  15817. /*
  15818. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15819. */
  15820. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15821. {
  15822. EXPECT_DECLS;
  15823. #ifdef HAVE_CAMELLIA
  15824. Camellia camellia;
  15825. static const byte key24[] = {
  15826. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15827. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15828. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15829. };
  15830. static const byte iv[] = {
  15831. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15832. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15833. };
  15834. static const byte plainT[] = {
  15835. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15836. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15837. };
  15838. byte enc[sizeof(plainT)];
  15839. byte dec[sizeof(enc)];
  15840. /* Init stack variables.*/
  15841. XMEMSET(enc, 0, 16);
  15842. XMEMSET(enc, 0, 16);
  15843. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15844. 0);
  15845. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  15846. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  15847. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15848. /* Pass bad args. */
  15849. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  15850. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  15851. BAD_FUNC_ARG);
  15852. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  15853. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  15854. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  15855. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  15856. #endif
  15857. return EXPECT_RESULT();
  15858. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15859. /*
  15860. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15861. */
  15862. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15863. {
  15864. EXPECT_DECLS;
  15865. #ifdef HAVE_CAMELLIA
  15866. Camellia camellia;
  15867. static const byte key24[] = {
  15868. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15869. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15870. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15871. };
  15872. static const byte plainT[] = {
  15873. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15874. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15875. };
  15876. byte enc[CAMELLIA_BLOCK_SIZE];
  15877. byte dec[CAMELLIA_BLOCK_SIZE];
  15878. /* Init stack variables. */
  15879. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15880. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15881. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15882. NULL), 0);
  15883. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  15884. CAMELLIA_BLOCK_SIZE), 0);
  15885. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15886. NULL), 0);
  15887. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  15888. 0);
  15889. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15890. /* Pass in bad args. */
  15891. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  15892. BAD_FUNC_ARG);
  15893. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15894. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15895. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15896. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15897. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  15898. BAD_FUNC_ARG);
  15899. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15900. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15901. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15902. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15903. #endif
  15904. return EXPECT_RESULT();
  15905. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15906. /*
  15907. * Testing wc_Arc4SetKey()
  15908. */
  15909. static int test_wc_Arc4SetKey(void)
  15910. {
  15911. EXPECT_DECLS;
  15912. #ifndef NO_RC4
  15913. Arc4 arc;
  15914. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15915. int keyLen = 8;
  15916. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  15917. /* Test bad args. */
  15918. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  15919. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  15920. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  15921. #endif
  15922. return EXPECT_RESULT();
  15923. } /* END test_wc_Arc4SetKey */
  15924. /*
  15925. * Testing wc_Arc4Process for ENC/DEC.
  15926. */
  15927. static int test_wc_Arc4Process(void)
  15928. {
  15929. EXPECT_DECLS;
  15930. #ifndef NO_RC4
  15931. Arc4 enc;
  15932. Arc4 dec;
  15933. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15934. int keyLen = 8;
  15935. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15936. byte cipher[8];
  15937. byte plain[8];
  15938. /* Init stack variables */
  15939. XMEMSET(&enc, 0, sizeof(Arc4));
  15940. XMEMSET(&dec, 0, sizeof(Arc4));
  15941. XMEMSET(cipher, 0, sizeof(cipher));
  15942. XMEMSET(plain, 0, sizeof(plain));
  15943. /* Use for async. */
  15944. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  15945. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  15946. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  15947. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  15948. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  15949. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  15950. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  15951. /* Bad args. */
  15952. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  15953. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  15954. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  15955. wc_Arc4Free(&enc);
  15956. wc_Arc4Free(&dec);
  15957. #endif
  15958. return EXPECT_RESULT();
  15959. } /* END test_wc_Arc4Process */
  15960. /*
  15961. * Testing wc_Init RsaKey()
  15962. */
  15963. static int test_wc_InitRsaKey(void)
  15964. {
  15965. EXPECT_DECLS;
  15966. #ifndef NO_RSA
  15967. RsaKey key;
  15968. XMEMSET(&key, 0, sizeof(RsaKey));
  15969. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15970. /* Test bad args. */
  15971. #ifndef HAVE_USER_RSA
  15972. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  15973. #else
  15974. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), USER_CRYPTO_ERROR);
  15975. #endif
  15976. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  15977. #endif
  15978. return EXPECT_RESULT();
  15979. } /* END test_wc_InitRsaKey */
  15980. /*
  15981. * Testing wc_RsaPrivateKeyDecode()
  15982. */
  15983. static int test_wc_RsaPrivateKeyDecode(void)
  15984. {
  15985. EXPECT_DECLS;
  15986. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15987. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15988. RsaKey key;
  15989. byte* tmp = NULL;
  15990. word32 idx = 0;
  15991. int bytes = 0;
  15992. XMEMSET(&key, 0, sizeof(RsaKey));
  15993. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  15994. DYNAMIC_TYPE_TMP_BUFFER));
  15995. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15996. if (tmp != NULL) {
  15997. #ifdef USE_CERT_BUFFERS_1024
  15998. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15999. bytes = sizeof_client_key_der_1024;
  16000. #else
  16001. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16002. bytes = sizeof_client_key_der_2048;
  16003. #endif /* Use cert buffers. */
  16004. }
  16005. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16006. #ifndef HAVE_USER_RSA
  16007. /* Test bad args. */
  16008. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16009. BAD_FUNC_ARG);
  16010. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16011. BAD_FUNC_ARG);
  16012. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16013. BAD_FUNC_ARG);
  16014. #else
  16015. /* Test bad args. User RSA. */
  16016. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16017. USER_CRYPTO_ERROR);
  16018. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16019. USER_CRYPTO_ERROR);
  16020. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16021. USER_CRYPTO_ERROR);
  16022. #endif
  16023. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16024. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16025. #endif
  16026. return EXPECT_RESULT();
  16027. } /* END test_wc_RsaPrivateKeyDecode */
  16028. /*
  16029. * Testing wc_RsaPublicKeyDecode()
  16030. */
  16031. static int test_wc_RsaPublicKeyDecode(void)
  16032. {
  16033. EXPECT_DECLS;
  16034. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16035. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16036. RsaKey keyPub;
  16037. byte* tmp = NULL;
  16038. word32 idx = 0;
  16039. int bytes = 0;
  16040. word32 keySz = 0;
  16041. word32 tstKeySz = 0;
  16042. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16043. XFILE f = XBADFILE;
  16044. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16045. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16046. byte buf[4096];
  16047. #endif
  16048. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16049. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16050. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16051. if (tmp != NULL) {
  16052. #ifdef USE_CERT_BUFFERS_1024
  16053. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16054. bytes = sizeof_client_keypub_der_1024;
  16055. keySz = 1024;
  16056. #else
  16057. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16058. bytes = sizeof_client_keypub_der_2048;
  16059. keySz = 2048;
  16060. #endif
  16061. }
  16062. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16063. #ifndef HAVE_USER_RSA
  16064. /* Pass in bad args. */
  16065. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16066. BAD_FUNC_ARG);
  16067. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16068. BAD_FUNC_ARG);
  16069. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16070. BAD_FUNC_ARG);
  16071. #else
  16072. /* Pass in bad args. */
  16073. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16074. USER_CRYPTO_ERROR);
  16075. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16076. USER_CRYPTO_ERROR);
  16077. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16078. USER_CRYPTO_ERROR);
  16079. #endif
  16080. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16081. /* Test for getting modulus key size */
  16082. idx = 0;
  16083. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16084. &tstKeySz, NULL, NULL), 0);
  16085. ExpectIntEQ(tstKeySz, keySz/8);
  16086. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16087. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16088. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16089. if (f != XBADFILE) {
  16090. XFCLOSE(f);
  16091. f = XBADFILE;
  16092. }
  16093. idx = 0;
  16094. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16095. NULL), 0);
  16096. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  16097. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16098. if (f != XBADFILE)
  16099. XFCLOSE(f);
  16100. idx = 0;
  16101. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16102. NULL), 0);
  16103. #endif
  16104. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16105. #endif
  16106. return EXPECT_RESULT();
  16107. } /* END test_wc_RsaPublicKeyDecode */
  16108. /*
  16109. * Testing wc_RsaPublicKeyDecodeRaw()
  16110. */
  16111. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16112. {
  16113. EXPECT_DECLS;
  16114. #if !defined(NO_RSA)
  16115. RsaKey key;
  16116. const byte n = 0x23;
  16117. const byte e = 0x03;
  16118. int nSz = sizeof(n);
  16119. int eSz = sizeof(e);
  16120. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16121. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16122. #ifndef HAVE_USER_RSA
  16123. /* Pass in bad args. */
  16124. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16125. BAD_FUNC_ARG);
  16126. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16127. BAD_FUNC_ARG);
  16128. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16129. BAD_FUNC_ARG);
  16130. #else
  16131. /* Pass in bad args. User RSA. */
  16132. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16133. USER_CRYPTO_ERROR);
  16134. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16135. USER_CRYPTO_ERROR);
  16136. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16137. USER_CRYPTO_ERROR);
  16138. #endif
  16139. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16140. #endif
  16141. return EXPECT_RESULT();
  16142. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16143. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16144. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16145. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16146. * trying until it gets a probable prime. */
  16147. #ifdef HAVE_FIPS
  16148. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16149. {
  16150. int ret;
  16151. for (;;) {
  16152. ret = wc_MakeRsaKey(key, size, e, rng);
  16153. if (ret != PRIME_GEN_E) break;
  16154. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16155. "trying again.\n");
  16156. }
  16157. return ret;
  16158. }
  16159. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16160. #else
  16161. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16162. #endif
  16163. #endif
  16164. /*
  16165. * Testing wc_MakeRsaKey()
  16166. */
  16167. static int test_wc_MakeRsaKey(void)
  16168. {
  16169. EXPECT_DECLS;
  16170. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16171. RsaKey genKey;
  16172. WC_RNG rng;
  16173. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16174. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16175. int bits = 1024;
  16176. #else
  16177. int bits = 2048;
  16178. #endif
  16179. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16180. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16181. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16182. ExpectIntEQ(wc_InitRng(&rng), 0);
  16183. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16184. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16185. #ifndef HAVE_USER_RSA
  16186. /* Test bad args. */
  16187. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16188. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16189. BAD_FUNC_ARG);
  16190. /* e < 3 */
  16191. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16192. /* e & 1 == 0 */
  16193. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16194. #else
  16195. /* Test bad args. */
  16196. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng),
  16197. USER_CRYPTO_ERROR);
  16198. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16199. USER_CRYPTO_ERROR);
  16200. /* e < 3 */
  16201. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), USER_CRYPTO_ERROR);
  16202. /* e & 1 == 0 */
  16203. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), USER_CRYPTO_ERROR);
  16204. #endif /* HAVE_USER_RSA */
  16205. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16206. #endif
  16207. return EXPECT_RESULT();
  16208. } /* END test_wc_MakeRsaKey */
  16209. /*
  16210. * Test the bounds checking on the cipher text versus the key modulus.
  16211. * 1. Make a new RSA key.
  16212. * 2. Set c to 1.
  16213. * 3. Decrypt c into k. (error)
  16214. * 4. Copy the key modulus to c and sub 1 from the copy.
  16215. * 5. Decrypt c into k. (error)
  16216. * Valid bounds test cases are covered by all the other RSA tests.
  16217. */
  16218. static int test_RsaDecryptBoundsCheck(void)
  16219. {
  16220. EXPECT_DECLS;
  16221. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16222. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16223. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16224. WC_RNG rng;
  16225. RsaKey key;
  16226. byte flatC[256];
  16227. word32 flatCSz;
  16228. byte out[256];
  16229. word32 outSz = sizeof(out);
  16230. XMEMSET(&key, 0, sizeof(RsaKey));
  16231. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16232. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16233. ExpectIntEQ(wc_InitRng(&rng), 0);
  16234. if (EXPECT_SUCCESS()) {
  16235. const byte* derKey;
  16236. word32 derKeySz;
  16237. word32 idx = 0;
  16238. #ifdef USE_CERT_BUFFERS_1024
  16239. derKey = server_key_der_1024;
  16240. derKeySz = (word32)sizeof_server_key_der_1024;
  16241. flatCSz = 128;
  16242. #else
  16243. derKey = server_key_der_2048;
  16244. derKeySz = (word32)sizeof_server_key_der_2048;
  16245. flatCSz = 256;
  16246. #endif
  16247. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16248. }
  16249. if (EXPECT_SUCCESS()) {
  16250. XMEMSET(flatC, 0, flatCSz);
  16251. flatC[flatCSz-1] = 1;
  16252. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16253. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16254. if (EXPECT_SUCCESS()) {
  16255. mp_int c;
  16256. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16257. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16258. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16259. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16260. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16261. mp_clear(&c);
  16262. }
  16263. }
  16264. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16265. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16266. #endif
  16267. return EXPECT_RESULT();
  16268. } /* END test_wc_RsaDecryptBoundsCheck */
  16269. /*
  16270. * Testing wc_SetKeyUsage()
  16271. */
  16272. static int test_wc_SetKeyUsage(void)
  16273. {
  16274. EXPECT_DECLS;
  16275. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16276. Cert myCert;
  16277. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16278. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16279. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16280. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16281. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16282. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16283. /* Test bad args. */
  16284. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16285. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16286. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16287. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16288. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16289. KEYUSAGE_E);
  16290. #endif
  16291. return EXPECT_RESULT();
  16292. } /* END test_wc_SetKeyUsage */
  16293. /*
  16294. * Testing wc_CheckProbablePrime()
  16295. */
  16296. static int test_wc_CheckProbablePrime(void)
  16297. {
  16298. EXPECT_DECLS;
  16299. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16300. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16301. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16302. RsaKey key;
  16303. WC_RNG rng;
  16304. byte e[3];
  16305. word32 eSz = (word32)sizeof(e);
  16306. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16307. word32 nSz = (word32)sizeof(n);
  16308. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16309. word32 dSz = (word32)sizeof(d);
  16310. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16311. word32 pSz = (word32)sizeof(p);
  16312. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16313. word32 qSz = (word32)sizeof(q);
  16314. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16315. int* isPrime;
  16316. int test[5];
  16317. isPrime = test;
  16318. XMEMSET(&key, 0, sizeof(RsaKey));
  16319. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16320. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16321. ExpectIntEQ(wc_InitRng(&rng), 0);
  16322. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16323. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16324. WC_RSA_EXPONENT, &rng), 0);
  16325. PRIVATE_KEY_UNLOCK();
  16326. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16327. &qSz), 0);
  16328. PRIVATE_KEY_LOCK();
  16329. /* Bad cases */
  16330. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16331. BAD_FUNC_ARG);
  16332. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16333. BAD_FUNC_ARG);
  16334. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16335. BAD_FUNC_ARG);
  16336. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16337. BAD_FUNC_ARG);
  16338. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16339. BAD_FUNC_ARG);
  16340. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16341. BAD_FUNC_ARG);
  16342. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16343. BAD_FUNC_ARG);
  16344. /* Good case */
  16345. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16346. 0);
  16347. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16348. wc_FreeRng(&rng);
  16349. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16350. #endif
  16351. return EXPECT_RESULT();
  16352. } /* END test_wc_CheckProbablePrime */
  16353. /*
  16354. * Testing wc_RsaPSS_Verify()
  16355. */
  16356. static int test_wc_RsaPSS_Verify(void)
  16357. {
  16358. EXPECT_DECLS;
  16359. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16360. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16361. RsaKey key;
  16362. WC_RNG rng;
  16363. int sz = 256;
  16364. const char* szMessage = "This is the string to be signed";
  16365. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16366. unsigned char pDecrypted[2048/8];
  16367. byte* pt = pDecrypted;
  16368. word32 outLen = sizeof(pDecrypted);
  16369. XMEMSET(&key, 0, sizeof(RsaKey));
  16370. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16371. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16372. ExpectIntEQ(wc_InitRng(&rng), 0);
  16373. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16374. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16375. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16376. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16377. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16378. /* Bad cases */
  16379. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16380. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16381. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16382. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16383. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16384. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16385. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16386. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16387. /* Good case */
  16388. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16389. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16390. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16391. wc_FreeRng(&rng);
  16392. #endif
  16393. return EXPECT_RESULT();
  16394. } /* END test_wc_RsaPSS_Verify */
  16395. /*
  16396. * Testing wc_RsaPSS_VerifyCheck()
  16397. */
  16398. static int test_wc_RsaPSS_VerifyCheck(void)
  16399. {
  16400. EXPECT_DECLS;
  16401. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16402. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16403. RsaKey key;
  16404. WC_RNG rng;
  16405. int sz = 256; /* 2048/8 */
  16406. byte digest[32];
  16407. word32 digestSz = sizeof(digest);
  16408. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16409. word32 pSignatureSz = sizeof(pSignature);
  16410. unsigned char pDecrypted[2048/8];
  16411. byte* pt = pDecrypted;
  16412. word32 outLen = sizeof(pDecrypted);
  16413. XMEMSET(&key, 0, sizeof(RsaKey));
  16414. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16415. XMEMSET(digest, 0, sizeof(digest));
  16416. XMEMSET(pSignature, 0, sizeof(pSignature));
  16417. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16418. ExpectIntEQ(wc_InitRng(&rng), 0);
  16419. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16420. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16421. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16422. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16423. 0);
  16424. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16425. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16426. /* Bad cases */
  16427. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16428. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16429. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16430. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16431. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16432. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16433. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16434. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16435. /* Good case */
  16436. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16437. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16438. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16439. wc_FreeRng(&rng);
  16440. #endif
  16441. return EXPECT_RESULT();
  16442. } /* END test_wc_RsaPSS_VerifyCheck */
  16443. /*
  16444. * Testing wc_RsaPSS_VerifyCheckInline()
  16445. */
  16446. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16447. {
  16448. EXPECT_DECLS;
  16449. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16450. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16451. RsaKey key;
  16452. WC_RNG rng;
  16453. int sz = 256;
  16454. byte digest[32];
  16455. word32 digestSz = sizeof(digest);
  16456. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16457. unsigned char pDecrypted[2048/8];
  16458. byte* pt = pDecrypted;
  16459. XMEMSET(&key, 0, sizeof(RsaKey));
  16460. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16461. XMEMSET(digest, 0, sizeof(digest));
  16462. XMEMSET(pSignature, 0, sizeof(pSignature));
  16463. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16464. ExpectIntEQ(wc_InitRng(&rng), 0);
  16465. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16466. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16467. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16468. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16469. 0);
  16470. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  16471. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16472. /* Bad Cases */
  16473. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  16474. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16475. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  16476. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16477. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  16478. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16479. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16480. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16481. /* Good case */
  16482. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16483. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16484. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16485. wc_FreeRng(&rng);
  16486. #endif
  16487. return EXPECT_RESULT();
  16488. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16489. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16490. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16491. {
  16492. (void)flag;
  16493. (void)type;
  16494. (void)file;
  16495. (void)line;
  16496. }
  16497. #endif
  16498. /*
  16499. * Testing wc_LockMutex_ex
  16500. */
  16501. static int test_wc_LockMutex_ex(void)
  16502. {
  16503. EXPECT_DECLS;
  16504. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16505. int flag = CRYPTO_LOCK;
  16506. int type = 0;
  16507. const char* file = "./test-LockMutex_ex.txt";
  16508. int line = 0;
  16509. /* without SetMutexCb */
  16510. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  16511. /* with SetMutexCb */
  16512. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16513. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  16514. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16515. #endif
  16516. return EXPECT_RESULT();
  16517. } /* End test_wc_LockMutex_ex*/
  16518. /*
  16519. * Testing wc_SetMutexCb
  16520. */
  16521. static int test_wc_SetMutexCb(void)
  16522. {
  16523. EXPECT_DECLS;
  16524. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16525. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16526. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16527. #endif
  16528. return EXPECT_RESULT();
  16529. } /* End test_wc_SetMutexCb*/
  16530. /*
  16531. * Testing wc_RsaKeyToDer()
  16532. */
  16533. static int test_wc_RsaKeyToDer(void)
  16534. {
  16535. EXPECT_DECLS;
  16536. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16537. RsaKey genKey;
  16538. WC_RNG rng;
  16539. byte* der = NULL;
  16540. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16541. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16542. int bits = 1024;
  16543. word32 derSz = 611;
  16544. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16545. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16546. #else
  16547. int bits = 2048;
  16548. word32 derSz = 1196;
  16549. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16550. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16551. #endif
  16552. XMEMSET(&rng, 0, sizeof(rng));
  16553. XMEMSET(&genKey, 0, sizeof(genKey));
  16554. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16555. /* Init structures. */
  16556. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16557. ExpectIntEQ(wc_InitRng(&rng), 0);
  16558. /* Make key. */
  16559. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16560. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  16561. #ifndef HAVE_USER_RSA
  16562. /* Pass good/bad args. */
  16563. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  16564. /* Get just the output length */
  16565. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16566. /* Try Public Key. */
  16567. genKey.type = 0;
  16568. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  16569. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16570. /* Put back to Private Key */
  16571. genKey.type = 1;
  16572. #endif
  16573. #else
  16574. /* Pass good/bad args. */
  16575. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16576. /* Get just the output length */
  16577. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16578. /* Try Public Key. */
  16579. genKey.type = 0;
  16580. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16581. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16582. /* Put back to Private Key */
  16583. genKey.type = 1;
  16584. #endif
  16585. #endif
  16586. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16587. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16588. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16589. #endif
  16590. return EXPECT_RESULT();
  16591. } /* END test_wc_RsaKeyToDer */
  16592. /*
  16593. * Testing wc_RsaKeyToPublicDer()
  16594. */
  16595. static int test_wc_RsaKeyToPublicDer(void)
  16596. {
  16597. EXPECT_DECLS;
  16598. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16599. RsaKey key;
  16600. WC_RNG rng;
  16601. byte* der = NULL;
  16602. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16603. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16604. int bits = 1024;
  16605. word32 derLen = 162;
  16606. #else
  16607. int bits = 2048;
  16608. word32 derLen = 294;
  16609. #endif
  16610. #ifndef HAVE_USER_RSA
  16611. int ret;
  16612. #endif
  16613. XMEMSET(&rng, 0, sizeof(rng));
  16614. XMEMSET(&key, 0, sizeof(key));
  16615. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16616. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16617. ExpectIntEQ(wc_InitRng(&rng), 0);
  16618. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16619. /* test getting size only */
  16620. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  16621. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  16622. /* test getting size only */
  16623. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  16624. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  16625. #ifndef HAVE_USER_RSA
  16626. /* Pass in bad args. */
  16627. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  16628. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  16629. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  16630. #else
  16631. /* Pass in bad args. */
  16632. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), USER_CRYPTO_ERROR);
  16633. ExpectIntEQ(wc_RsaKeyToPublicDer(&key, der, -1), USER_CRYPTO_ERROR);
  16634. #endif
  16635. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16636. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16637. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16638. #endif
  16639. return EXPECT_RESULT();
  16640. } /* END test_wc_RsaKeyToPublicDer */
  16641. /*
  16642. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16643. */
  16644. static int test_wc_RsaPublicEncryptDecrypt(void)
  16645. {
  16646. EXPECT_DECLS;
  16647. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16648. RsaKey key;
  16649. WC_RNG rng;
  16650. const char inStr[] = TEST_STRING;
  16651. const word32 plainLen = (word32)TEST_STRING_SZ;
  16652. const word32 inLen = (word32)TEST_STRING_SZ;
  16653. int bits = TEST_RSA_BITS;
  16654. const word32 cipherLen = TEST_RSA_BYTES;
  16655. word32 cipherLenResult = cipherLen;
  16656. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16657. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16658. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16659. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16660. ExpectNotNull(in);
  16661. ExpectNotNull(plain);
  16662. ExpectNotNull(cipher);
  16663. #endif
  16664. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16665. /* Initialize stack structures. */
  16666. XMEMSET(&key, 0, sizeof(RsaKey));
  16667. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16668. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16669. ExpectIntEQ(wc_InitRng(&rng), 0);
  16670. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16671. /* Encrypt. */
  16672. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  16673. cipherLen, &key, &rng), 0);
  16674. /* Pass bad args - tested in another testing function.*/
  16675. /* Decrypt */
  16676. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16677. /* Bind rng */
  16678. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16679. #endif
  16680. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  16681. &key), 0);
  16682. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  16683. /* Pass bad args - tested in another testing function.*/
  16684. WC_FREE_VAR(in, NULL);
  16685. WC_FREE_VAR(plain, NULL);
  16686. WC_FREE_VAR(cipher, NULL);
  16687. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16688. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16689. #endif
  16690. return EXPECT_RESULT();
  16691. } /* END test_wc_RsaPublicEncryptDecrypt */
  16692. /*
  16693. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16694. */
  16695. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16696. {
  16697. EXPECT_DECLS;
  16698. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16699. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16700. && !defined(NO_SHA256)
  16701. RsaKey key;
  16702. WC_RNG rng;
  16703. const char inStr[] = TEST_STRING;
  16704. const word32 inLen = (word32)TEST_STRING_SZ;
  16705. const word32 plainSz = (word32)TEST_STRING_SZ;
  16706. byte* res = NULL;
  16707. int idx = 0;
  16708. int bits = TEST_RSA_BITS;
  16709. const word32 cipherSz = TEST_RSA_BYTES;
  16710. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16711. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16712. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16713. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16714. ExpectNotNull(in);
  16715. ExpectNotNull(plain);
  16716. ExpectNotNull(cipher);
  16717. #endif
  16718. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16719. /* Initialize stack structures. */
  16720. XMEMSET(&key, 0, sizeof(RsaKey));
  16721. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16722. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  16723. ExpectIntEQ(wc_InitRng(&rng), 0);
  16724. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16725. /* Encrypt */
  16726. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  16727. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16728. /* Pass bad args - tested in another testing function.*/
  16729. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16730. /* Decrypt */
  16731. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16732. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16733. #endif
  16734. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  16735. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16736. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  16737. /* Pass bad args - tested in another testing function.*/
  16738. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  16739. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16740. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  16741. #endif
  16742. WC_FREE_VAR(in, NULL);
  16743. WC_FREE_VAR(plain, NULL);
  16744. WC_FREE_VAR(cipher, NULL);
  16745. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16746. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16747. #endif
  16748. return EXPECT_RESULT();
  16749. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16750. /*
  16751. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16752. */
  16753. static int test_wc_RsaSSL_SignVerify(void)
  16754. {
  16755. EXPECT_DECLS;
  16756. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16757. RsaKey key;
  16758. WC_RNG rng;
  16759. const char inStr[] = TEST_STRING;
  16760. const word32 plainSz = (word32)TEST_STRING_SZ;
  16761. const word32 inLen = (word32)TEST_STRING_SZ;
  16762. word32 idx = 0;
  16763. int bits = TEST_RSA_BITS;
  16764. const word32 outSz = TEST_RSA_BYTES;
  16765. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16766. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16767. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16768. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16769. ExpectNotNull(in);
  16770. ExpectNotNull(out);
  16771. ExpectNotNull(plain);
  16772. #endif
  16773. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16774. XMEMSET(&key, 0, sizeof(RsaKey));
  16775. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16776. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16777. ExpectIntEQ(wc_InitRng(&rng), 0);
  16778. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16779. /* Sign. */
  16780. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  16781. idx = (int)outSz;
  16782. #ifndef HAVE_USER_RSA
  16783. /* Test bad args. */
  16784. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16785. BAD_FUNC_ARG);
  16786. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16787. BAD_FUNC_ARG);
  16788. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16789. BAD_FUNC_ARG);
  16790. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16791. BAD_FUNC_ARG);
  16792. #else
  16793. /* Test bad args. */
  16794. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16795. USER_CRYPTO_ERROR);
  16796. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16797. USER_CRYPTO_ERROR);
  16798. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16799. USER_CRYPTO_ERROR);
  16800. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16801. USER_CRYPTO_ERROR);
  16802. #endif
  16803. /* Verify. */
  16804. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  16805. #ifndef HAVE_USER_RSA
  16806. /* Pass bad args. */
  16807. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16808. BAD_FUNC_ARG);
  16809. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16810. BAD_FUNC_ARG);
  16811. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16812. BAD_FUNC_ARG);
  16813. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16814. BAD_FUNC_ARG);
  16815. #else
  16816. /* Pass bad args. */
  16817. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16818. USER_CRYPTO_ERROR);
  16819. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16820. USER_CRYPTO_ERROR);
  16821. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16822. USER_CRYPTO_ERROR);
  16823. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16824. USER_CRYPTO_ERROR);
  16825. #endif
  16826. WC_FREE_VAR(in, NULL);
  16827. WC_FREE_VAR(out, NULL);
  16828. WC_FREE_VAR(plain, NULL);
  16829. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16830. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16831. #endif
  16832. return EXPECT_RESULT();
  16833. } /* END test_wc_RsaSSL_SignVerify */
  16834. /*
  16835. * Testing wc_RsaEncryptSize()
  16836. */
  16837. static int test_wc_RsaEncryptSize(void)
  16838. {
  16839. EXPECT_DECLS;
  16840. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16841. RsaKey key;
  16842. WC_RNG rng;
  16843. XMEMSET(&key, 0, sizeof(RsaKey));
  16844. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16845. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16846. ExpectIntEQ(wc_InitRng(&rng), 0);
  16847. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16848. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16849. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  16850. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  16851. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16852. #endif
  16853. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16854. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  16855. /* Pass in bad arg. */
  16856. #ifndef HAVE_USER_RSA
  16857. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  16858. #else
  16859. ExpectIntEQ(wc_RsaEncryptSize(NULL), 0);
  16860. #endif
  16861. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16862. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16863. #endif
  16864. return EXPECT_RESULT();
  16865. } /* END test_wc_RsaEncryptSize*/
  16866. /*
  16867. * Testing wc_RsaFlattenPublicKey()
  16868. */
  16869. static int test_wc_RsaFlattenPublicKey(void)
  16870. {
  16871. EXPECT_DECLS;
  16872. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16873. RsaKey key;
  16874. WC_RNG rng;
  16875. byte e[256];
  16876. byte n[256];
  16877. word32 eSz = sizeof(e);
  16878. word32 nSz = sizeof(n);
  16879. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16880. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16881. int bits = 1024;
  16882. #else
  16883. int bits = 2048;
  16884. #endif
  16885. XMEMSET(&key, 0, sizeof(RsaKey));
  16886. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16887. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16888. ExpectIntEQ(wc_InitRng(&rng), 0);
  16889. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16890. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  16891. #ifndef HAVE_USER_RSA
  16892. /* Pass bad args. */
  16893. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16894. BAD_FUNC_ARG);
  16895. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16896. BAD_FUNC_ARG);
  16897. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16898. BAD_FUNC_ARG);
  16899. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16900. BAD_FUNC_ARG);
  16901. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16902. BAD_FUNC_ARG);
  16903. #else
  16904. /* Pass bad args. */
  16905. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16906. USER_CRYPTO_ERROR);
  16907. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16908. USER_CRYPTO_ERROR);
  16909. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16910. USER_CRYPTO_ERROR);
  16911. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16912. USER_CRYPTO_ERROR);
  16913. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16914. USER_CRYPTO_ERROR);
  16915. #endif
  16916. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16917. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16918. #endif
  16919. return EXPECT_RESULT();
  16920. } /* END test_wc_RsaFlattenPublicKey */
  16921. /*
  16922. * unit test for wc_AesCcmSetKey
  16923. */
  16924. static int test_wc_AesCcmSetKey(void)
  16925. {
  16926. EXPECT_DECLS;
  16927. #ifdef HAVE_AESCCM
  16928. Aes aes;
  16929. const byte key16[] = {
  16930. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16931. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16932. };
  16933. const byte key24[] = {
  16934. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16935. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16936. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16937. };
  16938. const byte key32[] = {
  16939. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16940. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16941. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16942. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16943. };
  16944. XMEMSET(&aes, 0, sizeof(Aes));
  16945. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16946. #ifdef WOLFSSL_AES_128
  16947. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16948. #endif
  16949. #ifdef WOLFSSL_AES_192
  16950. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  16951. #endif
  16952. #ifdef WOLFSSL_AES_256
  16953. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  16954. #endif
  16955. /* Test bad args. */
  16956. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  16957. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  16958. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  16959. wc_AesFree(&aes);
  16960. #endif
  16961. return EXPECT_RESULT();
  16962. } /* END test_wc_AesCcmSetKey */
  16963. /*
  16964. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  16965. */
  16966. static int test_wc_AesCcmEncryptDecrypt(void)
  16967. {
  16968. EXPECT_DECLS;
  16969. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  16970. Aes aes;
  16971. const byte key16[] = {
  16972. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16973. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16974. };
  16975. /* plaintext */
  16976. const byte plainT[] = {
  16977. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16978. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  16979. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  16980. };
  16981. /* nonce */
  16982. const byte iv[] = {
  16983. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  16984. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  16985. };
  16986. const byte c[] = { /* cipher text. */
  16987. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  16988. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  16989. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  16990. };
  16991. const byte t[] = { /* Auth tag */
  16992. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  16993. };
  16994. const byte authIn[] = {
  16995. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  16996. };
  16997. byte cipherOut[sizeof(plainT)];
  16998. byte authTag[sizeof(t)];
  16999. #ifdef HAVE_AES_DECRYPT
  17000. byte plainOut[sizeof(cipherOut)];
  17001. #endif
  17002. XMEMSET(&aes, 0, sizeof(Aes));
  17003. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17004. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17005. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17006. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17007. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17008. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17009. #ifdef HAVE_AES_DECRYPT
  17010. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17011. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17012. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17013. #endif
  17014. /* Pass in bad args. Encrypt*/
  17015. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17016. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17017. BAD_FUNC_ARG);
  17018. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17019. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17020. BAD_FUNC_ARG);
  17021. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17022. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17023. BAD_FUNC_ARG);
  17024. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17025. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17026. BAD_FUNC_ARG);
  17027. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17028. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17029. BAD_FUNC_ARG);
  17030. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17031. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17032. BAD_FUNC_ARG);
  17033. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17034. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17035. BAD_FUNC_ARG);
  17036. #ifdef HAVE_AES_DECRYPT
  17037. /* Pass in bad args. Decrypt*/
  17038. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17039. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17040. BAD_FUNC_ARG);
  17041. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17042. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17043. BAD_FUNC_ARG);
  17044. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17045. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17046. BAD_FUNC_ARG);
  17047. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17048. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17049. BAD_FUNC_ARG);
  17050. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17051. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17052. BAD_FUNC_ARG);
  17053. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17054. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17055. BAD_FUNC_ARG);
  17056. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17057. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17058. BAD_FUNC_ARG);
  17059. #endif
  17060. wc_AesFree(&aes);
  17061. #endif /* HAVE_AESCCM */
  17062. return EXPECT_RESULT();
  17063. } /* END test_wc_AesCcmEncryptDecrypt */
  17064. #if defined(WOLFSSL_AES_EAX) && \
  17065. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17066. /*
  17067. * Testing test_wc_AesEaxVectors()
  17068. */
  17069. static int test_wc_AesEaxVectors(void)
  17070. {
  17071. EXPECT_DECLS;
  17072. typedef struct {
  17073. byte key[AES_256_KEY_SIZE];
  17074. int key_length;
  17075. byte iv[AES_BLOCK_SIZE];
  17076. int iv_length;
  17077. byte aad[AES_BLOCK_SIZE * 2];
  17078. int aad_length;
  17079. byte msg[AES_BLOCK_SIZE * 5];
  17080. int msg_length;
  17081. byte ct[AES_BLOCK_SIZE * 5];
  17082. int ct_length;
  17083. byte tag[AES_BLOCK_SIZE];
  17084. int tag_length;
  17085. int valid;
  17086. } AadVector;
  17087. /* Test vectors obtained from Google wycheproof project
  17088. * https://github.com/google/wycheproof
  17089. * from testvectors/aes_eax_test.json
  17090. */
  17091. const AadVector vectors[] = {
  17092. {
  17093. /* key, key length */
  17094. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17095. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17096. /* iv, iv length */
  17097. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17098. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17099. /* aad, aad length */
  17100. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17101. /* msg, msg length */
  17102. {0x00}, 0,
  17103. /* ct, ct length */
  17104. {0x00}, 0,
  17105. /* tag, tag length */
  17106. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17107. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17108. /* valid */
  17109. 1,
  17110. },
  17111. {
  17112. /* key, key length */
  17113. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17114. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17115. /* iv, iv length */
  17116. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17117. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17118. /* aad, aad length */
  17119. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17120. /* msg, msg length */
  17121. {0xf7, 0xfb}, 2,
  17122. /* ct, ct length */
  17123. {0x19, 0xdd}, 2,
  17124. /* tag, tag length */
  17125. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17126. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17127. /* valid */
  17128. 1,
  17129. },
  17130. {
  17131. /* key, key length */
  17132. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17133. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17134. /* iv, iv length */
  17135. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17136. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17137. /* aad, aad length */
  17138. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17139. /* msg, msg length */
  17140. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17141. /* ct, ct length */
  17142. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17143. /* tag, tag length */
  17144. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17145. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17146. /* valid */
  17147. 1,
  17148. },
  17149. {
  17150. /* key, key length */
  17151. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17152. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17153. /* iv, iv length */
  17154. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17155. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17156. /* aad, aad length */
  17157. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17158. /* msg, msg length */
  17159. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17160. /* ct, ct length */
  17161. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17162. /* tag, tag length */
  17163. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17164. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17165. /* valid */
  17166. 1,
  17167. },
  17168. {
  17169. /* key, key length */
  17170. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17171. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17172. /* iv, iv length */
  17173. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  17174. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  17175. /* aad, aad length */
  17176. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  17177. /* msg, msg length */
  17178. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  17179. /* ct, ct length */
  17180. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  17181. /* tag, tag length */
  17182. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  17183. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  17184. /* valid */
  17185. 1,
  17186. },
  17187. {
  17188. /* key, key length */
  17189. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  17190. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  17191. /* iv, iv length */
  17192. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  17193. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  17194. /* aad, aad length */
  17195. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  17196. /* msg, msg length */
  17197. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  17198. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  17199. /* ct, ct length */
  17200. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  17201. 0x0e, 0x72, 0x84, 0x14}, 12,
  17202. /* tag, tag length */
  17203. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  17204. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  17205. /* valid */
  17206. 1,
  17207. },
  17208. {
  17209. /* key, key length */
  17210. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  17211. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  17212. /* iv, iv length */
  17213. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  17214. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  17215. /* aad, aad length */
  17216. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  17217. /* msg, msg length */
  17218. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  17219. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  17220. 0x36}, 17,
  17221. /* ct, ct length */
  17222. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  17223. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  17224. 0x30}, 17,
  17225. /* tag, tag length */
  17226. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  17227. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  17228. /* valid */
  17229. 1,
  17230. },
  17231. {
  17232. /* key, key length */
  17233. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  17234. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  17235. /* iv, iv length */
  17236. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  17237. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  17238. /* aad, aad length */
  17239. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  17240. /* msg, msg length */
  17241. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  17242. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  17243. 0x2d, 0x56}, 18,
  17244. /* ct, ct length */
  17245. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  17246. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  17247. 0x8c, 0xc3}, 18,
  17248. /* tag, tag length */
  17249. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  17250. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  17251. /* valid */
  17252. 1,
  17253. },
  17254. {
  17255. /* key, key length */
  17256. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  17257. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  17258. /* iv, iv length */
  17259. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  17260. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  17261. /* aad, aad length */
  17262. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  17263. /* msg, msg length */
  17264. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  17265. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  17266. 0xef, 0x11}, 18,
  17267. /* ct, ct length */
  17268. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  17269. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  17270. 0xb1, 0xe8}, 18,
  17271. /* tag, tag length */
  17272. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  17273. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  17274. /* valid */
  17275. 1,
  17276. },
  17277. {
  17278. /* key, key length */
  17279. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  17280. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  17281. /* iv, iv length */
  17282. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  17283. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  17284. /* aad, aad length */
  17285. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  17286. /* msg, msg length */
  17287. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  17288. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  17289. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  17290. /* ct, ct length */
  17291. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  17292. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  17293. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  17294. /* tag, tag length */
  17295. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  17296. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  17297. /* valid */
  17298. 1,
  17299. },
  17300. {
  17301. /* key, key length */
  17302. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17303. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17304. /* iv, iv length */
  17305. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  17306. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  17307. /* aad, aad length */
  17308. {0x00}, 0,
  17309. /* msg, msg length */
  17310. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17311. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17312. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17313. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17314. /* ct, ct length */
  17315. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  17316. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  17317. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  17318. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  17319. /* tag, tag length */
  17320. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  17321. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  17322. /* valid */
  17323. 1,
  17324. },
  17325. {
  17326. /* key, key length */
  17327. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17328. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17329. /* iv, iv length */
  17330. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  17331. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  17332. /* aad, aad length */
  17333. {0x00}, 0,
  17334. /* msg, msg length */
  17335. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17336. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17337. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17338. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17339. /* ct, ct length */
  17340. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  17341. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  17342. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  17343. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  17344. /* tag, tag length */
  17345. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  17346. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  17347. /* valid */
  17348. 1,
  17349. },
  17350. {
  17351. /* key, key length */
  17352. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17353. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17354. /* iv, iv length */
  17355. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  17356. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  17357. /* aad, aad length */
  17358. {0x00}, 0,
  17359. /* msg, msg length */
  17360. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17361. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17362. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17363. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17364. /* ct, ct length */
  17365. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  17366. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  17367. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  17368. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  17369. /* tag, tag length */
  17370. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  17371. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  17372. /* valid */
  17373. 1,
  17374. },
  17375. {
  17376. /* key, key length */
  17377. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17378. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17379. /* iv, iv length */
  17380. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  17381. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  17382. /* aad, aad length */
  17383. {0x00}, 0,
  17384. /* msg, msg length */
  17385. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17386. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17387. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17388. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17389. /* ct, ct length */
  17390. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  17391. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  17392. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  17393. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  17394. /* tag, tag length */
  17395. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  17396. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  17397. /* valid */
  17398. 1,
  17399. },
  17400. {
  17401. /* key, key length */
  17402. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17403. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17404. /* iv, iv length */
  17405. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  17406. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  17407. /* aad, aad length */
  17408. {0x00}, 0,
  17409. /* msg, msg length */
  17410. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17412. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17413. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17414. /* ct, ct length */
  17415. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  17416. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  17417. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  17418. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  17419. /* tag, tag length */
  17420. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  17421. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  17422. /* valid */
  17423. 1,
  17424. },
  17425. {
  17426. /* key, key length */
  17427. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17428. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17429. /* iv, iv length */
  17430. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  17431. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  17432. /* aad, aad length */
  17433. {0x00}, 0,
  17434. /* msg, msg length */
  17435. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17436. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17437. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17438. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  17439. /* ct, ct length */
  17440. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  17441. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  17442. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  17443. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  17444. /* tag, tag length */
  17445. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  17446. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  17447. /* valid */
  17448. 1,
  17449. },
  17450. {
  17451. /* key, key length */
  17452. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17453. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17454. /* iv, iv length */
  17455. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  17456. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  17457. /* aad, aad length */
  17458. {0x00}, 0,
  17459. /* msg, msg length */
  17460. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17461. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17462. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17463. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17464. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17465. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17466. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17467. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  17468. /* ct, ct length */
  17469. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  17470. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  17471. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  17472. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  17473. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  17474. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  17475. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  17476. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  17477. /* tag, tag length */
  17478. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  17479. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  17480. /* valid */
  17481. 1,
  17482. },
  17483. {
  17484. /* key, key length */
  17485. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17486. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17487. /* iv, iv length */
  17488. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  17489. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  17490. /* aad, aad length */
  17491. {0x00}, 0,
  17492. /* msg, msg length */
  17493. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17494. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  17495. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17496. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  17497. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17498. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  17499. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17500. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  17501. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  17502. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  17503. /* ct, ct length */
  17504. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  17505. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  17506. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  17507. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  17508. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  17509. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  17510. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  17511. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  17512. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  17513. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  17514. /* tag, tag length */
  17515. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  17516. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  17517. /* valid */
  17518. 1,
  17519. },
  17520. {
  17521. /* key, key length */
  17522. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  17523. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  17524. /* iv, iv length */
  17525. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  17526. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  17527. /* aad, aad length */
  17528. {0x00}, 0,
  17529. /* msg, msg length */
  17530. {0x00}, 0,
  17531. /* ct, ct length */
  17532. {0x00}, 0,
  17533. /* tag, tag length */
  17534. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  17535. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  17536. /* valid */
  17537. 1,
  17538. },
  17539. {
  17540. /* key, key length */
  17541. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  17542. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  17543. /* iv, iv length */
  17544. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  17545. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  17546. /* aad, aad length */
  17547. {0x00}, 0,
  17548. /* msg, msg length */
  17549. {0x01}, 1,
  17550. /* ct, ct length */
  17551. {0x1d}, 1,
  17552. /* tag, tag length */
  17553. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  17554. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  17555. /* valid */
  17556. 1,
  17557. },
  17558. {
  17559. /* key, key length */
  17560. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  17561. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  17562. /* iv, iv length */
  17563. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  17564. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  17565. /* aad, aad length */
  17566. {0x00}, 0,
  17567. /* msg, msg length */
  17568. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  17569. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  17570. /* ct, ct length */
  17571. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  17572. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  17573. /* tag, tag length */
  17574. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  17575. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  17576. /* valid */
  17577. 1,
  17578. },
  17579. {
  17580. /* key, key length */
  17581. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  17582. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  17583. /* iv, iv length */
  17584. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  17585. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  17586. /* aad, aad length */
  17587. {0x00}, 0,
  17588. /* msg, msg length */
  17589. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  17590. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  17591. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  17592. /* ct, ct length */
  17593. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  17594. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  17595. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  17596. /* tag, tag length */
  17597. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  17598. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  17599. /* valid */
  17600. 1,
  17601. },
  17602. {
  17603. /* key, key length */
  17604. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  17605. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  17606. /* iv, iv length */
  17607. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  17608. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  17609. /* aad, aad length */
  17610. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  17611. /* msg, msg length */
  17612. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  17613. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  17614. /* ct, ct length */
  17615. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  17616. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  17617. /* tag, tag length */
  17618. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  17619. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  17620. /* valid */
  17621. 1,
  17622. },
  17623. {
  17624. /* key, key length */
  17625. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  17626. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  17627. /* iv, iv length */
  17628. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  17629. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  17630. /* aad, aad length */
  17631. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  17632. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  17633. /* msg, msg length */
  17634. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  17635. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  17636. /* ct, ct length */
  17637. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  17638. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  17639. /* tag, tag length */
  17640. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  17641. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  17642. /* valid */
  17643. 1,
  17644. },
  17645. {
  17646. /* key, key length */
  17647. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  17648. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  17649. /* iv, iv length */
  17650. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  17651. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  17652. /* aad, aad length */
  17653. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  17654. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  17655. /* msg, msg length */
  17656. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  17657. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  17658. /* ct, ct length */
  17659. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  17660. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  17661. /* tag, tag length */
  17662. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  17663. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  17664. /* valid */
  17665. 1,
  17666. },
  17667. {
  17668. /* key, key length */
  17669. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  17670. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  17671. /* iv, iv length */
  17672. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  17673. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  17674. /* aad, aad length */
  17675. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  17676. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  17677. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  17678. /* msg, msg length */
  17679. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  17680. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  17681. /* ct, ct length */
  17682. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  17683. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  17684. /* tag, tag length */
  17685. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  17686. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  17687. /* valid */
  17688. 1,
  17689. },
  17690. {
  17691. /* key, key length */
  17692. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17693. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17694. /* iv, iv length */
  17695. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17696. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17697. /* aad, aad length */
  17698. {0x00}, 0,
  17699. /* msg, msg length */
  17700. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17701. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17702. /* ct, ct length */
  17703. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17704. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17705. /* tag, tag length */
  17706. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17707. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17708. /* valid */
  17709. 0,
  17710. },
  17711. {
  17712. /* key, key length */
  17713. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17714. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17715. /* iv, iv length */
  17716. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17717. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17718. /* aad, aad length */
  17719. {0x00}, 0,
  17720. /* msg, msg length */
  17721. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17722. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17723. /* ct, ct length */
  17724. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17725. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17726. /* tag, tag length */
  17727. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17728. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17729. /* valid */
  17730. 0,
  17731. },
  17732. {
  17733. /* key, key length */
  17734. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17735. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17736. /* iv, iv length */
  17737. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17738. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17739. /* aad, aad length */
  17740. {0x00}, 0,
  17741. /* msg, msg length */
  17742. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17743. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17744. /* ct, ct length */
  17745. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17746. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17747. /* tag, tag length */
  17748. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17749. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17750. /* valid */
  17751. 0,
  17752. },
  17753. {
  17754. /* key, key length */
  17755. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17756. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17757. /* iv, iv length */
  17758. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17759. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17760. /* aad, aad length */
  17761. {0x00}, 0,
  17762. /* msg, msg length */
  17763. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17764. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17765. /* ct, ct length */
  17766. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17767. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17768. /* tag, tag length */
  17769. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17770. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17771. /* valid */
  17772. 0,
  17773. },
  17774. {
  17775. /* key, key length */
  17776. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17777. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17778. /* iv, iv length */
  17779. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17780. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17781. /* aad, aad length */
  17782. {0x00}, 0,
  17783. /* msg, msg length */
  17784. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17785. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17786. /* ct, ct length */
  17787. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17788. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17789. /* tag, tag length */
  17790. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  17791. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17792. /* valid */
  17793. 0,
  17794. },
  17795. {
  17796. /* key, key length */
  17797. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17798. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17799. /* iv, iv length */
  17800. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17801. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17802. /* aad, aad length */
  17803. {0x00}, 0,
  17804. /* msg, msg length */
  17805. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17806. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17807. /* ct, ct length */
  17808. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17809. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17810. /* tag, tag length */
  17811. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  17812. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17813. /* valid */
  17814. 0,
  17815. },
  17816. {
  17817. /* key, key length */
  17818. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17819. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17820. /* iv, iv length */
  17821. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17822. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17823. /* aad, aad length */
  17824. {0x00}, 0,
  17825. /* msg, msg length */
  17826. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17827. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17828. /* ct, ct length */
  17829. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17830. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17831. /* tag, tag length */
  17832. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  17833. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17834. /* valid */
  17835. 0,
  17836. },
  17837. {
  17838. /* key, key length */
  17839. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17840. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17841. /* iv, iv length */
  17842. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17843. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17844. /* aad, aad length */
  17845. {0x00}, 0,
  17846. /* msg, msg length */
  17847. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17848. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17849. /* ct, ct length */
  17850. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17851. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17852. /* tag, tag length */
  17853. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  17854. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17855. /* valid */
  17856. 0,
  17857. },
  17858. {
  17859. /* key, key length */
  17860. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17861. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17862. /* iv, iv length */
  17863. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17864. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17865. /* aad, aad length */
  17866. {0x00}, 0,
  17867. /* msg, msg length */
  17868. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17869. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17870. /* ct, ct length */
  17871. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17872. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17873. /* tag, tag length */
  17874. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17875. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17876. /* valid */
  17877. 0,
  17878. },
  17879. {
  17880. /* key, key length */
  17881. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17882. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17883. /* iv, iv length */
  17884. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17885. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17886. /* aad, aad length */
  17887. {0x00}, 0,
  17888. /* msg, msg length */
  17889. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17890. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17891. /* ct, ct length */
  17892. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17893. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17894. /* tag, tag length */
  17895. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17896. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17897. /* valid */
  17898. 0,
  17899. },
  17900. {
  17901. /* key, key length */
  17902. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17903. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17904. /* iv, iv length */
  17905. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17906. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17907. /* aad, aad length */
  17908. {0x00}, 0,
  17909. /* msg, msg length */
  17910. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17911. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17912. /* ct, ct length */
  17913. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17914. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17915. /* tag, tag length */
  17916. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17917. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17918. /* valid */
  17919. 0,
  17920. },
  17921. {
  17922. /* key, key length */
  17923. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17924. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17925. /* iv, iv length */
  17926. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17927. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17928. /* aad, aad length */
  17929. {0x00}, 0,
  17930. /* msg, msg length */
  17931. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17932. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17933. /* ct, ct length */
  17934. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17935. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17936. /* tag, tag length */
  17937. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17938. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  17939. /* valid */
  17940. 0,
  17941. },
  17942. {
  17943. /* key, key length */
  17944. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17945. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17946. /* iv, iv length */
  17947. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17948. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17949. /* aad, aad length */
  17950. {0x00}, 0,
  17951. /* msg, msg length */
  17952. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17953. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17954. /* ct, ct length */
  17955. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17956. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17957. /* tag, tag length */
  17958. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17959. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  17960. /* valid */
  17961. 0,
  17962. },
  17963. {
  17964. /* key, key length */
  17965. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17966. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17967. /* iv, iv length */
  17968. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17969. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17970. /* aad, aad length */
  17971. {0x00}, 0,
  17972. /* msg, msg length */
  17973. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17974. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17975. /* ct, ct length */
  17976. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17977. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17978. /* tag, tag length */
  17979. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  17980. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  17981. /* valid */
  17982. 0,
  17983. },
  17984. {
  17985. /* key, key length */
  17986. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  17987. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  17988. /* iv, iv length */
  17989. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  17990. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  17991. /* aad, aad length */
  17992. {0x00}, 0,
  17993. /* msg, msg length */
  17994. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  17995. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  17996. /* ct, ct length */
  17997. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  17998. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  17999. /* tag, tag length */
  18000. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18001. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18002. /* valid */
  18003. 0,
  18004. },
  18005. {
  18006. /* key, key length */
  18007. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18008. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18009. /* iv, iv length */
  18010. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18011. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18012. /* aad, aad length */
  18013. {0x00}, 0,
  18014. /* msg, msg length */
  18015. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18016. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18017. /* ct, ct length */
  18018. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18019. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18020. /* tag, tag length */
  18021. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18022. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18023. /* valid */
  18024. 0,
  18025. },
  18026. {
  18027. /* key, key length */
  18028. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18029. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18030. /* iv, iv length */
  18031. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18032. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18033. /* aad, aad length */
  18034. {0x00}, 0,
  18035. /* msg, msg length */
  18036. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18037. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18038. /* ct, ct length */
  18039. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18040. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18041. /* tag, tag length */
  18042. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18043. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18044. /* valid */
  18045. 0,
  18046. },
  18047. {
  18048. /* key, key length */
  18049. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18050. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18051. /* iv, iv length */
  18052. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18053. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18054. /* aad, aad length */
  18055. {0x00}, 0,
  18056. /* msg, msg length */
  18057. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18058. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18059. /* ct, ct length */
  18060. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18061. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18062. /* tag, tag length */
  18063. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18064. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18065. /* valid */
  18066. 0,
  18067. },
  18068. {
  18069. /* key, key length */
  18070. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18071. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18072. /* iv, iv length */
  18073. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18074. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18075. /* aad, aad length */
  18076. {0x00}, 0,
  18077. /* msg, msg length */
  18078. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18079. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18080. /* ct, ct length */
  18081. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18082. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18083. /* tag, tag length */
  18084. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18085. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18086. /* valid */
  18087. 0,
  18088. },
  18089. {
  18090. /* key, key length */
  18091. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18092. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18093. /* iv, iv length */
  18094. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18095. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18096. /* aad, aad length */
  18097. {0x00}, 0,
  18098. /* msg, msg length */
  18099. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18100. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18101. /* ct, ct length */
  18102. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18103. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18104. /* tag, tag length */
  18105. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18106. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18107. /* valid */
  18108. 0,
  18109. },
  18110. {
  18111. /* key, key length */
  18112. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18113. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18114. /* iv, iv length */
  18115. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18116. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18117. /* aad, aad length */
  18118. {0x00}, 0,
  18119. /* msg, msg length */
  18120. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18121. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18122. /* ct, ct length */
  18123. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18124. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18125. /* tag, tag length */
  18126. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18127. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18128. /* valid */
  18129. 0,
  18130. },
  18131. {
  18132. /* key, key length */
  18133. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18134. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18135. /* iv, iv length */
  18136. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18137. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18138. /* aad, aad length */
  18139. {0x00}, 0,
  18140. /* msg, msg length */
  18141. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18142. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18143. /* ct, ct length */
  18144. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18145. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18146. /* tag, tag length */
  18147. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18148. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18149. /* valid */
  18150. 0,
  18151. },
  18152. {
  18153. /* key, key length */
  18154. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18155. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18156. /* iv, iv length */
  18157. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18158. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18159. /* aad, aad length */
  18160. {0x00}, 0,
  18161. /* msg, msg length */
  18162. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18163. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18164. /* ct, ct length */
  18165. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18166. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18167. /* tag, tag length */
  18168. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18169. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18170. /* valid */
  18171. 0,
  18172. },
  18173. {
  18174. /* key, key length */
  18175. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18176. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18177. /* iv, iv length */
  18178. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18179. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18180. /* aad, aad length */
  18181. {0x00}, 0,
  18182. /* msg, msg length */
  18183. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18184. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18185. /* ct, ct length */
  18186. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18187. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18188. /* tag, tag length */
  18189. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18190. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  18191. /* valid */
  18192. 0,
  18193. },
  18194. {
  18195. /* key, key length */
  18196. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18197. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18198. /* iv, iv length */
  18199. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18200. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18201. /* aad, aad length */
  18202. {0x00}, 0,
  18203. /* msg, msg length */
  18204. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18205. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18206. /* ct, ct length */
  18207. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18208. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18209. /* tag, tag length */
  18210. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  18211. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  18212. /* valid */
  18213. 0,
  18214. },
  18215. {
  18216. /* key, key length */
  18217. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18218. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18219. /* iv, iv length */
  18220. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18221. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18222. /* aad, aad length */
  18223. {0x00}, 0,
  18224. /* msg, msg length */
  18225. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18226. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18227. /* ct, ct length */
  18228. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18229. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18230. /* tag, tag length */
  18231. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  18232. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  18233. /* valid */
  18234. 0,
  18235. },
  18236. {
  18237. /* key, key length */
  18238. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18239. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18240. /* iv, iv length */
  18241. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18242. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18243. /* aad, aad length */
  18244. {0x00}, 0,
  18245. /* msg, msg length */
  18246. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18247. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18248. /* ct, ct length */
  18249. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18250. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18251. /* tag, tag length */
  18252. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  18253. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  18254. /* valid */
  18255. 0,
  18256. },
  18257. };
  18258. byte ciphertext[sizeof(vectors[0].ct)];
  18259. byte authtag[sizeof(vectors[0].tag)];
  18260. int i;
  18261. int len;
  18262. int ret;
  18263. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  18264. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18265. len = sizeof(authtag);
  18266. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  18267. ciphertext,
  18268. vectors[i].msg, vectors[i].msg_length,
  18269. vectors[i].iv, vectors[i].iv_length,
  18270. authtag, len,
  18271. vectors[i].aad, vectors[i].aad_length),
  18272. 0);
  18273. /* check ciphertext matches vector */
  18274. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  18275. 0);
  18276. /* check that computed tag matches vector only for vectors marked as valid */
  18277. ret = XMEMCMP(authtag, vectors[i].tag, len);
  18278. if (vectors[i].valid) {
  18279. ExpectIntEQ(ret, 0);
  18280. }
  18281. else {
  18282. ExpectIntNE(ret, 0);
  18283. }
  18284. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  18285. /* Decrypt, checking that the computed auth tags match */
  18286. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  18287. ciphertext,
  18288. vectors[i].ct, vectors[i].ct_length,
  18289. vectors[i].iv, vectors[i].iv_length,
  18290. authtag, len,
  18291. vectors[i].aad, vectors[i].aad_length),
  18292. 0);
  18293. /* check decrypted ciphertext matches vector plaintext */
  18294. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  18295. 0);
  18296. }
  18297. return EXPECT_RESULT();
  18298. } /* END test_wc_AesEaxVectors */
  18299. /*
  18300. * Testing test_wc_AesEaxEncryptAuth()
  18301. */
  18302. static int test_wc_AesEaxEncryptAuth(void)
  18303. {
  18304. EXPECT_DECLS;
  18305. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18306. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18307. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18308. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18309. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18310. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18311. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18312. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18313. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18314. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18315. byte ciphertext[sizeof(msg)];
  18316. byte authtag[AES_BLOCK_SIZE];
  18317. int i;
  18318. int len;
  18319. len = sizeof(authtag);
  18320. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18321. ciphertext,
  18322. msg, sizeof(msg),
  18323. iv, sizeof(iv),
  18324. authtag, len,
  18325. aad, sizeof(aad)),
  18326. 0);
  18327. /* Test null checking */
  18328. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  18329. ciphertext,
  18330. msg, sizeof(msg),
  18331. iv, sizeof(iv),
  18332. authtag, len,
  18333. aad, sizeof(aad)),
  18334. BAD_FUNC_ARG);
  18335. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18336. NULL,
  18337. msg, sizeof(msg),
  18338. iv, sizeof(iv),
  18339. authtag, len,
  18340. aad, sizeof(aad)),
  18341. BAD_FUNC_ARG);
  18342. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18343. ciphertext,
  18344. NULL, sizeof(msg),
  18345. iv, sizeof(iv),
  18346. authtag, len,
  18347. aad, sizeof(aad)),
  18348. BAD_FUNC_ARG);
  18349. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18350. ciphertext,
  18351. msg, sizeof(msg),
  18352. NULL, sizeof(iv),
  18353. authtag, len,
  18354. aad, sizeof(aad)),
  18355. BAD_FUNC_ARG);
  18356. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18357. ciphertext,
  18358. msg, sizeof(msg),
  18359. iv, sizeof(iv),
  18360. NULL, len,
  18361. aad, sizeof(aad)),
  18362. BAD_FUNC_ARG);
  18363. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18364. ciphertext,
  18365. msg, sizeof(msg),
  18366. iv, sizeof(iv),
  18367. authtag, len,
  18368. NULL, sizeof(aad)),
  18369. BAD_FUNC_ARG);
  18370. /* Test bad key lengths */
  18371. for (i = 0; i <= 32; i++) {
  18372. int exp_ret;
  18373. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18374. || i == AES_256_KEY_SIZE) {
  18375. exp_ret = 0;
  18376. }
  18377. else {
  18378. exp_ret = BAD_FUNC_ARG;
  18379. }
  18380. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  18381. ciphertext,
  18382. msg, sizeof(msg),
  18383. iv, sizeof(iv),
  18384. authtag, len,
  18385. aad, sizeof(aad)),
  18386. exp_ret);
  18387. }
  18388. /* Test auth tag size out of range */
  18389. len = AES_BLOCK_SIZE + 1;
  18390. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  18391. ciphertext,
  18392. msg, sizeof(msg),
  18393. iv, sizeof(iv),
  18394. authtag, len,
  18395. aad, sizeof(aad)),
  18396. BAD_FUNC_ARG);
  18397. return EXPECT_RESULT();
  18398. } /* END test_wc_AesEaxEncryptAuth() */
  18399. /*
  18400. * Testing test_wc_AesEaxDecryptAuth()
  18401. */
  18402. static int test_wc_AesEaxDecryptAuth(void)
  18403. {
  18404. EXPECT_DECLS;
  18405. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18406. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18407. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18408. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18409. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18410. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  18411. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18412. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  18413. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  18414. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  18415. /* Garbage tag that should always fail for above aad */
  18416. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  18417. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  18418. byte plaintext[sizeof(ct)];
  18419. int i;
  18420. int len;
  18421. len = sizeof(tag);
  18422. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18423. plaintext,
  18424. ct, sizeof(ct),
  18425. iv, sizeof(iv),
  18426. tag, len,
  18427. aad, sizeof(aad)),
  18428. AES_EAX_AUTH_E);
  18429. /* Test null checking */
  18430. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  18431. plaintext,
  18432. ct, sizeof(ct),
  18433. iv, sizeof(iv),
  18434. tag, len,
  18435. aad, sizeof(aad)),
  18436. BAD_FUNC_ARG);
  18437. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18438. NULL,
  18439. ct, sizeof(ct),
  18440. iv, sizeof(iv),
  18441. tag, len,
  18442. aad, sizeof(aad)),
  18443. BAD_FUNC_ARG);
  18444. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18445. plaintext,
  18446. NULL, sizeof(ct),
  18447. iv, sizeof(iv),
  18448. tag, len,
  18449. aad, sizeof(aad)),
  18450. BAD_FUNC_ARG);
  18451. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18452. plaintext,
  18453. ct, sizeof(ct),
  18454. NULL, sizeof(iv),
  18455. tag, len,
  18456. aad, sizeof(aad)),
  18457. BAD_FUNC_ARG);
  18458. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18459. plaintext,
  18460. ct, sizeof(ct),
  18461. iv, sizeof(iv),
  18462. NULL, len,
  18463. aad, sizeof(aad)),
  18464. BAD_FUNC_ARG);
  18465. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18466. plaintext,
  18467. ct, sizeof(ct),
  18468. iv, sizeof(iv),
  18469. tag, len,
  18470. NULL, sizeof(aad)),
  18471. BAD_FUNC_ARG);
  18472. /* Test bad key lengths */
  18473. for (i = 0; i <= 32; i++) {
  18474. int exp_ret;
  18475. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  18476. || i == AES_256_KEY_SIZE) {
  18477. exp_ret = AES_EAX_AUTH_E;
  18478. }
  18479. else {
  18480. exp_ret = BAD_FUNC_ARG;
  18481. }
  18482. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  18483. plaintext,
  18484. ct, sizeof(ct),
  18485. iv, sizeof(iv),
  18486. tag, len,
  18487. aad, sizeof(aad)),
  18488. exp_ret);
  18489. }
  18490. /* Test auth tag size out of range */
  18491. len = AES_BLOCK_SIZE + 1;
  18492. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  18493. plaintext,
  18494. ct, sizeof(ct),
  18495. iv, sizeof(iv),
  18496. tag, len,
  18497. aad, sizeof(aad)),
  18498. BAD_FUNC_ARG);
  18499. return EXPECT_RESULT();
  18500. } /* END test_wc_AesEaxDecryptAuth() */
  18501. #endif /* WOLFSSL_AES_EAX &&
  18502. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  18503. */
  18504. /*
  18505. * Testing wc_InitDsaKey()
  18506. */
  18507. static int test_wc_InitDsaKey(void)
  18508. {
  18509. EXPECT_DECLS;
  18510. #ifndef NO_DSA
  18511. DsaKey key;
  18512. XMEMSET(&key, 0, sizeof(DsaKey));
  18513. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18514. /* Pass in bad args. */
  18515. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  18516. wc_FreeDsaKey(&key);
  18517. #endif
  18518. return EXPECT_RESULT();
  18519. } /* END test_wc_InitDsaKey */
  18520. /*
  18521. * Testing wc_DsaSign() and wc_DsaVerify()
  18522. */
  18523. static int test_wc_DsaSignVerify(void)
  18524. {
  18525. EXPECT_DECLS;
  18526. #if !defined(NO_DSA)
  18527. DsaKey key;
  18528. WC_RNG rng;
  18529. wc_Sha sha;
  18530. byte signature[DSA_SIG_SIZE];
  18531. byte hash[WC_SHA_DIGEST_SIZE];
  18532. word32 idx = 0;
  18533. word32 bytes;
  18534. int answer;
  18535. #ifdef USE_CERT_BUFFERS_1024
  18536. byte tmp[ONEK_BUF];
  18537. XMEMSET(tmp, 0, sizeof(tmp));
  18538. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18539. bytes = sizeof_dsa_key_der_1024;
  18540. #elif defined(USE_CERT_BUFFERS_2048)
  18541. byte tmp[TWOK_BUF];
  18542. XMEMSET(tmp, 0, sizeof(tmp));
  18543. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18544. bytes = sizeof_dsa_key_der_2048;
  18545. #else
  18546. byte tmp[TWOK_BUF];
  18547. XFILE fp = XBADFILE;
  18548. XMEMSET(tmp, 0, sizeof(tmp));
  18549. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18550. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18551. if (fp != XBADFILE)
  18552. XFCLOSE(fp);
  18553. #endif /* END USE_CERT_BUFFERS_1024 */
  18554. ExpectIntEQ(wc_InitSha(&sha), 0);
  18555. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  18556. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  18557. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18558. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18559. ExpectIntEQ(wc_InitRng(&rng), 0);
  18560. /* Sign. */
  18561. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  18562. /* Test bad args. */
  18563. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  18564. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  18565. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  18566. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18567. /* Verify. */
  18568. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  18569. ExpectIntEQ(answer, 1);
  18570. /* Pass in bad args. */
  18571. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  18572. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  18573. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  18574. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  18575. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18576. /* hard set q to 0 and test fail case */
  18577. mp_free(&key.q);
  18578. mp_init(&key.q);
  18579. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18580. mp_set(&key.q, 1);
  18581. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18582. #endif
  18583. DoExpectIntEQ(wc_FreeRng(&rng),0);
  18584. wc_FreeDsaKey(&key);
  18585. wc_ShaFree(&sha);
  18586. #endif
  18587. return EXPECT_RESULT();
  18588. } /* END test_wc_DsaSign */
  18589. /*
  18590. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18591. */
  18592. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18593. {
  18594. EXPECT_DECLS;
  18595. #if !defined(NO_DSA)
  18596. DsaKey key;
  18597. word32 bytes;
  18598. word32 idx = 0;
  18599. int ret;
  18600. #ifdef USE_CERT_BUFFERS_1024
  18601. byte tmp[ONEK_BUF];
  18602. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18603. bytes = sizeof_dsa_key_der_1024;
  18604. #elif defined(USE_CERT_BUFFERS_2048)
  18605. byte tmp[TWOK_BUF];
  18606. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18607. bytes = sizeof_dsa_key_der_2048;
  18608. #else
  18609. byte tmp[TWOK_BUF];
  18610. XFILE fp = XBADFILE;
  18611. XMEMSET(tmp, 0, sizeof(tmp));
  18612. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18613. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18614. if (fp != XBADFILE)
  18615. XFCLOSE(fp);
  18616. #endif /* END USE_CERT_BUFFERS_1024 */
  18617. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18618. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18619. /* Test bad args. */
  18620. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18621. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18622. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18623. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18624. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18625. wc_FreeDsaKey(&key);
  18626. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18627. idx = 0; /* Reset */
  18628. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18629. /* Test bad args. */
  18630. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  18631. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  18632. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  18633. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  18634. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  18635. wc_FreeDsaKey(&key);
  18636. #endif /* !NO_DSA */
  18637. return EXPECT_RESULT();
  18638. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18639. /*
  18640. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18641. */
  18642. static int test_wc_MakeDsaKey(void)
  18643. {
  18644. EXPECT_DECLS;
  18645. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18646. DsaKey genKey;
  18647. WC_RNG rng;
  18648. XMEMSET(&genKey, 0, sizeof(genKey));
  18649. XMEMSET(&rng, 0, sizeof(rng));
  18650. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  18651. ExpectIntEQ(wc_InitRng(&rng), 0);
  18652. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  18653. /* Test bad args. */
  18654. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  18655. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  18656. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  18657. BAD_FUNC_ARG);
  18658. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  18659. /* Test bad args. */
  18660. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  18661. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  18662. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18663. wc_FreeDsaKey(&genKey);
  18664. #endif
  18665. return EXPECT_RESULT();
  18666. } /* END test_wc_MakeDsaKey */
  18667. /*
  18668. * Testing wc_DsaKeyToDer()
  18669. */
  18670. static int test_wc_DsaKeyToDer(void)
  18671. {
  18672. EXPECT_DECLS;
  18673. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18674. DsaKey key;
  18675. word32 bytes;
  18676. word32 idx = 0;
  18677. #ifdef USE_CERT_BUFFERS_1024
  18678. byte tmp[ONEK_BUF];
  18679. byte der[ONEK_BUF];
  18680. XMEMSET(tmp, 0, sizeof(tmp));
  18681. XMEMSET(der, 0, sizeof(der));
  18682. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18683. bytes = sizeof_dsa_key_der_1024;
  18684. #elif defined(USE_CERT_BUFFERS_2048)
  18685. byte tmp[TWOK_BUF];
  18686. byte der[TWOK_BUF];
  18687. XMEMSET(tmp, 0, sizeof(tmp));
  18688. XMEMSET(der, 0, sizeof(der));
  18689. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18690. bytes = sizeof_dsa_key_der_2048;
  18691. #else
  18692. byte tmp[TWOK_BUF];
  18693. byte der[TWOK_BUF];
  18694. XFILE fp = XBADFILE;
  18695. XMEMSET(tmp, 0, sizeof(tmp));
  18696. XMEMSET(der, 0, sizeof(der));
  18697. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  18698. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  18699. if (fp != XBADFILE)
  18700. XFCLOSE(fp);
  18701. #endif /* END USE_CERT_BUFFERS_1024 */
  18702. XMEMSET(&key, 0, sizeof(DsaKey));
  18703. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18704. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  18705. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  18706. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  18707. /* Test bad args. */
  18708. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18709. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18710. wc_FreeDsaKey(&key);
  18711. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18712. return EXPECT_RESULT();
  18713. } /* END test_wc_DsaKeyToDer */
  18714. /*
  18715. * Testing wc_DsaKeyToPublicDer()
  18716. * (indirectly testing setDsaPublicKey())
  18717. */
  18718. static int test_wc_DsaKeyToPublicDer(void)
  18719. {
  18720. EXPECT_DECLS;
  18721. #ifndef HAVE_SELFTEST
  18722. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18723. DsaKey key;
  18724. WC_RNG rng;
  18725. byte* der = NULL;
  18726. word32 sz = 0;
  18727. word32 idx = 0;
  18728. XMEMSET(&key, 0, sizeof(DsaKey));
  18729. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18730. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  18731. DYNAMIC_TYPE_TMP_BUFFER));
  18732. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18733. ExpectIntEQ(wc_InitRng(&rng), 0);
  18734. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  18735. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18736. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  18737. wc_FreeDsaKey(&key);
  18738. idx = 0;
  18739. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18740. /* Test without the SubjectPublicKeyInfo header */
  18741. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  18742. wc_FreeDsaKey(&key);
  18743. idx = 0;
  18744. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  18745. /* Test bad args. */
  18746. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  18747. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  18748. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18749. wc_FreeDsaKey(&key);
  18750. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18751. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18752. #endif /* !HAVE_SELFTEST */
  18753. return EXPECT_RESULT();
  18754. } /* END test_wc_DsaKeyToPublicDer */
  18755. /*
  18756. * Testing wc_DsaImportParamsRaw()
  18757. */
  18758. static int test_wc_DsaImportParamsRaw(void)
  18759. {
  18760. EXPECT_DECLS;
  18761. #if !defined(NO_DSA)
  18762. DsaKey key;
  18763. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18764. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18765. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18766. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18767. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18768. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18769. "80a0093113a8bd73";
  18770. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18771. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18772. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18773. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18774. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18775. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18776. "76341a7e7d9";
  18777. /* invalid p and q parameters */
  18778. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18779. const char* invalidQ = "96c5390a";
  18780. XMEMSET(&key, 0, sizeof(DsaKey));
  18781. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18782. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  18783. /* test bad args */
  18784. /* null key struct */
  18785. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  18786. /* null param pointers */
  18787. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  18788. /* illegal p length */
  18789. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  18790. /* illegal q length */
  18791. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  18792. wc_FreeDsaKey(&key);
  18793. #endif
  18794. return EXPECT_RESULT();
  18795. } /* END test_wc_DsaImportParamsRaw */
  18796. /*
  18797. * Testing wc_DsaImportParamsRawCheck()
  18798. */
  18799. static int test_wc_DsaImportParamsRawCheck(void)
  18800. {
  18801. EXPECT_DECLS;
  18802. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18803. DsaKey key;
  18804. int trusted = 0;
  18805. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18806. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18807. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18808. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18809. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18810. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18811. "80a0093113a8bd73";
  18812. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18813. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18814. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18815. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18816. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18817. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18818. "76341a7e7d9";
  18819. /* invalid p and q parameters */
  18820. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18821. const char* invalidQ = "96c5390a";
  18822. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18823. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  18824. /* test bad args */
  18825. /* null key struct */
  18826. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  18827. BAD_FUNC_ARG);
  18828. /* null param pointers */
  18829. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  18830. NULL), BAD_FUNC_ARG);
  18831. /* illegal p length */
  18832. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  18833. BAD_FUNC_ARG);
  18834. /* illegal q length */
  18835. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  18836. BAD_FUNC_ARG);
  18837. wc_FreeDsaKey(&key);
  18838. #endif
  18839. return EXPECT_RESULT();
  18840. } /* END test_wc_DsaImportParamsRawCheck */
  18841. /*
  18842. * Testing wc_DsaExportParamsRaw()
  18843. */
  18844. static int test_wc_DsaExportParamsRaw(void)
  18845. {
  18846. EXPECT_DECLS;
  18847. #if !defined(NO_DSA)
  18848. DsaKey key;
  18849. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18850. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18851. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18852. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18853. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18854. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18855. "80a0093113a8bd73";
  18856. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18857. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18858. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18859. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18860. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18861. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18862. "76341a7e7d9";
  18863. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18864. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18865. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18866. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18867. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18868. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18869. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18870. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18871. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18872. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18873. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18874. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18875. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18876. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18877. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18878. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18879. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18880. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18881. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18882. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18883. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18884. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18885. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18886. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18887. byte pOut[MAX_DSA_PARAM_SIZE];
  18888. byte qOut[MAX_DSA_PARAM_SIZE];
  18889. byte gOut[MAX_DSA_PARAM_SIZE];
  18890. word32 pOutSz;
  18891. word32 qOutSz;
  18892. word32 gOutSz;
  18893. XMEMSET(&key, 0, sizeof(DsaKey));
  18894. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18895. /* first test using imported raw parameters, for expected */
  18896. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  18897. pOutSz = sizeof(pOut);
  18898. qOutSz = sizeof(qOut);
  18899. gOutSz = sizeof(gOut);
  18900. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18901. &gOutSz), 0);
  18902. /* validate exported parameters are correct */
  18903. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  18904. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  18905. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  18906. /* test bad args */
  18907. /* null key struct */
  18908. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18909. &gOutSz), BAD_FUNC_ARG);
  18910. /* null output pointers */
  18911. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  18912. &gOutSz), LENGTH_ONLY_E);
  18913. /* null output size pointers */
  18914. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  18915. NULL), BAD_FUNC_ARG);
  18916. /* p output buffer size too small */
  18917. pOutSz = 1;
  18918. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18919. &gOutSz), BUFFER_E);
  18920. pOutSz = sizeof(pOut);
  18921. /* q output buffer size too small */
  18922. qOutSz = 1;
  18923. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18924. &gOutSz), BUFFER_E);
  18925. qOutSz = sizeof(qOut);
  18926. /* g output buffer size too small */
  18927. gOutSz = 1;
  18928. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  18929. &gOutSz), BUFFER_E);
  18930. wc_FreeDsaKey(&key);
  18931. #endif
  18932. return EXPECT_RESULT();
  18933. } /* END test_wc_DsaExportParamsRaw */
  18934. /*
  18935. * Testing wc_DsaExportKeyRaw()
  18936. */
  18937. static int test_wc_DsaExportKeyRaw(void)
  18938. {
  18939. EXPECT_DECLS;
  18940. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18941. DsaKey key;
  18942. WC_RNG rng;
  18943. byte xOut[MAX_DSA_PARAM_SIZE];
  18944. byte yOut[MAX_DSA_PARAM_SIZE];
  18945. word32 xOutSz, yOutSz;
  18946. XMEMSET(&key, 0, sizeof(key));
  18947. XMEMSET(&rng, 0, sizeof(rng));
  18948. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  18949. ExpectIntEQ(wc_InitRng(&rng), 0);
  18950. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  18951. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  18952. /* try successful export */
  18953. xOutSz = sizeof(xOut);
  18954. yOutSz = sizeof(yOut);
  18955. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  18956. /* test bad args */
  18957. /* null key struct */
  18958. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  18959. BAD_FUNC_ARG);
  18960. /* null output pointers */
  18961. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  18962. LENGTH_ONLY_E);
  18963. /* null output size pointers */
  18964. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  18965. BAD_FUNC_ARG);
  18966. /* x output buffer size too small */
  18967. xOutSz = 1;
  18968. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  18969. BUFFER_E);
  18970. xOutSz = sizeof(xOut);
  18971. /* y output buffer size too small */
  18972. yOutSz = 1;
  18973. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  18974. BUFFER_E);
  18975. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18976. wc_FreeDsaKey(&key);
  18977. #endif
  18978. return EXPECT_RESULT();
  18979. } /* END test_wc_DsaExportParamsRaw */
  18980. /*
  18981. * Testing wc_ed25519_make_key().
  18982. */
  18983. static int test_wc_ed25519_make_key(void)
  18984. {
  18985. EXPECT_DECLS;
  18986. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  18987. ed25519_key key;
  18988. WC_RNG rng;
  18989. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  18990. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  18991. XMEMSET(&key, 0, sizeof(ed25519_key));
  18992. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18993. ExpectIntEQ(wc_ed25519_init(&key), 0);
  18994. ExpectIntEQ(wc_InitRng(&rng), 0);
  18995. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  18996. ECC_PRIV_KEY_E);
  18997. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  18998. ECC_PRIV_KEY_E);
  18999. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19000. /* Test bad args. */
  19001. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19002. BAD_FUNC_ARG);
  19003. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19004. BAD_FUNC_ARG);
  19005. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19006. BAD_FUNC_ARG);
  19007. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19008. BAD_FUNC_ARG);
  19009. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19010. wc_ed25519_free(&key);
  19011. #endif
  19012. return EXPECT_RESULT();
  19013. } /* END test_wc_ed25519_make_key */
  19014. /*
  19015. * Testing wc_ed25519_init()
  19016. */
  19017. static int test_wc_ed25519_init(void)
  19018. {
  19019. EXPECT_DECLS;
  19020. #if defined(HAVE_ED25519)
  19021. ed25519_key key;
  19022. XMEMSET(&key, 0, sizeof(ed25519_key));
  19023. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19024. /* Test bad args. */
  19025. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19026. wc_ed25519_free(&key);
  19027. #endif
  19028. return EXPECT_RESULT();
  19029. } /* END test_wc_ed25519_init */
  19030. /*
  19031. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19032. */
  19033. static int test_wc_ed25519_sign_msg(void)
  19034. {
  19035. EXPECT_DECLS;
  19036. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19037. WC_RNG rng;
  19038. ed25519_key key;
  19039. byte msg[] = "Everybody gets Friday off.\n";
  19040. byte sig[ED25519_SIG_SIZE+1];
  19041. word32 msglen = sizeof(msg);
  19042. word32 siglen = ED25519_SIG_SIZE;
  19043. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19044. #ifdef HAVE_ED25519_VERIFY
  19045. int verify_ok = 0; /*1 = Verify success.*/
  19046. #endif
  19047. /* Initialize stack variables. */
  19048. XMEMSET(&key, 0, sizeof(ed25519_key));
  19049. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19050. XMEMSET(sig, 0, sizeof(sig));
  19051. /* Initialize key. */
  19052. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19053. ExpectIntEQ(wc_InitRng(&rng), 0);
  19054. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19055. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19056. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19057. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19058. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19059. /* Test bad args. */
  19060. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19061. BAD_FUNC_ARG);
  19062. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19063. BAD_FUNC_ARG);
  19064. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19065. BAD_FUNC_ARG);
  19066. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19067. BAD_FUNC_ARG);
  19068. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19069. BUFFER_E);
  19070. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19071. badSigLen -= 1;
  19072. #ifdef HAVE_ED25519_VERIFY
  19073. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19074. &key), 0);
  19075. ExpectIntEQ(verify_ok, 1);
  19076. /* Test bad args. */
  19077. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19078. &verify_ok, &key), BAD_FUNC_ARG);
  19079. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19080. &verify_ok, &key), BAD_FUNC_ARG);
  19081. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19082. &key), BAD_FUNC_ARG);
  19083. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19084. &key), BAD_FUNC_ARG);
  19085. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19086. BAD_FUNC_ARG);
  19087. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19088. NULL), BAD_FUNC_ARG);
  19089. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19090. &key), BAD_FUNC_ARG);
  19091. #endif /* Verify. */
  19092. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19093. wc_ed25519_free(&key);
  19094. #endif
  19095. return EXPECT_RESULT();
  19096. } /* END test_wc_ed25519_sign_msg */
  19097. /*
  19098. * Testing wc_ed25519_import_public()
  19099. */
  19100. static int test_wc_ed25519_import_public(void)
  19101. {
  19102. EXPECT_DECLS;
  19103. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19104. ed25519_key pubKey;
  19105. WC_RNG rng;
  19106. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19107. word32 inlen = sizeof(in);
  19108. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19109. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19110. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19111. ExpectIntEQ(wc_InitRng(&rng), 0);
  19112. #ifdef HAVE_ED25519_MAKE_KEY
  19113. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19114. #endif
  19115. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19116. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19117. /* Test bad args. */
  19118. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19119. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19120. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19121. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19122. wc_ed25519_free(&pubKey);
  19123. #endif
  19124. return EXPECT_RESULT();
  19125. } /* END wc_ed25519_import_public */
  19126. /*
  19127. * Testing wc_ed25519_import_private_key()
  19128. */
  19129. static int test_wc_ed25519_import_private_key(void)
  19130. {
  19131. EXPECT_DECLS;
  19132. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19133. ed25519_key key;
  19134. WC_RNG rng;
  19135. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19136. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19137. word32 privKeySz = sizeof(privKey);
  19138. word32 pubKeySz = sizeof(pubKey);
  19139. #ifdef HAVE_ED25519_KEY_EXPORT
  19140. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19141. word32 bothKeysSz = sizeof(bothKeys);
  19142. #endif
  19143. XMEMSET(&key, 0, sizeof(ed25519_key));
  19144. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19145. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19146. ExpectIntEQ(wc_InitRng(&rng), 0);
  19147. #ifdef HAVE_ED25519_MAKE_KEY
  19148. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19149. #endif
  19150. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19151. pubKeySz, &key, 1), 0);
  19152. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19153. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19154. #ifdef HAVE_ED25519_KEY_EXPORT
  19155. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19156. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19157. &key, 1), 0);
  19158. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19159. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19160. #endif
  19161. /* Test bad args. */
  19162. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19163. &key), BAD_FUNC_ARG);
  19164. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19165. pubKeySz, &key), BAD_FUNC_ARG);
  19166. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19167. pubKeySz, NULL), BAD_FUNC_ARG);
  19168. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19169. pubKeySz, &key), BAD_FUNC_ARG);
  19170. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19171. pubKeySz - 1, &key), BAD_FUNC_ARG);
  19172. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  19173. &key), BAD_FUNC_ARG);
  19174. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19175. wc_ed25519_free(&key);
  19176. #endif
  19177. return EXPECT_RESULT();
  19178. } /* END test_wc_ed25519_import_private_key */
  19179. /*
  19180. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  19181. */
  19182. static int test_wc_ed25519_export(void)
  19183. {
  19184. EXPECT_DECLS;
  19185. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19186. ed25519_key key;
  19187. WC_RNG rng;
  19188. byte priv[ED25519_PRV_KEY_SIZE];
  19189. byte pub[ED25519_PUB_KEY_SIZE];
  19190. word32 privSz = sizeof(priv);
  19191. word32 pubSz = sizeof(pub);
  19192. #ifndef HAVE_ED25519_MAKE_KEY
  19193. const byte privKey[] = {
  19194. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19195. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19196. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19197. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19198. };
  19199. const byte pubKey[] = {
  19200. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19201. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19202. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19203. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19204. };
  19205. #endif
  19206. XMEMSET(&key, 0, sizeof(ed25519_key));
  19207. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19208. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19209. ExpectIntEQ(wc_InitRng(&rng), 0);
  19210. #ifdef HAVE_ED25519_MAKE_KEY
  19211. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19212. #else
  19213. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19214. pubKey, sizeof(pubKey), &key, 1), 0);
  19215. #endif
  19216. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  19217. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  19218. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  19219. /* Test bad args. */
  19220. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  19221. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  19222. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  19223. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  19224. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  19225. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  19226. /* Test bad args. */
  19227. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  19228. BAD_FUNC_ARG);
  19229. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  19230. BAD_FUNC_ARG);
  19231. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  19232. BAD_FUNC_ARG);
  19233. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19234. wc_ed25519_free(&key);
  19235. #endif
  19236. return EXPECT_RESULT();
  19237. } /* END test_wc_ed25519_export */
  19238. /*
  19239. * Testing wc_ed25519_size()
  19240. */
  19241. static int test_wc_ed25519_size(void)
  19242. {
  19243. EXPECT_DECLS;
  19244. #if defined(HAVE_ED25519)
  19245. ed25519_key key;
  19246. WC_RNG rng;
  19247. #ifndef HAVE_ED25519_MAKE_KEY
  19248. const byte privKey[] = {
  19249. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19250. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19251. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19252. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19253. };
  19254. const byte pubKey[] = {
  19255. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19256. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19257. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19258. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19259. };
  19260. #endif
  19261. XMEMSET(&key, 0, sizeof(ed25519_key));
  19262. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19263. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19264. ExpectIntEQ(wc_InitRng(&rng), 0);
  19265. #ifdef HAVE_ED25519_MAKE_KEY
  19266. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19267. #else
  19268. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19269. pubKey, sizeof(pubKey), &key, 1), 0);
  19270. #endif
  19271. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  19272. /* Test bad args. */
  19273. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  19274. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  19275. /* Test bad args. */
  19276. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  19277. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  19278. /* Test bad args. */
  19279. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  19280. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  19281. /* Test bad args. */
  19282. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  19283. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19284. wc_ed25519_free(&key);
  19285. #endif
  19286. return EXPECT_RESULT();
  19287. } /* END test_wc_ed25519_size */
  19288. /*
  19289. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19290. */
  19291. static int test_wc_ed25519_exportKey(void)
  19292. {
  19293. EXPECT_DECLS;
  19294. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19295. WC_RNG rng;
  19296. ed25519_key key;
  19297. byte priv[ED25519_PRV_KEY_SIZE];
  19298. byte pub[ED25519_PUB_KEY_SIZE];
  19299. byte privOnly[ED25519_PRV_KEY_SIZE];
  19300. word32 privSz = sizeof(priv);
  19301. word32 pubSz = sizeof(pub);
  19302. word32 privOnlySz = sizeof(privOnly);
  19303. #ifndef HAVE_ED25519_MAKE_KEY
  19304. const byte privKey[] = {
  19305. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  19306. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  19307. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  19308. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  19309. };
  19310. const byte pubKey[] = {
  19311. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  19312. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  19313. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  19314. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  19315. };
  19316. #endif
  19317. XMEMSET(&key, 0, sizeof(ed25519_key));
  19318. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19319. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19320. ExpectIntEQ(wc_InitRng(&rng), 0);
  19321. #ifdef HAVE_ED25519_MAKE_KEY
  19322. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19323. #else
  19324. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  19325. pubKey, sizeof(pubKey), &key, 1), 0);
  19326. #endif
  19327. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  19328. /* Test bad args. */
  19329. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  19330. BAD_FUNC_ARG);
  19331. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  19332. BAD_FUNC_ARG);
  19333. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  19334. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  19335. /* Test bad args. */
  19336. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  19337. BAD_FUNC_ARG);
  19338. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  19339. BAD_FUNC_ARG);
  19340. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  19341. BAD_FUNC_ARG);
  19342. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  19343. BAD_FUNC_ARG);
  19344. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  19345. BAD_FUNC_ARG);
  19346. /* Cross check output. */
  19347. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  19348. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19349. wc_ed25519_free(&key);
  19350. #endif
  19351. return EXPECT_RESULT();
  19352. } /* END test_wc_ed25519_exportKey */
  19353. /*
  19354. * Testing wc_Ed25519PublicKeyToDer
  19355. */
  19356. static int test_wc_Ed25519PublicKeyToDer(void)
  19357. {
  19358. EXPECT_DECLS;
  19359. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19360. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19361. ed25519_key key;
  19362. byte derBuf[1024];
  19363. XMEMSET(&key, 0, sizeof(ed25519_key));
  19364. /* Test bad args */
  19365. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  19366. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19367. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  19368. wc_ed25519_free(&key);
  19369. /* Test good args */
  19370. if (EXPECT_SUCCESS()) {
  19371. WC_RNG rng;
  19372. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19373. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19374. ExpectIntEQ(wc_InitRng(&rng), 0);
  19375. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19376. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  19377. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19378. wc_ed25519_free(&key);
  19379. }
  19380. #endif
  19381. return EXPECT_RESULT();
  19382. } /* END testing wc_Ed25519PublicKeyToDer */
  19383. /*
  19384. * Testing wc_curve25519_init and wc_curve25519_free.
  19385. */
  19386. static int test_wc_curve25519_init(void)
  19387. {
  19388. EXPECT_DECLS;
  19389. #if defined(HAVE_CURVE25519)
  19390. curve25519_key key;
  19391. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19392. /* Test bad args for wc_curve25519_init */
  19393. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  19394. /* Test good args for wc_curve_25519_free */
  19395. wc_curve25519_free(&key);
  19396. /* Test bad args for wc_curve25519 free. */
  19397. wc_curve25519_free(NULL);
  19398. #endif
  19399. return EXPECT_RESULT();
  19400. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19401. /*
  19402. * Testing test_wc_curve25519_size.
  19403. */
  19404. static int test_wc_curve25519_size(void)
  19405. {
  19406. EXPECT_DECLS;
  19407. #if defined(HAVE_CURVE25519)
  19408. curve25519_key key;
  19409. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19410. /* Test good args for wc_curve25519_size */
  19411. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19412. /* Test bad args for wc_curve25519_size */
  19413. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  19414. wc_curve25519_free(&key);
  19415. #endif
  19416. return EXPECT_RESULT();
  19417. } /* END test_wc_curve25519_size*/
  19418. /*
  19419. * Testing test_wc_curve25519_export_key_raw().
  19420. */
  19421. static int test_wc_curve25519_export_key_raw(void)
  19422. {
  19423. EXPECT_DECLS;
  19424. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19425. curve25519_key key;
  19426. WC_RNG rng;
  19427. byte privateKey[CURVE25519_KEYSIZE];
  19428. byte publicKey[CURVE25519_KEYSIZE];
  19429. word32 prvkSz;
  19430. word32 pubkSz;
  19431. byte prik[CURVE25519_KEYSIZE];
  19432. byte pubk[CURVE25519_KEYSIZE];
  19433. word32 prksz;
  19434. word32 pbksz;
  19435. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19436. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19437. ExpectIntEQ(wc_InitRng(&rng), 0);
  19438. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19439. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19440. prvkSz = CURVE25519_KEYSIZE;
  19441. pubkSz = CURVE25519_KEYSIZE;
  19442. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  19443. publicKey, &pubkSz), BAD_FUNC_ARG);
  19444. prvkSz = CURVE25519_KEYSIZE;
  19445. pubkSz = CURVE25519_KEYSIZE;
  19446. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  19447. &pubkSz), BAD_FUNC_ARG);
  19448. prvkSz = CURVE25519_KEYSIZE;
  19449. pubkSz = CURVE25519_KEYSIZE;
  19450. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  19451. publicKey, &pubkSz), BAD_FUNC_ARG);
  19452. /* prvkSz = CURVE25519_KEYSIZE; */
  19453. pubkSz = CURVE25519_KEYSIZE;
  19454. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19455. NULL, &pubkSz), BAD_FUNC_ARG);
  19456. prvkSz = CURVE25519_KEYSIZE;
  19457. pubkSz = CURVE25519_KEYSIZE;
  19458. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19459. publicKey, NULL), BAD_FUNC_ARG);
  19460. /* cross-testing */
  19461. prksz = CURVE25519_KEYSIZE;
  19462. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  19463. pbksz = CURVE25519_KEYSIZE;
  19464. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  19465. prvkSz = CURVE25519_KEYSIZE;
  19466. /* pubkSz = CURVE25519_KEYSIZE; */
  19467. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19468. publicKey, &pubkSz), 0);
  19469. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19470. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19471. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19472. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19473. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19474. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19475. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19476. wc_curve25519_free(&key);
  19477. #endif
  19478. return EXPECT_RESULT();
  19479. } /* end of test_wc_curve25519_export_key_raw */
  19480. /*
  19481. * Testing test_wc_curve25519_export_key_raw_ex().
  19482. */
  19483. static int test_wc_curve25519_export_key_raw_ex(void)
  19484. {
  19485. EXPECT_DECLS;
  19486. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19487. curve25519_key key;
  19488. WC_RNG rng;
  19489. byte privateKey[CURVE25519_KEYSIZE];
  19490. byte publicKey[CURVE25519_KEYSIZE];
  19491. word32 prvkSz;
  19492. word32 pubkSz;
  19493. byte prik[CURVE25519_KEYSIZE];
  19494. byte pubk[CURVE25519_KEYSIZE];
  19495. word32 prksz;
  19496. word32 pbksz;
  19497. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19498. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19499. ExpectIntEQ(wc_InitRng(&rng), 0);
  19500. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19501. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  19502. prvkSz = CURVE25519_KEYSIZE;
  19503. pubkSz = CURVE25519_KEYSIZE;
  19504. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19505. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19506. prvkSz = CURVE25519_KEYSIZE;
  19507. pubkSz = CURVE25519_KEYSIZE;
  19508. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19509. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19510. prvkSz = CURVE25519_KEYSIZE;
  19511. pubkSz = CURVE25519_KEYSIZE;
  19512. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19513. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19514. /* prvkSz = CURVE25519_KEYSIZE; */
  19515. pubkSz = CURVE25519_KEYSIZE;
  19516. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19517. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19518. prvkSz = CURVE25519_KEYSIZE;
  19519. pubkSz = CURVE25519_KEYSIZE;
  19520. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19521. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  19522. prvkSz = CURVE25519_KEYSIZE;
  19523. /* pubkSz = CURVE25519_KEYSIZE; */
  19524. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  19525. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19526. prvkSz = CURVE25519_KEYSIZE;
  19527. pubkSz = CURVE25519_KEYSIZE;
  19528. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  19529. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19530. prvkSz = CURVE25519_KEYSIZE;
  19531. pubkSz = CURVE25519_KEYSIZE;
  19532. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19533. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19534. /* prvkSz = CURVE25519_KEYSIZE; */
  19535. pubkSz = CURVE25519_KEYSIZE;
  19536. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19537. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19538. prvkSz = CURVE25519_KEYSIZE;
  19539. pubkSz = CURVE25519_KEYSIZE;
  19540. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  19541. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  19542. /* illegal value for endian */
  19543. prvkSz = CURVE25519_KEYSIZE;
  19544. /* pubkSz = CURVE25519_KEYSIZE; */
  19545. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19546. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  19547. /* cross-testing */
  19548. prksz = CURVE25519_KEYSIZE;
  19549. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  19550. pbksz = CURVE25519_KEYSIZE;
  19551. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  19552. prvkSz = CURVE25519_KEYSIZE;
  19553. /* pubkSz = CURVE25519_KEYSIZE; */
  19554. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19555. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19556. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  19557. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  19558. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19559. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19560. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  19561. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  19562. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  19563. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  19564. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19565. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19566. /* try once with another endian */
  19567. prvkSz = CURVE25519_KEYSIZE;
  19568. pubkSz = CURVE25519_KEYSIZE;
  19569. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19570. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  19571. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  19572. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  19573. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19574. wc_curve25519_free(&key);
  19575. #endif
  19576. return EXPECT_RESULT();
  19577. } /* end of test_wc_curve25519_export_key_raw_ex */
  19578. /*
  19579. * Testing wc_curve25519_make_key
  19580. */
  19581. static int test_wc_curve25519_make_key(void)
  19582. {
  19583. EXPECT_DECLS;
  19584. #if defined(HAVE_CURVE25519)
  19585. curve25519_key key;
  19586. WC_RNG rng;
  19587. int keysize;
  19588. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19589. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19590. ExpectIntEQ(wc_InitRng(&rng), 0);
  19591. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19592. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  19593. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  19594. /* test bad cases*/
  19595. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  19596. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  19597. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  19598. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  19599. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19600. wc_curve25519_free(&key);
  19601. #endif
  19602. return EXPECT_RESULT();
  19603. } /* END test_wc_curve25519_make_key*/
  19604. /*
  19605. * Testing wc_curve25519_shared_secret_ex
  19606. */
  19607. static int test_wc_curve25519_shared_secret_ex(void)
  19608. {
  19609. EXPECT_DECLS;
  19610. #if defined(HAVE_CURVE25519)
  19611. curve25519_key private_key;
  19612. curve25519_key public_key;
  19613. WC_RNG rng;
  19614. byte out[CURVE25519_KEYSIZE];
  19615. word32 outLen = sizeof(out);
  19616. int endian = EC25519_BIG_ENDIAN;
  19617. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  19618. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  19619. ExpectIntEQ(wc_InitRng(&rng), 0);
  19620. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  19621. 0);
  19622. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  19623. 0);
  19624. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19625. &outLen, endian), 0);
  19626. /* test bad cases*/
  19627. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  19628. BAD_FUNC_ARG);
  19629. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  19630. endian), BAD_FUNC_ARG);
  19631. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  19632. endian), BAD_FUNC_ARG);
  19633. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19634. &outLen, endian), BAD_FUNC_ARG);
  19635. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19636. NULL, endian), BAD_FUNC_ARG);
  19637. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  19638. * increasing to 0x8f checks for error being returned*/
  19639. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19640. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19641. &outLen, endian), ECC_BAD_ARG_E);
  19642. outLen = outLen - 2;
  19643. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19644. &outLen, endian), BAD_FUNC_ARG);
  19645. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19646. wc_curve25519_free(&private_key);
  19647. wc_curve25519_free(&public_key);
  19648. #endif
  19649. return EXPECT_RESULT();
  19650. } /* END test_wc_curve25519_shared_secret_ex*/
  19651. /*
  19652. * Testing wc_curve25519_make_pub
  19653. */
  19654. static int test_wc_curve25519_make_pub(void)
  19655. {
  19656. EXPECT_DECLS;
  19657. #ifdef HAVE_CURVE25519
  19658. curve25519_key key;
  19659. WC_RNG rng;
  19660. byte out[CURVE25519_KEYSIZE];
  19661. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19662. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19663. ExpectIntEQ(wc_InitRng(&rng), 0);
  19664. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19665. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  19666. (int)sizeof(key.k), key.k), 0);
  19667. /* test bad cases*/
  19668. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  19669. (int)sizeof out, out), ECC_BAD_ARG_E);
  19670. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19671. NULL), ECC_BAD_ARG_E);
  19672. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  19673. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19674. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  19675. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  19676. /* verify clamping test */
  19677. key.k[0] |= ~248;
  19678. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19679. key.k), ECC_BAD_ARG_E);
  19680. key.k[0] &= 248;
  19681. /* repeat the expected-to-succeed test. */
  19682. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  19683. key.k), 0);
  19684. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19685. wc_curve25519_free(&key);
  19686. #endif
  19687. return EXPECT_RESULT();
  19688. } /* END test_wc_curve25519_make_pub */
  19689. /*
  19690. * Testing test_wc_curve25519_export_public_ex
  19691. */
  19692. static int test_wc_curve25519_export_public_ex(void)
  19693. {
  19694. EXPECT_DECLS;
  19695. #if defined(HAVE_CURVE25519)
  19696. curve25519_key key;
  19697. WC_RNG rng;
  19698. byte out[CURVE25519_KEYSIZE];
  19699. word32 outLen = sizeof(out);
  19700. int endian = EC25519_BIG_ENDIAN;
  19701. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19702. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19703. ExpectIntEQ(wc_InitRng(&rng), 0);
  19704. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19705. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  19706. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  19707. /* test bad cases*/
  19708. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  19709. BAD_FUNC_ARG);
  19710. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  19711. BAD_FUNC_ARG);
  19712. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  19713. BAD_FUNC_ARG);
  19714. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  19715. BAD_FUNC_ARG);
  19716. outLen = outLen - 2;
  19717. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  19718. ECC_BAD_ARG_E);
  19719. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19720. wc_curve25519_free(&key);
  19721. #endif
  19722. return EXPECT_RESULT();
  19723. } /* END test_wc_curve25519_export_public_ex*/
  19724. /*
  19725. * Testing test_wc_curve25519_import_private_raw_ex
  19726. */
  19727. static int test_wc_curve25519_import_private_raw_ex(void)
  19728. {
  19729. EXPECT_DECLS;
  19730. #if defined(HAVE_CURVE25519)
  19731. curve25519_key key;
  19732. WC_RNG rng;
  19733. byte priv[CURVE25519_KEYSIZE];
  19734. byte pub[CURVE25519_KEYSIZE];
  19735. word32 privSz = sizeof(priv);
  19736. word32 pubSz = sizeof(pub);
  19737. int endian = EC25519_BIG_ENDIAN;
  19738. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19739. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19740. ExpectIntEQ(wc_InitRng(&rng), 0);
  19741. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19742. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  19743. endian), 0);
  19744. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  19745. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19746. &key, endian), 0);
  19747. /* test bad cases*/
  19748. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19749. endian), BAD_FUNC_ARG);
  19750. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19751. &key, endian), BAD_FUNC_ARG);
  19752. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19753. &key, endian), BAD_FUNC_ARG);
  19754. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19755. NULL, endian), BAD_FUNC_ARG);
  19756. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19757. &key, endian), ECC_BAD_ARG_E);
  19758. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19759. &key, endian), ECC_BAD_ARG_E);
  19760. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19761. &key, EC25519_LITTLE_ENDIAN), 0);
  19762. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19763. wc_curve25519_free(&key);
  19764. #endif
  19765. return EXPECT_RESULT();
  19766. } /* END test_wc_curve25519_import_private_raw_ex*/
  19767. /*
  19768. * Testing test_wc_curve25519_import_private
  19769. */
  19770. static int test_wc_curve25519_import_private(void)
  19771. {
  19772. EXPECT_DECLS;
  19773. #if defined(HAVE_CURVE25519)
  19774. curve25519_key key;
  19775. WC_RNG rng;
  19776. byte priv[CURVE25519_KEYSIZE];
  19777. word32 privSz = sizeof(priv);
  19778. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19779. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19780. ExpectIntEQ(wc_InitRng(&rng), 0);
  19781. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  19782. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  19783. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  19784. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19785. wc_curve25519_free(&key);
  19786. #endif
  19787. return EXPECT_RESULT();
  19788. } /* END test_wc_curve25519_import*/
  19789. /*
  19790. * Testing test_wc_curve25519_export_private_raw_ex
  19791. */
  19792. static int test_wc_curve25519_export_private_raw_ex(void)
  19793. {
  19794. EXPECT_DECLS;
  19795. #if defined(HAVE_CURVE25519)
  19796. curve25519_key key;
  19797. byte out[CURVE25519_KEYSIZE];
  19798. word32 outLen = sizeof(out);
  19799. int endian = EC25519_BIG_ENDIAN;
  19800. ExpectIntEQ(wc_curve25519_init(&key), 0);
  19801. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  19802. 0);
  19803. /* test bad cases*/
  19804. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  19805. BAD_FUNC_ARG);
  19806. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  19807. BAD_FUNC_ARG);
  19808. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  19809. endian), BAD_FUNC_ARG);
  19810. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  19811. BAD_FUNC_ARG);
  19812. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19813. EC25519_LITTLE_ENDIAN), 0);
  19814. outLen = outLen - 2;
  19815. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  19816. ECC_BAD_ARG_E);
  19817. wc_curve25519_free(&key);
  19818. #endif
  19819. return EXPECT_RESULT();
  19820. } /* END test_wc_curve25519_export_private_raw_ex*/
  19821. /*
  19822. * Testing wc_ed448_make_key().
  19823. */
  19824. static int test_wc_ed448_make_key(void)
  19825. {
  19826. EXPECT_DECLS;
  19827. #if defined(HAVE_ED448)
  19828. ed448_key key;
  19829. WC_RNG rng;
  19830. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  19831. XMEMSET(&key, 0, sizeof(ed448_key));
  19832. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19833. ExpectIntEQ(wc_ed448_init(&key), 0);
  19834. ExpectIntEQ(wc_InitRng(&rng), 0);
  19835. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  19836. ECC_PRIV_KEY_E);
  19837. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19838. /* Test bad args. */
  19839. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  19840. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  19841. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  19842. BAD_FUNC_ARG);
  19843. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  19844. BAD_FUNC_ARG);
  19845. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19846. wc_ed448_free(&key);
  19847. #endif
  19848. return EXPECT_RESULT();
  19849. } /* END test_wc_ed448_make_key */
  19850. /*
  19851. * Testing wc_ed448_init()
  19852. */
  19853. static int test_wc_ed448_init(void)
  19854. {
  19855. EXPECT_DECLS;
  19856. #if defined(HAVE_ED448)
  19857. ed448_key key;
  19858. XMEMSET(&key, 0, sizeof(ed448_key));
  19859. ExpectIntEQ(wc_ed448_init(&key), 0);
  19860. /* Test bad args. */
  19861. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  19862. wc_ed448_free(&key);
  19863. #endif
  19864. return EXPECT_RESULT();
  19865. } /* END test_wc_ed448_init */
  19866. /*
  19867. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19868. */
  19869. static int test_wc_ed448_sign_msg(void)
  19870. {
  19871. EXPECT_DECLS;
  19872. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19873. ed448_key key;
  19874. WC_RNG rng;
  19875. byte msg[] = "Everybody gets Friday off.\n";
  19876. byte sig[ED448_SIG_SIZE];
  19877. word32 msglen = sizeof(msg);
  19878. word32 siglen = sizeof(sig);
  19879. word32 badSigLen = sizeof(sig) - 1;
  19880. #ifdef HAVE_ED448_VERIFY
  19881. int verify_ok = 0; /*1 = Verify success.*/
  19882. #endif
  19883. /* Initialize stack variables. */
  19884. XMEMSET(&key, 0, sizeof(ed448_key));
  19885. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19886. XMEMSET(sig, 0, siglen);
  19887. /* Initialize key. */
  19888. ExpectIntEQ(wc_ed448_init(&key), 0);
  19889. ExpectIntEQ(wc_InitRng(&rng), 0);
  19890. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19891. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  19892. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  19893. /* Test bad args. */
  19894. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  19895. BAD_FUNC_ARG);
  19896. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  19897. BAD_FUNC_ARG);
  19898. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  19899. BAD_FUNC_ARG);
  19900. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  19901. BAD_FUNC_ARG);
  19902. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  19903. BUFFER_E);
  19904. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  19905. badSigLen -= 1;
  19906. #ifdef HAVE_ED448_VERIFY
  19907. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  19908. NULL, 0), 0);
  19909. ExpectIntEQ(verify_ok, 1);
  19910. /* Test bad args. */
  19911. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  19912. &key, NULL, 0), BAD_FUNC_ARG);
  19913. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  19914. &key, NULL, 0), BAD_FUNC_ARG);
  19915. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19916. &key, NULL, 0), BAD_FUNC_ARG);
  19917. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  19918. &key, NULL, 0), BAD_FUNC_ARG);
  19919. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  19920. &key, NULL, 0), BAD_FUNC_ARG);
  19921. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19922. NULL, NULL, 0), BAD_FUNC_ARG);
  19923. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  19924. &key, NULL, 0), BAD_FUNC_ARG);
  19925. #endif /* Verify. */
  19926. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19927. wc_ed448_free(&key);
  19928. #endif
  19929. return EXPECT_RESULT();
  19930. } /* END test_wc_ed448_sign_msg */
  19931. /*
  19932. * Testing wc_ed448_import_public()
  19933. */
  19934. static int test_wc_ed448_import_public(void)
  19935. {
  19936. EXPECT_DECLS;
  19937. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19938. ed448_key pubKey;
  19939. WC_RNG rng;
  19940. const byte in[] =
  19941. "Ed448PublicKeyUnitTest.................................\n";
  19942. word32 inlen = sizeof(in);
  19943. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  19944. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19945. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  19946. ExpectIntEQ(wc_InitRng(&rng), 0);
  19947. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  19948. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  19949. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19950. /* Test bad args. */
  19951. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19952. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19953. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19954. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19955. wc_ed448_free(&pubKey);
  19956. #endif
  19957. return EXPECT_RESULT();
  19958. } /* END wc_ed448_import_public */
  19959. /*
  19960. * Testing wc_ed448_import_private_key()
  19961. */
  19962. static int test_wc_ed448_import_private_key(void)
  19963. {
  19964. EXPECT_DECLS;
  19965. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19966. ed448_key key;
  19967. WC_RNG rng;
  19968. const byte privKey[] =
  19969. "Ed448PrivateKeyUnitTest................................\n";
  19970. const byte pubKey[] =
  19971. "Ed448PublicKeyUnitTest.................................\n";
  19972. word32 privKeySz = sizeof(privKey);
  19973. word32 pubKeySz = sizeof(pubKey);
  19974. #ifdef HAVE_ED448_KEY_EXPORT
  19975. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19976. word32 bothKeysSz = sizeof(bothKeys);
  19977. #endif
  19978. XMEMSET(&key, 0, sizeof(ed448_key));
  19979. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19980. ExpectIntEQ(wc_ed448_init(&key), 0);
  19981. ExpectIntEQ(wc_InitRng(&rng), 0);
  19982. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  19983. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  19984. pubKeySz, &key, 1), 0);
  19985. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19986. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19987. #ifdef HAVE_ED448_KEY_EXPORT
  19988. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  19989. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19990. &key, 1), 0);
  19991. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19992. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19993. #endif
  19994. /* Test bad args. */
  19995. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19996. &key), BAD_FUNC_ARG);
  19997. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  19998. &key), BAD_FUNC_ARG);
  19999. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20000. pubKeySz, NULL), BAD_FUNC_ARG);
  20001. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20002. pubKeySz, &key), BAD_FUNC_ARG);
  20003. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20004. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20005. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20006. BAD_FUNC_ARG);
  20007. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20008. wc_ed448_free(&key);
  20009. #endif
  20010. return EXPECT_RESULT();
  20011. } /* END test_wc_ed448_import_private_key */
  20012. /*
  20013. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20014. */
  20015. static int test_wc_ed448_export(void)
  20016. {
  20017. EXPECT_DECLS;
  20018. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20019. ed448_key key;
  20020. WC_RNG rng;
  20021. byte priv[ED448_PRV_KEY_SIZE];
  20022. byte pub[ED448_PUB_KEY_SIZE];
  20023. word32 privSz = sizeof(priv);
  20024. word32 pubSz = sizeof(pub);
  20025. XMEMSET(&key, 0, sizeof(ed448_key));
  20026. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20027. ExpectIntEQ(wc_ed448_init(&key), 0);
  20028. ExpectIntEQ(wc_InitRng(&rng), 0);
  20029. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20030. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20031. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20032. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20033. /* Test bad args. */
  20034. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20035. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20036. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20037. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20038. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20039. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20040. /* Test bad args. */
  20041. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20042. BAD_FUNC_ARG);
  20043. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20044. BAD_FUNC_ARG);
  20045. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20046. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20047. wc_ed448_free(&key);
  20048. #endif
  20049. return EXPECT_RESULT();
  20050. } /* END test_wc_ed448_export */
  20051. /*
  20052. * Testing wc_ed448_size()
  20053. */
  20054. static int test_wc_ed448_size(void)
  20055. {
  20056. EXPECT_DECLS;
  20057. #if defined(HAVE_ED448)
  20058. ed448_key key;
  20059. WC_RNG rng;
  20060. XMEMSET(&key, 0, sizeof(ed448_key));
  20061. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20062. ExpectIntEQ(wc_ed448_init(&key), 0);
  20063. ExpectIntEQ(wc_InitRng(&rng), 0);
  20064. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20065. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20066. /* Test bad args. */
  20067. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20068. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20069. /* Test bad args. */
  20070. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20071. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20072. /* Test bad args. */
  20073. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20074. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20075. /* Test bad args. */
  20076. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20077. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20078. wc_ed448_free(&key);
  20079. #endif
  20080. return EXPECT_RESULT();
  20081. } /* END test_wc_ed448_size */
  20082. /*
  20083. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20084. */
  20085. static int test_wc_ed448_exportKey(void)
  20086. {
  20087. EXPECT_DECLS;
  20088. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20089. ed448_key key;
  20090. WC_RNG rng;
  20091. byte priv[ED448_PRV_KEY_SIZE];
  20092. byte pub[ED448_PUB_KEY_SIZE];
  20093. byte privOnly[ED448_PRV_KEY_SIZE];
  20094. word32 privSz = sizeof(priv);
  20095. word32 pubSz = sizeof(pub);
  20096. word32 privOnlySz = sizeof(privOnly);
  20097. XMEMSET(&key, 0, sizeof(ed448_key));
  20098. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20099. ExpectIntEQ(wc_ed448_init(&key), 0);
  20100. ExpectIntEQ(wc_InitRng(&rng), 0);
  20101. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20102. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20103. /* Test bad args. */
  20104. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20105. BAD_FUNC_ARG);
  20106. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20107. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20108. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20109. /* Test bad args. */
  20110. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20111. BAD_FUNC_ARG);
  20112. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20113. BAD_FUNC_ARG);
  20114. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20115. BAD_FUNC_ARG);
  20116. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20117. BAD_FUNC_ARG);
  20118. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20119. BAD_FUNC_ARG);
  20120. /* Cross check output. */
  20121. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20122. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20123. wc_ed448_free(&key);
  20124. #endif
  20125. return EXPECT_RESULT();
  20126. } /* END test_wc_ed448_exportKey */
  20127. /*
  20128. * Testing wc_Ed448PublicKeyToDer
  20129. */
  20130. static int test_wc_Ed448PublicKeyToDer(void)
  20131. {
  20132. EXPECT_DECLS;
  20133. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20134. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20135. ed448_key key;
  20136. byte derBuf[1024];
  20137. XMEMSET(&key, 0, sizeof(ed448_key));
  20138. /* Test bad args */
  20139. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20140. ExpectIntEQ(wc_ed448_init(&key), 0);
  20141. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20142. wc_ed448_free(&key);
  20143. /* Test good args */
  20144. if (EXPECT_SUCCESS()) {
  20145. WC_RNG rng;
  20146. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20147. ExpectIntEQ(wc_ed448_init(&key), 0);
  20148. ExpectIntEQ(wc_InitRng(&rng), 0);
  20149. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20150. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20151. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20152. wc_ed448_free(&key);
  20153. }
  20154. #endif
  20155. return EXPECT_RESULT();
  20156. } /* END testing wc_Ed448PublicKeyToDer */
  20157. /*
  20158. * Testing wc_curve448_init and wc_curve448_free.
  20159. */
  20160. static int test_wc_curve448_init(void)
  20161. {
  20162. EXPECT_DECLS;
  20163. #if defined(HAVE_CURVE448)
  20164. curve448_key key;
  20165. /* Test bad args for wc_curve448_init */
  20166. ExpectIntEQ(wc_curve448_init(&key), 0);
  20167. /* Test bad args for wc_curve448_init */
  20168. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  20169. /* Test good args for wc_curve_448_free */
  20170. wc_curve448_free(&key);
  20171. /* Test bad args for wc_curve448_free */
  20172. wc_curve448_free(NULL);
  20173. #endif
  20174. return EXPECT_RESULT();
  20175. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20176. /*
  20177. * Testing wc_curve448_make_key
  20178. */
  20179. static int test_wc_curve448_make_key(void)
  20180. {
  20181. EXPECT_DECLS;
  20182. #if defined(HAVE_CURVE448)
  20183. curve448_key key;
  20184. WC_RNG rng;
  20185. int keysize;
  20186. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20187. ExpectIntEQ(wc_curve448_init(&key), 0);
  20188. ExpectIntEQ(wc_InitRng(&rng), 0);
  20189. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20190. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20191. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  20192. /* test bad cases */
  20193. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20194. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20195. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20196. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20197. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20198. wc_curve448_free(&key);
  20199. #endif
  20200. return EXPECT_RESULT();
  20201. } /* END test_wc_curve448_make_key*/
  20202. /*
  20203. * Testing test_wc_curve448_shared_secret_ex
  20204. */
  20205. static int test_wc_curve448_shared_secret_ex(void)
  20206. {
  20207. EXPECT_DECLS;
  20208. #if defined(HAVE_CURVE448)
  20209. curve448_key private_key;
  20210. curve448_key public_key;
  20211. WC_RNG rng;
  20212. byte out[CURVE448_KEY_SIZE];
  20213. word32 outLen = sizeof(out);
  20214. int endian = EC448_BIG_ENDIAN;
  20215. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20216. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  20217. ExpectIntEQ(wc_InitRng(&rng), 0);
  20218. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  20219. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  20220. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  20221. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20222. &outLen, endian), 0);
  20223. /* test bad cases */
  20224. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20225. BAD_FUNC_ARG);
  20226. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  20227. endian), BAD_FUNC_ARG);
  20228. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  20229. endian), BAD_FUNC_ARG);
  20230. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20231. &outLen, endian), BAD_FUNC_ARG);
  20232. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20233. NULL, endian), BAD_FUNC_ARG);
  20234. outLen = outLen - 2;
  20235. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20236. &outLen, endian), BAD_FUNC_ARG);
  20237. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20238. wc_curve448_free(&private_key);
  20239. wc_curve448_free(&public_key);
  20240. #endif
  20241. return EXPECT_RESULT();
  20242. } /* END test_wc_curve448_shared_secret_ex*/
  20243. /*
  20244. * Testing test_wc_curve448_export_public_ex
  20245. */
  20246. static int test_wc_curve448_export_public_ex(void)
  20247. {
  20248. EXPECT_DECLS;
  20249. #if defined(HAVE_CURVE448)
  20250. WC_RNG rng;
  20251. curve448_key key;
  20252. byte out[CURVE448_KEY_SIZE];
  20253. word32 outLen = sizeof(out);
  20254. int endian = EC448_BIG_ENDIAN;
  20255. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20256. ExpectIntEQ(wc_curve448_init(&key), 0);
  20257. ExpectIntEQ(wc_InitRng(&rng), 0);
  20258. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20259. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  20260. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  20261. /* test bad cases*/
  20262. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  20263. BAD_FUNC_ARG);
  20264. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  20265. BAD_FUNC_ARG);
  20266. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  20267. BAD_FUNC_ARG);
  20268. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  20269. BAD_FUNC_ARG);
  20270. outLen = outLen - 2;
  20271. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  20272. ECC_BAD_ARG_E);
  20273. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20274. wc_curve448_free(&key);
  20275. #endif
  20276. return EXPECT_RESULT();
  20277. } /* END test_wc_curve448_export_public_ex*/
  20278. /*
  20279. * Testing test_wc_curve448_export_private_raw_ex
  20280. */
  20281. static int test_wc_curve448_export_private_raw_ex(void)
  20282. {
  20283. EXPECT_DECLS;
  20284. #if defined(HAVE_CURVE448)
  20285. curve448_key key;
  20286. byte out[CURVE448_KEY_SIZE];
  20287. word32 outLen = sizeof(out);
  20288. int endian = EC448_BIG_ENDIAN;
  20289. ExpectIntEQ(wc_curve448_init(&key), 0);
  20290. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20291. 0);
  20292. /* test bad cases*/
  20293. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  20294. BAD_FUNC_ARG);
  20295. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  20296. BAD_FUNC_ARG);
  20297. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  20298. BAD_FUNC_ARG);
  20299. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  20300. BAD_FUNC_ARG);
  20301. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20302. EC448_LITTLE_ENDIAN), 0);
  20303. outLen = outLen - 2;
  20304. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  20305. ECC_BAD_ARG_E);
  20306. wc_curve448_free(&key);
  20307. #endif
  20308. return EXPECT_RESULT();
  20309. } /* END test_wc_curve448_export_private_raw_ex*/
  20310. /*
  20311. * Testing test_wc_curve448_import_private_raw_ex
  20312. */
  20313. static int test_wc_curve448_import_private_raw_ex(void)
  20314. {
  20315. EXPECT_DECLS;
  20316. #if defined(HAVE_CURVE448)
  20317. curve448_key key;
  20318. WC_RNG rng;
  20319. byte priv[CURVE448_KEY_SIZE];
  20320. byte pub[CURVE448_KEY_SIZE];
  20321. word32 privSz = sizeof(priv);
  20322. word32 pubSz = sizeof(pub);
  20323. int endian = EC448_BIG_ENDIAN;
  20324. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20325. ExpectIntEQ(wc_curve448_init(&key), 0);
  20326. ExpectIntEQ(wc_InitRng(&rng), 0);
  20327. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20328. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20329. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20330. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20331. &key, endian), 0);
  20332. /* test bad cases */
  20333. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  20334. BAD_FUNC_ARG);
  20335. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20336. &key, endian), BAD_FUNC_ARG);
  20337. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20338. &key, endian), BAD_FUNC_ARG);
  20339. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20340. NULL, endian), BAD_FUNC_ARG);
  20341. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20342. &key, endian), ECC_BAD_ARG_E);
  20343. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20344. &key, endian), ECC_BAD_ARG_E);
  20345. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20346. &key, EC448_LITTLE_ENDIAN), 0);
  20347. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20348. wc_curve448_free(&key);
  20349. #endif
  20350. return EXPECT_RESULT();
  20351. } /* END test_wc_curve448_import_private_raw_ex*/
  20352. /*
  20353. * Testing test_curve448_export_key_raw
  20354. */
  20355. static int test_wc_curve448_export_key_raw(void)
  20356. {
  20357. EXPECT_DECLS;
  20358. #if defined(HAVE_CURVE448)
  20359. curve448_key key;
  20360. WC_RNG rng;
  20361. byte priv[CURVE448_KEY_SIZE];
  20362. byte pub[CURVE448_KEY_SIZE];
  20363. word32 privSz = sizeof(priv);
  20364. word32 pubSz = sizeof(pub);
  20365. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20366. ExpectIntEQ(wc_curve448_init(&key), 0);
  20367. ExpectIntEQ(wc_InitRng(&rng), 0);
  20368. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20369. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20370. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  20371. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  20372. 0);
  20373. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20374. wc_curve448_free(&key);
  20375. #endif
  20376. return EXPECT_RESULT();
  20377. } /* END test_wc_curve448_import_private_raw_ex*/
  20378. /*
  20379. * Testing test_wc_curve448_import_private
  20380. */
  20381. static int test_wc_curve448_import_private(void)
  20382. {
  20383. EXPECT_DECLS;
  20384. #if defined(HAVE_CURVE448)
  20385. curve448_key key;
  20386. WC_RNG rng;
  20387. byte priv[CURVE448_KEY_SIZE];
  20388. word32 privSz = sizeof(priv);
  20389. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20390. ExpectIntEQ(wc_curve448_init(&key), 0);
  20391. ExpectIntEQ(wc_InitRng(&rng), 0);
  20392. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  20393. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  20394. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  20395. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20396. wc_curve448_free(&key);
  20397. #endif
  20398. return EXPECT_RESULT();
  20399. } /* END test_wc_curve448_import*/
  20400. /*
  20401. * Testing test_wc_curve448_size.
  20402. */
  20403. static int test_wc_curve448_size(void)
  20404. {
  20405. EXPECT_DECLS;
  20406. #if defined(HAVE_CURVE448)
  20407. curve448_key key;
  20408. ExpectIntEQ(wc_curve448_init(&key), 0);
  20409. /* Test good args for wc_curve448_size */
  20410. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  20411. /* Test bad args for wc_curve448_size */
  20412. ExpectIntEQ(wc_curve448_size(NULL), 0);
  20413. wc_curve448_free(&key);
  20414. #endif
  20415. return EXPECT_RESULT();
  20416. } /* END test_wc_curve448_size*/
  20417. /*
  20418. * Testing wc_ecc_make_key.
  20419. */
  20420. static int test_wc_ecc_make_key(void)
  20421. {
  20422. EXPECT_DECLS;
  20423. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20424. ecc_key key;
  20425. WC_RNG rng;
  20426. int ret;
  20427. XMEMSET(&key, 0, sizeof(ecc_key));
  20428. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20429. ExpectIntEQ(wc_ecc_init(&key), 0);
  20430. ExpectIntEQ(wc_InitRng(&rng), 0);
  20431. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20432. #if defined(WOLFSSL_ASYNC_CRYPT)
  20433. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20434. #endif
  20435. ExpectIntEQ(ret, 0);
  20436. /* Pass in bad args. */
  20437. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  20438. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  20439. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20440. wc_ecc_free(&key);
  20441. #ifdef FP_ECC
  20442. wc_ecc_fp_free();
  20443. #endif
  20444. #endif
  20445. return EXPECT_RESULT();
  20446. } /* END test_wc_ecc_make_key */
  20447. /*
  20448. * Testing wc_ecc_init()
  20449. */
  20450. static int test_wc_ecc_init(void)
  20451. {
  20452. EXPECT_DECLS;
  20453. #ifdef HAVE_ECC
  20454. ecc_key key;
  20455. XMEMSET(&key, 0, sizeof(ecc_key));
  20456. ExpectIntEQ(wc_ecc_init(&key), 0);
  20457. /* Pass in bad args. */
  20458. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  20459. wc_ecc_free(&key);
  20460. #endif
  20461. return EXPECT_RESULT();
  20462. } /* END test_wc_ecc_init */
  20463. /*
  20464. * Testing wc_ecc_check_key()
  20465. */
  20466. static int test_wc_ecc_check_key(void)
  20467. {
  20468. EXPECT_DECLS;
  20469. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20470. ecc_key key;
  20471. WC_RNG rng;
  20472. int ret;
  20473. XMEMSET(&rng, 0, sizeof(rng));
  20474. XMEMSET(&key, 0, sizeof(key));
  20475. ExpectIntEQ(wc_ecc_init(&key), 0);
  20476. ExpectIntEQ(wc_InitRng(&rng), 0);
  20477. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20478. #if defined(WOLFSSL_ASYNC_CRYPT)
  20479. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20480. #endif
  20481. ExpectIntEQ(ret, 0);
  20482. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  20483. /* Pass in bad args. */
  20484. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  20485. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20486. wc_ecc_free(&key);
  20487. #ifdef FP_ECC
  20488. wc_ecc_fp_free();
  20489. #endif
  20490. #endif
  20491. return EXPECT_RESULT();
  20492. } /* END test_wc_ecc_check_key */
  20493. /*
  20494. * Testing wc_ecc_get_generator()
  20495. */
  20496. static int test_wc_ecc_get_generator(void)
  20497. {
  20498. EXPECT_DECLS;
  20499. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20500. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20501. ecc_point* pt = NULL;
  20502. ExpectNotNull(pt = wc_ecc_new_point());
  20503. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20504. MP_OKAY);
  20505. /* Test bad args. */
  20506. /* Returns Zero for bad arg. */
  20507. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  20508. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  20509. MP_OKAY);
  20510. /* If we ever get to 1000 curves increase this number */
  20511. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  20512. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  20513. wc_ecc_del_point(pt);
  20514. #endif
  20515. return EXPECT_RESULT();
  20516. } /* END test_wc_ecc_get_generator */
  20517. /*
  20518. * Testing wc_ecc_size()
  20519. */
  20520. static int test_wc_ecc_size(void)
  20521. {
  20522. EXPECT_DECLS;
  20523. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20524. WC_RNG rng;
  20525. ecc_key key;
  20526. int ret;
  20527. XMEMSET(&key, 0, sizeof(ecc_key));
  20528. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20529. ExpectIntEQ(wc_ecc_init(&key), 0);
  20530. ExpectIntEQ(wc_InitRng(&rng), 0);
  20531. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20532. #if defined(WOLFSSL_ASYNC_CRYPT)
  20533. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20534. #endif
  20535. ExpectIntEQ(ret, 0);
  20536. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  20537. /* Test bad args. */
  20538. /* Returns Zero for bad arg. */
  20539. ExpectIntEQ(wc_ecc_size(NULL), 0);
  20540. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20541. wc_ecc_free(&key);
  20542. #endif
  20543. return EXPECT_RESULT();
  20544. } /* END test_wc_ecc_size */
  20545. static int test_wc_ecc_params(void)
  20546. {
  20547. EXPECT_DECLS;
  20548. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  20549. It was added after certifications */
  20550. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  20551. const ecc_set_type* ecc_set;
  20552. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  20553. /* Test for SECP256R1 curve */
  20554. int curve_id = ECC_SECP256R1;
  20555. int curve_idx;
  20556. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  20557. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  20558. ExpectIntEQ(ecc_set->id, curve_id);
  20559. #endif
  20560. /* Test case when SECP256R1 is not enabled */
  20561. /* Test that we get curve params for index 0 */
  20562. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  20563. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  20564. return EXPECT_RESULT();
  20565. }
  20566. /*
  20567. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  20568. */
  20569. static int test_wc_ecc_signVerify_hash(void)
  20570. {
  20571. EXPECT_DECLS;
  20572. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  20573. ecc_key key;
  20574. WC_RNG rng;
  20575. int ret;
  20576. #ifdef HAVE_ECC_VERIFY
  20577. int verify = 0;
  20578. #endif
  20579. word32 siglen = ECC_BUFSIZE;
  20580. byte sig[ECC_BUFSIZE];
  20581. byte adjustedSig[ECC_BUFSIZE+1];
  20582. byte digest[] = TEST_STRING;
  20583. word32 digestlen = (word32)TEST_STRING_SZ;
  20584. /* Init stack var */
  20585. XMEMSET(&key, 0, sizeof(ecc_key));
  20586. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20587. XMEMSET(sig, 0, siglen);
  20588. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  20589. /* Init structs. */
  20590. ExpectIntEQ(wc_ecc_init(&key), 0);
  20591. ExpectIntEQ(wc_InitRng(&rng), 0);
  20592. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20593. #if defined(WOLFSSL_ASYNC_CRYPT)
  20594. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20595. #endif
  20596. ExpectIntEQ(ret, 0);
  20597. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  20598. 0);
  20599. /* Check bad args. */
  20600. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  20601. ECC_BAD_ARG_E);
  20602. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  20603. ECC_BAD_ARG_E);
  20604. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  20605. ECC_BAD_ARG_E);
  20606. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  20607. ECC_BAD_ARG_E);
  20608. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  20609. ECC_BAD_ARG_E);
  20610. #ifdef HAVE_ECC_VERIFY
  20611. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20612. &key), 0);
  20613. ExpectIntEQ(verify, 1);
  20614. /* test check on length of signature passed in */
  20615. XMEMCPY(adjustedSig, sig, siglen);
  20616. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  20617. #ifndef NO_STRICT_ECDSA_LEN
  20618. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20619. &verify, &key), 0);
  20620. #else
  20621. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  20622. * is allowed */
  20623. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  20624. &verify, &key), 0);
  20625. #endif
  20626. /* Test bad args. */
  20627. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  20628. &key), ECC_BAD_ARG_E);
  20629. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  20630. ECC_BAD_ARG_E);
  20631. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  20632. ECC_BAD_ARG_E);
  20633. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  20634. NULL), ECC_BAD_ARG_E);
  20635. #endif /* HAVE_ECC_VERIFY */
  20636. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20637. wc_ecc_free(&key);
  20638. #ifdef FP_ECC
  20639. wc_ecc_fp_free();
  20640. #endif
  20641. #endif
  20642. return EXPECT_RESULT();
  20643. } /* END test_wc_ecc_sign_hash */
  20644. /*
  20645. * Testing wc_ecc_shared_secret()
  20646. */
  20647. static int test_wc_ecc_shared_secret(void)
  20648. {
  20649. EXPECT_DECLS;
  20650. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  20651. ecc_key key;
  20652. ecc_key pubKey;
  20653. WC_RNG rng;
  20654. #if defined(NO_ECC256)
  20655. int ret;
  20656. #endif
  20657. byte out[KEY32];
  20658. int keySz = sizeof(out);
  20659. word32 outlen = (word32)sizeof(out);
  20660. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  20661. const char* qx =
  20662. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  20663. const char* qy =
  20664. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  20665. const char* d =
  20666. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  20667. const char* curveName = "SECP256R1";
  20668. const byte expected_shared_secret[] =
  20669. {
  20670. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  20671. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  20672. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  20673. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  20674. };
  20675. #endif
  20676. PRIVATE_KEY_UNLOCK();
  20677. /* Initialize variables. */
  20678. XMEMSET(&key, 0, sizeof(ecc_key));
  20679. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20680. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20681. XMEMSET(out, 0, keySz);
  20682. ExpectIntEQ(wc_ecc_init(&key), 0);
  20683. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20684. ExpectIntEQ(wc_InitRng(&rng), 0);
  20685. #if !defined(NO_ECC256)
  20686. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  20687. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  20688. #else
  20689. ret = wc_ecc_make_key(&rng, keySz, &key);
  20690. #if defined(WOLFSSL_ASYNC_CRYPT)
  20691. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20692. #endif
  20693. ExpectIntEQ(ret, 0);
  20694. ret = wc_ecc_make_key(&rng, keySz, &key);
  20695. #if defined(WOLFSSL_ASYNC_CRYPT)
  20696. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20697. #endif
  20698. ExpectIntEQ(ret, 0);
  20699. #endif
  20700. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20701. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20702. !defined(HAVE_SELFTEST)
  20703. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  20704. #endif
  20705. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  20706. #if !defined(NO_ECC256)
  20707. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  20708. #endif
  20709. /* Test bad args. */
  20710. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  20711. BAD_FUNC_ARG);
  20712. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  20713. BAD_FUNC_ARG);
  20714. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  20715. BAD_FUNC_ARG);
  20716. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  20717. BAD_FUNC_ARG);
  20718. /* Invalid length */
  20719. outlen = 1;
  20720. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  20721. BUFFER_E);
  20722. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20723. wc_ecc_free(&pubKey);
  20724. wc_ecc_free(&key);
  20725. #ifdef FP_ECC
  20726. wc_ecc_fp_free();
  20727. #endif
  20728. PRIVATE_KEY_LOCK();
  20729. #endif
  20730. return EXPECT_RESULT();
  20731. } /* END tests_wc_ecc_shared_secret */
  20732. /*
  20733. * testint wc_ecc_export_x963()
  20734. */
  20735. static int test_wc_ecc_export_x963(void)
  20736. {
  20737. EXPECT_DECLS;
  20738. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20739. ecc_key key;
  20740. WC_RNG rng;
  20741. byte out[ECC_ASN963_MAX_BUF_SZ];
  20742. word32 outlen = sizeof(out);
  20743. int ret;
  20744. PRIVATE_KEY_UNLOCK();
  20745. /* Initialize variables. */
  20746. XMEMSET(&key, 0, sizeof(ecc_key));
  20747. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20748. XMEMSET(out, 0, outlen);
  20749. ExpectIntEQ(wc_ecc_init(&key), 0);
  20750. ExpectIntEQ(wc_InitRng(&rng), 0);
  20751. ret = wc_ecc_make_key(&rng, KEY20, &key);
  20752. #if defined(WOLFSSL_ASYNC_CRYPT)
  20753. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20754. #endif
  20755. ExpectIntEQ(ret, 0);
  20756. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  20757. /* Test bad args. */
  20758. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  20759. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  20760. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  20761. key.idx = -4;
  20762. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  20763. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20764. wc_ecc_free(&key);
  20765. #ifdef FP_ECC
  20766. wc_ecc_fp_free();
  20767. #endif
  20768. PRIVATE_KEY_LOCK();
  20769. #endif
  20770. return EXPECT_RESULT();
  20771. } /* END test_wc_ecc_export_x963 */
  20772. /*
  20773. * Testing wc_ecc_export_x963_ex()
  20774. * compile with --enable-compkey will use compression.
  20775. */
  20776. static int test_wc_ecc_export_x963_ex(void)
  20777. {
  20778. EXPECT_DECLS;
  20779. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20780. ecc_key key;
  20781. WC_RNG rng;
  20782. int ret;
  20783. byte out[ECC_ASN963_MAX_BUF_SZ];
  20784. word32 outlen = sizeof(out);
  20785. #ifdef HAVE_COMP_KEY
  20786. word32 badOutLen = 5;
  20787. #endif
  20788. /* Init stack variables. */
  20789. XMEMSET(&key, 0, sizeof(ecc_key));
  20790. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20791. XMEMSET(out, 0, outlen);
  20792. ExpectIntEQ(wc_ecc_init(&key), 0);
  20793. ExpectIntEQ(wc_InitRng(&rng), 0);
  20794. ret = wc_ecc_make_key(&rng, KEY64, &key);
  20795. #if defined(WOLFSSL_ASYNC_CRYPT)
  20796. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20797. #endif
  20798. ExpectIntEQ(ret, 0);
  20799. #ifdef HAVE_COMP_KEY
  20800. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  20801. #else
  20802. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  20803. #endif
  20804. /* Test bad args. */
  20805. #ifdef HAVE_COMP_KEY
  20806. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  20807. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  20808. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  20809. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  20810. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  20811. #else
  20812. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  20813. LENGTH_ONLY_E);
  20814. #endif
  20815. key.idx = -4;
  20816. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  20817. #else
  20818. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  20819. ECC_BAD_ARG_E);
  20820. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  20821. LENGTH_ONLY_E);
  20822. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  20823. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  20824. ECC_BAD_ARG_E);
  20825. key.idx = -4;
  20826. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  20827. ECC_BAD_ARG_E);
  20828. #endif
  20829. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20830. wc_ecc_free(&key);
  20831. #ifdef FP_ECC
  20832. wc_ecc_fp_free();
  20833. #endif
  20834. #endif
  20835. return EXPECT_RESULT();
  20836. } /* END test_wc_ecc_export_x963_ex */
  20837. /*
  20838. * testing wc_ecc_import_x963()
  20839. */
  20840. static int test_wc_ecc_import_x963(void)
  20841. {
  20842. EXPECT_DECLS;
  20843. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  20844. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20845. ecc_key pubKey;
  20846. ecc_key key;
  20847. WC_RNG rng;
  20848. byte x963[ECC_ASN963_MAX_BUF_SZ];
  20849. word32 x963Len = (word32)sizeof(x963);
  20850. int ret;
  20851. /* Init stack variables. */
  20852. XMEMSET(&key, 0, sizeof(ecc_key));
  20853. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  20854. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20855. XMEMSET(x963, 0, x963Len);
  20856. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  20857. ExpectIntEQ(wc_ecc_init(&key), 0);
  20858. ExpectIntEQ(wc_InitRng(&rng), 0);
  20859. ret = wc_ecc_make_key(&rng, KEY24, &key);
  20860. #if defined(WOLFSSL_ASYNC_CRYPT)
  20861. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20862. #endif
  20863. ExpectIntEQ(ret, 0);
  20864. PRIVATE_KEY_UNLOCK();
  20865. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  20866. PRIVATE_KEY_LOCK();
  20867. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  20868. /* Test bad args. */
  20869. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  20870. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  20871. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  20872. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20873. wc_ecc_free(&key);
  20874. wc_ecc_free(&pubKey);
  20875. #ifdef FP_ECC
  20876. wc_ecc_fp_free();
  20877. #endif
  20878. #endif
  20879. return EXPECT_RESULT();
  20880. } /* END wc_ecc_import_x963 */
  20881. /*
  20882. * testing wc_ecc_import_private_key()
  20883. */
  20884. static int test_wc_ecc_import_private_key(void)
  20885. {
  20886. EXPECT_DECLS;
  20887. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  20888. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20889. ecc_key key;
  20890. ecc_key keyImp;
  20891. WC_RNG rng;
  20892. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  20893. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  20894. word32 privKeySz = (word32)sizeof(privKey);
  20895. word32 x963KeySz = (word32)sizeof(x963Key);
  20896. int ret;
  20897. /* Init stack variables. */
  20898. XMEMSET(&key, 0, sizeof(ecc_key));
  20899. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  20900. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20901. XMEMSET(privKey, 0, privKeySz);
  20902. XMEMSET(x963Key, 0, x963KeySz);
  20903. ExpectIntEQ(wc_ecc_init(&key), 0);
  20904. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  20905. ExpectIntEQ(wc_InitRng(&rng), 0);
  20906. ret = wc_ecc_make_key(&rng, KEY48, &key);
  20907. #if defined(WOLFSSL_ASYNC_CRYPT)
  20908. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20909. #endif
  20910. ExpectIntEQ(ret, 0);
  20911. PRIVATE_KEY_UNLOCK();
  20912. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  20913. PRIVATE_KEY_LOCK();
  20914. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  20915. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  20916. x963KeySz, &keyImp), 0);
  20917. /* Pass in bad args. */
  20918. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  20919. x963KeySz, NULL), BAD_FUNC_ARG);
  20920. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  20921. &keyImp), BAD_FUNC_ARG);
  20922. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20923. wc_ecc_free(&keyImp);
  20924. wc_ecc_free(&key);
  20925. #ifdef FP_ECC
  20926. wc_ecc_fp_free();
  20927. #endif
  20928. #endif
  20929. return EXPECT_RESULT();
  20930. } /* END test_wc_ecc_import_private_key */
  20931. /*
  20932. * Testing wc_ecc_export_private_only()
  20933. */
  20934. static int test_wc_ecc_export_private_only(void)
  20935. {
  20936. EXPECT_DECLS;
  20937. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20938. ecc_key key;
  20939. WC_RNG rng;
  20940. byte out[ECC_PRIV_KEY_BUF];
  20941. word32 outlen = sizeof(out);
  20942. int ret;
  20943. /* Init stack variables. */
  20944. XMEMSET(&key, 0, sizeof(ecc_key));
  20945. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20946. XMEMSET(out, 0, outlen);
  20947. ExpectIntEQ(wc_ecc_init(&key), 0);
  20948. ExpectIntEQ(wc_InitRng(&rng), 0);
  20949. ret = wc_ecc_make_key(&rng, KEY32, &key);
  20950. #if defined(WOLFSSL_ASYNC_CRYPT)
  20951. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20952. #endif
  20953. ExpectIntEQ(ret, 0);
  20954. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  20955. /* Pass in bad args. */
  20956. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  20957. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  20958. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  20959. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20960. wc_ecc_free(&key);
  20961. #ifdef FP_ECC
  20962. wc_ecc_fp_free();
  20963. #endif
  20964. #endif
  20965. return EXPECT_RESULT();
  20966. } /* END test_wc_ecc_export_private_only */
  20967. /*
  20968. * Testing wc_ecc_rs_to_sig()
  20969. */
  20970. static int test_wc_ecc_rs_to_sig(void)
  20971. {
  20972. EXPECT_DECLS;
  20973. #if defined(HAVE_ECC) && !defined(NO_ASN)
  20974. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  20975. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20976. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20977. const char* zeroStr = "0";
  20978. byte sig[ECC_MAX_SIG_SIZE];
  20979. word32 siglen = (word32)sizeof(sig);
  20980. /* R and S max size is the order of curve. 2^192.*/
  20981. int keySz = KEY24;
  20982. byte r[KEY24];
  20983. byte s[KEY24];
  20984. word32 rlen = (word32)sizeof(r);
  20985. word32 slen = (word32)sizeof(s);
  20986. /* Init stack variables. */
  20987. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  20988. XMEMSET(r, 0, keySz);
  20989. XMEMSET(s, 0, keySz);
  20990. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  20991. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  20992. /* Test bad args. */
  20993. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  20994. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  20995. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  20996. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  20997. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  20998. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  20999. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21000. ECC_BAD_ARG_E);
  21001. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21002. ECC_BAD_ARG_E);
  21003. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21004. ECC_BAD_ARG_E);
  21005. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21006. ECC_BAD_ARG_E);
  21007. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21008. ECC_BAD_ARG_E);
  21009. #endif
  21010. return EXPECT_RESULT();
  21011. } /* END test_wc_ecc_rs_to_sig */
  21012. static int test_wc_ecc_import_raw(void)
  21013. {
  21014. EXPECT_DECLS;
  21015. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21016. ecc_key key;
  21017. const char* qx =
  21018. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21019. const char* qy =
  21020. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21021. const char* d =
  21022. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21023. const char* curveName = "SECP256R1";
  21024. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21025. const char* kNullStr = "";
  21026. int ret;
  21027. #endif
  21028. XMEMSET(&key, 0, sizeof(ecc_key));
  21029. ExpectIntEQ(wc_ecc_init(&key), 0);
  21030. /* Test good import */
  21031. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21032. /* Test bad args. */
  21033. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21034. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21035. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21036. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21037. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21038. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21039. wc_ecc_free(&key);
  21040. #endif
  21041. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21042. curveName), 0);
  21043. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21044. #endif
  21045. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21046. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21047. wc_ecc_free(&key);
  21048. #endif
  21049. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21050. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21051. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21052. #else
  21053. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21054. #endif
  21055. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21056. wc_ecc_free(&key);
  21057. #endif
  21058. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21059. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21060. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21061. #else
  21062. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21063. #endif
  21064. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21065. wc_ecc_free(&key);
  21066. #endif
  21067. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21068. #endif
  21069. wc_ecc_free(&key);
  21070. #endif
  21071. return EXPECT_RESULT();
  21072. } /* END test_wc_ecc_import_raw */
  21073. static int test_wc_ecc_import_unsigned(void)
  21074. {
  21075. EXPECT_DECLS;
  21076. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21077. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21078. HAVE_FIPS_VERSION >= 2))
  21079. ecc_key key;
  21080. const byte qx[] = {
  21081. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21082. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21083. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21084. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21085. };
  21086. const byte qy[] = {
  21087. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21088. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21089. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21090. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21091. };
  21092. const byte d[] = {
  21093. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21094. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21095. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21096. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21097. };
  21098. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21099. const byte nullBytes[32] = {0};
  21100. int ret;
  21101. #endif
  21102. int curveId = ECC_SECP256R1;
  21103. XMEMSET(&key, 0, sizeof(ecc_key));
  21104. ExpectIntEQ(wc_ecc_init(&key), 0);
  21105. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21106. curveId), 0);
  21107. /* Test bad args. */
  21108. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21109. curveId), BAD_FUNC_ARG);
  21110. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21111. curveId), BAD_FUNC_ARG);
  21112. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21113. curveId), BAD_FUNC_ARG);
  21114. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21115. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21116. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21117. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21118. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21119. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21120. #endif
  21121. wc_ecc_free(&key);
  21122. #endif
  21123. return EXPECT_RESULT();
  21124. } /* END test_wc_ecc_import_unsigned */
  21125. /*
  21126. * Testing wc_ecc_sig_size()
  21127. */
  21128. static int test_wc_ecc_sig_size(void)
  21129. {
  21130. EXPECT_DECLS;
  21131. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21132. ecc_key key;
  21133. WC_RNG rng;
  21134. int keySz = KEY16;
  21135. int ret;
  21136. XMEMSET(&rng, 0, sizeof(rng));
  21137. XMEMSET(&key, 0, sizeof(key));
  21138. ExpectIntEQ(wc_ecc_init(&key), 0);
  21139. ExpectIntEQ(wc_InitRng(&rng), 0);
  21140. ret = wc_ecc_make_key(&rng, keySz, &key);
  21141. #if defined(WOLFSSL_ASYNC_CRYPT)
  21142. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21143. #endif
  21144. ExpectIntEQ(ret, 0);
  21145. ExpectIntLE(wc_ecc_sig_size(&key),
  21146. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21147. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21148. wc_ecc_free(&key);
  21149. #endif
  21150. return EXPECT_RESULT();
  21151. } /* END test_wc_ecc_sig_size */
  21152. /*
  21153. * Testing wc_ecc_ctx_new()
  21154. */
  21155. static int test_wc_ecc_ctx_new(void)
  21156. {
  21157. EXPECT_DECLS;
  21158. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21159. WC_RNG rng;
  21160. ecEncCtx* cli = NULL;
  21161. ecEncCtx* srv = NULL;
  21162. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21163. ExpectIntEQ(wc_InitRng(&rng), 0);
  21164. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21165. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21166. wc_ecc_ctx_free(cli);
  21167. cli = NULL;
  21168. wc_ecc_ctx_free(srv);
  21169. /* Test bad args. */
  21170. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21171. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  21172. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  21173. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21174. wc_ecc_ctx_free(cli);
  21175. #endif
  21176. return EXPECT_RESULT();
  21177. } /* END test_wc_ecc_ctx_new */
  21178. /*
  21179. * Tesing wc_ecc_reset()
  21180. */
  21181. static int test_wc_ecc_ctx_reset(void)
  21182. {
  21183. EXPECT_DECLS;
  21184. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21185. ecEncCtx* ctx = NULL;
  21186. WC_RNG rng;
  21187. XMEMSET(&rng, 0, sizeof(rng));
  21188. ExpectIntEQ(wc_InitRng(&rng), 0);
  21189. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21190. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  21191. /* Pass in bad args. */
  21192. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  21193. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  21194. wc_ecc_ctx_free(ctx);
  21195. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21196. #endif
  21197. return EXPECT_RESULT();
  21198. } /* END test_wc_ecc_ctx_reset */
  21199. /*
  21200. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  21201. */
  21202. static int test_wc_ecc_ctx_set_peer_salt(void)
  21203. {
  21204. EXPECT_DECLS;
  21205. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21206. WC_RNG rng;
  21207. ecEncCtx* cliCtx = NULL;
  21208. ecEncCtx* servCtx = NULL;
  21209. const byte* cliSalt = NULL;
  21210. const byte* servSalt = NULL;
  21211. XMEMSET(&rng, 0, sizeof(rng));
  21212. ExpectIntEQ(wc_InitRng(&rng), 0);
  21213. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21214. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  21215. /* Test bad args. */
  21216. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  21217. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  21218. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  21219. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  21220. /* Test bad args. */
  21221. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  21222. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  21223. wc_ecc_ctx_free(cliCtx);
  21224. wc_ecc_ctx_free(servCtx);
  21225. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21226. #endif
  21227. return EXPECT_RESULT();
  21228. } /* END test_wc_ecc_ctx_set_peer_salt */
  21229. /*
  21230. * Testing wc_ecc_ctx_set_info()
  21231. */
  21232. static int test_wc_ecc_ctx_set_info(void)
  21233. {
  21234. EXPECT_DECLS;
  21235. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21236. ecEncCtx* ctx = NULL;
  21237. WC_RNG rng;
  21238. const char* optInfo = "Optional Test Info.";
  21239. int optInfoSz = (int)XSTRLEN(optInfo);
  21240. const char* badOptInfo = NULL;
  21241. XMEMSET(&rng, 0, sizeof(rng));
  21242. ExpectIntEQ(wc_InitRng(&rng), 0);
  21243. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  21244. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  21245. /* Test bad args. */
  21246. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  21247. BAD_FUNC_ARG);
  21248. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  21249. BAD_FUNC_ARG);
  21250. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  21251. wc_ecc_ctx_free(ctx);
  21252. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21253. #endif
  21254. return EXPECT_RESULT();
  21255. } /* END test_wc_ecc_ctx_set_info */
  21256. /*
  21257. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  21258. */
  21259. static int test_wc_ecc_encryptDecrypt(void)
  21260. {
  21261. EXPECT_DECLS;
  21262. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  21263. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  21264. ecc_key srvKey;
  21265. ecc_key cliKey;
  21266. ecc_key tmpKey;
  21267. WC_RNG rng;
  21268. int ret;
  21269. const char* msg = "EccBlock Size 16";
  21270. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  21271. #ifdef WOLFSSL_ECIES_OLD
  21272. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21273. #elif defined(WOLFSSL_ECIES_GEN_IV)
  21274. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  21275. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  21276. #else
  21277. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  21278. WC_SHA256_DIGEST_SIZE];
  21279. #endif
  21280. word32 outSz = (word32)sizeof(out);
  21281. byte plain[sizeof("EccBlock Size 16")];
  21282. word32 plainSz = (word32)sizeof(plain);
  21283. int keySz = KEY20;
  21284. /* Init stack variables. */
  21285. XMEMSET(out, 0, outSz);
  21286. XMEMSET(plain, 0, plainSz);
  21287. XMEMSET(&rng, 0, sizeof(rng));
  21288. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  21289. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  21290. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  21291. ExpectIntEQ(wc_InitRng(&rng), 0);
  21292. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  21293. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  21294. #if defined(WOLFSSL_ASYNC_CRYPT)
  21295. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21296. #endif
  21297. ExpectIntEQ(ret, 0);
  21298. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  21299. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  21300. #if defined(WOLFSSL_ASYNC_CRYPT)
  21301. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21302. #endif
  21303. ExpectIntEQ(ret, 0);
  21304. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  21305. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21306. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21307. !defined(HAVE_SELFTEST)
  21308. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  21309. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  21310. #endif
  21311. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  21312. &outSz, NULL), 0);
  21313. /* Test bad args. */
  21314. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  21315. NULL), BAD_FUNC_ARG);
  21316. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  21317. NULL), BAD_FUNC_ARG);
  21318. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  21319. NULL), BAD_FUNC_ARG);
  21320. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  21321. &outSz, NULL), BAD_FUNC_ARG);
  21322. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  21323. NULL), BAD_FUNC_ARG);
  21324. #ifdef WOLFSSL_ECIES_OLD
  21325. tmpKey.dp = cliKey.dp;
  21326. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  21327. #endif
  21328. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  21329. NULL), 0);
  21330. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  21331. NULL), BAD_FUNC_ARG);
  21332. #ifdef WOLFSSL_ECIES_OLD
  21333. /* NULL parameter allowed in new implementations - public key comes from
  21334. * the message. */
  21335. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  21336. NULL), BAD_FUNC_ARG);
  21337. #endif
  21338. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  21339. NULL), BAD_FUNC_ARG);
  21340. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  21341. NULL), BAD_FUNC_ARG);
  21342. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  21343. BAD_FUNC_ARG);
  21344. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  21345. wc_ecc_free(&tmpKey);
  21346. wc_ecc_free(&srvKey);
  21347. wc_ecc_free(&cliKey);
  21348. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21349. #endif
  21350. return EXPECT_RESULT();
  21351. } /* END test_wc_ecc_encryptDecrypt */
  21352. /*
  21353. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  21354. */
  21355. static int test_wc_ecc_del_point(void)
  21356. {
  21357. EXPECT_DECLS;
  21358. #if defined(HAVE_ECC)
  21359. ecc_point* pt = NULL;
  21360. ExpectNotNull(pt = wc_ecc_new_point());
  21361. wc_ecc_del_point(pt);
  21362. #endif
  21363. return EXPECT_RESULT();
  21364. } /* END test_wc_ecc_del_point */
  21365. /*
  21366. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  21367. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  21368. * and wc_ecc_cmp_point()
  21369. */
  21370. static int test_wc_ecc_pointFns(void)
  21371. {
  21372. EXPECT_DECLS;
  21373. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  21374. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21375. !defined(WOLFSSL_ATECC608A)
  21376. ecc_key key;
  21377. WC_RNG rng;
  21378. int ret;
  21379. ecc_point* point = NULL;
  21380. ecc_point* cpypt = NULL;
  21381. int idx = 0;
  21382. int keySz = KEY32;
  21383. byte der[DER_SZ(KEY32)];
  21384. word32 derlenChk = 0;
  21385. word32 derSz = DER_SZ(KEY32);
  21386. /* Init stack variables. */
  21387. XMEMSET(der, 0, derSz);
  21388. XMEMSET(&key, 0, sizeof(ecc_key));
  21389. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21390. ExpectIntEQ(wc_InitRng(&rng), 0);
  21391. ExpectIntEQ(wc_ecc_init(&key), 0);
  21392. ret = wc_ecc_make_key(&rng, keySz, &key);
  21393. #if defined(WOLFSSL_ASYNC_CRYPT)
  21394. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21395. #endif
  21396. ExpectIntEQ(ret, 0);
  21397. ExpectNotNull(point = wc_ecc_new_point());
  21398. ExpectNotNull(cpypt = wc_ecc_new_point());
  21399. /* Export */
  21400. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  21401. &derlenChk), LENGTH_ONLY_E);
  21402. /* Check length value. */
  21403. ExpectIntEQ(derSz, derlenChk);
  21404. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21405. &derSz), 0);
  21406. /* Test bad args. */
  21407. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  21408. ECC_BAD_ARG_E);
  21409. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  21410. ECC_BAD_ARG_E);
  21411. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  21412. NULL), ECC_BAD_ARG_E);
  21413. /* Import */
  21414. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  21415. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  21416. /* Test bad args. */
  21417. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  21418. ECC_BAD_ARG_E);
  21419. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  21420. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  21421. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  21422. ECC_BAD_ARG_E);
  21423. /* Copy */
  21424. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  21425. /* Test bad args. */
  21426. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  21427. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  21428. /* Compare point */
  21429. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  21430. /* Test bad args. */
  21431. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  21432. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  21433. /* At infinity if return == 1, otherwise return == 0. */
  21434. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  21435. /* Test bad args. */
  21436. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  21437. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  21438. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  21439. #ifdef USE_ECC_B_PARAM
  21440. /* On curve if ret == 0 */
  21441. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  21442. /* Test bad args. */
  21443. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  21444. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  21445. #endif /* USE_ECC_B_PARAM */
  21446. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  21447. /* Free */
  21448. wc_ecc_del_point(point);
  21449. wc_ecc_del_point(cpypt);
  21450. wc_ecc_free(&key);
  21451. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21452. #endif
  21453. return EXPECT_RESULT();
  21454. } /* END test_wc_ecc_pointFns */
  21455. /*
  21456. * Testing wc_ecc_sahred_secret_ssh()
  21457. */
  21458. static int test_wc_ecc_shared_secret_ssh(void)
  21459. {
  21460. EXPECT_DECLS;
  21461. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  21462. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21463. !defined(WOLFSSL_ATECC608A)
  21464. ecc_key key;
  21465. ecc_key key2;
  21466. WC_RNG rng;
  21467. int ret;
  21468. int keySz = KEY32;
  21469. int key2Sz = KEY24;
  21470. byte secret[KEY32];
  21471. word32 secretLen = keySz;
  21472. /* Init stack variables. */
  21473. XMEMSET(&key, 0, sizeof(ecc_key));
  21474. XMEMSET(&key2, 0, sizeof(ecc_key));
  21475. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21476. XMEMSET(secret, 0, secretLen);
  21477. /* Make keys */
  21478. ExpectIntEQ(wc_ecc_init(&key), 0);
  21479. ExpectIntEQ(wc_InitRng(&rng), 0);
  21480. ret = wc_ecc_make_key(&rng, keySz, &key);
  21481. #if defined(WOLFSSL_ASYNC_CRYPT)
  21482. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21483. #endif
  21484. ExpectIntEQ(ret, 0);
  21485. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21486. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21487. ExpectIntEQ(wc_InitRng(&rng), 0);
  21488. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  21489. #if defined(WOLFSSL_ASYNC_CRYPT)
  21490. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  21491. #endif
  21492. ExpectIntEQ(ret, 0);
  21493. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21494. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21495. !defined(HAVE_SELFTEST)
  21496. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21497. #endif
  21498. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21499. &secretLen), 0);
  21500. /* Pass in bad args. */
  21501. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  21502. &secretLen), BAD_FUNC_ARG);
  21503. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  21504. BAD_FUNC_ARG);
  21505. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  21506. BAD_FUNC_ARG);
  21507. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  21508. BAD_FUNC_ARG);
  21509. key.type = ECC_PUBLICKEY;
  21510. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  21511. &secretLen), ECC_BAD_ARG_E);
  21512. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21513. wc_ecc_free(&key);
  21514. wc_ecc_free(&key2);
  21515. #ifdef FP_ECC
  21516. wc_ecc_fp_free();
  21517. #endif
  21518. #endif
  21519. return EXPECT_RESULT();
  21520. } /* END test_wc_ecc_shared_secret_ssh */
  21521. /*
  21522. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  21523. */
  21524. static int test_wc_ecc_verify_hash_ex(void)
  21525. {
  21526. EXPECT_DECLS;
  21527. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  21528. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  21529. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  21530. ecc_key key;
  21531. WC_RNG rng;
  21532. int ret;
  21533. mp_int r;
  21534. mp_int s;
  21535. mp_int z;
  21536. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  21537. unsigned char iHash[] = "Everyone gets Friday off.......";
  21538. unsigned char shortHash[] = TEST_STRING;
  21539. word32 hashlen = sizeof(hash);
  21540. word32 iHashLen = sizeof(iHash);
  21541. word32 shortHashLen = sizeof(shortHash);
  21542. int keySz = KEY32;
  21543. int verify_ok = 0;
  21544. XMEMSET(&key, 0, sizeof(ecc_key));
  21545. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21546. XMEMSET(&r, 0, sizeof(mp_int));
  21547. XMEMSET(&s, 0, sizeof(mp_int));
  21548. XMEMSET(&z, 0, sizeof(mp_int));
  21549. /* Initialize r, s and z. */
  21550. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  21551. ExpectIntEQ(wc_ecc_init(&key), 0);
  21552. ExpectIntEQ(wc_InitRng(&rng), 0);
  21553. ret = wc_ecc_make_key(&rng, keySz, &key);
  21554. #if defined(WOLFSSL_ASYNC_CRYPT)
  21555. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21556. #endif
  21557. ExpectIntEQ(ret, 0);
  21558. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  21559. /* verify_ok should be 1. */
  21560. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  21561. 0);
  21562. ExpectIntEQ(verify_ok, 1);
  21563. /* verify_ok should be 0 */
  21564. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  21565. &key), 0);
  21566. ExpectIntEQ(verify_ok, 0);
  21567. /* verify_ok should be 0. */
  21568. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21569. &verify_ok, &key), 0);
  21570. ExpectIntEQ(verify_ok, 0);
  21571. /* Test bad args. */
  21572. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  21573. ECC_BAD_ARG_E);
  21574. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  21575. ECC_BAD_ARG_E);
  21576. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  21577. ECC_BAD_ARG_E);
  21578. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  21579. ECC_BAD_ARG_E);
  21580. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  21581. ECC_BAD_ARG_E);
  21582. /* Test bad args. */
  21583. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  21584. &verify_ok, &key), ECC_BAD_ARG_E);
  21585. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  21586. &verify_ok, &key), ECC_BAD_ARG_E);
  21587. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  21588. &verify_ok, &key), MP_ZERO_E);
  21589. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  21590. &verify_ok, &key), MP_ZERO_E);
  21591. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  21592. &verify_ok, &key), MP_ZERO_E);
  21593. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  21594. &key), ECC_BAD_ARG_E);
  21595. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  21596. &key), ECC_BAD_ARG_E);
  21597. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  21598. &verify_ok, NULL), ECC_BAD_ARG_E);
  21599. wc_ecc_free(&key);
  21600. mp_free(&r);
  21601. mp_free(&s);
  21602. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21603. #endif
  21604. return EXPECT_RESULT();
  21605. } /* END test_wc_ecc_verify_hash_ex */
  21606. /*
  21607. * Testing wc_ecc_mulmod()
  21608. */
  21609. static int test_wc_ecc_mulmod(void)
  21610. {
  21611. EXPECT_DECLS;
  21612. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  21613. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  21614. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21615. ecc_key key1;
  21616. ecc_key key2;
  21617. ecc_key key3;
  21618. WC_RNG rng;
  21619. int ret;
  21620. XMEMSET(&key1, 0, sizeof(ecc_key));
  21621. XMEMSET(&key2, 0, sizeof(ecc_key));
  21622. XMEMSET(&key3, 0, sizeof(ecc_key));
  21623. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21624. ExpectIntEQ(wc_ecc_init(&key1), 0);
  21625. ExpectIntEQ(wc_ecc_init(&key2), 0);
  21626. ExpectIntEQ(wc_ecc_init(&key3), 0);
  21627. ExpectIntEQ(wc_InitRng(&rng), 0);
  21628. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  21629. #if defined(WOLFSSL_ASYNC_CRYPT)
  21630. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  21631. #endif
  21632. ExpectIntEQ(ret, 0);
  21633. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21634. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  21635. key1.dp->Af, ECC_SECP256R1), 0);
  21636. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  21637. key1.dp->prime, ECC_SECP256R1), 0);
  21638. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21639. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  21640. 1), 0);
  21641. /* Test bad args. */
  21642. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  21643. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21644. ECC_BAD_ARG_E);
  21645. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  21646. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21647. ECC_BAD_ARG_E);
  21648. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  21649. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  21650. ECC_BAD_ARG_E);
  21651. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  21652. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  21653. wc_ecc_free(&key1);
  21654. wc_ecc_free(&key2);
  21655. wc_ecc_free(&key3);
  21656. #ifdef FP_ECC
  21657. wc_ecc_fp_free();
  21658. #endif
  21659. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  21660. return EXPECT_RESULT();
  21661. } /* END test_wc_ecc_mulmod */
  21662. /*
  21663. * Testing wc_ecc_is_valid_idx()
  21664. */
  21665. static int test_wc_ecc_is_valid_idx(void)
  21666. {
  21667. EXPECT_DECLS;
  21668. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21669. ecc_key key;
  21670. WC_RNG rng;
  21671. int ret;
  21672. int iVal = -2;
  21673. int iVal2 = 3000;
  21674. XMEMSET(&key, 0, sizeof(ecc_key));
  21675. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21676. ExpectIntEQ(wc_ecc_init(&key), 0);
  21677. ExpectIntEQ(wc_InitRng(&rng), 0);
  21678. ret = wc_ecc_make_key(&rng, 32, &key);
  21679. #if defined(WOLFSSL_ASYNC_CRYPT)
  21680. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21681. #endif
  21682. ExpectIntEQ(ret, 0);
  21683. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  21684. /* Test bad args. */
  21685. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  21686. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  21687. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21688. wc_ecc_free(&key);
  21689. #ifdef FP_ECC
  21690. wc_ecc_fp_free();
  21691. #endif
  21692. #endif
  21693. return EXPECT_RESULT();
  21694. } /* END test_wc_ecc_is_valid_idx */
  21695. /*
  21696. * Testing wc_ecc_get_curve_id_from_oid()
  21697. */
  21698. static int test_wc_ecc_get_curve_id_from_oid(void)
  21699. {
  21700. EXPECT_DECLS;
  21701. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21702. !defined(HAVE_FIPS)
  21703. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  21704. word32 len = sizeof(oid);
  21705. /* Bad Cases */
  21706. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  21707. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  21708. /* Good Case */
  21709. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  21710. #endif
  21711. return EXPECT_RESULT();
  21712. } /* END test_wc_ecc_get_curve_id_from_oid */
  21713. /*
  21714. * Testing wc_ecc_sig_size_calc()
  21715. */
  21716. static int test_wc_ecc_sig_size_calc(void)
  21717. {
  21718. EXPECT_DECLS;
  21719. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  21720. ecc_key key;
  21721. WC_RNG rng;
  21722. int sz = 0;
  21723. int ret;
  21724. XMEMSET(&key, 0, sizeof(ecc_key));
  21725. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21726. ExpectIntEQ(wc_ecc_init(&key), 0);
  21727. ExpectIntEQ(wc_InitRng(&rng), 0);
  21728. ret = wc_ecc_make_key(&rng, 16, &key);
  21729. #if defined(WOLFSSL_ASYNC_CRYPT)
  21730. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21731. #endif
  21732. ExpectIntEQ(ret, 0);
  21733. sz = key.dp->size;
  21734. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  21735. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21736. wc_ecc_free(&key);
  21737. #endif
  21738. return EXPECT_RESULT();
  21739. } /* END test_wc_ecc_sig_size_calc */
  21740. /*
  21741. * Testing wc_ecc_sm2_make_key()
  21742. */
  21743. static int test_wc_ecc_sm2_make_key(void)
  21744. {
  21745. int res = TEST_SKIPPED;
  21746. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21747. EXPECT_DECLS;
  21748. WC_RNG rng[1];
  21749. ecc_key key[1];
  21750. XMEMSET(rng, 0, sizeof(*rng));
  21751. XMEMSET(key, 0, sizeof(*key));
  21752. ExpectIntEQ(wc_InitRng(rng), 0);
  21753. ExpectIntEQ(wc_ecc_init(key), 0);
  21754. /* Test invalid parameters. */
  21755. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  21756. BAD_FUNC_ARG);
  21757. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  21758. BAD_FUNC_ARG);
  21759. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  21760. BAD_FUNC_ARG);
  21761. /* Test valid parameters. */
  21762. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  21763. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  21764. wc_ecc_free(key);
  21765. wc_FreeRng(rng);
  21766. #ifdef FP_ECC
  21767. wc_ecc_fp_free();
  21768. #endif
  21769. res = EXPECT_RESULT();
  21770. #endif
  21771. return res;
  21772. }
  21773. /*
  21774. * Testing wc_ecc_sm2_shared_secret()
  21775. */
  21776. static int test_wc_ecc_sm2_shared_secret(void)
  21777. {
  21778. int res = TEST_SKIPPED;
  21779. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  21780. EXPECT_DECLS;
  21781. WC_RNG rng[1];
  21782. ecc_key keyA[1];
  21783. ecc_key keyB[1];
  21784. byte outA[32];
  21785. byte outB[32];
  21786. word32 outALen = 32;
  21787. word32 outBLen = 32;
  21788. XMEMSET(rng, 0, sizeof(*rng));
  21789. XMEMSET(keyA, 0, sizeof(*keyA));
  21790. XMEMSET(keyB, 0, sizeof(*keyB));
  21791. ExpectIntEQ(wc_InitRng(rng), 0);
  21792. ExpectIntEQ(wc_ecc_init(keyA), 0);
  21793. ExpectIntEQ(wc_ecc_init(keyB), 0);
  21794. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  21795. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  21796. #ifdef ECC_TIMING_RESISTANT
  21797. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  21798. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  21799. #endif
  21800. /* Test invalid parameters. */
  21801. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  21802. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  21803. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  21804. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  21805. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  21806. BAD_FUNC_ARG);
  21807. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  21808. BAD_FUNC_ARG);
  21809. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  21810. BAD_FUNC_ARG);
  21811. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  21812. BAD_FUNC_ARG);
  21813. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  21814. /* Test valid parameters. */
  21815. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  21816. ExpectIntLE(outALen, 32);
  21817. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  21818. ExpectIntLE(outBLen, 32);
  21819. ExpectIntEQ(outALen, outBLen);
  21820. ExpectBufEQ(outA, outB, outALen);
  21821. wc_ecc_free(keyB);
  21822. wc_ecc_free(keyA);
  21823. wc_FreeRng(rng);
  21824. #ifdef FP_ECC
  21825. wc_ecc_fp_free();
  21826. #endif
  21827. res = EXPECT_RESULT();
  21828. #endif
  21829. return res;
  21830. }
  21831. /*
  21832. * Testing wc_ecc_sm2_create_digest()
  21833. */
  21834. static int test_wc_ecc_sm2_create_digest(void)
  21835. {
  21836. int res = TEST_SKIPPED;
  21837. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  21838. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  21839. EXPECT_DECLS;
  21840. ecc_key key[1];
  21841. enum wc_HashType hashType;
  21842. unsigned char pub[] = {
  21843. 0x04,
  21844. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  21845. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  21846. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  21847. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  21848. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  21849. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  21850. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  21851. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  21852. };
  21853. unsigned char id[] = {
  21854. 0x01, 0x02, 0x03,
  21855. };
  21856. unsigned char msg[] = {
  21857. 0x01, 0x02, 0x03,
  21858. };
  21859. unsigned char hash[32];
  21860. #ifdef WOLFSSL_SM3
  21861. unsigned char expHash[32] = {
  21862. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  21863. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  21864. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  21865. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  21866. };
  21867. #else
  21868. unsigned char expHash[32] = {
  21869. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  21870. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  21871. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  21872. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  21873. };
  21874. #endif
  21875. #ifdef WOLFSSL_SM3
  21876. hashType = WC_HASH_TYPE_SM3;
  21877. #else
  21878. hashType = WC_HASH_TYPE_SHA256;
  21879. #endif
  21880. XMEMSET(key, 0, sizeof(*key));
  21881. ExpectIntEQ(wc_ecc_init(key), 0);
  21882. /* Test with no curve set. */
  21883. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21884. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21885. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  21886. /* Test invalid parameters. */
  21887. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21888. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21889. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  21890. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21891. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  21892. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  21893. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21894. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  21895. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  21896. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  21897. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  21898. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21899. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  21900. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  21901. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21902. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  21903. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21904. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  21905. /* Bad hash type. */
  21906. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21907. -1, hash, 0, key), BAD_FUNC_ARG);
  21908. /* Bad hash size. */
  21909. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21910. hashType, hash, 0, key), BUFFER_E);
  21911. /* Test valid parameters. */
  21912. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  21913. hashType, hash, sizeof(hash), key), 0);
  21914. ExpectBufEQ(hash, expHash, sizeof(expHash));
  21915. wc_ecc_free(key);
  21916. res = EXPECT_RESULT();
  21917. #endif
  21918. return res;
  21919. }
  21920. /*
  21921. * Testing wc_ecc_sm2_verify_hash_ex()
  21922. */
  21923. static int test_wc_ecc_sm2_verify_hash_ex(void)
  21924. {
  21925. int res = TEST_SKIPPED;
  21926. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  21927. defined(WOLFSSL_PUBLIC_MP)
  21928. EXPECT_DECLS;
  21929. ecc_key key[1];
  21930. mp_int r[1];
  21931. mp_int s[1];
  21932. int verified;
  21933. unsigned char pub[] = {
  21934. 0x04,
  21935. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  21936. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  21937. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  21938. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  21939. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  21940. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  21941. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  21942. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  21943. };
  21944. unsigned char hash[] = {
  21945. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  21946. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  21947. 0x50, 0x69, 0x5B, 0x20
  21948. };
  21949. unsigned char rData[] = {
  21950. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  21951. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  21952. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  21953. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  21954. };
  21955. unsigned char sData[] = {
  21956. 0x1D,
  21957. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  21958. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  21959. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  21960. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  21961. };
  21962. unsigned char rBadData[] = {
  21963. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  21964. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  21965. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  21966. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  21967. };
  21968. XMEMSET(key, 0, sizeof(*key));
  21969. XMEMSET(r, 0, sizeof(*r));
  21970. XMEMSET(s, 0, sizeof(*s));
  21971. ExpectIntEQ(mp_init(r), 0);
  21972. ExpectIntEQ(mp_init(s), 0);
  21973. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  21974. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  21975. ExpectIntEQ(wc_ecc_init(key), 0);
  21976. /* Test with no curve set. */
  21977. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  21978. &verified, key), BAD_FUNC_ARG);
  21979. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  21980. /* Test invalid parameters. */
  21981. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  21982. NULL, NULL), BAD_FUNC_ARG);
  21983. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  21984. NULL, NULL), BAD_FUNC_ARG);
  21985. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  21986. NULL, NULL), BAD_FUNC_ARG);
  21987. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  21988. NULL, NULL), BAD_FUNC_ARG);
  21989. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  21990. &verified, NULL), BAD_FUNC_ARG);
  21991. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  21992. NULL, key), BAD_FUNC_ARG);
  21993. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  21994. &verified, key), BAD_FUNC_ARG);
  21995. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  21996. &verified, key), BAD_FUNC_ARG);
  21997. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  21998. &verified, key), BAD_FUNC_ARG);
  21999. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22000. NULL, key), BAD_FUNC_ARG);
  22001. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22002. &verified, NULL), BAD_FUNC_ARG);
  22003. /* Make key not on the SM2 curve. */
  22004. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22005. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22006. &verified, key), BAD_FUNC_ARG);
  22007. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22008. /* Test valid parameters. */
  22009. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22010. &verified, key), 0);
  22011. ExpectIntEQ(verified, 1);
  22012. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22013. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22014. &verified, key), 0);
  22015. ExpectIntEQ(verified, 0);
  22016. mp_free(s);
  22017. mp_free(r);
  22018. wc_ecc_free(key);
  22019. #ifdef FP_ECC
  22020. wc_ecc_fp_free();
  22021. #endif
  22022. res = EXPECT_RESULT();
  22023. #endif
  22024. return res;
  22025. }
  22026. /*
  22027. * Testing wc_ecc_sm2_verify_hash()
  22028. */
  22029. static int test_wc_ecc_sm2_verify_hash(void)
  22030. {
  22031. int res = TEST_SKIPPED;
  22032. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22033. EXPECT_DECLS;
  22034. ecc_key key[1];
  22035. int verified;
  22036. unsigned char pub[] = {
  22037. 0x04,
  22038. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22039. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22040. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22041. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22042. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22043. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22044. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22045. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22046. };
  22047. unsigned char hash[] = {
  22048. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22049. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22050. 0x50, 0x69, 0x5B, 0x20
  22051. };
  22052. unsigned char sig[] = {
  22053. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22054. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22055. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22056. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22057. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22058. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22059. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22060. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22061. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22062. };
  22063. unsigned char sigBad[] = {
  22064. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22065. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22066. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22067. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22068. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22069. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22070. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22071. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22072. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22073. };
  22074. XMEMSET(key, 0, sizeof(*key));
  22075. ExpectIntEQ(wc_ecc_init(key), 0);
  22076. /* Test with no curve set. */
  22077. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22078. &verified, key), BAD_FUNC_ARG);
  22079. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22080. /* Test invalid parameters. */
  22081. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22082. NULL, NULL), BAD_FUNC_ARG);
  22083. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22084. NULL, NULL), BAD_FUNC_ARG);
  22085. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22086. NULL, NULL), BAD_FUNC_ARG);
  22087. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22088. &verified, NULL), BAD_FUNC_ARG);
  22089. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22090. NULL, key), BAD_FUNC_ARG);
  22091. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22092. &verified, key), BAD_FUNC_ARG);
  22093. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22094. &verified, key), BAD_FUNC_ARG);
  22095. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22096. NULL, key), BAD_FUNC_ARG);
  22097. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22098. &verified, NULL), BAD_FUNC_ARG);
  22099. /* Make key not on the SM2 curve. */
  22100. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22101. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22102. &verified, key), BAD_FUNC_ARG);
  22103. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22104. /* Test valid parameters. */
  22105. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22106. &verified, key), 0);
  22107. ExpectIntEQ(verified, 1);
  22108. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22109. sizeof(hash), &verified, key), 0);
  22110. ExpectIntEQ(verified, 0);
  22111. wc_ecc_free(key);
  22112. #ifdef FP_ECC
  22113. wc_ecc_fp_free();
  22114. #endif
  22115. res = EXPECT_RESULT();
  22116. #endif
  22117. return res;
  22118. }
  22119. /*
  22120. * Testing wc_ecc_sm2_verify_hash_ex()
  22121. */
  22122. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22123. {
  22124. int res = TEST_SKIPPED;
  22125. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22126. defined(WOLFSSL_PUBLIC_MP)
  22127. EXPECT_DECLS;
  22128. WC_RNG rng[1];
  22129. ecc_key key[1];
  22130. mp_int r[1];
  22131. mp_int s[1];
  22132. unsigned char hash[32];
  22133. #ifdef HAVE_ECC_VERIFY
  22134. int verified;
  22135. #endif
  22136. XMEMSET(rng, 0, sizeof(*rng));
  22137. XMEMSET(key, 0, sizeof(*key));
  22138. XMEMSET(r, 0, sizeof(*r));
  22139. XMEMSET(s, 0, sizeof(*s));
  22140. ExpectIntEQ(wc_InitRng(rng), 0);
  22141. ExpectIntEQ(mp_init(r), 0);
  22142. ExpectIntEQ(mp_init(s), 0);
  22143. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22144. ExpectIntEQ(wc_ecc_init(key), 0);
  22145. /* Test with no curve set. */
  22146. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22147. BAD_FUNC_ARG);
  22148. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22149. /* Test invalid parameters. */
  22150. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22151. NULL), BAD_FUNC_ARG);
  22152. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  22153. NULL), BAD_FUNC_ARG);
  22154. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  22155. NULL), BAD_FUNC_ARG);
  22156. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  22157. NULL), BAD_FUNC_ARG);
  22158. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  22159. NULL), BAD_FUNC_ARG);
  22160. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  22161. s), BAD_FUNC_ARG);
  22162. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  22163. BAD_FUNC_ARG);
  22164. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  22165. BAD_FUNC_ARG);
  22166. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  22167. BAD_FUNC_ARG);
  22168. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  22169. BAD_FUNC_ARG);
  22170. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  22171. BAD_FUNC_ARG);
  22172. /* Make key not on the SM2 curve. */
  22173. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22174. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22175. BAD_FUNC_ARG);
  22176. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22177. #ifdef WOLFSSL_SP_MATH_ALL
  22178. {
  22179. mp_int smallR[1];
  22180. sp_init_size(smallR, 1);
  22181. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  22182. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  22183. smallR, s), 0);
  22184. }
  22185. #endif
  22186. /* Test valid parameters. */
  22187. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  22188. 0);
  22189. #ifdef HAVE_ECC_VERIFY
  22190. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  22191. key), 0);
  22192. ExpectIntEQ(verified, 1);
  22193. #endif
  22194. mp_free(s);
  22195. mp_free(r);
  22196. wc_ecc_free(key);
  22197. wc_FreeRng(rng);
  22198. #ifdef FP_ECC
  22199. wc_ecc_fp_free();
  22200. #endif
  22201. res = EXPECT_RESULT();
  22202. #endif
  22203. return res;
  22204. }
  22205. /*
  22206. * Testing wc_ecc_sm2_verify_hash()
  22207. */
  22208. static int test_wc_ecc_sm2_sign_hash(void)
  22209. {
  22210. int res = TEST_SKIPPED;
  22211. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  22212. EXPECT_DECLS;
  22213. WC_RNG rng[1];
  22214. ecc_key key[1];
  22215. unsigned char hash[32];
  22216. unsigned char sig[72];
  22217. word32 sigSz = sizeof(sig);
  22218. #ifdef HAVE_ECC_VERIFY
  22219. int verified;
  22220. #endif
  22221. XMEMSET(rng, 0, sizeof(*rng));
  22222. XMEMSET(key, 0, sizeof(*key));
  22223. ExpectIntEQ(wc_InitRng(rng), 0);
  22224. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  22225. ExpectIntEQ(wc_ecc_init(key), 0);
  22226. /* Test with no curve set. */
  22227. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22228. BAD_FUNC_ARG);
  22229. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22230. /* Test invalid parameters. */
  22231. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22232. NULL), BAD_FUNC_ARG);
  22233. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  22234. NULL), BAD_FUNC_ARG);
  22235. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  22236. NULL), BAD_FUNC_ARG);
  22237. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  22238. NULL), BAD_FUNC_ARG);
  22239. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  22240. NULL), BAD_FUNC_ARG);
  22241. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  22242. key), BAD_FUNC_ARG);
  22243. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  22244. key), BAD_FUNC_ARG);
  22245. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  22246. key), BAD_FUNC_ARG);
  22247. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  22248. key), BAD_FUNC_ARG);
  22249. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  22250. key), BAD_FUNC_ARG);
  22251. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  22252. NULL), BAD_FUNC_ARG);
  22253. /* Make key not on the SM2 curve. */
  22254. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22255. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22256. BAD_FUNC_ARG);
  22257. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22258. /* Test valid parameters. */
  22259. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  22260. 0);
  22261. #ifdef HAVE_ECC_VERIFY
  22262. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  22263. &verified, key), 0);
  22264. ExpectIntEQ(verified, 1);
  22265. #endif
  22266. wc_ecc_free(key);
  22267. wc_FreeRng(rng);
  22268. #ifdef FP_ECC
  22269. wc_ecc_fp_free();
  22270. #endif
  22271. res = EXPECT_RESULT();
  22272. #endif
  22273. return res;
  22274. }
  22275. /*
  22276. * Testing ToTraditional
  22277. */
  22278. static int test_ToTraditional(void)
  22279. {
  22280. EXPECT_DECLS;
  22281. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22282. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22283. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  22284. XFILE f = XBADFILE;
  22285. byte input[TWOK_BUF];
  22286. word32 sz;
  22287. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  22288. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  22289. if (f != XBADFILE)
  22290. XFCLOSE(f);
  22291. /* Good case */
  22292. ExpectIntGT(ToTraditional(input, sz), 0);
  22293. /* Bad cases */
  22294. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  22295. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  22296. #ifdef WOLFSSL_ASN_TEMPLATE
  22297. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  22298. #else
  22299. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  22300. #endif
  22301. #endif
  22302. return EXPECT_RESULT();
  22303. } /* End test_ToTraditional*/
  22304. /*
  22305. * Testing wc_EccPrivateKeyToDer
  22306. */
  22307. static int test_wc_EccPrivateKeyToDer(void)
  22308. {
  22309. EXPECT_DECLS;
  22310. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22311. byte output[ONEK_BUF];
  22312. ecc_key eccKey;
  22313. WC_RNG rng;
  22314. word32 inLen;
  22315. word32 outLen = 0;
  22316. int ret;
  22317. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22318. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22319. ExpectIntEQ(wc_InitRng(&rng), 0);
  22320. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22321. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22322. #if defined(WOLFSSL_ASYNC_CRYPT)
  22323. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22324. #endif
  22325. ExpectIntEQ(ret, 0);
  22326. inLen = (word32)sizeof(output);
  22327. /* Bad Cases */
  22328. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22329. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22330. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  22331. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  22332. /* Good Case */
  22333. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  22334. wc_ecc_free(&eccKey);
  22335. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22336. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  22337. {
  22338. /* test importing private only into a PKEY struct */
  22339. EC_KEY* ec = NULL;
  22340. EVP_PKEY* pkey = NULL;
  22341. const unsigned char* der;
  22342. der = output;
  22343. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  22344. der = output;
  22345. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  22346. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  22347. if (EXPECT_FAIL()) {
  22348. EC_KEY_free(ec);
  22349. }
  22350. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  22351. }
  22352. #endif
  22353. #endif
  22354. return EXPECT_RESULT();
  22355. } /* End test_wc_EccPrivateKeyToDer*/
  22356. /*
  22357. * Testing wc_DhPublicKeyDecode
  22358. */
  22359. static int test_wc_DhPublicKeyDecode(void)
  22360. {
  22361. EXPECT_DECLS;
  22362. #ifndef NO_DH
  22363. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  22364. DhKey key;
  22365. word32 inOutIdx;
  22366. XMEMSET(&key, 0, sizeof(DhKey));
  22367. ExpectIntEQ(wc_InitDhKey(&key), 0);
  22368. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  22369. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22370. BAD_FUNC_ARG);
  22371. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  22372. BAD_FUNC_ARG);
  22373. inOutIdx = 0;
  22374. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  22375. BAD_FUNC_ARG);
  22376. inOutIdx = 0;
  22377. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  22378. BAD_FUNC_ARG);
  22379. inOutIdx = 0;
  22380. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  22381. sizeof_dh_pub_key_der_2048), 0);
  22382. ExpectIntNE(key.p.used, 0);
  22383. ExpectIntNE(key.g.used, 0);
  22384. ExpectIntEQ(key.q.used, 0);
  22385. ExpectIntNE(key.pub.used, 0);
  22386. ExpectIntEQ(key.priv.used, 0);
  22387. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  22388. #endif
  22389. #endif /* !NO_DH */
  22390. return EXPECT_RESULT();
  22391. }
  22392. /*
  22393. * Testing wc_Ed25519KeyToDer
  22394. */
  22395. static int test_wc_Ed25519KeyToDer(void)
  22396. {
  22397. EXPECT_DECLS;
  22398. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22399. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22400. byte output[ONEK_BUF];
  22401. ed25519_key ed25519Key;
  22402. WC_RNG rng;
  22403. word32 inLen;
  22404. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22405. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22406. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22407. ExpectIntEQ(wc_InitRng(&rng), 0);
  22408. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22409. inLen = (word32)sizeof(output);
  22410. /* Bad Cases */
  22411. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22412. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22413. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  22414. /* Good Cases */
  22415. /* length only */
  22416. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  22417. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  22418. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22419. wc_ed25519_free(&ed25519Key);
  22420. #endif
  22421. return EXPECT_RESULT();
  22422. } /* End test_wc_Ed25519KeyToDer*/
  22423. /*
  22424. * Testing wc_Ed25519PrivateKeyToDer
  22425. */
  22426. static int test_wc_Ed25519PrivateKeyToDer(void)
  22427. {
  22428. EXPECT_DECLS;
  22429. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  22430. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22431. byte output[ONEK_BUF];
  22432. ed25519_key ed25519PrivKey;
  22433. WC_RNG rng;
  22434. word32 inLen;
  22435. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  22436. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22437. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  22438. ExpectIntEQ(wc_InitRng(&rng), 0);
  22439. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  22440. 0);
  22441. inLen = (word32)sizeof(output);
  22442. /* Bad Cases */
  22443. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22444. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22445. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  22446. BAD_FUNC_ARG);
  22447. /* Good Cases */
  22448. /* length only */
  22449. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  22450. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  22451. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22452. wc_ed25519_free(&ed25519PrivKey);
  22453. #endif
  22454. return EXPECT_RESULT();
  22455. } /* End test_wc_Ed25519PrivateKeyToDer*/
  22456. /*
  22457. * Testing wc_Ed448KeyToDer
  22458. */
  22459. static int test_wc_Ed448KeyToDer(void)
  22460. {
  22461. EXPECT_DECLS;
  22462. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22463. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22464. byte output[ONEK_BUF];
  22465. ed448_key ed448Key;
  22466. WC_RNG rng;
  22467. word32 inLen;
  22468. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22469. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22470. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22471. ExpectIntEQ(wc_InitRng(&rng), 0);
  22472. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22473. inLen = (word32)sizeof(output);
  22474. /* Bad Cases */
  22475. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22476. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22477. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  22478. /* Good Cases */
  22479. /* length only */
  22480. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  22481. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  22482. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22483. wc_ed448_free(&ed448Key);
  22484. #endif
  22485. return EXPECT_RESULT();
  22486. } /* End test_wc_Ed448KeyToDer*/
  22487. /*
  22488. * Testing wc_Ed448PrivateKeyToDer
  22489. */
  22490. static int test_wc_Ed448PrivateKeyToDer(void)
  22491. {
  22492. EXPECT_DECLS;
  22493. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  22494. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  22495. byte output[ONEK_BUF];
  22496. ed448_key ed448PrivKey;
  22497. WC_RNG rng;
  22498. word32 inLen;
  22499. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  22500. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22501. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  22502. ExpectIntEQ(wc_InitRng(&rng), 0);
  22503. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  22504. 0);
  22505. inLen = (word32)sizeof(output);
  22506. /* Bad Cases */
  22507. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  22508. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  22509. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  22510. BAD_FUNC_ARG);
  22511. /* Good cases */
  22512. /* length only */
  22513. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  22514. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  22515. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22516. wc_ed448_free(&ed448PrivKey);
  22517. #endif
  22518. return EXPECT_RESULT();
  22519. } /* End test_wc_Ed448PrivateKeyToDer*/
  22520. /*
  22521. * Testing wc_SetSubjectBuffer
  22522. */
  22523. static int test_wc_SetSubjectBuffer(void)
  22524. {
  22525. EXPECT_DECLS;
  22526. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  22527. Cert cert;
  22528. XFILE file = XBADFILE;
  22529. byte* der = NULL;
  22530. word32 derSz;
  22531. derSz = FOURK_BUF;
  22532. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22533. DYNAMIC_TYPE_TMP_BUFFER));
  22534. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  22535. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  22536. if (file != XBADFILE)
  22537. XFCLOSE(file);
  22538. ExpectIntEQ(wc_InitCert(&cert), 0);
  22539. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  22540. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  22541. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22542. #endif
  22543. return EXPECT_RESULT();
  22544. } /* End test_wc_SetSubjectBuffer*/
  22545. /*
  22546. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  22547. */
  22548. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  22549. {
  22550. EXPECT_DECLS;
  22551. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22552. WC_RNG rng;
  22553. Cert cert;
  22554. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22555. RsaKey rsaKey;
  22556. int bits = 2048;
  22557. #endif
  22558. #if defined(HAVE_ECC)
  22559. ecc_key eccKey;
  22560. int ret;
  22561. #endif
  22562. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22563. ed25519_key ed25519Key;
  22564. #endif
  22565. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22566. ed448_key ed448Key;
  22567. #endif
  22568. #ifndef HAVE_FIPS
  22569. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22570. #else
  22571. ExpectIntEQ(wc_InitRng(&rng), 0);
  22572. #endif
  22573. ExpectIntEQ(wc_InitCert(&cert), 0);
  22574. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22575. /* RSA */
  22576. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22577. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22578. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22579. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  22580. 0);
  22581. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22582. #endif
  22583. #if defined(HAVE_ECC)
  22584. /* ECC */
  22585. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22586. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22587. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22588. #if defined(WOLFSSL_ASYNC_CRYPT)
  22589. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22590. #endif
  22591. ExpectIntEQ(ret, 0);
  22592. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  22593. 0);
  22594. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22595. #endif
  22596. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22597. /* ED25519 */
  22598. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22599. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22600. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22601. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22602. &ed25519Key), 0);
  22603. wc_ed25519_free(&ed25519Key);
  22604. #endif
  22605. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22606. /* ED448 */
  22607. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22608. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22609. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22610. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  22611. &ed448Key), 0);
  22612. wc_ed448_free(&ed448Key);
  22613. #endif
  22614. wc_FreeRng(&rng);
  22615. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22616. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  22617. return EXPECT_RESULT();
  22618. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  22619. /*
  22620. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  22621. */
  22622. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  22623. {
  22624. EXPECT_DECLS;
  22625. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  22626. WC_RNG rng;
  22627. Cert cert;
  22628. #if !defined(NO_RSA) && defined(HAVE_RSA)
  22629. RsaKey rsaKey;
  22630. int bits = 2048;
  22631. #endif
  22632. #if defined(HAVE_ECC)
  22633. ecc_key eccKey;
  22634. int ret;
  22635. #endif
  22636. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22637. ed25519_key ed25519Key;
  22638. #endif
  22639. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22640. ed448_key ed448Key;
  22641. #endif
  22642. #ifndef HAVE_FIPS
  22643. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  22644. #else
  22645. ExpectIntEQ(wc_InitRng(&rng), 0);
  22646. #endif
  22647. ExpectIntEQ(wc_InitCert(&cert), 0);
  22648. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  22649. /* RSA */
  22650. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  22651. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  22652. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  22653. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  22654. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  22655. #endif
  22656. #if defined(HAVE_ECC)
  22657. /* ECC */
  22658. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  22659. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  22660. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22661. #if defined(WOLFSSL_ASYNC_CRYPT)
  22662. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22663. #endif
  22664. ExpectIntEQ(ret, 0);
  22665. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  22666. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  22667. #endif
  22668. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  22669. /* ED25519 */
  22670. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  22671. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  22672. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  22673. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  22674. &ed25519Key), 0);
  22675. wc_ed25519_free(&ed25519Key);
  22676. #endif
  22677. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  22678. /* ED448 */
  22679. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  22680. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  22681. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  22682. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  22683. 0);
  22684. wc_ed448_free(&ed448Key);
  22685. #endif
  22686. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22687. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  22688. return EXPECT_RESULT();
  22689. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  22690. /*
  22691. * Testing wc_PKCS7_New()
  22692. */
  22693. static int test_wc_PKCS7_New(void)
  22694. {
  22695. EXPECT_DECLS;
  22696. #if defined(HAVE_PKCS7)
  22697. PKCS7* pkcs7 = NULL;
  22698. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  22699. wc_PKCS7_Free(pkcs7);
  22700. #endif
  22701. return EXPECT_RESULT();
  22702. } /* END test-wc_PKCS7_New */
  22703. /*
  22704. * Testing wc_PKCS7_Init()
  22705. */
  22706. static int test_wc_PKCS7_Init(void)
  22707. {
  22708. EXPECT_DECLS;
  22709. #if defined(HAVE_PKCS7)
  22710. PKCS7* pkcs7 = NULL;
  22711. void* heap = NULL;
  22712. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  22713. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  22714. /* Pass in bad args. */
  22715. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  22716. wc_PKCS7_Free(pkcs7);
  22717. #endif
  22718. return EXPECT_RESULT();
  22719. } /* END test-wc_PKCS7_Init */
  22720. /*
  22721. * Testing wc_PKCS7_InitWithCert()
  22722. */
  22723. static int test_wc_PKCS7_InitWithCert(void)
  22724. {
  22725. EXPECT_DECLS;
  22726. #if defined(HAVE_PKCS7)
  22727. PKCS7* pkcs7 = NULL;
  22728. #ifndef NO_RSA
  22729. #if defined(USE_CERT_BUFFERS_2048)
  22730. unsigned char cert[sizeof(client_cert_der_2048)];
  22731. int certSz = (int)sizeof(cert);
  22732. XMEMSET(cert, 0, certSz);
  22733. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  22734. #elif defined(USE_CERT_BUFFERS_1024)
  22735. unsigned char cert[sizeof(client_cert_der_1024)];
  22736. int certSz = (int)sizeof(cert);
  22737. XMEMSET(cert, 0, certSz);
  22738. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  22739. #else
  22740. unsigned char cert[ONEK_BUF];
  22741. XFILE fp = XBADFILE;
  22742. int certSz;
  22743. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22744. XBADFILE);
  22745. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22746. fp), 0);
  22747. if (fp != XBADFILE)
  22748. XFCLOSE(fp);
  22749. #endif
  22750. #elif defined(HAVE_ECC)
  22751. #if defined(USE_CERT_BUFFERS_256)
  22752. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22753. int certSz = (int)sizeof(cert);
  22754. XMEMSET(cert, 0, certSz);
  22755. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  22756. #else
  22757. unsigned char cert[ONEK_BUF];
  22758. XFILE fp = XBADFILE;
  22759. int certSz;
  22760. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22761. XBADFILE);
  22762. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  22763. fp), 0);
  22764. if (fp != XBADFILE)
  22765. XFCLOSE(fp);
  22766. #endif
  22767. #else
  22768. #error PKCS7 requires ECC or RSA
  22769. #endif
  22770. #ifdef HAVE_ECC
  22771. {
  22772. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  22773. static unsigned char certWithInvalidEccKey[] = {
  22774. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  22775. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  22776. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  22777. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  22778. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  22779. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  22780. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  22781. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  22782. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  22783. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  22784. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  22785. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  22786. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  22787. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  22788. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  22789. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  22790. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  22791. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  22792. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  22793. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  22794. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  22795. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  22796. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  22797. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  22798. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  22799. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  22800. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  22801. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  22802. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  22803. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  22804. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  22805. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  22806. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  22807. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  22808. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  22809. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  22810. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  22811. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  22812. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  22813. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  22814. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  22815. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  22816. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  22817. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  22818. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  22819. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  22820. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  22821. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  22822. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  22823. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  22824. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  22825. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  22826. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  22827. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  22828. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  22829. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  22830. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  22831. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  22832. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  22833. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  22834. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  22835. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  22836. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  22837. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  22838. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  22839. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  22840. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  22841. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  22842. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  22843. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  22844. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  22845. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  22846. 0x08, 0xA8, 0xB4
  22847. };
  22848. #endif
  22849. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22850. /* If initialization is not successful, it's free'd in init func. */
  22851. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  22852. 0);
  22853. wc_PKCS7_Free(pkcs7);
  22854. pkcs7 = NULL;
  22855. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22856. /* Valid initialization usage. */
  22857. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22858. /* Pass in bad args. No need free for null checks, free at end.*/
  22859. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  22860. BAD_FUNC_ARG);
  22861. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  22862. BAD_FUNC_ARG);
  22863. #ifdef HAVE_ECC
  22864. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  22865. sizeof(certWithInvalidEccKey)), 0);
  22866. }
  22867. #endif
  22868. wc_PKCS7_Free(pkcs7);
  22869. #endif
  22870. return EXPECT_RESULT();
  22871. } /* END test_wc_PKCS7_InitWithCert */
  22872. /*
  22873. * Testing wc_PKCS7_EncodeData()
  22874. */
  22875. static int test_wc_PKCS7_EncodeData(void)
  22876. {
  22877. EXPECT_DECLS;
  22878. #if defined(HAVE_PKCS7)
  22879. PKCS7* pkcs7 = NULL;
  22880. byte output[FOURK_BUF];
  22881. byte data[] = "My encoded DER cert.";
  22882. #ifndef NO_RSA
  22883. #if defined(USE_CERT_BUFFERS_2048)
  22884. unsigned char cert[sizeof(client_cert_der_2048)];
  22885. unsigned char key[sizeof(client_key_der_2048)];
  22886. int certSz = (int)sizeof(cert);
  22887. int keySz = (int)sizeof(key);
  22888. XMEMSET(cert, 0, certSz);
  22889. XMEMSET(key, 0, keySz);
  22890. XMEMCPY(cert, client_cert_der_2048, certSz);
  22891. XMEMCPY(key, client_key_der_2048, keySz);
  22892. #elif defined(USE_CERT_BUFFERS_1024)
  22893. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  22894. unsigned char key[sizeof_client_key_der_1024];
  22895. int certSz = (int)sizeof(cert);
  22896. int keySz = (int)sizeof(key);
  22897. XMEMSET(cert, 0, certSz);
  22898. XMEMSET(key, 0, keySz);
  22899. XMEMCPY(cert, client_cert_der_1024, certSz);
  22900. XMEMCPY(key, client_key_der_1024, keySz);
  22901. #else
  22902. unsigned char cert[ONEK_BUF];
  22903. unsigned char key[ONEK_BUF];
  22904. XFILE fp = XBADFILE;
  22905. int certSz;
  22906. int keySz;
  22907. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  22908. XBADFILE);
  22909. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  22910. fp), 0);
  22911. if (fp != XBADFILE) {
  22912. XFCLOSE(fp);
  22913. fp = XBADFILE;
  22914. }
  22915. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  22916. XBADFILE);
  22917. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  22918. 0);
  22919. if (fp != XBADFILE)
  22920. XFCLOSE(fp);
  22921. #endif
  22922. #elif defined(HAVE_ECC)
  22923. #if defined(USE_CERT_BUFFERS_256)
  22924. unsigned char cert[sizeof(cliecc_cert_der_256)];
  22925. unsigned char key[sizeof(ecc_clikey_der_256)];
  22926. int certSz = (int)sizeof(cert);
  22927. int keySz = (int)sizeof(key);
  22928. XMEMSET(cert, 0, certSz);
  22929. XMEMSET(key, 0, keySz);
  22930. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  22931. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  22932. #else
  22933. unsigned char cert[ONEK_BUF];
  22934. unsigned char key[ONEK_BUF];
  22935. XFILE fp = XBADFILE;
  22936. int certSz, keySz;
  22937. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  22938. XBADFILE);
  22939. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  22940. fp), 0);
  22941. if (fp != XBADFILE) {
  22942. XFCLOSE(fp);
  22943. fp = XBADFILE;
  22944. }
  22945. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  22946. XBADFILE);
  22947. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  22948. 0);
  22949. if (fp != XBADFILE)
  22950. XFCLOSE(fp);
  22951. #endif
  22952. #endif
  22953. XMEMSET(output, 0, sizeof(output));
  22954. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22955. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22956. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  22957. if (pkcs7 != NULL) {
  22958. pkcs7->content = data;
  22959. pkcs7->contentSz = sizeof(data);
  22960. pkcs7->privateKey = key;
  22961. pkcs7->privateKeySz = keySz;
  22962. }
  22963. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  22964. /* Test bad args. */
  22965. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  22966. BAD_FUNC_ARG);
  22967. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  22968. BAD_FUNC_ARG);
  22969. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  22970. wc_PKCS7_Free(pkcs7);
  22971. #endif
  22972. return EXPECT_RESULT();
  22973. } /* END test_wc_PKCS7_EncodeData */
  22974. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  22975. !defined(NO_RSA) && !defined(NO_SHA256)
  22976. /* RSA sign raw digest callback */
  22977. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  22978. byte* out, word32 outSz, byte* privateKey,
  22979. word32 privateKeySz, int devid, int hashOID)
  22980. {
  22981. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  22982. byte digInfoEncoding[] = {
  22983. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  22984. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  22985. 0x00, 0x04, 0x20
  22986. };
  22987. int ret;
  22988. byte digestInfo[ONEK_BUF];
  22989. byte sig[FOURK_BUF];
  22990. word32 digestInfoSz = 0;
  22991. word32 idx = 0;
  22992. RsaKey rsa;
  22993. /* SHA-256 required only for this example callback due to above
  22994. * digInfoEncoding[] */
  22995. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  22996. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  22997. (hashOID != SHA256h)) {
  22998. return -1;
  22999. }
  23000. /* build DigestInfo */
  23001. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23002. digestInfoSz += sizeof(digInfoEncoding);
  23003. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23004. digestInfoSz += digestSz;
  23005. /* set up RSA key */
  23006. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23007. if (ret != 0) {
  23008. return ret;
  23009. }
  23010. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23011. /* sign DigestInfo */
  23012. if (ret == 0) {
  23013. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23014. &rsa, pkcs7->rng);
  23015. if (ret > 0) {
  23016. if (ret > (int)outSz) {
  23017. /* output buffer too small */
  23018. ret = -1;
  23019. }
  23020. else {
  23021. /* success, ret holds sig size */
  23022. XMEMCPY(out, sig, ret);
  23023. }
  23024. }
  23025. }
  23026. wc_FreeRsaKey(&rsa);
  23027. return ret;
  23028. }
  23029. #endif
  23030. /*
  23031. * Testing wc_PKCS7_EncodeSignedData()
  23032. */
  23033. static int test_wc_PKCS7_EncodeSignedData(void)
  23034. {
  23035. EXPECT_DECLS;
  23036. #if defined(HAVE_PKCS7)
  23037. PKCS7* pkcs7 = NULL;
  23038. WC_RNG rng;
  23039. byte output[FOURK_BUF];
  23040. byte badOut[1];
  23041. word32 outputSz = (word32)sizeof(output);
  23042. word32 badOutSz = 0;
  23043. byte data[] = "Test data to encode.";
  23044. #ifndef NO_RSA
  23045. #if defined(USE_CERT_BUFFERS_2048)
  23046. byte key[sizeof(client_key_der_2048)];
  23047. byte cert[sizeof(client_cert_der_2048)];
  23048. word32 keySz = (word32)sizeof(key);
  23049. word32 certSz = (word32)sizeof(cert);
  23050. XMEMSET(key, 0, keySz);
  23051. XMEMSET(cert, 0, certSz);
  23052. XMEMCPY(key, client_key_der_2048, keySz);
  23053. XMEMCPY(cert, client_cert_der_2048, certSz);
  23054. #elif defined(USE_CERT_BUFFERS_1024)
  23055. byte key[sizeof_client_key_der_1024];
  23056. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23057. word32 keySz = (word32)sizeof(key);
  23058. word32 certSz = (word32)sizeof(cert);
  23059. XMEMSET(key, 0, keySz);
  23060. XMEMSET(cert, 0, certSz);
  23061. XMEMCPY(key, client_key_der_1024, keySz);
  23062. XMEMCPY(cert, client_cert_der_1024, certSz);
  23063. #else
  23064. unsigned char cert[ONEK_BUF];
  23065. unsigned char key[ONEK_BUF];
  23066. XFILE fp = XBADFILE;
  23067. int certSz;
  23068. int keySz;
  23069. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23070. XBADFILE);
  23071. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23072. fp), 0);
  23073. if (fp != XBADFILE) {
  23074. XFCLOSE(fp);
  23075. fp = XBADFILE;
  23076. }
  23077. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23078. XBADFILE);
  23079. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23080. 0);
  23081. if (fp != XBADFILE)
  23082. XFCLOSE(fp);
  23083. #endif
  23084. #elif defined(HAVE_ECC)
  23085. #if defined(USE_CERT_BUFFERS_256)
  23086. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23087. unsigned char key[sizeof(ecc_clikey_der_256)];
  23088. int certSz = (int)sizeof(cert);
  23089. int keySz = (int)sizeof(key);
  23090. XMEMSET(cert, 0, certSz);
  23091. XMEMSET(key, 0, keySz);
  23092. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23093. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23094. #else
  23095. unsigned char cert[ONEK_BUF];
  23096. unsigned char key[ONEK_BUF];
  23097. XFILE fp = XBADFILE;
  23098. int certSz;
  23099. int keySz;
  23100. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23101. XBADFILE);
  23102. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23103. if (fp != XBADFILE) {
  23104. XFCLOSE(fp);
  23105. fp = XBADFILE;
  23106. }
  23107. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23108. XBADFILE);
  23109. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23110. if (fp != XBADFILE)
  23111. XFCLOSE(fp);
  23112. #endif
  23113. #endif
  23114. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23115. XMEMSET(output, 0, outputSz);
  23116. ExpectIntEQ(wc_InitRng(&rng), 0);
  23117. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23118. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23119. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23120. if (pkcs7 != NULL) {
  23121. pkcs7->content = data;
  23122. pkcs7->contentSz = (word32)sizeof(data);
  23123. pkcs7->privateKey = key;
  23124. pkcs7->privateKeySz = (word32)sizeof(key);
  23125. pkcs7->encryptOID = RSAk;
  23126. #ifdef NO_SHA
  23127. pkcs7->hashOID = SHA256h;
  23128. #else
  23129. pkcs7->hashOID = SHAh;
  23130. #endif
  23131. pkcs7->rng = &rng;
  23132. }
  23133. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23134. wc_PKCS7_Free(pkcs7);
  23135. pkcs7 = NULL;
  23136. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23137. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23138. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23139. /* Pass in bad args. */
  23140. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  23141. BAD_FUNC_ARG);
  23142. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23143. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23144. badOutSz), BAD_FUNC_ARG);
  23145. if (pkcs7 != NULL) {
  23146. pkcs7->hashOID = 0; /* bad hashOID */
  23147. }
  23148. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  23149. BAD_FUNC_ARG);
  23150. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23151. !defined(NO_RSA) && !defined(NO_SHA256)
  23152. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23153. * Example callback assumes SHA-256, so only run test if compiled in. */
  23154. wc_PKCS7_Free(pkcs7);
  23155. pkcs7 = NULL;
  23156. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23157. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23158. if (pkcs7 != NULL) {
  23159. pkcs7->content = data;
  23160. pkcs7->contentSz = (word32)sizeof(data);
  23161. pkcs7->privateKey = key;
  23162. pkcs7->privateKeySz = (word32)sizeof(key);
  23163. pkcs7->encryptOID = RSAk;
  23164. pkcs7->hashOID = SHA256h;
  23165. pkcs7->rng = &rng;
  23166. }
  23167. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23168. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23169. #endif
  23170. wc_PKCS7_Free(pkcs7);
  23171. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23172. #endif
  23173. return EXPECT_RESULT();
  23174. } /* END test_wc_PKCS7_EncodeSignedData */
  23175. /*
  23176. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23177. */
  23178. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23179. {
  23180. EXPECT_DECLS;
  23181. #if defined(HAVE_PKCS7)
  23182. int i;
  23183. PKCS7* pkcs7 = NULL;
  23184. WC_RNG rng;
  23185. byte outputHead[FOURK_BUF/2];
  23186. byte outputFoot[FOURK_BUF/2];
  23187. word32 outputHeadSz = (word32)sizeof(outputHead);
  23188. word32 outputFootSz = (word32)sizeof(outputFoot);
  23189. byte data[FOURK_BUF];
  23190. wc_HashAlg hash;
  23191. #ifdef NO_SHA
  23192. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23193. #else
  23194. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23195. #endif
  23196. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23197. word32 hashSz = wc_HashGetDigestSize(hashType);
  23198. #ifndef NO_RSA
  23199. #if defined(USE_CERT_BUFFERS_2048)
  23200. byte key[sizeof(client_key_der_2048)];
  23201. byte cert[sizeof(client_cert_der_2048)];
  23202. word32 keySz = (word32)sizeof(key);
  23203. word32 certSz = (word32)sizeof(cert);
  23204. XMEMSET(key, 0, keySz);
  23205. XMEMSET(cert, 0, certSz);
  23206. XMEMCPY(key, client_key_der_2048, keySz);
  23207. XMEMCPY(cert, client_cert_der_2048, certSz);
  23208. #elif defined(USE_CERT_BUFFERS_1024)
  23209. byte key[sizeof_client_key_der_1024];
  23210. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23211. word32 keySz = (word32)sizeof(key);
  23212. word32 certSz = (word32)sizeof(cert);
  23213. XMEMSET(key, 0, keySz);
  23214. XMEMSET(cert, 0, certSz);
  23215. XMEMCPY(key, client_key_der_1024, keySz);
  23216. XMEMCPY(cert, client_cert_der_1024, certSz);
  23217. #else
  23218. unsigned char cert[ONEK_BUF];
  23219. unsigned char key[ONEK_BUF];
  23220. XFILE fp = XBADFILE;
  23221. int certSz;
  23222. int keySz;
  23223. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23224. XBADFILE);
  23225. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23226. fp), 0);
  23227. if (fp != XBADFILE) {
  23228. XFCLOSE(fp);
  23229. fp = XBADFILE;
  23230. }
  23231. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23232. XBADFILE);
  23233. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23234. 0);
  23235. if (fp != XBADFILE)
  23236. XFCLOSE(fp);
  23237. #endif
  23238. #elif defined(HAVE_ECC)
  23239. #if defined(USE_CERT_BUFFERS_256)
  23240. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23241. unsigned char key[sizeof(ecc_clikey_der_256)];
  23242. int certSz = (int)sizeof(cert);
  23243. int keySz = (int)sizeof(key);
  23244. XMEMSET(cert, 0, certSz);
  23245. XMEMSET(key, 0, keySz);
  23246. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23247. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23248. #else
  23249. unsigned char cert[ONEK_BUF];
  23250. unsigned char key[ONEK_BUF];
  23251. XFILE fp = XBADFILE;
  23252. int certSz;
  23253. int keySz;
  23254. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23255. XBADFILE);
  23256. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23257. fp), 0);
  23258. if (fp != XBADFILE) {
  23259. XFCLOSE(fp);
  23260. fp = XBADFILE;
  23261. }
  23262. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23263. XBADFILE);
  23264. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23265. 0);
  23266. if (fp != XBADFILE)
  23267. XFCLOSE(fp);
  23268. #endif
  23269. #endif
  23270. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23271. /* initialize large data with sequence */
  23272. for (i=0; i<(int)sizeof(data); i++)
  23273. data[i] = i & 0xff;
  23274. XMEMSET(outputHead, 0, outputHeadSz);
  23275. XMEMSET(outputFoot, 0, outputFootSz);
  23276. ExpectIntEQ(wc_InitRng(&rng), 0);
  23277. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23278. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23279. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23280. if (pkcs7 != NULL) {
  23281. pkcs7->content = NULL; /* not used for ex */
  23282. pkcs7->contentSz = (word32)sizeof(data);
  23283. pkcs7->privateKey = key;
  23284. pkcs7->privateKeySz = (word32)sizeof(key);
  23285. pkcs7->encryptOID = RSAk;
  23286. #ifdef NO_SHA
  23287. pkcs7->hashOID = SHA256h;
  23288. #else
  23289. pkcs7->hashOID = SHAh;
  23290. #endif
  23291. pkcs7->rng = &rng;
  23292. }
  23293. /* calculate hash for content */
  23294. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23295. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23296. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23297. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23298. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23299. /* Perform PKCS7 sign using hash directly */
  23300. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23301. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  23302. ExpectIntGT(outputHeadSz, 0);
  23303. ExpectIntGT(outputFootSz, 0);
  23304. wc_PKCS7_Free(pkcs7);
  23305. pkcs7 = NULL;
  23306. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23307. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23308. /* required parameter even on verify when using _ex, if using outputHead
  23309. * and outputFoot */
  23310. if (pkcs7 != NULL) {
  23311. pkcs7->contentSz = (word32)sizeof(data);
  23312. }
  23313. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23314. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  23315. wc_PKCS7_Free(pkcs7);
  23316. pkcs7 = NULL;
  23317. /* assembly complete PKCS7 sign and use normal verify */
  23318. {
  23319. byte* output = NULL;
  23320. word32 outputSz = 0;
  23321. ExpectNotNull(output = (byte*)XMALLOC(
  23322. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  23323. DYNAMIC_TYPE_TMP_BUFFER));
  23324. if (output != NULL) {
  23325. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  23326. outputSz += outputHeadSz;
  23327. XMEMCPY(&output[outputSz], data, sizeof(data));
  23328. outputSz += sizeof(data);
  23329. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  23330. outputSz += outputFootSz;
  23331. }
  23332. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23333. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23334. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23335. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23336. }
  23337. /* Pass in bad args. */
  23338. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23339. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23340. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23341. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23342. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23343. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23344. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23345. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23346. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23347. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23348. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23349. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  23350. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23351. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  23352. if (pkcs7 != NULL) {
  23353. pkcs7->hashOID = 0; /* bad hashOID */
  23354. }
  23355. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  23356. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  23357. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  23358. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23359. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  23360. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23361. #ifndef NO_PKCS7_STREAM
  23362. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23363. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23364. #else
  23365. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  23366. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  23367. #endif
  23368. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  23369. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23370. #ifndef NO_PKCS7_STREAM
  23371. /* can pass in 0 buffer length with streaming API */
  23372. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23373. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  23374. #else
  23375. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23376. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  23377. #endif
  23378. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23379. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  23380. #ifndef NO_PKCS7_STREAM
  23381. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23382. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  23383. #else
  23384. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23385. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  23386. #endif
  23387. wc_PKCS7_Free(pkcs7);
  23388. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23389. #endif
  23390. return EXPECT_RESULT();
  23391. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  23392. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23393. /**
  23394. * Loads certs/keys from files or buffers into the argument buffers,
  23395. * helper function called by CreatePKCS7SignedData().
  23396. *
  23397. * Returns 0 on success, negative on error.
  23398. */
  23399. static int LoadPKCS7SignedDataCerts(
  23400. int useIntermediateCertChain, int pkAlgoType,
  23401. byte* intCARoot, word32* intCARootSz,
  23402. byte* intCA1, word32* intCA1Sz,
  23403. byte* intCA2, word32* intCA2Sz,
  23404. byte* cert, word32* certSz,
  23405. byte* key, word32* keySz)
  23406. {
  23407. EXPECT_DECLS;
  23408. int ret = 0;
  23409. XFILE fp = XBADFILE;
  23410. #ifndef NO_RSA
  23411. const char* intCARootRSA = "./certs/ca-cert.der";
  23412. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  23413. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  23414. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  23415. const char* intServKeyRSA = "./certs/server-key.der";
  23416. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  23417. const char* cli1024Cert = "./certs/1024/client-cert.der";
  23418. const char* cli1024Key = "./certs/1024/client-key.der";
  23419. #endif
  23420. #endif
  23421. #ifdef HAVE_ECC
  23422. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  23423. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  23424. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  23425. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  23426. const char* intServKeyECC = "./certs/ecc-key.der";
  23427. #ifndef USE_CERT_BUFFERS_256
  23428. const char* cliEccCert = "./certs/client-ecc-cert.der";
  23429. const char* cliEccKey = "./certs/client-ecc-key.der";
  23430. #endif
  23431. #endif
  23432. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  23433. ((useIntermediateCertChain == 1) &&
  23434. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  23435. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  23436. return BAD_FUNC_ARG;
  23437. }
  23438. /* Read/load certs and keys to use for signing based on PK type and chain */
  23439. switch (pkAlgoType) {
  23440. #ifndef NO_RSA
  23441. case RSA_TYPE:
  23442. if (useIntermediateCertChain == 1) {
  23443. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  23444. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  23445. if (fp != XBADFILE) {
  23446. XFCLOSE(fp);
  23447. fp = XBADFILE;
  23448. }
  23449. ExpectIntGT(*intCARootSz, 0);
  23450. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  23451. if (fp != XBADFILE) {
  23452. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23453. XFCLOSE(fp);
  23454. fp = XBADFILE;
  23455. }
  23456. ExpectIntGT(*intCA1Sz, 0);
  23457. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  23458. if (fp != XBADFILE) {
  23459. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23460. XFCLOSE(fp);
  23461. fp = XBADFILE;
  23462. }
  23463. ExpectIntGT(*intCA2Sz, 0);
  23464. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  23465. if (fp != XBADFILE) {
  23466. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23467. XFCLOSE(fp);
  23468. fp = XBADFILE;
  23469. }
  23470. ExpectIntGT(*certSz, 0);
  23471. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  23472. if (fp != XBADFILE) {
  23473. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23474. XFCLOSE(fp);
  23475. fp = XBADFILE;
  23476. }
  23477. ExpectIntGT(*keySz, 0);
  23478. }
  23479. else {
  23480. #if defined(USE_CERT_BUFFERS_2048)
  23481. *keySz = sizeof_client_key_der_2048;
  23482. *certSz = sizeof_client_cert_der_2048;
  23483. XMEMCPY(key, client_key_der_2048, *keySz);
  23484. XMEMCPY(cert, client_cert_der_2048, *certSz);
  23485. #elif defined(USE_CERT_BUFFERS_1024)
  23486. *keySz = sizeof_client_key_der_1024;
  23487. *certSz = sizeof_client_cert_der_1024;
  23488. XMEMCPY(key, client_key_der_1024, *keySz);
  23489. XMEMCPY(cert, client_cert_der_1024, *certSz);
  23490. #else
  23491. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  23492. if (fp != XBADFILE) {
  23493. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23494. XFCLOSE(fp);
  23495. fp = XBADFILE;
  23496. }
  23497. ExpectIntGT(*keySz, 0);
  23498. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  23499. if (fp != XBADFILE) {
  23500. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23501. XFCLOSE(fp);
  23502. fp = XBADFILE;
  23503. }
  23504. ExpectIntGT(*certSz, 0);
  23505. #endif /* USE_CERT_BUFFERS_2048 */
  23506. }
  23507. break;
  23508. #endif /* !NO_RSA */
  23509. #ifdef HAVE_ECC
  23510. case ECC_TYPE:
  23511. if (useIntermediateCertChain == 1) {
  23512. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  23513. if (fp != XBADFILE) {
  23514. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  23515. fp);
  23516. XFCLOSE(fp);
  23517. fp = XBADFILE;
  23518. }
  23519. ExpectIntGT(*intCARootSz, 0);
  23520. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  23521. if (fp != XBADFILE) {
  23522. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  23523. XFCLOSE(fp);
  23524. fp = XBADFILE;
  23525. }
  23526. ExpectIntGT(*intCA1Sz, 0);
  23527. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  23528. if (fp != XBADFILE) {
  23529. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  23530. XFCLOSE(fp);
  23531. fp = XBADFILE;
  23532. }
  23533. ExpectIntGT(*intCA2Sz, 0);
  23534. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  23535. if (fp != XBADFILE) {
  23536. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23537. XFCLOSE(fp);
  23538. fp = XBADFILE;
  23539. }
  23540. ExpectIntGT(*certSz, 0);
  23541. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  23542. if (fp != XBADFILE) {
  23543. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23544. XFCLOSE(fp);
  23545. fp = XBADFILE;
  23546. }
  23547. ExpectIntGT(*keySz, 0);
  23548. }
  23549. else {
  23550. #if defined(USE_CERT_BUFFERS_256)
  23551. *keySz = sizeof_ecc_clikey_der_256;
  23552. *certSz = sizeof_cliecc_cert_der_256;
  23553. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  23554. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  23555. #else
  23556. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  23557. if (fp != XBADFILE) {
  23558. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  23559. XFCLOSE(fp);
  23560. fp = XBADFILE;
  23561. }
  23562. ExpectIntGT(*keySz, 0);
  23563. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  23564. if (fp != XBADFILE) {
  23565. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  23566. XFCLOSE(fp);
  23567. fp = XBADFILE;
  23568. }
  23569. ExpectIntGT(*certSz, 0);
  23570. #endif /* USE_CERT_BUFFERS_256 */
  23571. }
  23572. break;
  23573. #endif /* HAVE_ECC */
  23574. default:
  23575. WOLFSSL_MSG("Unsupported SignedData PK type");
  23576. ret = BAD_FUNC_ARG;
  23577. break;
  23578. }
  23579. if (EXPECT_FAIL() && (ret == 0)) {
  23580. ret = BAD_FUNC_ARG;
  23581. }
  23582. return ret;
  23583. }
  23584. /**
  23585. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  23586. *
  23587. * output output buffer to place SignedData
  23588. * outputSz size of output buffer
  23589. * data data buffer to be signed
  23590. * dataSz size of data buffer
  23591. * withAttribs [1/0] include attributes in SignedData message
  23592. * detachedSig [1/0] create detached signature, no content
  23593. * useIntCertChain [1/0] use certificate chain and include intermediate and
  23594. * root CAs in bundle
  23595. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  23596. *
  23597. * Return size of bundle created on success, negative on error */
  23598. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  23599. byte* data, word32 dataSz,
  23600. int withAttribs, int detachedSig,
  23601. int useIntermediateCertChain,
  23602. int pkAlgoType)
  23603. {
  23604. EXPECT_DECLS;
  23605. int ret = 0;
  23606. WC_RNG rng;
  23607. PKCS7* pkcs7 = NULL;
  23608. static byte messageTypeOid[] =
  23609. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  23610. 0x09, 0x02 };
  23611. static byte messageType[] = { 0x13, 2, '1', '9' };
  23612. PKCS7Attrib attribs[] =
  23613. {
  23614. { messageTypeOid, sizeof(messageTypeOid), messageType,
  23615. sizeof(messageType) }
  23616. };
  23617. byte intCARoot[TWOK_BUF];
  23618. byte intCA1[TWOK_BUF];
  23619. byte intCA2[TWOK_BUF];
  23620. byte cert[TWOK_BUF];
  23621. byte key[TWOK_BUF];
  23622. word32 intCARootSz = sizeof(intCARoot);
  23623. word32 intCA1Sz = sizeof(intCA1);
  23624. word32 intCA2Sz = sizeof(intCA2);
  23625. word32 certSz = sizeof(cert);
  23626. word32 keySz = sizeof(key);
  23627. XMEMSET(intCARoot, 0, intCARootSz);
  23628. XMEMSET(intCA1, 0, intCA1Sz);
  23629. XMEMSET(intCA2, 0, intCA2Sz);
  23630. XMEMSET(cert, 0, certSz);
  23631. XMEMSET(key, 0, keySz);
  23632. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  23633. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  23634. cert, &certSz, key, &keySz);
  23635. ExpectIntEQ(ret, 0);
  23636. XMEMSET(output, 0, outputSz);
  23637. ExpectIntEQ(wc_InitRng(&rng), 0);
  23638. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23639. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23640. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23641. if (useIntermediateCertChain == 1) {
  23642. /* Add intermediate and root CA certs into SignedData Certs SET */
  23643. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  23644. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  23645. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  23646. }
  23647. if (pkcs7 != NULL) {
  23648. pkcs7->content = data;
  23649. pkcs7->contentSz = dataSz;
  23650. pkcs7->privateKey = key;
  23651. pkcs7->privateKeySz = (word32)sizeof(key);
  23652. if (pkAlgoType == RSA_TYPE) {
  23653. pkcs7->encryptOID = RSAk;
  23654. }
  23655. else {
  23656. pkcs7->encryptOID = ECDSAk;
  23657. }
  23658. #ifdef NO_SHA
  23659. pkcs7->hashOID = SHA256h;
  23660. #else
  23661. pkcs7->hashOID = SHAh;
  23662. #endif
  23663. pkcs7->rng = &rng;
  23664. if (withAttribs) {
  23665. /* include a signed attribute */
  23666. pkcs7->signedAttribs = attribs;
  23667. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  23668. }
  23669. }
  23670. if (detachedSig) {
  23671. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  23672. }
  23673. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  23674. ExpectIntGT(outputSz, 0);
  23675. wc_PKCS7_Free(pkcs7);
  23676. pkcs7 = NULL;
  23677. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23678. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23679. if (detachedSig && (pkcs7 != NULL)) {
  23680. pkcs7->content = data;
  23681. pkcs7->contentSz = dataSz;
  23682. }
  23683. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23684. wc_PKCS7_Free(pkcs7);
  23685. wc_FreeRng(&rng);
  23686. if (EXPECT_FAIL()) {
  23687. outputSz = 0;
  23688. }
  23689. return outputSz;
  23690. }
  23691. #endif
  23692. /*
  23693. * Testing wc_PKCS_VerifySignedData()
  23694. */
  23695. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  23696. {
  23697. EXPECT_DECLS;
  23698. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  23699. PKCS7* pkcs7 = NULL;
  23700. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23701. word32 outputSz = sizeof(output);
  23702. byte data[] = "Test data to encode.";
  23703. byte badOut[1];
  23704. word32 badOutSz = 0;
  23705. byte badContent[] = "This is different content than was signed";
  23706. wc_HashAlg hash;
  23707. #ifdef NO_SHA
  23708. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23709. #else
  23710. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23711. #endif
  23712. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23713. word32 hashSz = wc_HashGetDigestSize(hashType);
  23714. #ifndef NO_RSA
  23715. PKCS7DecodedAttrib* decodedAttrib = NULL;
  23716. /* contentType OID (1.2.840.113549.1.9.3) */
  23717. static const byte contentTypeOid[] =
  23718. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  23719. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  23720. static const byte dataType[] =
  23721. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  23722. /* messageDigest OID (1.2.840.113549.1.9.4) */
  23723. static const byte messageDigestOid[] =
  23724. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  23725. #ifndef NO_ASN_TIME
  23726. /* signingTime OID () */
  23727. static const byte signingTimeOid[] =
  23728. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  23729. #endif
  23730. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23731. int dateLength = 0;
  23732. byte dateFormat;
  23733. const byte* datePart = NULL;
  23734. struct tm timearg;
  23735. time_t now;
  23736. struct tm* nowTm = NULL;
  23737. #ifdef NEED_TMP_TIME
  23738. struct tm tmpTimeStorage;
  23739. struct tm* tmpTime = &tmpTimeStorage;
  23740. #endif
  23741. #endif /* !NO_ASN && !NO_ASN_TIME */
  23742. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23743. /* Success test with RSA certs/key */
  23744. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23745. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  23746. /* calculate hash for content, used later */
  23747. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23748. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23749. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23750. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23751. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23752. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23753. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23754. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23755. /* Check that decoded signed attributes are correct */
  23756. /* messageDigest should be first */
  23757. if (pkcs7 != NULL) {
  23758. decodedAttrib = pkcs7->decodedAttrib;
  23759. }
  23760. ExpectNotNull(decodedAttrib);
  23761. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  23762. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  23763. decodedAttrib->oidSz), 0);
  23764. /* + 2 for OCTET STRING and length bytes */
  23765. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  23766. ExpectNotNull(decodedAttrib->value);
  23767. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  23768. #ifndef NO_ASN_TIME
  23769. /* signingTime should be second */
  23770. if (decodedAttrib != NULL) {
  23771. decodedAttrib = decodedAttrib->next;
  23772. }
  23773. ExpectNotNull(decodedAttrib);
  23774. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  23775. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  23776. decodedAttrib->oidSz), 0);
  23777. ExpectIntGT(decodedAttrib->valueSz, 0);
  23778. ExpectNotNull(decodedAttrib->value);
  23779. #endif
  23780. /* Verify signingTime if ASN and time are available */
  23781. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  23782. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  23783. &datePart, &dateFormat, &dateLength), 0);
  23784. ExpectNotNull(datePart);
  23785. ExpectIntGT(dateLength, 0);
  23786. XMEMSET(&timearg, 0, sizeof(timearg));
  23787. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  23788. &timearg), 0);
  23789. /* Get current time and compare year/month/day against attribute value */
  23790. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  23791. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  23792. ExpectNotNull(nowTm);
  23793. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  23794. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  23795. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  23796. #endif /* !NO_ASN && !NO_ASN_TIME */
  23797. /* contentType should be third */
  23798. if (decodedAttrib != NULL) {
  23799. decodedAttrib = decodedAttrib->next;
  23800. }
  23801. ExpectNotNull(decodedAttrib);
  23802. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  23803. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  23804. decodedAttrib->oidSz), 0);
  23805. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  23806. ExpectNotNull(decodedAttrib->value);
  23807. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  23808. 0);
  23809. #endif /* !NO_RSA */
  23810. /* Test bad args. */
  23811. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  23812. BAD_FUNC_ARG);
  23813. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  23814. BAD_FUNC_ARG);
  23815. #ifndef NO_PKCS7_STREAM
  23816. /* can pass in 0 buffer length with streaming API */
  23817. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23818. badOutSz), WC_PKCS7_WANT_READ_E);
  23819. #else
  23820. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  23821. badOutSz), BAD_FUNC_ARG);
  23822. #endif
  23823. wc_PKCS7_Free(pkcs7);
  23824. pkcs7 = NULL;
  23825. #ifndef NO_RSA
  23826. /* Try RSA certs/key/sig first */
  23827. outputSz = sizeof(output);
  23828. XMEMSET(output, 0, outputSz);
  23829. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23830. (word32)sizeof(data),
  23831. 1, 1, 0, RSA_TYPE)), 0);
  23832. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23833. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23834. if (pkcs7 != NULL) {
  23835. pkcs7->content = badContent;
  23836. pkcs7->contentSz = sizeof(badContent);
  23837. }
  23838. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  23839. SIG_VERIFY_E);
  23840. wc_PKCS7_Free(pkcs7);
  23841. pkcs7 = NULL;
  23842. /* Test success case with detached signature and valid content */
  23843. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23844. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23845. if (pkcs7 != NULL) {
  23846. pkcs7->content = data;
  23847. pkcs7->contentSz = sizeof(data);
  23848. }
  23849. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23850. wc_PKCS7_Free(pkcs7);
  23851. pkcs7 = NULL;
  23852. /* verify using pre-computed content digest only (no content) */
  23853. {
  23854. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23855. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23856. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23857. output, outputSz, NULL, 0), 0);
  23858. wc_PKCS7_Free(pkcs7);
  23859. pkcs7 = NULL;
  23860. }
  23861. #endif /* !NO_RSA */
  23862. /* Test verify on signedData containing intermediate/root CA certs */
  23863. #ifndef NO_RSA
  23864. outputSz = sizeof(output);
  23865. XMEMSET(output, 0, outputSz);
  23866. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23867. (word32)sizeof(data),
  23868. 0, 0, 1, RSA_TYPE)), 0);
  23869. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23870. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23871. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23872. wc_PKCS7_Free(pkcs7);
  23873. pkcs7 = NULL;
  23874. #endif /* !NO_RSA */
  23875. #endif
  23876. return EXPECT_RESULT();
  23877. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  23878. /*
  23879. * Testing wc_PKCS_VerifySignedData()
  23880. */
  23881. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  23882. {
  23883. EXPECT_DECLS;
  23884. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  23885. PKCS7* pkcs7 = NULL;
  23886. byte output[6000]; /* Large size needed for bundles with int CA certs */
  23887. word32 outputSz = sizeof(output);
  23888. byte data[] = "Test data to encode.";
  23889. byte badContent[] = "This is different content than was signed";
  23890. wc_HashAlg hash;
  23891. #ifdef NO_SHA
  23892. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  23893. #else
  23894. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23895. #endif
  23896. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23897. word32 hashSz = wc_HashGetDigestSize(hashType);
  23898. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  23899. /* Success test with ECC certs/key */
  23900. outputSz = sizeof(output);
  23901. XMEMSET(output, 0, outputSz);
  23902. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23903. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  23904. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23905. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23906. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23907. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23908. wc_PKCS7_Free(pkcs7);
  23909. pkcs7 = NULL;
  23910. /* Invalid content should error, use detached signature so we can
  23911. * easily change content */
  23912. outputSz = sizeof(output);
  23913. XMEMSET(output, 0, outputSz);
  23914. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23915. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  23916. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23917. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23918. if (pkcs7 != NULL) {
  23919. pkcs7->content = badContent;
  23920. pkcs7->contentSz = sizeof(badContent);
  23921. }
  23922. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  23923. SIG_VERIFY_E);
  23924. wc_PKCS7_Free(pkcs7);
  23925. pkcs7 = NULL;
  23926. /* Test success case with detached signature and valid content */
  23927. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23928. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23929. if (pkcs7 != NULL) {
  23930. pkcs7->content = data;
  23931. pkcs7->contentSz = sizeof(data);
  23932. }
  23933. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23934. wc_PKCS7_Free(pkcs7);
  23935. pkcs7 = NULL;
  23936. /* verify using pre-computed content digest only (no content) */
  23937. {
  23938. /* calculate hash for content */
  23939. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  23940. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  23941. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  23942. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  23943. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23944. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  23945. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  23946. output, outputSz, NULL, 0), 0);
  23947. wc_PKCS7_Free(pkcs7);
  23948. pkcs7 = NULL;
  23949. }
  23950. /* Test verify on signedData containing intermediate/root CA certs */
  23951. outputSz = sizeof(output);
  23952. XMEMSET(output, 0, outputSz);
  23953. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  23954. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  23955. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23956. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23957. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23958. wc_PKCS7_Free(pkcs7);
  23959. pkcs7 = NULL;
  23960. #endif
  23961. return EXPECT_RESULT();
  23962. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  23963. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  23964. !defined(NO_AES_256)
  23965. static const byte defKey[] = {
  23966. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23967. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23968. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  23969. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  23970. };
  23971. static byte aesHandle[32]; /* simulated hardware key handle */
  23972. /* return 0 on success */
  23973. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  23974. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  23975. byte* in, int inSz, byte* out, void* usrCtx)
  23976. {
  23977. int ret;
  23978. Aes aes;
  23979. if (usrCtx == NULL) {
  23980. /* no simulated handle passed in */
  23981. return -1;
  23982. }
  23983. switch (encryptOID) {
  23984. case AES256CBCb:
  23985. if (ivSz != AES_BLOCK_SIZE)
  23986. return BAD_FUNC_ARG;
  23987. break;
  23988. default:
  23989. WOLFSSL_MSG("Unsupported content cipher type for test");
  23990. return ALGO_ID_E;
  23991. };
  23992. /* simulate using handle to get key */
  23993. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  23994. if (ret == 0) {
  23995. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  23996. if (ret == 0)
  23997. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  23998. wc_AesFree(&aes);
  23999. }
  24000. (void)aad;
  24001. (void)aadSz;
  24002. (void)authTag;
  24003. (void)authTagSz;
  24004. (void)pkcs7;
  24005. return ret;
  24006. }
  24007. /* returns key size on success */
  24008. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24009. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24010. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24011. {
  24012. int ret = -1;
  24013. if (out == NULL)
  24014. return BAD_FUNC_ARG;
  24015. if (keyId[0] != 0x00) {
  24016. return -1;
  24017. }
  24018. if (type != (int)PKCS7_KEKRI) {
  24019. return -1;
  24020. }
  24021. switch (keyWrapAlgo) {
  24022. case AES256_WRAP:
  24023. /* simulate setting a handle for later decryption but use key
  24024. * as handle in the test case here */
  24025. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24026. aesHandle, sizeof(aesHandle), NULL);
  24027. if (ret < 0)
  24028. return ret;
  24029. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24030. if (ret < 0)
  24031. return ret;
  24032. /* return key size on success */
  24033. return sizeof(defKey);
  24034. default:
  24035. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24036. return BAD_KEYWRAP_ALG_E;
  24037. };
  24038. (void)cekSz;
  24039. (void)cek;
  24040. (void)outSz;
  24041. (void)keyIdSz;
  24042. (void)direction;
  24043. (void)orginKey; /* used with KAKRI */
  24044. (void)orginKeySz;
  24045. return ret;
  24046. }
  24047. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24048. /*
  24049. * Testing wc_PKCS7_EncodeEnvelopedData()
  24050. */
  24051. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  24052. {
  24053. EXPECT_DECLS;
  24054. #if defined(HAVE_PKCS7)
  24055. PKCS7* pkcs7 = NULL;
  24056. #ifdef ECC_TIMING_RESISTANT
  24057. WC_RNG rng;
  24058. #endif
  24059. word32 tempWrd32 = 0;
  24060. byte* tmpBytePtr = NULL;
  24061. const char input[] = "Test data to encode.";
  24062. int i;
  24063. int testSz = 0;
  24064. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24065. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24066. byte* rsaCert = NULL;
  24067. byte* rsaPrivKey = NULL;
  24068. word32 rsaCertSz;
  24069. word32 rsaPrivKeySz;
  24070. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24071. !defined(USE_CERT_BUFFERS_2048) )
  24072. static const char* rsaClientCert = "./certs/client-cert.der";
  24073. static const char* rsaClientKey = "./certs/client-key.der";
  24074. rsaCertSz = (word32)sizeof(rsaClientCert);
  24075. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24076. #endif
  24077. #endif
  24078. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24079. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24080. byte* eccCert = NULL;
  24081. byte* eccPrivKey = NULL;
  24082. word32 eccCertSz;
  24083. word32 eccPrivKeySz;
  24084. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24085. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24086. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24087. #endif
  24088. #endif
  24089. /* Generic buffer size. */
  24090. byte output[ONEK_BUF];
  24091. byte decoded[sizeof(input)/sizeof(char)];
  24092. int decodedSz = 0;
  24093. #ifndef NO_FILESYSTEM
  24094. XFILE certFile = XBADFILE;
  24095. XFILE keyFile = XBADFILE;
  24096. #endif
  24097. #ifdef ECC_TIMING_RESISTANT
  24098. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24099. #endif
  24100. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24101. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24102. /* RSA certs and keys. */
  24103. #if defined(USE_CERT_BUFFERS_1024)
  24104. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24105. /* Allocate buffer space. */
  24106. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24107. DYNAMIC_TYPE_TMP_BUFFER));
  24108. /* Init buffer. */
  24109. if (rsaCert != NULL) {
  24110. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24111. }
  24112. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24113. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24114. DYNAMIC_TYPE_TMP_BUFFER));
  24115. if (rsaPrivKey != NULL) {
  24116. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24117. }
  24118. #elif defined(USE_CERT_BUFFERS_2048)
  24119. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24120. /* Allocate buffer */
  24121. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  24122. DYNAMIC_TYPE_TMP_BUFFER));
  24123. /* Init buffer. */
  24124. if (rsaCert != NULL) {
  24125. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24126. }
  24127. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24128. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  24129. DYNAMIC_TYPE_TMP_BUFFER));
  24130. if (rsaPrivKey != NULL) {
  24131. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24132. }
  24133. #else
  24134. /* File system. */
  24135. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  24136. rsaCertSz = (word32)FOURK_BUF;
  24137. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24138. DYNAMIC_TYPE_TMP_BUFFER));
  24139. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  24140. certFile)) > 0);
  24141. if (certFile != XBADFILE)
  24142. XFCLOSE(certFile);
  24143. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  24144. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24145. DYNAMIC_TYPE_TMP_BUFFER));
  24146. rsaPrivKeySz = (word32)FOURK_BUF;
  24147. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  24148. keyFile)) > 0);
  24149. if (keyFile != XBADFILE)
  24150. XFCLOSE(keyFile);
  24151. #endif /* USE_CERT_BUFFERS */
  24152. #endif /* NO_RSA */
  24153. /* ECC */
  24154. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24155. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24156. #ifdef USE_CERT_BUFFERS_256
  24157. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24158. DYNAMIC_TYPE_TMP_BUFFER));
  24159. /* Init buffer. */
  24160. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24161. if (eccCert != NULL) {
  24162. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24163. }
  24164. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24165. DYNAMIC_TYPE_TMP_BUFFER));
  24166. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24167. if (eccPrivKey != NULL) {
  24168. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24169. }
  24170. #else /* File system. */
  24171. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  24172. eccCertSz = (word32)FOURK_BUF;
  24173. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24174. DYNAMIC_TYPE_TMP_BUFFER));
  24175. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  24176. certFile)) > 0);
  24177. if (certFile != XBADFILE) {
  24178. XFCLOSE(certFile);
  24179. }
  24180. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  24181. eccPrivKeySz = (word32)FOURK_BUF;
  24182. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24183. DYNAMIC_TYPE_TMP_BUFFER));
  24184. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  24185. keyFile)) > 0);
  24186. if (keyFile != XBADFILE) {
  24187. XFCLOSE(keyFile);
  24188. }
  24189. #endif /* USE_CERT_BUFFERS_256 */
  24190. #endif /* END HAVE_ECC */
  24191. #ifndef NO_FILESYSTEM
  24192. /* Silence. */
  24193. (void)keyFile;
  24194. (void)certFile;
  24195. #endif
  24196. {
  24197. const pkcs7EnvelopedVector testVectors[] = {
  24198. /* DATA is a global variable defined in the makefile. */
  24199. #if !defined(NO_RSA)
  24200. #ifndef NO_DES3
  24201. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24202. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24203. #endif /* NO_DES3 */
  24204. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24205. #ifndef NO_AES_128
  24206. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24207. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24208. #endif
  24209. #ifndef NO_AES_192
  24210. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24211. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24212. #endif
  24213. #ifndef NO_AES_256
  24214. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24215. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24216. #endif
  24217. #endif /* NO_AES && HAVE_AES_CBC */
  24218. #endif /* NO_RSA */
  24219. #if defined(HAVE_ECC)
  24220. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24221. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24222. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24223. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  24224. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24225. #endif
  24226. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24227. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24228. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  24229. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24230. #endif
  24231. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24232. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  24233. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  24234. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  24235. #endif
  24236. #endif /* NO_AES && HAVE_AES_CBC*/
  24237. #endif /* END HAVE_ECC */
  24238. }; /* END pkcs7EnvelopedVector */
  24239. #ifdef ECC_TIMING_RESISTANT
  24240. ExpectIntEQ(wc_InitRng(&rng), 0);
  24241. #endif
  24242. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24243. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24244. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24245. for (i = 0; i < testSz; i++) {
  24246. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24247. (word32)(testVectors + i)->certSz), 0);
  24248. if (pkcs7 != NULL) {
  24249. #ifdef ECC_TIMING_RESISTANT
  24250. pkcs7->rng = &rng;
  24251. #endif
  24252. pkcs7->content = (byte*)(testVectors + i)->content;
  24253. pkcs7->contentSz = (testVectors + i)->contentSz;
  24254. pkcs7->contentOID = (testVectors + i)->contentOID;
  24255. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24256. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24257. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24258. pkcs7->privateKey = (testVectors + i)->privateKey;
  24259. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24260. }
  24261. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24262. (word32)sizeof(output)), 0);
  24263. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24264. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24265. ExpectIntGE(decodedSz, 0);
  24266. /* Verify the size of each buffer. */
  24267. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24268. /* Don't free the last time through the loop. */
  24269. if (i < testSz - 1) {
  24270. wc_PKCS7_Free(pkcs7);
  24271. pkcs7 = NULL;
  24272. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24273. }
  24274. } /* END test loop. */
  24275. }
  24276. /* Test bad args. */
  24277. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24278. (word32)sizeof(output)), BAD_FUNC_ARG);
  24279. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24280. (word32)sizeof(output)), BAD_FUNC_ARG);
  24281. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24282. /* Decode. */
  24283. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24284. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24285. BAD_FUNC_ARG);
  24286. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24287. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24288. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24289. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24290. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24291. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24292. BAD_FUNC_ARG);
  24293. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24294. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24295. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24296. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24297. /* only a failure for KARI test cases */
  24298. if (pkcs7 != NULL) {
  24299. tempWrd32 = pkcs7->singleCertSz;
  24300. pkcs7->singleCertSz = 0;
  24301. }
  24302. #if defined(WOLFSSL_ASN_TEMPLATE)
  24303. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24304. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24305. BUFFER_E);
  24306. #else
  24307. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24308. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24309. ASN_PARSE_E);
  24310. #endif
  24311. if (pkcs7 != NULL) {
  24312. pkcs7->singleCertSz = tempWrd32;
  24313. tmpBytePtr = pkcs7->singleCert;
  24314. pkcs7->singleCert = NULL;
  24315. }
  24316. #ifndef NO_RSA
  24317. #if defined(NO_PKCS7_STREAM)
  24318. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  24319. * is returned from kari parse which gets set to bad func arg */
  24320. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24321. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24322. BAD_FUNC_ARG);
  24323. #else
  24324. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24325. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24326. ASN_PARSE_E);
  24327. #endif
  24328. #endif /* !NO_RSA */
  24329. if (pkcs7 != NULL) {
  24330. pkcs7->singleCert = tmpBytePtr;
  24331. }
  24332. #endif
  24333. if (pkcs7 != NULL) {
  24334. tempWrd32 = pkcs7->privateKeySz;
  24335. pkcs7->privateKeySz = 0;
  24336. }
  24337. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24338. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24339. BAD_FUNC_ARG);
  24340. if (pkcs7 != NULL) {
  24341. pkcs7->privateKeySz = tempWrd32;
  24342. tmpBytePtr = pkcs7->privateKey;
  24343. pkcs7->privateKey = NULL;
  24344. }
  24345. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24346. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  24347. BAD_FUNC_ARG);
  24348. if (pkcs7 != NULL) {
  24349. pkcs7->privateKey = tmpBytePtr;
  24350. }
  24351. wc_PKCS7_Free(pkcs7);
  24352. pkcs7 = NULL;
  24353. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24354. /* test of decrypt callback with KEKRI enveloped data */
  24355. {
  24356. int envelopedSz = 0;
  24357. const byte keyId[] = { 0x00 };
  24358. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24359. if (pkcs7 != NULL) {
  24360. pkcs7->content = (byte*)input;
  24361. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24362. pkcs7->contentOID = DATA;
  24363. pkcs7->encryptOID = AES256CBCb;
  24364. }
  24365. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24366. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24367. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24368. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24369. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24370. (word32)sizeof(output))), 0);
  24371. wc_PKCS7_Free(pkcs7);
  24372. pkcs7 = NULL;
  24373. /* decode envelopedData */
  24374. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24375. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24376. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24377. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24378. envelopedSz, decoded, sizeof(decoded))), 0);
  24379. wc_PKCS7_Free(pkcs7);
  24380. pkcs7 = NULL;
  24381. }
  24382. #endif /* !NO_AES && !NO_AES_256 */
  24383. #ifndef NO_RSA
  24384. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24385. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24386. #endif /* NO_RSA */
  24387. #ifdef HAVE_ECC
  24388. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24389. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24390. #endif /* HAVE_ECC */
  24391. #ifdef ECC_TIMING_RESISTANT
  24392. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24393. #endif
  24394. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  24395. !defined(NO_RSA) && !defined(NO_SHA)
  24396. {
  24397. byte out[7];
  24398. byte *cms = NULL;
  24399. word32 cmsSz;
  24400. XFILE cmsFile = XBADFILE;
  24401. XMEMSET(out, 0, sizeof(out));
  24402. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24403. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  24404. != XBADFILE);
  24405. cmsSz = (word32)FOURK_BUF;
  24406. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24407. DYNAMIC_TYPE_TMP_BUFFER));
  24408. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  24409. if (cmsFile != XBADFILE)
  24410. XFCLOSE(cmsFile);
  24411. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24412. sizeof_client_cert_der_2048), 0);
  24413. if (pkcs7 != NULL) {
  24414. pkcs7->privateKey = (byte*)client_key_der_2048;
  24415. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24416. }
  24417. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24418. 2), 0);
  24419. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24420. sizeof(out)), 0);
  24421. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24422. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  24423. wc_PKCS7_Free(pkcs7);
  24424. pkcs7 = NULL;
  24425. }
  24426. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  24427. #endif /* HAVE_PKCS7 */
  24428. return EXPECT_RESULT();
  24429. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  24430. /*
  24431. * Testing wc_PKCS7_EncodeEncryptedData()
  24432. */
  24433. static int test_wc_PKCS7_EncodeEncryptedData(void)
  24434. {
  24435. EXPECT_DECLS;
  24436. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24437. PKCS7* pkcs7 = NULL;
  24438. byte* tmpBytePtr = NULL;
  24439. byte encrypted[TWOK_BUF];
  24440. byte decoded[TWOK_BUF];
  24441. word32 tmpWrd32 = 0;
  24442. int tmpInt = 0;
  24443. int decodedSz;
  24444. int encryptedSz = 0;
  24445. int testSz;
  24446. int i;
  24447. const byte data[] = { /* Hello World */
  24448. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24449. 0x72,0x6c,0x64
  24450. };
  24451. #ifndef NO_DES3
  24452. byte desKey[] = {
  24453. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24454. };
  24455. byte des3Key[] = {
  24456. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24457. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24458. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24459. };
  24460. #endif
  24461. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24462. #ifndef NO_AES_128
  24463. byte aes128Key[] = {
  24464. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24465. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24466. };
  24467. #endif
  24468. #ifndef NO_AES_192
  24469. byte aes192Key[] = {
  24470. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24471. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24472. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24473. };
  24474. #endif
  24475. #ifndef NO_AES_256
  24476. byte aes256Key[] = {
  24477. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24478. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24479. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24480. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24481. };
  24482. #endif
  24483. #endif /* !NO_AES && HAVE_AES_CBC */
  24484. const pkcs7EncryptedVector testVectors[] =
  24485. {
  24486. #ifndef NO_DES3
  24487. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24488. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24489. #endif /* !NO_DES3 */
  24490. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24491. #ifndef NO_AES_128
  24492. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24493. sizeof(aes128Key)},
  24494. #endif
  24495. #ifndef NO_AES_192
  24496. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24497. sizeof(aes192Key)},
  24498. #endif
  24499. #ifndef NO_AES_256
  24500. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24501. sizeof(aes256Key)},
  24502. #endif
  24503. #endif /* !NO_AES && HAVE_AES_CBC */
  24504. };
  24505. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24506. for (i = 0; i < testSz; i++) {
  24507. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24508. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24509. if (pkcs7 != NULL) {
  24510. pkcs7->content = (byte*)testVectors[i].content;
  24511. pkcs7->contentSz = testVectors[i].contentSz;
  24512. pkcs7->contentOID = testVectors[i].contentOID;
  24513. pkcs7->encryptOID = testVectors[i].encryptOID;
  24514. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24515. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24516. pkcs7->heap = HEAP_HINT;
  24517. }
  24518. /* encode encryptedData */
  24519. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24520. sizeof(encrypted)), 0);
  24521. /* Decode encryptedData */
  24522. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  24523. encryptedSz, decoded, sizeof(decoded)), 0);
  24524. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24525. /* Keep values for last itr. */
  24526. if (i < testSz - 1) {
  24527. wc_PKCS7_Free(pkcs7);
  24528. pkcs7 = NULL;
  24529. }
  24530. }
  24531. if (pkcs7 == NULL || testSz == 0) {
  24532. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24533. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24534. }
  24535. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24536. sizeof(encrypted)),BAD_FUNC_ARG);
  24537. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24538. sizeof(encrypted)), BAD_FUNC_ARG);
  24539. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24540. 0), BAD_FUNC_ARG);
  24541. /* Testing the struct. */
  24542. if (pkcs7 != NULL) {
  24543. tmpBytePtr = pkcs7->content;
  24544. pkcs7->content = NULL;
  24545. }
  24546. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24547. sizeof(encrypted)), BAD_FUNC_ARG);
  24548. if (pkcs7 != NULL) {
  24549. pkcs7->content = tmpBytePtr;
  24550. tmpWrd32 = pkcs7->contentSz;
  24551. pkcs7->contentSz = 0;
  24552. }
  24553. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24554. sizeof(encrypted)), BAD_FUNC_ARG);
  24555. if (pkcs7 != NULL) {
  24556. pkcs7->contentSz = tmpWrd32;
  24557. tmpInt = pkcs7->encryptOID;
  24558. pkcs7->encryptOID = 0;
  24559. }
  24560. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24561. sizeof(encrypted)), BAD_FUNC_ARG);
  24562. if (pkcs7 != NULL) {
  24563. pkcs7->encryptOID = tmpInt;
  24564. tmpBytePtr = pkcs7->encryptionKey;
  24565. pkcs7->encryptionKey = NULL;
  24566. }
  24567. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24568. sizeof(encrypted)), BAD_FUNC_ARG);
  24569. if (pkcs7 != NULL) {
  24570. pkcs7->encryptionKey = tmpBytePtr;
  24571. tmpWrd32 = pkcs7->encryptionKeySz;
  24572. pkcs7->encryptionKeySz = 0;
  24573. }
  24574. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24575. sizeof(encrypted)), BAD_FUNC_ARG);
  24576. if (pkcs7 != NULL) {
  24577. pkcs7->encryptionKeySz = tmpWrd32;
  24578. }
  24579. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24580. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24581. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24582. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24583. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24584. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24585. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24586. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24587. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24588. decoded, 0), BAD_FUNC_ARG);
  24589. /* Test struct fields */
  24590. if (pkcs7 != NULL) {
  24591. tmpBytePtr = pkcs7->encryptionKey;
  24592. pkcs7->encryptionKey = NULL;
  24593. }
  24594. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24595. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24596. if (pkcs7 != NULL) {
  24597. pkcs7->encryptionKey = tmpBytePtr;
  24598. pkcs7->encryptionKeySz = 0;
  24599. }
  24600. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24601. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24602. wc_PKCS7_Free(pkcs7);
  24603. #endif
  24604. return EXPECT_RESULT();
  24605. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24606. /*
  24607. * Testing wc_PKCS7_Degenerate()
  24608. */
  24609. static int test_wc_PKCS7_Degenerate(void)
  24610. {
  24611. EXPECT_DECLS;
  24612. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24613. PKCS7* pkcs7 = NULL;
  24614. char fName[] = "./certs/test-degenerate.p7b";
  24615. XFILE f = XBADFILE;
  24616. byte der[4096];
  24617. word32 derSz = 0;
  24618. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24619. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24620. if (f != XBADFILE)
  24621. XFCLOSE(f);
  24622. /* test degenerate success */
  24623. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24624. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24625. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24626. #ifndef NO_RSA
  24627. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24628. #else
  24629. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24630. #endif
  24631. wc_PKCS7_Free(pkcs7);
  24632. pkcs7 = NULL;
  24633. /* test with turning off degenerate cases */
  24634. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24635. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24636. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24637. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24638. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  24639. PKCS7_NO_SIGNER_E);
  24640. wc_PKCS7_Free(pkcs7);
  24641. #endif
  24642. return EXPECT_RESULT();
  24643. } /* END test_wc_PKCS7_Degenerate() */
  24644. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24645. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  24646. static byte berContent[] = {
  24647. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24648. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24649. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24650. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24651. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24652. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24653. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24654. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24655. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24656. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24657. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24658. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24659. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24660. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24661. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24662. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24663. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24664. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24665. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24666. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24667. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24668. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24669. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24670. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24671. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24672. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24673. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24674. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24675. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24676. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24677. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24678. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24679. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24680. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24681. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24682. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24683. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24684. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24685. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24686. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24687. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24688. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24689. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24690. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24691. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24692. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  24693. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  24694. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  24695. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  24696. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  24697. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  24698. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  24699. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  24700. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  24701. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  24702. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  24703. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  24704. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  24705. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  24706. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  24707. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  24708. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  24709. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  24710. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  24711. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  24712. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  24713. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  24714. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  24715. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  24716. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  24717. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  24718. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  24719. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  24720. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  24721. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  24722. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  24723. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  24724. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  24725. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  24726. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  24727. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  24728. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  24729. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  24730. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  24731. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  24732. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  24733. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  24734. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  24735. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  24736. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  24737. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  24738. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  24739. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  24740. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  24741. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  24742. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  24743. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  24744. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  24745. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  24746. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  24747. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  24748. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  24749. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  24750. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  24751. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  24752. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  24753. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  24754. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  24755. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  24756. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  24757. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  24758. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  24759. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  24760. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  24761. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  24762. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  24763. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  24764. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  24765. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  24766. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  24767. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  24768. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  24769. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  24770. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  24771. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  24772. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  24773. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  24774. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  24775. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  24776. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  24777. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  24778. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  24779. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  24780. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  24781. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  24782. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  24783. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  24784. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  24785. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  24786. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  24787. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  24788. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  24789. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  24790. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  24791. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  24792. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  24793. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  24794. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  24795. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  24796. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  24797. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  24798. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  24799. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  24800. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  24801. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  24802. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  24803. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  24804. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  24805. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  24806. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  24807. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  24808. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  24809. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  24810. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  24811. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  24812. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  24813. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  24814. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  24815. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  24816. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  24817. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  24818. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  24819. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  24820. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  24821. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  24822. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  24823. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  24824. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  24825. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  24826. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  24827. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  24828. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  24829. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  24830. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  24831. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  24832. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  24833. 0x00, 0x00, 0x00, 0x00, 0x00
  24834. };
  24835. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  24836. * !NO_DES3 && !NO_SHA
  24837. */
  24838. /*
  24839. * Testing wc_PKCS7_BER()
  24840. */
  24841. static int test_wc_PKCS7_BER(void)
  24842. {
  24843. EXPECT_DECLS;
  24844. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24845. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  24846. PKCS7* pkcs7 = NULL;
  24847. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  24848. XFILE f = XBADFILE;
  24849. byte der[4096];
  24850. #ifndef NO_DES3
  24851. byte decoded[2048];
  24852. #endif
  24853. word32 derSz = 0;
  24854. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  24855. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24856. if (f != XBADFILE) {
  24857. XFCLOSE(f);
  24858. f = XBADFILE;
  24859. }
  24860. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24861. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24862. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24863. #ifndef NO_RSA
  24864. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24865. #else
  24866. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24867. #endif
  24868. wc_PKCS7_Free(pkcs7);
  24869. pkcs7 = NULL;
  24870. #ifndef NO_DES3
  24871. /* decode BER content */
  24872. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  24873. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24874. if (f != XBADFILE) {
  24875. XFCLOSE(f);
  24876. f = XBADFILE;
  24877. }
  24878. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24879. #ifndef NO_RSA
  24880. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24881. #else
  24882. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  24883. #endif
  24884. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  24885. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  24886. if (f != XBADFILE) {
  24887. XFCLOSE(f);
  24888. f = XBADFILE;
  24889. }
  24890. if (pkcs7 != NULL) {
  24891. pkcs7->privateKey = der;
  24892. pkcs7->privateKeySz = derSz;
  24893. }
  24894. #ifndef NO_RSA
  24895. #ifdef WOLFSSL_SP_MATH
  24896. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24897. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  24898. #else
  24899. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24900. sizeof(berContent), decoded, sizeof(decoded)), 0);
  24901. #endif
  24902. #else
  24903. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  24904. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  24905. #endif
  24906. wc_PKCS7_Free(pkcs7);
  24907. #endif /* !NO_DES3 */
  24908. #endif
  24909. return EXPECT_RESULT();
  24910. } /* END test_wc_PKCS7_BER() */
  24911. static int test_wc_PKCS7_signed_enveloped(void)
  24912. {
  24913. EXPECT_DECLS;
  24914. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  24915. !defined(NO_FILESYSTEM)
  24916. XFILE f = XBADFILE;
  24917. PKCS7* pkcs7 = NULL;
  24918. #ifdef HAVE_AES_CBC
  24919. PKCS7* inner = NULL;
  24920. #endif
  24921. void* pt = NULL;
  24922. WC_RNG rng;
  24923. unsigned char key[FOURK_BUF/2];
  24924. unsigned char cert[FOURK_BUF/2];
  24925. unsigned char env[FOURK_BUF];
  24926. int envSz = FOURK_BUF;
  24927. int keySz = 0;
  24928. int certSz = 0;
  24929. unsigned char sig[FOURK_BUF * 2];
  24930. int sigSz = FOURK_BUF * 2;
  24931. #ifdef HAVE_AES_CBC
  24932. unsigned char decoded[FOURK_BUF];
  24933. int decodedSz = FOURK_BUF;
  24934. #endif
  24935. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24936. /* load cert */
  24937. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  24938. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  24939. if (f != XBADFILE) {
  24940. XFCLOSE(f);
  24941. f = XBADFILE;
  24942. }
  24943. /* load key */
  24944. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  24945. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  24946. if (f != XBADFILE) {
  24947. XFCLOSE(f);
  24948. f = XBADFILE;
  24949. }
  24950. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  24951. /* sign cert for envelope */
  24952. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24953. ExpectIntEQ(wc_InitRng(&rng), 0);
  24954. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24955. if (pkcs7 != NULL) {
  24956. pkcs7->content = cert;
  24957. pkcs7->contentSz = certSz;
  24958. pkcs7->contentOID = DATA;
  24959. pkcs7->privateKey = key;
  24960. pkcs7->privateKeySz = keySz;
  24961. pkcs7->encryptOID = RSAk;
  24962. pkcs7->hashOID = SHA256h;
  24963. pkcs7->rng = &rng;
  24964. }
  24965. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  24966. wc_PKCS7_Free(pkcs7);
  24967. pkcs7 = NULL;
  24968. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24969. #ifdef HAVE_AES_CBC
  24970. /* create envelope */
  24971. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24972. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24973. if (pkcs7 != NULL) {
  24974. pkcs7->content = sig;
  24975. pkcs7->contentSz = sigSz;
  24976. pkcs7->contentOID = DATA;
  24977. pkcs7->encryptOID = AES256CBCb;
  24978. pkcs7->privateKey = key;
  24979. pkcs7->privateKeySz = keySz;
  24980. }
  24981. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  24982. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  24983. wc_PKCS7_Free(pkcs7);
  24984. pkcs7 = NULL;
  24985. #endif
  24986. /* create bad signed enveloped data */
  24987. sigSz = FOURK_BUF * 2;
  24988. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  24989. ExpectIntEQ(wc_InitRng(&rng), 0);
  24990. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24991. if (pkcs7 != NULL) {
  24992. pkcs7->content = env;
  24993. pkcs7->contentSz = envSz;
  24994. pkcs7->contentOID = DATA;
  24995. pkcs7->privateKey = key;
  24996. pkcs7->privateKeySz = keySz;
  24997. pkcs7->encryptOID = RSAk;
  24998. pkcs7->hashOID = SHA256h;
  24999. pkcs7->rng = &rng;
  25000. }
  25001. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25002. * free it later. */
  25003. if (pkcs7 != NULL) {
  25004. pt = (void*)pkcs7->certList;
  25005. pkcs7->certList = NULL; /* no certs in bundle */
  25006. }
  25007. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25008. if (pkcs7 != NULL) {
  25009. /* restore pointer for PKCS7 free call */
  25010. pkcs7->certList = (Pkcs7Cert*)pt;
  25011. }
  25012. wc_PKCS7_Free(pkcs7);
  25013. pkcs7 = NULL;
  25014. /* check verify fails */
  25015. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25016. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25017. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25018. PKCS7_SIGNEEDS_CHECK);
  25019. /* try verifying the signature manually */
  25020. {
  25021. RsaKey rKey;
  25022. word32 idx = 0;
  25023. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25024. WC_MAX_DIGEST_SIZE];
  25025. int digestSz;
  25026. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25027. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25028. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  25029. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  25030. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25031. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25032. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  25033. /* verify was success */
  25034. }
  25035. wc_PKCS7_Free(pkcs7);
  25036. pkcs7 = NULL;
  25037. /* initializing the PKCS7 struct with the signing certificate should pass */
  25038. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25039. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25040. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25041. wc_PKCS7_Free(pkcs7);
  25042. pkcs7 = NULL;
  25043. /* create valid degenerate bundle */
  25044. sigSz = FOURK_BUF * 2;
  25045. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25046. if (pkcs7 != NULL) {
  25047. pkcs7->content = env;
  25048. pkcs7->contentSz = envSz;
  25049. pkcs7->contentOID = DATA;
  25050. pkcs7->privateKey = key;
  25051. pkcs7->privateKeySz = keySz;
  25052. pkcs7->encryptOID = RSAk;
  25053. pkcs7->hashOID = SHA256h;
  25054. pkcs7->rng = &rng;
  25055. }
  25056. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25057. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25058. wc_PKCS7_Free(pkcs7);
  25059. pkcs7 = NULL;
  25060. wc_FreeRng(&rng);
  25061. /* check verify */
  25062. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25063. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25064. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25065. ExpectNotNull(pkcs7->content);
  25066. #ifdef HAVE_AES_CBC
  25067. /* check decode */
  25068. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  25069. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25070. if (inner != NULL) {
  25071. inner->privateKey = key;
  25072. inner->privateKeySz = keySz;
  25073. }
  25074. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25075. pkcs7->contentSz, decoded, decodedSz)), 0);
  25076. wc_PKCS7_Free(inner);
  25077. inner = NULL;
  25078. #endif
  25079. wc_PKCS7_Free(pkcs7);
  25080. pkcs7 = NULL;
  25081. #ifdef HAVE_AES_CBC
  25082. /* check cert set */
  25083. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25084. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25085. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25086. ExpectNotNull(pkcs7->singleCert);
  25087. ExpectIntNE(pkcs7->singleCertSz, 0);
  25088. wc_PKCS7_Free(pkcs7);
  25089. pkcs7 = NULL;
  25090. #endif
  25091. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25092. return EXPECT_RESULT();
  25093. }
  25094. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  25095. {
  25096. EXPECT_DECLS;
  25097. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25098. && !defined(NO_AES)
  25099. PKCS7* pkcs7 = NULL;
  25100. void* heap = NULL;
  25101. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25102. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25103. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25104. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25105. wc_PKCS7_Free(pkcs7);
  25106. #endif
  25107. return EXPECT_RESULT();
  25108. }
  25109. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  25110. {
  25111. EXPECT_DECLS;
  25112. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25113. && !defined(NO_AES)
  25114. PKCS7* pkcs7 = NULL;
  25115. void* heap = NULL;
  25116. WOLFSSL_CTX* ctx = NULL;
  25117. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25118. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25119. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25120. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25121. wc_PKCS7_Free(pkcs7);
  25122. #endif
  25123. return EXPECT_RESULT();
  25124. }
  25125. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  25126. {
  25127. EXPECT_DECLS;
  25128. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25129. && !defined(NO_AES)
  25130. PKCS7* pkcs7 = NULL;
  25131. void* heap = NULL;
  25132. WOLFSSL_CTX* ctx = NULL;
  25133. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25134. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25135. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25136. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25137. wc_PKCS7_Free(pkcs7);
  25138. #endif
  25139. return EXPECT_RESULT();
  25140. }
  25141. static int test_wc_PKCS7_DecodeCompressedData(void)
  25142. {
  25143. EXPECT_DECLS;
  25144. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25145. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25146. PKCS7* pkcs7 = NULL;
  25147. void* heap = NULL;
  25148. byte out[4096];
  25149. byte* decompressed = NULL;
  25150. int outSz;
  25151. int decompressedSz;
  25152. const char* cert = "./certs/client-cert.pem";
  25153. byte* cert_buf = NULL;
  25154. size_t cert_sz = 0;
  25155. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25156. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  25157. DYNAMIC_TYPE_TMP_BUFFER)));
  25158. decompressedSz = (int)cert_sz;
  25159. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25160. if (pkcs7 != NULL) {
  25161. pkcs7->content = (byte*)cert_buf;
  25162. pkcs7->contentSz = (word32)cert_sz;
  25163. pkcs7->contentOID = DATA;
  25164. }
  25165. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25166. sizeof(out))), 0);
  25167. wc_PKCS7_Free(pkcs7);
  25168. pkcs7 = NULL;
  25169. /* compressed key should be smaller than when started */
  25170. ExpectIntLT(outSz, cert_sz);
  25171. /* test decompression */
  25172. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25173. ExpectIntEQ(pkcs7->contentOID, 0);
  25174. /* fail case with out buffer too small */
  25175. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25176. decompressed, outSz), 0);
  25177. /* success case */
  25178. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25179. decompressed, decompressedSz), cert_sz);
  25180. ExpectIntEQ(pkcs7->contentOID, DATA);
  25181. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25182. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25183. decompressed = NULL;
  25184. /* test decompression function with different 'max' inputs */
  25185. outSz = sizeof(out);
  25186. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25187. 0);
  25188. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25189. out, outSz, 0, heap), 0);
  25190. ExpectNull(decompressed);
  25191. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25192. out, outSz, 0, heap), 0);
  25193. ExpectNotNull(decompressed);
  25194. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25195. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25196. decompressed = NULL;
  25197. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25198. out, outSz, 0, heap), 0);
  25199. ExpectNotNull(decompressed);
  25200. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25201. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25202. if (cert_buf != NULL)
  25203. free(cert_buf);
  25204. wc_PKCS7_Free(pkcs7);
  25205. #endif
  25206. return EXPECT_RESULT();
  25207. }
  25208. static int test_wc_i2d_PKCS12(void)
  25209. {
  25210. EXPECT_DECLS;
  25211. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25212. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25213. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25214. WC_PKCS12* pkcs12 = NULL;
  25215. unsigned char der[FOURK_BUF * 2];
  25216. unsigned char* pt;
  25217. int derSz = 0;
  25218. unsigned char out[FOURK_BUF * 2];
  25219. int outSz = FOURK_BUF * 2;
  25220. const char p12_f[] = "./certs/test-servercert.p12";
  25221. XFILE f = XBADFILE;
  25222. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  25223. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  25224. if (f != XBADFILE)
  25225. XFCLOSE(f);
  25226. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25227. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25228. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25229. ExpectIntEQ(outSz, derSz);
  25230. outSz = derSz - 1;
  25231. pt = out;
  25232. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25233. outSz = derSz;
  25234. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25235. ExpectIntEQ((pt == out), 0);
  25236. pt = NULL;
  25237. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25238. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25239. wc_PKCS12_free(pkcs12);
  25240. pkcs12 = NULL;
  25241. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25242. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  25243. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25244. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25245. ExpectIntEQ(outSz, derSz);
  25246. wc_PKCS12_free(pkcs12);
  25247. pkcs12 = NULL;
  25248. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25249. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25250. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25251. ExpectIntEQ(outSz, derSz);
  25252. wc_PKCS12_free(pkcs12);
  25253. pkcs12 = NULL;
  25254. #endif
  25255. return EXPECT_RESULT();
  25256. }
  25257. /* Testing wc_SignatureGetSize() for signature type ECC */
  25258. static int test_wc_SignatureGetSize_ecc(void)
  25259. {
  25260. EXPECT_DECLS;
  25261. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  25262. enum wc_SignatureType sig_type;
  25263. word32 key_len;
  25264. ecc_key ecc;
  25265. const char* qx =
  25266. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25267. const char* qy =
  25268. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25269. const char* d =
  25270. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25271. XMEMSET(&ecc, 0, sizeof(ecc_key));
  25272. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  25273. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  25274. /* Input for signature type ECC */
  25275. sig_type = WC_SIGNATURE_TYPE_ECC;
  25276. key_len = sizeof(ecc_key);
  25277. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  25278. /* Test bad args */
  25279. sig_type = (enum wc_SignatureType) 100;
  25280. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25281. sig_type = WC_SIGNATURE_TYPE_ECC;
  25282. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25283. key_len = (word32)0;
  25284. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  25285. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  25286. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  25287. return EXPECT_RESULT();
  25288. } /* END test_wc_SignatureGetSize_ecc() */
  25289. /* Testing wc_SignatureGetSize() for signature type rsa */
  25290. static int test_wc_SignatureGetSize_rsa(void)
  25291. {
  25292. EXPECT_DECLS;
  25293. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  25294. enum wc_SignatureType sig_type;
  25295. word32 key_len;
  25296. word32 idx = 0;
  25297. RsaKey rsa_key;
  25298. byte* tmp = NULL;
  25299. size_t bytes;
  25300. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  25301. #ifdef USE_CERT_BUFFERS_1024
  25302. bytes = (size_t)sizeof_client_key_der_1024;
  25303. if (bytes < (size_t)sizeof_client_key_der_1024)
  25304. bytes = (size_t)sizeof_client_cert_der_1024;
  25305. #elif defined(USE_CERT_BUFFERS_2048)
  25306. bytes = (size_t)sizeof_client_key_der_2048;
  25307. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25308. bytes = (size_t)sizeof_client_cert_der_2048;
  25309. #else
  25310. bytes = FOURK_BUF;
  25311. #endif
  25312. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  25313. DYNAMIC_TYPE_TMP_BUFFER));
  25314. if (tmp != NULL) {
  25315. #ifdef USE_CERT_BUFFERS_1024
  25316. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  25317. #elif defined(USE_CERT_BUFFERS_2048)
  25318. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  25319. #elif !defined(NO_FILESYSTEM)
  25320. XFILE file = XBADFILE;
  25321. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  25322. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  25323. if (file != XBADFILE)
  25324. XFCLOSE(file);
  25325. }
  25326. #else
  25327. ExpectFail();
  25328. #endif
  25329. }
  25330. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  25331. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  25332. /* Input for signature type RSA */
  25333. sig_type = WC_SIGNATURE_TYPE_RSA;
  25334. key_len = sizeof(RsaKey);
  25335. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  25336. /* Test bad args */
  25337. sig_type = (enum wc_SignatureType)100;
  25338. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25339. sig_type = WC_SIGNATURE_TYPE_RSA;
  25340. #ifndef HAVE_USER_RSA
  25341. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  25342. #else
  25343. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  25344. #endif
  25345. key_len = (word32)0;
  25346. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  25347. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  25348. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25349. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  25350. return EXPECT_RESULT();
  25351. } /* END test_wc_SignatureGetSize_rsa(void) */
  25352. /*----------------------------------------------------------------------------*
  25353. | hash.h Tests
  25354. *----------------------------------------------------------------------------*/
  25355. static int test_wc_HashInit(void)
  25356. {
  25357. EXPECT_DECLS;
  25358. int i; /* 0 indicates tests passed, 1 indicates failure */
  25359. wc_HashAlg hash;
  25360. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25361. enum wc_HashType enumArray[] = {
  25362. #ifndef NO_MD5
  25363. WC_HASH_TYPE_MD5,
  25364. #endif
  25365. #ifndef NO_SHA
  25366. WC_HASH_TYPE_SHA,
  25367. #endif
  25368. #ifdef WOLFSSL_SHA224
  25369. WC_HASH_TYPE_SHA224,
  25370. #endif
  25371. #ifndef NO_SHA256
  25372. WC_HASH_TYPE_SHA256,
  25373. #endif
  25374. #ifdef WOLFSSL_SHA384
  25375. WC_HASH_TYPE_SHA384,
  25376. #endif
  25377. #ifdef WOLFSSL_SHA512
  25378. WC_HASH_TYPE_SHA512,
  25379. #endif
  25380. };
  25381. /* dynamically finds the length */
  25382. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25383. /* For loop to test various arguments... */
  25384. for (i = 0; i < enumlen; i++) {
  25385. /* check for bad args */
  25386. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25387. wc_HashFree(&hash, enumArray[i]);
  25388. /* check for null ptr */
  25389. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  25390. } /* end of for loop */
  25391. return EXPECT_RESULT();
  25392. } /* end of test_wc_HashInit */
  25393. /*
  25394. * Unit test function for wc_HashSetFlags()
  25395. */
  25396. static int test_wc_HashSetFlags(void)
  25397. {
  25398. EXPECT_DECLS;
  25399. #ifdef WOLFSSL_HASH_FLAGS
  25400. wc_HashAlg hash;
  25401. word32 flags = 0;
  25402. int i, j;
  25403. int notSupportedLen;
  25404. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25405. enum wc_HashType enumArray[] = {
  25406. #ifndef NO_MD5
  25407. WC_HASH_TYPE_MD5,
  25408. #endif
  25409. #ifndef NO_SHA
  25410. WC_HASH_TYPE_SHA,
  25411. #endif
  25412. #ifdef WOLFSSL_SHA224
  25413. WC_HASH_TYPE_SHA224,
  25414. #endif
  25415. #ifndef NO_SHA256
  25416. WC_HASH_TYPE_SHA256,
  25417. #endif
  25418. #ifdef WOLFSSL_SHA384
  25419. WC_HASH_TYPE_SHA384,
  25420. #endif
  25421. #ifdef WOLFSSL_SHA512
  25422. WC_HASH_TYPE_SHA512,
  25423. #endif
  25424. #ifdef WOLFSSL_SHA3
  25425. WC_HASH_TYPE_SHA3_224,
  25426. #endif
  25427. };
  25428. enum wc_HashType notSupported[] = {
  25429. WC_HASH_TYPE_MD5_SHA,
  25430. WC_HASH_TYPE_MD2,
  25431. WC_HASH_TYPE_MD4,
  25432. WC_HASH_TYPE_BLAKE2B,
  25433. WC_HASH_TYPE_BLAKE2S,
  25434. WC_HASH_TYPE_NONE,
  25435. };
  25436. /* dynamically finds the length */
  25437. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25438. /* For loop to test various arguments... */
  25439. for (i = 0; i < enumlen; i++) {
  25440. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25441. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  25442. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25443. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  25444. wc_HashFree(&hash, enumArray[i]);
  25445. }
  25446. /* For loop to test not supported cases */
  25447. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25448. for (j = 0; j < notSupportedLen; j++) {
  25449. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25450. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  25451. BAD_FUNC_ARG);
  25452. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25453. }
  25454. #endif
  25455. return EXPECT_RESULT();
  25456. } /* END test_wc_HashSetFlags */
  25457. /*
  25458. * Unit test function for wc_HashGetFlags()
  25459. */
  25460. static int test_wc_HashGetFlags(void)
  25461. {
  25462. EXPECT_DECLS;
  25463. #ifdef WOLFSSL_HASH_FLAGS
  25464. wc_HashAlg hash;
  25465. word32 flags = 0;
  25466. int i, j;
  25467. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25468. enum wc_HashType enumArray[] = {
  25469. #ifndef NO_MD5
  25470. WC_HASH_TYPE_MD5,
  25471. #endif
  25472. #ifndef NO_SHA
  25473. WC_HASH_TYPE_SHA,
  25474. #endif
  25475. #ifdef WOLFSSL_SHA224
  25476. WC_HASH_TYPE_SHA224,
  25477. #endif
  25478. #ifndef NO_SHA256
  25479. WC_HASH_TYPE_SHA256,
  25480. #endif
  25481. #ifdef WOLFSSL_SHA384
  25482. WC_HASH_TYPE_SHA384,
  25483. #endif
  25484. #ifdef WOLFSSL_SHA512
  25485. WC_HASH_TYPE_SHA512,
  25486. #endif
  25487. #ifdef WOLFSSL_SHA3
  25488. WC_HASH_TYPE_SHA3_224,
  25489. #endif
  25490. };
  25491. enum wc_HashType notSupported[] = {
  25492. WC_HASH_TYPE_MD5_SHA,
  25493. WC_HASH_TYPE_MD2,
  25494. WC_HASH_TYPE_MD4,
  25495. WC_HASH_TYPE_BLAKE2B,
  25496. WC_HASH_TYPE_BLAKE2S,
  25497. WC_HASH_TYPE_NONE,
  25498. };
  25499. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25500. int notSupportedLen;
  25501. /* For loop to test various arguments... */
  25502. for (i = 0; i < enumlen; i++) {
  25503. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  25504. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  25505. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  25506. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  25507. wc_HashFree(&hash, enumArray[i]);
  25508. }
  25509. /* For loop to test not supported cases */
  25510. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25511. for (j = 0; j < notSupportedLen; j++) {
  25512. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  25513. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  25514. BAD_FUNC_ARG);
  25515. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  25516. }
  25517. #endif
  25518. return EXPECT_RESULT();
  25519. } /* END test_wc_HashGetFlags */
  25520. /*----------------------------------------------------------------------------*
  25521. | Compatibility Tests
  25522. *----------------------------------------------------------------------------*/
  25523. /*----------------------------------------------------------------------------*
  25524. | ASN.1 Tests
  25525. *----------------------------------------------------------------------------*/
  25526. static int test_wolfSSL_ASN1_BIT_STRING(void)
  25527. {
  25528. EXPECT_DECLS;
  25529. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  25530. ASN1_BIT_STRING* str = NULL;
  25531. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25532. /* Empty data testing. */
  25533. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  25534. ASN1_BIT_STRING_free(str);
  25535. str = NULL;
  25536. ExpectNotNull(str = ASN1_BIT_STRING_new());
  25537. /* Invalid parameter testing. */
  25538. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  25539. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  25540. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  25541. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  25542. /* No bit string - bit is always 0. */
  25543. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  25544. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  25545. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25546. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  25547. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  25548. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  25549. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  25550. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  25551. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  25552. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  25553. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  25554. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  25555. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  25556. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  25557. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  25558. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  25559. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  25560. ASN1_BIT_STRING_free(str);
  25561. ASN1_BIT_STRING_free(NULL);
  25562. #endif
  25563. return EXPECT_RESULT();
  25564. }
  25565. static int test_wolfSSL_ASN1_INTEGER(void)
  25566. {
  25567. EXPECT_DECLS;
  25568. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25569. ASN1_INTEGER* a = NULL;
  25570. ASN1_INTEGER* dup = NULL;
  25571. const unsigned char invalidLenDer[] = {
  25572. 0x02, 0x20, 0x00
  25573. };
  25574. const unsigned char longDer[] = {
  25575. 0x02, 0x20,
  25576. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25577. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25578. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  25579. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  25580. };
  25581. const unsigned char* p;
  25582. /* Invalid parameter testing. */
  25583. ASN1_INTEGER_free(NULL);
  25584. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  25585. ExpectNotNull(a = ASN1_INTEGER_new());
  25586. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25587. ASN1_INTEGER_free(dup);
  25588. dup = NULL;
  25589. ASN1_INTEGER_free(a);
  25590. a = NULL;
  25591. p = longDer;
  25592. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  25593. p = longDer;
  25594. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  25595. ExpectPtrNE(p, longDer);
  25596. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  25597. ASN1_INTEGER_free(dup);
  25598. ASN1_INTEGER_free(a);
  25599. #endif
  25600. return EXPECT_RESULT();
  25601. }
  25602. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  25603. {
  25604. EXPECT_DECLS;
  25605. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25606. ASN1_INTEGER* a = NULL;
  25607. ASN1_INTEGER* b = NULL;
  25608. ExpectNotNull(a = ASN1_INTEGER_new());
  25609. ExpectNotNull(b = ASN1_INTEGER_new());
  25610. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  25611. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25612. /* Invalid parameter testing. */
  25613. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  25614. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  25615. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  25616. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25617. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  25618. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25619. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  25620. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25621. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  25622. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25623. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  25624. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  25625. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25626. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  25627. ASN1_INTEGER_free(b);
  25628. ASN1_INTEGER_free(a);
  25629. #endif
  25630. return EXPECT_RESULT();
  25631. }
  25632. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  25633. {
  25634. EXPECT_DECLS;
  25635. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25636. ASN1_INTEGER* ai = NULL;
  25637. ASN1_INTEGER* ai2 = NULL;
  25638. BIGNUM* bn = NULL;
  25639. BIGNUM* bn2 = NULL;
  25640. ExpectNotNull(ai = ASN1_INTEGER_new());
  25641. ExpectNotNull(bn2 = BN_new());
  25642. /* Invalid parameter testing. */
  25643. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  25644. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  25645. /* at the moment hard setting since no set function */
  25646. if (ai != NULL) {
  25647. ai->data[0] = 0xff; /* No DER encoding. */
  25648. ai->length = 1;
  25649. }
  25650. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25651. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25652. BN_free(bn);
  25653. bn = NULL;
  25654. #else
  25655. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25656. #endif
  25657. if (ai != NULL) {
  25658. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25659. ai->data[1] = 0x04; /* bad length of integer */
  25660. ai->data[2] = 0x03;
  25661. ai->length = 3;
  25662. }
  25663. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25664. /* Interpreted as a number 0x020403. */
  25665. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25666. BN_free(bn);
  25667. bn = NULL;
  25668. #else
  25669. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  25670. #endif
  25671. if (ai != NULL) {
  25672. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25673. ai->data[1] = 0x01; /* length of integer */
  25674. ai->data[2] = 0x03;
  25675. ai->length = 3;
  25676. }
  25677. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  25678. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  25679. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25680. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25681. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25682. if (ai != NULL) {
  25683. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25684. ai->data[1] = 0x02; /* length of integer */
  25685. ai->data[2] = 0x00; /* padding byte to ensure positive */
  25686. ai->data[3] = 0xff;
  25687. ai->length = 4;
  25688. }
  25689. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25690. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25691. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25692. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25693. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25694. if (ai != NULL) {
  25695. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25696. ai->data[1] = 0x01; /* length of integer */
  25697. ai->data[2] = 0x00;
  25698. ai->length = 3;
  25699. }
  25700. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25701. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25702. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25703. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25704. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25705. if (ai != NULL) {
  25706. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  25707. ai->data[1] = 0x01; /* length of integer */
  25708. ai->data[2] = 0x01;
  25709. ai->length = 3;
  25710. ai->negative = 1;
  25711. }
  25712. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  25713. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  25714. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  25715. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  25716. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  25717. BN_free(bn2);
  25718. BN_free(bn);
  25719. ASN1_INTEGER_free(ai2);
  25720. ASN1_INTEGER_free(ai);
  25721. #endif
  25722. return EXPECT_RESULT();
  25723. }
  25724. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  25725. {
  25726. EXPECT_DECLS;
  25727. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25728. ASN1_INTEGER *a = NULL;
  25729. long val;
  25730. ExpectNotNull(a = ASN1_INTEGER_new());
  25731. /* Invalid parameter testing. */
  25732. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  25733. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  25734. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  25735. #else
  25736. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  25737. #endif
  25738. ASN1_INTEGER_free(a);
  25739. a = NULL;
  25740. ExpectNotNull(a = ASN1_INTEGER_new());
  25741. val = 0;
  25742. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  25743. ASN1_INTEGER_free(a);
  25744. a = NULL;
  25745. /* 0 */
  25746. ExpectNotNull(a = ASN1_INTEGER_new());
  25747. val = 0;
  25748. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25749. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25750. ASN1_INTEGER_free(a);
  25751. a = NULL;
  25752. /* 40 */
  25753. ExpectNotNull(a = ASN1_INTEGER_new());
  25754. val = 40;
  25755. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25756. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25757. ASN1_INTEGER_free(a);
  25758. a = NULL;
  25759. /* -40 */
  25760. ExpectNotNull(a = ASN1_INTEGER_new());
  25761. val = -40;
  25762. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25763. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25764. ASN1_INTEGER_free(a);
  25765. a = NULL;
  25766. /* 128 */
  25767. ExpectNotNull(a = ASN1_INTEGER_new());
  25768. val = 128;
  25769. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25770. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25771. ASN1_INTEGER_free(a);
  25772. a = NULL;
  25773. /* -128 */
  25774. ExpectNotNull(a = ASN1_INTEGER_new());
  25775. val = -128;
  25776. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25777. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25778. ASN1_INTEGER_free(a);
  25779. a = NULL;
  25780. /* 200 */
  25781. ExpectNotNull(a = ASN1_INTEGER_new());
  25782. val = 200;
  25783. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25784. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25785. ASN1_INTEGER_free(a);
  25786. a = NULL;
  25787. /* int max (2147483647) */
  25788. ExpectNotNull(a = ASN1_INTEGER_new());
  25789. val = 2147483647;
  25790. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25791. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25792. ASN1_INTEGER_free(a);
  25793. a = NULL;
  25794. /* int min (-2147483648) */
  25795. ExpectNotNull(a = ASN1_INTEGER_new());
  25796. val = -2147483647 - 1;
  25797. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25798. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25799. ASN1_INTEGER_free(a);
  25800. a = NULL;
  25801. /* long max positive */
  25802. ExpectNotNull(a = ASN1_INTEGER_new());
  25803. val = (long)(((unsigned long)-1) >> 1);
  25804. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  25805. ExpectTrue(ASN1_INTEGER_get(a) == val);
  25806. ASN1_INTEGER_free(a);
  25807. #endif
  25808. return EXPECT_RESULT();
  25809. }
  25810. #if defined(OPENSSL_EXTRA)
  25811. typedef struct ASN1IntTestVector {
  25812. const byte* der;
  25813. const size_t derSz;
  25814. const long value;
  25815. } ASN1IntTestVector;
  25816. #endif
  25817. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  25818. {
  25819. EXPECT_DECLS;
  25820. #if defined(OPENSSL_EXTRA)
  25821. size_t i;
  25822. WOLFSSL_ASN1_INTEGER* a = NULL;
  25823. WOLFSSL_ASN1_INTEGER* b = NULL;
  25824. WOLFSSL_ASN1_INTEGER* c = NULL;
  25825. const byte* p = NULL;
  25826. byte* p2 = NULL;
  25827. byte* reEncoded = NULL;
  25828. int reEncodedSz;
  25829. static const byte zeroDer[] = {
  25830. 0x02, 0x01, 0x00
  25831. };
  25832. static const byte oneDer[] = {
  25833. 0x02, 0x01, 0x01
  25834. };
  25835. static const byte negativeDer[] = {
  25836. 0x02, 0x03, 0xC1, 0x16, 0x0D
  25837. };
  25838. static const byte positiveDer[] = {
  25839. 0x02, 0x03, 0x01, 0x00, 0x01
  25840. };
  25841. static const byte primeDer[] = {
  25842. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  25843. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  25844. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  25845. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  25846. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  25847. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  25848. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  25849. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  25850. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  25851. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  25852. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  25853. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  25854. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  25855. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  25856. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  25857. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  25858. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  25859. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  25860. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  25861. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  25862. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  25863. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  25864. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  25865. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  25866. };
  25867. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  25868. static const ASN1IntTestVector testVectors[] = {
  25869. {zeroDer, sizeof(zeroDer), 0},
  25870. {oneDer, sizeof(oneDer), 1},
  25871. {negativeDer, sizeof(negativeDer), -4123123},
  25872. {positiveDer, sizeof(positiveDer), 65537},
  25873. {primeDer, sizeof(primeDer), 0}
  25874. };
  25875. static const size_t NUM_TEST_VECTORS =
  25876. sizeof(testVectors)/sizeof(testVectors[0]);
  25877. /* Check d2i error conditions */
  25878. /* NULL pointer to input. */
  25879. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  25880. ExpectNull(b);
  25881. /* NULL input. */
  25882. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  25883. ExpectNull(b);
  25884. /* 0 length. */
  25885. p = testVectors[0].der;
  25886. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  25887. ExpectNull(b);
  25888. /* Negative length. */
  25889. p = testVectors[0].der;
  25890. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  25891. ExpectNull(b);
  25892. /* Garbage DER input. */
  25893. p = garbageDer;
  25894. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  25895. ExpectNull(b);
  25896. /* Check i2d error conditions */
  25897. /* NULL input. */
  25898. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  25899. /* 0 length input data buffer (a->length == 0). */
  25900. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  25901. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  25902. if (a != NULL)
  25903. a->data = NULL;
  25904. /* NULL input data buffer. */
  25905. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  25906. if (a != NULL) {
  25907. /* Reset a->data. */
  25908. a->data = a->intData;
  25909. }
  25910. /* Set a to valid value. */
  25911. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  25912. /* NULL output buffer. */
  25913. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  25914. wolfSSL_ASN1_INTEGER_free(a);
  25915. a = NULL;
  25916. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  25917. p = testVectors[i].der;
  25918. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  25919. testVectors[i].derSz));
  25920. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  25921. if (testVectors[i].derSz <= sizeof(long)) {
  25922. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  25923. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  25924. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  25925. wolfSSL_ASN1_INTEGER_free(c);
  25926. c = NULL;
  25927. }
  25928. /* Convert to DER without a pre-allocated output buffer. */
  25929. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  25930. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  25931. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  25932. /* Convert to DER with a pre-allocated output buffer. In this case, the
  25933. * output buffer pointer should be incremented just past the end of the
  25934. * encoded data. */
  25935. p2 = reEncoded;
  25936. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  25937. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  25938. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  25939. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  25940. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  25941. reEncoded = NULL;
  25942. wolfSSL_ASN1_INTEGER_free(a);
  25943. a = NULL;
  25944. }
  25945. #endif /* OPENSSL_EXTRA */
  25946. return EXPECT_RESULT();
  25947. }
  25948. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  25949. {
  25950. EXPECT_DECLS;
  25951. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  25952. BIO* bio = NULL;
  25953. BIO* out = NULL;
  25954. BIO* fixed = NULL;
  25955. ASN1_INTEGER* ai = NULL;
  25956. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  25957. char tmp[1024];
  25958. int tmpSz;
  25959. const char expected1[] = "123456";
  25960. const char expected2[] = "112345678912345678901234567890";
  25961. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  25962. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  25963. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  25964. ExpectNotNull(ai = ASN1_INTEGER_new());
  25965. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  25966. /* Invalid parameter testing. */
  25967. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  25968. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  25969. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  25970. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  25971. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  25972. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  25973. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  25974. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  25975. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  25976. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  25977. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  25978. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  25979. /* No data to read from BIO. */
  25980. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  25981. /* read first line */
  25982. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  25983. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  25984. XMEMSET(tmp, 0, 1024);
  25985. tmpSz = BIO_read(out, tmp, 1024);
  25986. ExpectIntEQ(tmpSz, 6);
  25987. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  25988. /* fail on second line (not % 2) */
  25989. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  25990. /* read 3rd long line */
  25991. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  25992. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  25993. XMEMSET(tmp, 0, 1024);
  25994. tmpSz = BIO_read(out, tmp, 1024);
  25995. ExpectIntEQ(tmpSz, 30);
  25996. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  25997. /* fail on empty line */
  25998. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  25999. BIO_free(bio);
  26000. bio = NULL;
  26001. /* Make long integer, requiring dynamic memory, even longer. */
  26002. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  26003. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26004. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  26005. XMEMSET(tmp, 0, 1024);
  26006. tmpSz = BIO_read(out, tmp, 1024);
  26007. ExpectIntEQ(tmpSz, 48);
  26008. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  26009. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  26010. XMEMSET(tmp, 0, 1024);
  26011. tmpSz = BIO_read(out, tmp, 1024);
  26012. ExpectIntEQ(tmpSz, 56);
  26013. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  26014. BIO_free(bio);
  26015. BIO_free(out);
  26016. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26017. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26018. /* Ensure there is 0 bytes available to write into. */
  26019. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  26020. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26021. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26022. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  26023. BIO_free(fixed);
  26024. ASN1_INTEGER_free(ai);
  26025. #endif
  26026. return EXPECT_RESULT();
  26027. }
  26028. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  26029. {
  26030. EXPECT_DECLS;
  26031. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26032. ASN1_INTEGER *a = NULL;
  26033. unsigned char *pp,*tpp;
  26034. int ret;
  26035. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  26036. /* Invalid parameter testing. */
  26037. /* Set pp to an invalid value. */
  26038. pp = NULL;
  26039. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  26040. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  26041. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  26042. /* 40 */
  26043. if (a != NULL) {
  26044. a->intData[0] = ASN_INTEGER;
  26045. a->intData[1] = 1;
  26046. a->intData[2] = 40;
  26047. }
  26048. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26049. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26050. DYNAMIC_TYPE_TMP_BUFFER));
  26051. tpp = pp;
  26052. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26053. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26054. tpp--;
  26055. ExpectIntEQ(*tpp, 40);
  26056. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26057. pp = NULL;
  26058. /* 128 */
  26059. if (a != NULL) {
  26060. a->intData[0] = ASN_INTEGER;
  26061. a->intData[1] = 1;
  26062. a->intData[2] = 128;
  26063. }
  26064. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26065. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26066. DYNAMIC_TYPE_TMP_BUFFER));
  26067. tpp = pp;
  26068. if (tpp != NULL) {
  26069. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26070. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26071. tpp--;
  26072. ExpectIntEQ(*(tpp--), 128);
  26073. ExpectIntEQ(*tpp, 0);
  26074. }
  26075. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26076. pp = NULL;
  26077. /* -40 */
  26078. if (a != NULL) {
  26079. a->intData[0] = ASN_INTEGER;
  26080. a->intData[1] = 1;
  26081. a->intData[2] = 40;
  26082. a->negative = 1;
  26083. }
  26084. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26085. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26086. DYNAMIC_TYPE_TMP_BUFFER));
  26087. tpp = pp;
  26088. if (tpp != NULL) {
  26089. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26090. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26091. tpp--;
  26092. ExpectIntEQ(*tpp, 216);
  26093. }
  26094. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26095. pp = NULL;
  26096. /* -128 */
  26097. if (a != NULL) {
  26098. a->intData[0] = ASN_INTEGER;
  26099. a->intData[1] = 1;
  26100. a->intData[2] = 128;
  26101. a->negative = 1;
  26102. }
  26103. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26104. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26105. DYNAMIC_TYPE_TMP_BUFFER));
  26106. tpp = pp;
  26107. if (tpp != NULL) {
  26108. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26109. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26110. tpp--;
  26111. ExpectIntEQ(*tpp, 128);
  26112. }
  26113. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26114. pp = NULL;
  26115. /* -200 */
  26116. if (a != NULL) {
  26117. a->intData[0] = ASN_INTEGER;
  26118. a->intData[1] = 1;
  26119. a->intData[2] = 200;
  26120. a->negative = 1;
  26121. }
  26122. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26123. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26124. DYNAMIC_TYPE_TMP_BUFFER));
  26125. tpp = pp;
  26126. if (tpp != NULL) {
  26127. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26128. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26129. tpp--;
  26130. ExpectIntEQ(*(tpp--), 56);
  26131. ExpectIntEQ(*tpp, 255);
  26132. }
  26133. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26134. pp = NULL;
  26135. /* Empty */
  26136. if (a != NULL) {
  26137. a->intData[0] = ASN_INTEGER;
  26138. a->intData[1] = 0;
  26139. a->negative = 0;
  26140. }
  26141. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26142. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26143. DYNAMIC_TYPE_TMP_BUFFER));
  26144. tpp = pp;
  26145. if (tpp != NULL) {
  26146. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26147. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26148. tpp--;
  26149. ExpectIntEQ(*tpp, 0);
  26150. }
  26151. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26152. pp = NULL;
  26153. /* 0 */
  26154. if (a != NULL) {
  26155. a->intData[0] = ASN_INTEGER;
  26156. a->intData[1] = 1;
  26157. a->intData[2] = 0;
  26158. a->negative = 1;
  26159. }
  26160. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  26161. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26162. DYNAMIC_TYPE_TMP_BUFFER));
  26163. if (tpp != NULL) {
  26164. tpp = pp;
  26165. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26166. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  26167. tpp--;
  26168. ExpectIntEQ(*tpp, 0);
  26169. }
  26170. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26171. pp = NULL;
  26172. /* 0x100 */
  26173. if (a != NULL) {
  26174. a->intData[0] = ASN_INTEGER;
  26175. a->intData[1] = 2;
  26176. a->intData[2] = 0x01;
  26177. a->intData[3] = 0x00;
  26178. a->negative = 0;
  26179. }
  26180. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26181. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26182. DYNAMIC_TYPE_TMP_BUFFER));
  26183. if (tpp != NULL) {
  26184. tpp = pp;
  26185. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26186. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26187. tpp -= 2;
  26188. ExpectIntEQ(tpp[0], 0x01);
  26189. ExpectIntEQ(tpp[1], 0x00);
  26190. }
  26191. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26192. pp = NULL;
  26193. /* -0x8000 => 0x8000 */
  26194. if (a != NULL) {
  26195. a->intData[0] = ASN_INTEGER;
  26196. a->intData[1] = 2;
  26197. a->intData[2] = 0x80;
  26198. a->intData[3] = 0x00;
  26199. a->negative = 1;
  26200. }
  26201. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  26202. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26203. DYNAMIC_TYPE_TMP_BUFFER));
  26204. tpp = pp;
  26205. if (tpp != NULL) {
  26206. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26207. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  26208. tpp -= 2;
  26209. ExpectIntEQ(tpp[0], 0x80);
  26210. ExpectIntEQ(tpp[1], 0x00);
  26211. }
  26212. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26213. pp = NULL;
  26214. /* -0x8001 => 0xFF7FFF */
  26215. if (a != NULL) {
  26216. a->intData[0] = ASN_INTEGER;
  26217. a->intData[1] = 2;
  26218. a->intData[2] = 0x80;
  26219. a->intData[3] = 0x01;
  26220. a->negative = 1;
  26221. }
  26222. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  26223. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  26224. DYNAMIC_TYPE_TMP_BUFFER));
  26225. tpp = pp;
  26226. if (tpp != NULL) {
  26227. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  26228. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  26229. tpp -= 3;
  26230. ExpectIntEQ(tpp[0], 0xFF);
  26231. ExpectIntEQ(tpp[1], 0x7F);
  26232. ExpectIntEQ(tpp[2], 0xFF);
  26233. }
  26234. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26235. wolfSSL_ASN1_INTEGER_free(a);
  26236. #endif /* OPENSSL_EXTRA && !NO_ASN */
  26237. return EXPECT_RESULT();
  26238. }
  26239. static int test_wolfSSL_ASN1_OBJECT(void)
  26240. {
  26241. EXPECT_DECLS;
  26242. #if defined(OPENSSL_EXTRA)
  26243. ASN1_OBJECT* a = NULL;
  26244. ASN1_OBJECT s;
  26245. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  26246. /* Invalid parameter testing. */
  26247. ASN1_OBJECT_free(NULL);
  26248. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  26249. /* Test that a static ASN1_OBJECT can be freed. */
  26250. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26251. ASN1_OBJECT_free(&s);
  26252. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26253. ASN1_OBJECT_free(a);
  26254. a = NULL;
  26255. s.obj = der;
  26256. s.objSz = sizeof(der);
  26257. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  26258. ASN1_OBJECT_free(a);
  26259. ASN1_OBJECT_free(&s);
  26260. #endif /* OPENSSL_EXTRA */
  26261. return EXPECT_RESULT();
  26262. }
  26263. static int test_wolfSSL_ASN1_get_object(void)
  26264. {
  26265. EXPECT_DECLS;
  26266. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  26267. const unsigned char* derBuf = cliecc_cert_der_256;
  26268. const unsigned char* nullPtr = NULL;
  26269. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  26270. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  26271. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  26272. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  26273. const unsigned char* p;
  26274. unsigned char objDer[8];
  26275. unsigned char* der;
  26276. unsigned char* derPtr;
  26277. int len = sizeof_cliecc_cert_der_256;
  26278. long asnLen = 0;
  26279. int tag = 0;
  26280. int cls = 0;
  26281. ASN1_OBJECT* a = NULL;
  26282. ASN1_OBJECT s;
  26283. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  26284. /* Invalid encoding at length. */
  26285. p = objDerInvalidLen;
  26286. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26287. 0x80);
  26288. p = objDerBadLen;
  26289. /* Error = 0x80, Constructed = 0x20 */
  26290. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  26291. 0x80 | 0x20);
  26292. /* Read a couple TLV triplets and make sure they match the expected values
  26293. */
  26294. /* SEQUENCE */
  26295. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  26296. ExpectIntEQ(asnLen, 862);
  26297. ExpectIntEQ(tag, 0x10);
  26298. ExpectIntEQ(cls, 0);
  26299. /* SEQUENCE */
  26300. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26301. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26302. ExpectIntEQ(asnLen, 772);
  26303. ExpectIntEQ(tag, 0x10);
  26304. ExpectIntEQ(cls, 0);
  26305. /* [0] */
  26306. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26307. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26308. ExpectIntEQ(asnLen, 3);
  26309. ExpectIntEQ(tag, 0);
  26310. ExpectIntEQ(cls, 0x80);
  26311. /* INTEGER */
  26312. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26313. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26314. ExpectIntEQ(asnLen, 1);
  26315. ExpectIntEQ(tag, 0x2);
  26316. ExpectIntEQ(cls, 0);
  26317. derBuf += asnLen;
  26318. /* INTEGER */
  26319. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26320. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26321. ExpectIntEQ(asnLen, 20);
  26322. ExpectIntEQ(tag, 0x2);
  26323. ExpectIntEQ(cls, 0);
  26324. derBuf += asnLen;
  26325. /* SEQUENCE */
  26326. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  26327. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  26328. ExpectIntEQ(asnLen, 10);
  26329. ExpectIntEQ(tag, 0x10);
  26330. ExpectIntEQ(cls, 0);
  26331. /* Found OBJECT_ID. */
  26332. /* Invalid parameter testing. */
  26333. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  26334. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  26335. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  26336. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  26337. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  26338. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  26339. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  26340. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  26341. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  26342. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  26343. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26344. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26345. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  26346. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  26347. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  26348. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  26349. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  26350. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  26351. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  26352. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  26353. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  26354. /* Invalid encoding at length. */
  26355. p = objDerInvalidLen;
  26356. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  26357. p = objDerBadLen;
  26358. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  26359. p = objDerNotObj;
  26360. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  26361. p = objDerNoData;
  26362. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  26363. /* Create an ASN OBJECT from content */
  26364. p = derBuf + 2;
  26365. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  26366. ASN1_OBJECT_free(a);
  26367. a = NULL;
  26368. /* Create an ASN OBJECT from DER */
  26369. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  26370. /* Invalid parameter testing. */
  26371. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  26372. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  26373. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  26374. der = NULL;
  26375. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  26376. derPtr = objDer;
  26377. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  26378. ExpectPtrNE(derPtr, objDer);
  26379. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  26380. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  26381. ASN1_OBJECT_free(a);
  26382. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26383. return EXPECT_RESULT();
  26384. }
  26385. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  26386. {
  26387. EXPECT_DECLS;
  26388. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26389. ASN1_OBJECT* obj = NULL;
  26390. ASN1_OBJECT* a = NULL;
  26391. BIO *bio = NULL;
  26392. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  26393. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  26394. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  26395. const unsigned char* p;
  26396. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  26397. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  26398. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  26399. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  26400. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  26401. /* No DER encoding in ASN1_OBJECT. */
  26402. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  26403. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26404. ASN1_OBJECT_free(a);
  26405. a = NULL;
  26406. /* DER encoding - not OBJECT_ID */
  26407. p = notObjDer;
  26408. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26409. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26410. ASN1_OBJECT_free(a);
  26411. a = NULL;
  26412. /* Bad length encoding. */
  26413. p = badLenDer;
  26414. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26415. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26416. ASN1_OBJECT_free(a);
  26417. a = NULL;
  26418. /* Good encoding - but unknown. */
  26419. p = goodDer;
  26420. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  26421. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  26422. ASN1_OBJECT_free(a);
  26423. BIO_free(bio);
  26424. ASN1_OBJECT_free(obj);
  26425. #endif
  26426. return EXPECT_RESULT();
  26427. }
  26428. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  26429. {
  26430. EXPECT_DECLS;
  26431. #if defined(OPENSSL_EXTRA) && \
  26432. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  26433. char buf[50] = {0};
  26434. ASN1_OBJECT* obj;
  26435. const char* oid = "2.5.29.19";
  26436. const char* ln = "X509v3 Basic Constraints";
  26437. obj = NULL;
  26438. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  26439. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  26440. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  26441. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  26442. XMEMSET(buf, 0, sizeof(buf));
  26443. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  26444. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  26445. ASN1_OBJECT_free(obj);
  26446. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  26447. return EXPECT_RESULT();
  26448. }
  26449. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  26450. {
  26451. EXPECT_DECLS;
  26452. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  26453. WOLFSSL_STACK* sk = NULL;
  26454. WOLFSSL_ASN1_OBJECT* obj;
  26455. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26456. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26457. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26458. sk = NULL;
  26459. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26460. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  26461. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  26462. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  26463. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26464. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  26465. sk = NULL;
  26466. /* obj freed in pop_free call. */
  26467. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  26468. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  26469. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  26470. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  26471. wolfSSL_sk_ASN1_OBJECT_free(sk);
  26472. wolfSSL_ASN1_OBJECT_free(obj);
  26473. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  26474. return EXPECT_RESULT();
  26475. }
  26476. static int test_wolfSSL_ASN1_STRING(void)
  26477. {
  26478. EXPECT_DECLS;
  26479. #if defined(OPENSSL_EXTRA)
  26480. ASN1_STRING* str = NULL;
  26481. ASN1_STRING* c = NULL;
  26482. const char data[] = "hello wolfSSL";
  26483. const char data2[] = "Same len data";
  26484. const char longData[] =
  26485. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26486. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26487. ASN1_STRING_free(str);
  26488. str = NULL;
  26489. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26490. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  26491. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  26492. /* Check setting to NULL works. */
  26493. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  26494. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  26495. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26496. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  26497. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  26498. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  26499. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  26500. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  26501. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  26502. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  26503. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  26504. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  26505. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  26506. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26507. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  26508. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  26509. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  26510. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  26511. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  26512. /* Check setting back to smaller size frees dynamic data. */
  26513. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  26514. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  26515. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  26516. ExpectNull(ASN1_STRING_get0_data(NULL));
  26517. ExpectNotNull(ASN1_STRING_get0_data(str));
  26518. ExpectNull(ASN1_STRING_data(NULL));
  26519. ExpectNotNull(ASN1_STRING_data(str));
  26520. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  26521. ExpectIntGT(ASN1_STRING_length(str), 0);
  26522. ASN1_STRING_free(c);
  26523. ASN1_STRING_free(str);
  26524. ASN1_STRING_free(NULL);
  26525. #ifndef NO_WOLFSSL_STUB
  26526. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  26527. #endif
  26528. #endif
  26529. return EXPECT_RESULT();
  26530. }
  26531. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  26532. {
  26533. EXPECT_DECLS;
  26534. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  26535. !defined(NO_FILESYSTEM)
  26536. WOLFSSL_X509* x509 = NULL;
  26537. WOLFSSL_X509_NAME* subject = NULL;
  26538. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  26539. WOLFSSL_ASN1_STRING* a = NULL;
  26540. FILE* file = XBADFILE;
  26541. int idx = 0;
  26542. char targetOutput[16] = "www.wolfssl.com";
  26543. unsigned char* actual_output = NULL;
  26544. int len = 0;
  26545. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  26546. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  26547. if (file != NULL)
  26548. fclose(file);
  26549. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  26550. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  26551. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  26552. NID_commonName, -1)), 5);
  26553. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  26554. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  26555. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  26556. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  26557. a = NULL;
  26558. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  26559. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  26560. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  26561. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  26562. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  26563. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  26564. wolfSSL_X509_free(x509);
  26565. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26566. ExpectNotNull(a = ASN1_STRING_new());
  26567. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  26568. ASN1_STRING_free(a);
  26569. #endif
  26570. return EXPECT_RESULT();
  26571. }
  26572. static int test_wolfSSL_i2s_ASN1_STRING(void)
  26573. {
  26574. EXPECT_DECLS;
  26575. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  26576. WOLFSSL_ASN1_STRING* str = NULL;
  26577. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  26578. char* ret = NULL;
  26579. ExpectNotNull(str = ASN1_STRING_new());
  26580. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  26581. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26582. ret = NULL;
  26583. /* No data. */
  26584. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26585. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26586. ret = NULL;
  26587. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  26588. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26589. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26590. ret = NULL;
  26591. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  26592. /* No type. */
  26593. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  26594. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26595. ASN1_STRING_free(str);
  26596. #endif
  26597. return EXPECT_RESULT();
  26598. }
  26599. static int test_wolfSSL_ASN1_STRING_canon(void)
  26600. {
  26601. EXPECT_DECLS;
  26602. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  26603. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  26604. defined(OPENSSL_EXTRA_X509_SMALL))
  26605. WOLFSSL_ASN1_STRING* orig = NULL;
  26606. WOLFSSL_ASN1_STRING* canon = NULL;
  26607. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  26608. const char* whitespaceOnly = "\t\r\n";
  26609. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  26610. const char* canonData = "\x01 \x02 \xff tt";
  26611. const char longData[] =
  26612. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  26613. ExpectNotNull(orig = ASN1_STRING_new());
  26614. ExpectNotNull(canon = ASN1_STRING_new());
  26615. /* Invalid parameter testing. */
  26616. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  26617. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  26618. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  26619. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26620. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26621. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  26622. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26623. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26624. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  26625. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26626. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26627. ASN1_STRING_free(orig);
  26628. orig = NULL;
  26629. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26630. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  26631. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26632. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  26633. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26634. ASN1_STRING_free(orig);
  26635. orig = NULL;
  26636. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26637. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  26638. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  26639. ASN1_STRING_free(orig);
  26640. orig = NULL;
  26641. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  26642. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  26643. ASN1_STRING_free(orig);
  26644. ASN1_STRING_free(canon);
  26645. #endif
  26646. #endif
  26647. return EXPECT_RESULT();
  26648. }
  26649. static int test_wolfSSL_ASN1_STRING_print(void)
  26650. {
  26651. EXPECT_DECLS;
  26652. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  26653. !defined(NO_BIO)
  26654. ASN1_STRING* asnStr = NULL;
  26655. const char HELLO_DATA[]= \
  26656. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  26657. #define MAX_UNPRINTABLE_CHAR 32
  26658. #define MAX_BUF 255
  26659. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  26660. unsigned char expected[sizeof(unprintableData)+1];
  26661. unsigned char rbuf[MAX_BUF];
  26662. BIO *bio = NULL;
  26663. int p_len;
  26664. int i;
  26665. /* setup */
  26666. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  26667. unprintableData[i] = HELLO_DATA[i];
  26668. expected[i] = HELLO_DATA[i];
  26669. }
  26670. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  26671. unprintableData[sizeof(HELLO_DATA)+i] = i;
  26672. if (i == (int)'\n' || i == (int)'\r')
  26673. expected[sizeof(HELLO_DATA)+i] = i;
  26674. else
  26675. expected[sizeof(HELLO_DATA)+i] = '.';
  26676. }
  26677. unprintableData[sizeof(unprintableData)-1] = '\0';
  26678. expected[sizeof(expected)-1] = '\0';
  26679. XMEMSET(rbuf, 0, MAX_BUF);
  26680. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26681. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  26682. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26683. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  26684. (int)sizeof(unprintableData)), 1);
  26685. /* test */
  26686. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  26687. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  26688. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  26689. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  26690. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  26691. ExpectStrEQ((char*)rbuf, (const char*)expected);
  26692. BIO_free(bio);
  26693. bio = NULL;
  26694. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26695. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26696. /* Ensure there is 0 bytes available to write into. */
  26697. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  26698. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26699. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26700. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26701. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  26702. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  26703. BIO_free(bio);
  26704. ASN1_STRING_free(asnStr);
  26705. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  26706. return EXPECT_RESULT();
  26707. }
  26708. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  26709. {
  26710. EXPECT_DECLS;
  26711. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  26712. ASN1_STRING* asn_str = NULL;
  26713. const char data[] = "Hello wolfSSL!";
  26714. ASN1_STRING* esc_str = NULL;
  26715. const char esc_data[] = "a+;<>";
  26716. ASN1_STRING* neg_int = NULL;
  26717. const char neg_int_data[] = "\xff";
  26718. ASN1_STRING* neg_enum = NULL;
  26719. const char neg_enum_data[] = "\xff";
  26720. BIO *bio = NULL;
  26721. BIO *fixed = NULL;
  26722. unsigned long flags;
  26723. int p_len;
  26724. unsigned char rbuf[255];
  26725. /* setup */
  26726. XMEMSET(rbuf, 0, 255);
  26727. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26728. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  26729. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26730. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26731. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  26732. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26733. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  26734. sizeof(esc_data)), 1);
  26735. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  26736. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  26737. sizeof(neg_int_data) - 1), 1);
  26738. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  26739. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  26740. sizeof(neg_enum_data) - 1), 1);
  26741. /* Invalid parameter testing. */
  26742. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  26743. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  26744. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  26745. /* no flags */
  26746. XMEMSET(rbuf, 0, 255);
  26747. flags = 0;
  26748. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  26749. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  26750. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  26751. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26752. /* Ensure there is 0 bytes available to write into. */
  26753. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26754. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26755. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26756. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26757. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  26758. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26759. /* RFC2253 Escape */
  26760. XMEMSET(rbuf, 0, 255);
  26761. flags = ASN1_STRFLGS_ESC_2253;
  26762. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  26763. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  26764. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  26765. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26766. /* Ensure there is 0 bytes available to write into. */
  26767. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26768. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26769. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26770. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26771. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  26772. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  26773. /* Show type */
  26774. XMEMSET(rbuf, 0, 255);
  26775. flags = ASN1_STRFLGS_SHOW_TYPE;
  26776. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  26777. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  26778. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  26779. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26780. /* Ensure there is 0 bytes available to write into. */
  26781. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26782. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26783. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26784. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26785. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  26786. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26787. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  26788. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26789. /* Dump All */
  26790. XMEMSET(rbuf, 0, 255);
  26791. flags = ASN1_STRFLGS_DUMP_ALL;
  26792. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  26793. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  26794. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  26795. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26796. /* Ensure there is 0 bytes available to write into. */
  26797. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26798. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26799. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26800. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26801. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  26802. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26803. /* Dump Der */
  26804. XMEMSET(rbuf, 0, 255);
  26805. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  26806. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  26807. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  26808. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  26809. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26810. /* Ensure there is 0 bytes available to write into. */
  26811. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  26812. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26813. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  26814. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26815. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  26816. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26817. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  26818. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  26819. /* Dump All + Show type */
  26820. XMEMSET(rbuf, 0, 255);
  26821. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26822. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  26823. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  26824. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  26825. /* Dump All + Show type - Negative Integer. */
  26826. XMEMSET(rbuf, 0, 255);
  26827. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26828. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  26829. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  26830. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  26831. /* Dump All + Show type - Negative Enumerated. */
  26832. XMEMSET(rbuf, 0, 255);
  26833. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  26834. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  26835. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  26836. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  26837. BIO_free(fixed);
  26838. BIO_free(bio);
  26839. ASN1_STRING_free(asn_str);
  26840. ASN1_STRING_free(esc_str);
  26841. ASN1_STRING_free(neg_int);
  26842. ASN1_STRING_free(neg_enum);
  26843. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  26844. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  26845. #endif
  26846. return EXPECT_RESULT();
  26847. }
  26848. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  26849. {
  26850. EXPECT_DECLS;
  26851. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  26852. ASN1_STRING* asn1str_test = NULL;
  26853. ASN1_STRING* asn1str_answer = NULL;
  26854. /* Each character is encoded using 4 bytes */
  26855. char input[] = {
  26856. 0, 0, 0, 'T',
  26857. 0, 0, 0, 'e',
  26858. 0, 0, 0, 's',
  26859. 0, 0, 0, 't',
  26860. };
  26861. char output[] = "Test";
  26862. char badInput[] = {
  26863. 1, 0, 0, 'T',
  26864. 0, 1, 0, 'e',
  26865. 0, 0, 1, 's',
  26866. };
  26867. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  26868. /* Test wrong type. */
  26869. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  26870. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26871. ASN1_STRING_free(asn1str_test);
  26872. asn1str_test = NULL;
  26873. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  26874. /* Test bad length. */
  26875. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  26876. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26877. /* Test bad input. */
  26878. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  26879. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26880. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  26881. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26882. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  26883. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  26884. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  26885. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  26886. ExpectNotNull(
  26887. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  26888. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  26889. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  26890. ASN1_STRING_free(asn1str_test);
  26891. ASN1_STRING_free(asn1str_answer);
  26892. #endif /* OPENSSL_ALL && !NO_ASN */
  26893. return EXPECT_RESULT();
  26894. }
  26895. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  26896. {
  26897. EXPECT_DECLS;
  26898. #if defined(OPENSSL_EXTRA)
  26899. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  26900. unsigned char nullstr[32];
  26901. XMEMSET(nullstr, 0, 32);
  26902. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  26903. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  26904. if (asn1_gtime != NULL) {
  26905. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  26906. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  26907. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  26908. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  26909. }
  26910. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  26911. #endif /* OPENSSL_EXTRA */
  26912. return EXPECT_RESULT();
  26913. }
  26914. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  26915. {
  26916. EXPECT_DECLS;
  26917. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  26918. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  26919. BIO* bio = NULL;
  26920. unsigned char buf[24];
  26921. int i;
  26922. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  26923. BIO_set_write_buf_size(bio, 24);
  26924. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  26925. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  26926. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  26927. /* Type not set. */
  26928. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26929. gtime.type = V_ASN1_GENERALIZEDTIME;
  26930. /* Invalid parameters testing. */
  26931. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  26932. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  26933. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  26934. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  26935. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  26936. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  26937. BIO_free(bio);
  26938. bio = NULL;
  26939. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  26940. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  26941. /* Ensure there is 0 bytes available to write into. */
  26942. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  26943. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26944. for (i = 1; i < 20; i++) {
  26945. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  26946. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  26947. }
  26948. BIO_free(bio);
  26949. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  26950. #endif /* OPENSSL_EXTRA */
  26951. return EXPECT_RESULT();
  26952. }
  26953. static int test_wolfSSL_ASN1_TIME(void)
  26954. {
  26955. EXPECT_DECLS;
  26956. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  26957. WOLFSSL_ASN1_TIME* asn_time = NULL;
  26958. unsigned char *data;
  26959. ExpectNotNull(asn_time = ASN1_TIME_new());
  26960. #ifndef NO_WOLFSSL_STUB
  26961. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  26962. #endif
  26963. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  26964. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  26965. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  26966. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  26967. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  26968. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  26969. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  26970. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  26971. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  26972. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  26973. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  26974. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  26975. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  26976. ASN1_TIME_free(asn_time);
  26977. ASN1_TIME_free(NULL);
  26978. #endif
  26979. return EXPECT_RESULT();
  26980. }
  26981. static int test_wolfSSL_ASN1_TIME_to_string(void)
  26982. {
  26983. EXPECT_DECLS;
  26984. #ifndef NO_ASN_TIME
  26985. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  26986. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  26987. WOLFSSL_ASN1_TIME* t = NULL;
  26988. char buf[ASN_GENERALIZED_TIME_SIZE];
  26989. ExpectNotNull((t = ASN1_TIME_new()));
  26990. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  26991. /* Invalid parameter testing. */
  26992. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  26993. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  26994. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  26995. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  26996. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  26997. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  26998. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  26999. /* Buffer needs to be longer than minimum of 5 characters. */
  27000. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  27001. ASN1_TIME_free(t);
  27002. #endif
  27003. #endif /* NO_ASN_TIME */
  27004. return EXPECT_RESULT();
  27005. }
  27006. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27007. {
  27008. EXPECT_DECLS;
  27009. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27010. ASN1_TIME* fromTime = NULL;
  27011. ASN1_TIME* closeToTime = NULL;
  27012. ASN1_TIME* toTime = NULL;
  27013. ASN1_TIME* invalidTime = NULL;
  27014. int daysDiff;
  27015. int secsDiff;
  27016. ExpectNotNull((fromTime = ASN1_TIME_new()));
  27017. /* Feb 22, 2003, 21:15:15 */
  27018. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  27019. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  27020. /* Feb 22, 2003, 21:16:15 */
  27021. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  27022. ExpectNotNull((toTime = ASN1_TIME_new()));
  27023. /* Dec 19, 2010, 18:10:11 */
  27024. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  27025. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  27026. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  27027. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  27028. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  27029. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  27030. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27031. /* Error conditions. */
  27032. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  27033. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  27034. /* If both times are NULL, difference is 0. */
  27035. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  27036. ExpectIntEQ(daysDiff, 0);
  27037. ExpectIntEQ(secsDiff, 0);
  27038. /* If one time is NULL, it defaults to the current time. */
  27039. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  27040. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  27041. /* Normal operation. Both times non-NULL. */
  27042. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27043. ExpectIntEQ(daysDiff, 2856);
  27044. ExpectIntEQ(secsDiff, 75296);
  27045. /* Swapping the times should return negative values. */
  27046. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  27047. ExpectIntEQ(daysDiff, -2856);
  27048. ExpectIntEQ(secsDiff, -75296);
  27049. /* Compare with invalid time string. */
  27050. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  27051. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  27052. /* Compare with days difference of 0. */
  27053. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  27054. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  27055. /* Days and seconds differences not 0. */
  27056. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27057. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27058. /* Same time. */
  27059. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27060. /* Compare regression test: No seconds difference, just difference in days.
  27061. */
  27062. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27063. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27064. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27065. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27066. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27067. /* Edge case with Unix epoch. */
  27068. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27069. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27070. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27071. ExpectIntEQ(daysDiff, 3652);
  27072. ExpectIntEQ(secsDiff, 0);
  27073. /* Edge case with year > 2038 (year 2038 problem). */
  27074. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27075. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  27076. ExpectIntEQ(daysDiff, 2932896);
  27077. ExpectIntEQ(secsDiff, 86399);
  27078. ASN1_TIME_free(fromTime);
  27079. ASN1_TIME_free(closeToTime);
  27080. ASN1_TIME_free(toTime);
  27081. ASN1_TIME_free(invalidTime);
  27082. #endif
  27083. return EXPECT_RESULT();
  27084. }
  27085. static int test_wolfSSL_ASN1_TIME_adj(void)
  27086. {
  27087. EXPECT_DECLS;
  27088. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  27089. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  27090. const int year = 365*24*60*60;
  27091. const int day = 24*60*60;
  27092. const int hour = 60*60;
  27093. const int mini = 60;
  27094. const byte asn_utc_time = ASN_UTC_TIME;
  27095. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27096. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  27097. #endif
  27098. WOLFSSL_ASN1_TIME* asn_time = NULL;
  27099. WOLFSSL_ASN1_TIME* s = NULL;
  27100. int offset_day;
  27101. long offset_sec;
  27102. char date_str[CTC_DATE_SIZE + 1];
  27103. time_t t;
  27104. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  27105. /* UTC notation test */
  27106. /* 2000/2/15 20:30:00 */
  27107. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  27108. offset_day = 7;
  27109. offset_sec = 45 * mini;
  27110. /* offset_sec = -45 * min;*/
  27111. ExpectNotNull(asn_time =
  27112. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  27113. ExpectTrue(asn_time->type == asn_utc_time);
  27114. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27115. CTC_DATE_SIZE));
  27116. date_str[CTC_DATE_SIZE] = '\0';
  27117. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  27118. /* negative offset */
  27119. offset_sec = -45 * mini;
  27120. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  27121. ExpectNotNull(asn_time);
  27122. ExpectTrue(asn_time->type == asn_utc_time);
  27123. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27124. CTC_DATE_SIZE));
  27125. date_str[CTC_DATE_SIZE] = '\0';
  27126. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  27127. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27128. s = NULL;
  27129. XMEMSET(date_str, 0, sizeof(date_str));
  27130. /* Generalized time will overflow time_t if not long */
  27131. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  27132. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  27133. DYNAMIC_TYPE_OPENSSL);
  27134. /* GeneralizedTime notation test */
  27135. /* 2055/03/01 09:00:00 */
  27136. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  27137. offset_day = 12;
  27138. offset_sec = 10 * mini;
  27139. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27140. offset_sec));
  27141. ExpectTrue(asn_time->type == asn_gen_time);
  27142. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27143. CTC_DATE_SIZE));
  27144. date_str[CTC_DATE_SIZE] = '\0';
  27145. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  27146. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  27147. s = NULL;
  27148. XMEMSET(date_str, 0, sizeof(date_str));
  27149. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  27150. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  27151. s = NULL;
  27152. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  27153. offset_day = 7;
  27154. offset_sec = 45 * mini;
  27155. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  27156. offset_sec));
  27157. ExpectTrue(asn_time->type == asn_utc_time);
  27158. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27159. CTC_DATE_SIZE));
  27160. date_str[CTC_DATE_SIZE] = '\0';
  27161. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27162. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27163. asn_time = NULL;
  27164. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  27165. offset_sec));
  27166. ExpectTrue(asn_time->type == asn_utc_time);
  27167. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  27168. CTC_DATE_SIZE));
  27169. date_str[CTC_DATE_SIZE] = '\0';
  27170. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  27171. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  27172. #endif
  27173. return EXPECT_RESULT();
  27174. }
  27175. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  27176. {
  27177. EXPECT_DECLS;
  27178. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27179. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27180. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  27181. ASN1_TIME asnTime;
  27182. struct tm tm;
  27183. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  27184. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27185. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  27186. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  27187. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27188. ExpectIntEQ(tm.tm_sec, 15);
  27189. ExpectIntEQ(tm.tm_min, 15);
  27190. ExpectIntEQ(tm.tm_hour, 21);
  27191. ExpectIntEQ(tm.tm_mday, 22);
  27192. ExpectIntEQ(tm.tm_mon, 1);
  27193. ExpectIntEQ(tm.tm_year, 100);
  27194. ExpectIntEQ(tm.tm_isdst, 0);
  27195. #ifdef XMKTIME
  27196. ExpectIntEQ(tm.tm_wday, 2);
  27197. ExpectIntEQ(tm.tm_yday, 52);
  27198. #endif
  27199. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  27200. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27201. ExpectIntEQ(tm.tm_year, 50);
  27202. /* Get current time. */
  27203. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  27204. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  27205. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  27206. /* 0 length. */
  27207. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27208. /* No type. */
  27209. asnTime.length = 1;
  27210. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27211. /* Not UTCTIME length. */
  27212. asnTime.type = V_ASN1_UTCTIME;
  27213. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27214. /* Not GENERALIZEDTIME length. */
  27215. asnTime.type = V_ASN1_GENERALIZEDTIME;
  27216. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27217. /* Not Zulu timezone. */
  27218. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  27219. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27220. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  27221. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  27222. #ifdef XMKTIME
  27223. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  27224. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  27225. ExpectIntEQ(tm.tm_sec, 47);
  27226. ExpectIntEQ(tm.tm_min, 22);
  27227. ExpectIntEQ(tm.tm_hour, 21);
  27228. ExpectIntEQ(tm.tm_mday, 12);
  27229. ExpectIntEQ(tm.tm_mon, 4);
  27230. ExpectIntEQ(tm.tm_year, 123);
  27231. ExpectIntEQ(tm.tm_wday, 5);
  27232. ExpectIntEQ(tm.tm_yday, 131);
  27233. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  27234. fields are zeroed out as expected. */
  27235. ExpectIntEQ(tm.tm_isdst, 0);
  27236. #endif
  27237. #endif
  27238. return EXPECT_RESULT();
  27239. }
  27240. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  27241. {
  27242. EXPECT_DECLS;
  27243. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27244. WOLFSSL_ASN1_TIME *t = NULL;
  27245. WOLFSSL_ASN1_TIME *out = NULL;
  27246. WOLFSSL_ASN1_TIME *gtime = NULL;
  27247. int tlen = 0;
  27248. unsigned char *data = NULL;
  27249. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27250. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  27251. /* type not set. */
  27252. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27253. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27254. t = NULL;
  27255. /* UTC Time test */
  27256. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  27257. if (t != NULL) {
  27258. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  27259. t->type = ASN_UTC_TIME;
  27260. t->length = ASN_UTC_TIME_SIZE;
  27261. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27262. }
  27263. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  27264. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  27265. out = NULL;
  27266. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27267. wolfSSL_ASN1_TIME_free(gtime);
  27268. gtime = NULL;
  27269. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  27270. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27271. ExpectPtrEq(gtime, out);
  27272. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27273. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27274. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27275. /* Generalized Time test */
  27276. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27277. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27278. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27279. if (t != NULL) {
  27280. t->type = ASN_GENERALIZED_TIME;
  27281. t->length = ASN_GENERALIZED_TIME_SIZE;
  27282. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  27283. }
  27284. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  27285. ASN_GENERALIZED_TIME_SIZE);
  27286. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  27287. "20050727123456Z");
  27288. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27289. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27290. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27291. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27292. /* UTC Time to Generalized Time 1900's test */
  27293. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27294. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  27295. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  27296. if (t != NULL) {
  27297. t->type = ASN_UTC_TIME;
  27298. t->length = ASN_UTC_TIME_SIZE;
  27299. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  27300. }
  27301. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  27302. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27303. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27304. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  27305. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27306. /* Null parameter test */
  27307. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  27308. gtime = NULL;
  27309. out = NULL;
  27310. if (t != NULL) {
  27311. t->type = ASN_UTC_TIME;
  27312. t->length = ASN_UTC_TIME_SIZE;
  27313. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  27314. }
  27315. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  27316. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  27317. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  27318. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  27319. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27320. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27321. #endif
  27322. return EXPECT_RESULT();
  27323. }
  27324. static int test_wolfSSL_ASN1_TIME_print(void)
  27325. {
  27326. EXPECT_DECLS;
  27327. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  27328. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27329. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  27330. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  27331. !defined(NO_ASN_TIME)
  27332. BIO* bio = NULL;
  27333. BIO* fixed = NULL;
  27334. X509* x509 = NULL;
  27335. const unsigned char* der = client_cert_der_2048;
  27336. ASN1_TIME* notAfter;
  27337. ASN1_TIME* notBefore;
  27338. unsigned char buf[25];
  27339. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27340. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27341. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27342. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27343. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  27344. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  27345. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  27346. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  27347. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  27348. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27349. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  27350. /* Test BIO_write fails. */
  27351. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27352. /* Ensure there is 0 bytes available to write into. */
  27353. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  27354. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27355. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27356. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27357. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  27358. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  27359. /* create a bad time and test results */
  27360. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  27361. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  27362. if (EXPECT_SUCCESS()) {
  27363. notAfter->data[8] = 0;
  27364. notAfter->data[3] = 0;
  27365. }
  27366. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  27367. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27368. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27369. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  27370. BIO_free(bio);
  27371. BIO_free(fixed);
  27372. X509_free(x509);
  27373. #endif
  27374. return EXPECT_RESULT();
  27375. }
  27376. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27377. {
  27378. EXPECT_DECLS;
  27379. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27380. BIO* bio = NULL;
  27381. ASN1_UTCTIME* utc = NULL;
  27382. unsigned char buf[25];
  27383. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27384. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27385. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  27386. /* Valid date */
  27387. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27388. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27389. DYNAMIC_TYPE_ASN1));
  27390. if (utc != NULL) {
  27391. utc->type = ASN_UTC_TIME;
  27392. utc->length = ASN_UTC_TIME_SIZE;
  27393. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27394. }
  27395. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  27396. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  27397. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  27398. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27399. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27400. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27401. XMEMSET(buf, 0, sizeof(buf));
  27402. BIO_free(bio);
  27403. bio = NULL;
  27404. /* Invalid format */
  27405. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  27406. if (utc != NULL) {
  27407. utc->type = ASN_UTC_TIME;
  27408. utc->length = ASN_UTC_TIME_SIZE;
  27409. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27410. }
  27411. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27412. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27413. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27414. /* Invalid type */
  27415. if (utc != NULL) {
  27416. utc->type = ASN_GENERALIZED_TIME;
  27417. utc->length = ASN_GENERALIZED_TIME_SIZE;
  27418. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  27419. }
  27420. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27421. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27422. BIO_free(bio);
  27423. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27424. return EXPECT_RESULT();
  27425. }
  27426. static int test_wolfSSL_ASN1_TYPE(void)
  27427. {
  27428. EXPECT_DECLS;
  27429. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  27430. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  27431. WOLFSSL_ASN1_TYPE* t = NULL;
  27432. WOLFSSL_ASN1_OBJECT* obj = NULL;
  27433. #ifndef NO_ASN_TIME
  27434. WOLFSSL_ASN1_TIME* time = NULL;
  27435. #endif
  27436. WOLFSSL_ASN1_STRING* str = NULL;
  27437. unsigned char data[] = { 0x00 };
  27438. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  27439. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27440. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  27441. wolfSSL_ASN1_TYPE_free(t);
  27442. t = NULL;
  27443. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27444. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  27445. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  27446. wolfSSL_ASN1_TYPE_free(t);
  27447. t = NULL;
  27448. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27449. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27450. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  27451. wolfSSL_ASN1_TYPE_free(t);
  27452. t = NULL;
  27453. #ifndef NO_ASN_TIME
  27454. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27455. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27456. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  27457. wolfSSL_ASN1_TYPE_free(t);
  27458. t = NULL;
  27459. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27460. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  27461. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  27462. wolfSSL_ASN1_TYPE_free(t);
  27463. t = NULL;
  27464. #endif
  27465. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27466. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27467. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  27468. wolfSSL_ASN1_TYPE_free(t);
  27469. t = NULL;
  27470. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27471. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27472. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  27473. wolfSSL_ASN1_TYPE_free(t);
  27474. t = NULL;
  27475. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27476. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27477. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  27478. wolfSSL_ASN1_TYPE_free(t);
  27479. t = NULL;
  27480. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27481. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27482. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  27483. wolfSSL_ASN1_TYPE_free(t);
  27484. t = NULL;
  27485. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27486. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27487. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  27488. wolfSSL_ASN1_TYPE_free(t);
  27489. t = NULL;
  27490. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  27491. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  27492. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  27493. wolfSSL_ASN1_TYPE_free(t);
  27494. t = NULL;
  27495. #endif
  27496. return EXPECT_RESULT();
  27497. }
  27498. /* Testing code used in dpp.c in hostap */
  27499. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27500. typedef struct {
  27501. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  27502. * as an OID identifying the curve */
  27503. X509_ALGOR *alg;
  27504. /* Compressed format public key per ANSI X9.63 */
  27505. ASN1_BIT_STRING *pub_key;
  27506. } DPP_BOOTSTRAPPING_KEY;
  27507. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  27508. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  27509. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  27510. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  27511. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  27512. typedef struct {
  27513. ASN1_INTEGER *integer;
  27514. } TEST_ASN1;
  27515. ASN1_SEQUENCE(TEST_ASN1) = {
  27516. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  27517. } ASN1_SEQUENCE_END(TEST_ASN1)
  27518. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  27519. typedef struct {
  27520. ASN1_OCTET_STRING *octet_string;
  27521. } TEST_FAIL_ASN1;
  27522. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  27523. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  27524. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  27525. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  27526. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  27527. #endif
  27528. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  27529. {
  27530. EXPECT_DECLS;
  27531. /* Testing code used in dpp.c in hostap */
  27532. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27533. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  27534. EC_KEY *eckey = NULL;
  27535. EVP_PKEY *key = NULL;
  27536. size_t len;
  27537. unsigned char *der = NULL;
  27538. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  27539. const unsigned char *in = ecc_clikey_der_256;
  27540. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  27541. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  27542. const EC_GROUP *group = NULL;
  27543. const EC_POINT *point = NULL;
  27544. int nid;
  27545. TEST_ASN1 *test_asn1 = NULL;
  27546. TEST_FAIL_ASN1 test_fail_asn1;
  27547. const unsigned char badObjDer[] = { 0x06, 0x00 };
  27548. const unsigned char goodObjDer[] = {
  27549. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  27550. };
  27551. WOLFSSL_ASN1_ITEM emptyTemplate;
  27552. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  27553. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27554. der = NULL;
  27555. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  27556. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  27557. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27558. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  27559. (long)sizeof_ecc_clikey_der_256));
  27560. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  27561. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  27562. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  27563. nid = EC_GROUP_get_curve_name(group);
  27564. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  27565. group_obj = OBJ_nid2obj(nid);
  27566. if ((ec_obj != NULL) && (group_obj != NULL)) {
  27567. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  27568. group_obj), 1);
  27569. if (EXPECT_SUCCESS()) {
  27570. ec_obj = NULL;
  27571. group_obj = NULL;
  27572. }
  27573. }
  27574. wolfSSL_ASN1_OBJECT_free(group_obj);
  27575. wolfSSL_ASN1_OBJECT_free(ec_obj);
  27576. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  27577. #ifdef HAVE_COMP_KEY
  27578. ExpectIntGT((len = EC_POINT_point2oct(
  27579. group, point, POINT_CONVERSION_COMPRESSED,
  27580. NULL, 0, NULL)), 0);
  27581. #else
  27582. ExpectIntGT((len = EC_POINT_point2oct(
  27583. group, point, POINT_CONVERSION_UNCOMPRESSED,
  27584. NULL, 0, NULL)), 0);
  27585. #endif
  27586. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  27587. #ifdef HAVE_COMP_KEY
  27588. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27589. der, len-1, NULL), 0);
  27590. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  27591. der, len, NULL), len);
  27592. #else
  27593. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27594. der, len-1, NULL), 0);
  27595. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  27596. der, len, NULL), len);
  27597. #endif
  27598. if (EXPECT_SUCCESS()) {
  27599. bootstrap->pub_key->data = der;
  27600. bootstrap->pub_key->length = (int)len;
  27601. /* Not actually used */
  27602. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27603. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27604. }
  27605. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  27606. der = NULL;
  27607. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27608. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27609. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27610. EVP_PKEY_free(key);
  27611. EC_KEY_free(eckey);
  27612. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27613. bootstrap = NULL;
  27614. DPP_BOOTSTRAPPING_KEY_free(NULL);
  27615. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  27616. * a NULL and an empty BIT_STRING. */
  27617. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  27618. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  27619. if (EXPECT_SUCCESS()) {
  27620. bootstrap->alg->algorithm->obj = badObjDer;
  27621. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  27622. }
  27623. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  27624. if (EXPECT_SUCCESS()) {
  27625. bootstrap->alg->parameter->type = V_ASN1_NULL;
  27626. bootstrap->alg->parameter->value.ptr = NULL;
  27627. bootstrap->pub_key->data = NULL;
  27628. bootstrap->pub_key->length = 0;
  27629. /* Not actually used */
  27630. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  27631. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  27632. }
  27633. /* Encode with bad OBJECT_ID. */
  27634. der = NULL;
  27635. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  27636. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  27637. if (EXPECT_SUCCESS()) {
  27638. bootstrap->alg->algorithm->obj = goodObjDer;
  27639. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  27640. bootstrap->alg->algorithm->grp = 2;
  27641. }
  27642. der = NULL;
  27643. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  27644. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  27645. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27646. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  27647. /* Test integer */
  27648. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  27649. der = NULL;
  27650. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  27651. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  27652. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  27653. TEST_ASN1_free(test_asn1);
  27654. /* Test integer cases. */
  27655. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27656. TEST_ASN1_free(NULL);
  27657. /* Test error cases. */
  27658. ExpectNull(TEST_FAIL_ASN1_new());
  27659. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  27660. TEST_FAIL_ASN1_free(NULL);
  27661. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  27662. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  27663. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  27664. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27665. return EXPECT_RESULT();
  27666. }
  27667. static int test_wolfSSL_lhash(void)
  27668. {
  27669. EXPECT_DECLS;
  27670. #ifdef OPENSSL_ALL
  27671. const char testStr[] = "Like a true nature's child\n"
  27672. "We were born\n"
  27673. "Born to be wild";
  27674. #ifdef NO_SHA
  27675. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  27676. #else
  27677. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  27678. #endif
  27679. #endif
  27680. return EXPECT_RESULT();
  27681. }
  27682. static int test_wolfSSL_X509_NAME(void)
  27683. {
  27684. EXPECT_DECLS;
  27685. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  27686. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27687. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  27688. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  27689. defined(OPENSSL_EXTRA))
  27690. X509* x509 = NULL;
  27691. const unsigned char* c;
  27692. unsigned char buf[4096];
  27693. int bytes;
  27694. XFILE f = XBADFILE;
  27695. const X509_NAME* a = NULL;
  27696. const X509_NAME* b = NULL;
  27697. X509_NAME* d2i_name = NULL;
  27698. int sz = 0;
  27699. unsigned char* tmp;
  27700. char file[] = "./certs/ca-cert.der";
  27701. #ifndef OPENSSL_EXTRA_X509_SMALL
  27702. byte empty[] = { /* CN=empty emailAddress= */
  27703. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  27704. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  27705. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  27706. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27707. 0x01, 0x16, 0x00
  27708. };
  27709. #endif
  27710. #ifndef OPENSSL_EXTRA_X509_SMALL
  27711. /* test compile of deprecated function, returns 0 */
  27712. ExpectIntEQ(CRYPTO_thread_id(), 0);
  27713. #endif
  27714. ExpectNotNull(a = X509_NAME_new());
  27715. X509_NAME_free((X509_NAME*)a);
  27716. a = NULL;
  27717. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  27718. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  27719. if (f != XBADFILE)
  27720. XFCLOSE(f);
  27721. c = buf;
  27722. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  27723. /* test cmp function */
  27724. ExpectNotNull(a = X509_get_issuer_name(x509));
  27725. ExpectNotNull(b = X509_get_subject_name(x509));
  27726. #ifndef OPENSSL_EXTRA_X509_SMALL
  27727. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  27728. #endif
  27729. tmp = buf;
  27730. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  27731. if (sz > 0 && tmp == buf) {
  27732. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  27733. __LINE__);
  27734. fprintf(stderr, " Expected pointer to be incremented\n");
  27735. abort();
  27736. }
  27737. #ifndef OPENSSL_EXTRA_X509_SMALL
  27738. tmp = buf;
  27739. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27740. #endif
  27741. /* if output parameter is NULL, should still return required size. */
  27742. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  27743. /* retry but with the function creating a buffer */
  27744. tmp = NULL;
  27745. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27746. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27747. tmp = NULL;
  27748. #ifdef WOLFSSL_CERT_NAME_ALL
  27749. /* test for givenName and name */
  27750. {
  27751. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  27752. const byte gName[] = "test-given-name";
  27753. const byte name[] = "test-name";
  27754. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27755. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  27756. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27757. 1);
  27758. wolfSSL_X509_NAME_ENTRY_free(entry);
  27759. entry = NULL;
  27760. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  27761. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  27762. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  27763. 1);
  27764. wolfSSL_X509_NAME_ENTRY_free(entry);
  27765. tmp = NULL;
  27766. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  27767. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  27768. }
  27769. #endif
  27770. b = NULL;
  27771. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  27772. #ifndef OPENSSL_EXTRA_X509_SMALL
  27773. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  27774. #endif
  27775. X509_NAME_free((X509_NAME*)b);
  27776. X509_NAME_free(d2i_name);
  27777. d2i_name = NULL;
  27778. X509_free(x509);
  27779. #ifndef OPENSSL_EXTRA_X509_SMALL
  27780. /* test with an empty domain component */
  27781. tmp = empty;
  27782. sz = sizeof(empty);
  27783. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  27784. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  27785. /* size of empty emailAddress will be 0 */
  27786. tmp = buf;
  27787. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  27788. (char*)tmp, sizeof(buf)), 0);
  27789. /* should contain no organization name */
  27790. tmp = buf;
  27791. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  27792. (char*)tmp, sizeof(buf)), -1);
  27793. X509_NAME_free(d2i_name);
  27794. #endif
  27795. #endif
  27796. return EXPECT_RESULT();
  27797. }
  27798. static int test_wolfSSL_X509_NAME_hash(void)
  27799. {
  27800. EXPECT_DECLS;
  27801. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  27802. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  27803. BIO* bio = NULL;
  27804. X509* x509 = NULL;
  27805. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  27806. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  27807. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  27808. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  27809. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  27810. X509_free(x509);
  27811. BIO_free(bio);
  27812. #endif
  27813. return EXPECT_RESULT();
  27814. }
  27815. static int test_wolfSSL_X509_NAME_print_ex(void)
  27816. {
  27817. EXPECT_DECLS;
  27818. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  27819. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  27820. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  27821. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  27822. !defined(NO_BIO) && !defined(NO_RSA)
  27823. int memSz;
  27824. byte* mem = NULL;
  27825. BIO* bio = NULL;
  27826. BIO* membio = NULL;
  27827. X509* x509 = NULL;
  27828. X509_NAME* name = NULL;
  27829. const char* expNormal = "C=US, CN=wolfssl.com";
  27830. const char* expReverse = "CN=wolfssl.com, C=US";
  27831. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  27832. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  27833. const char* expRFC5523 =
  27834. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  27835. /* Test with real cert (svrCertFile) first */
  27836. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  27837. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  27838. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  27839. ExpectNotNull(name = X509_get_subject_name(x509));
  27840. /* Test without flags */
  27841. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27842. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27843. BIO_free(membio);
  27844. membio = NULL;
  27845. /* Test flag: XN_FLAG_RFC2253 */
  27846. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27847. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27848. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27849. BIO_free(membio);
  27850. membio = NULL;
  27851. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  27852. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27853. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27854. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27855. BIO_free(membio);
  27856. membio = NULL;
  27857. X509_free(x509);
  27858. BIO_free(bio);
  27859. name = NULL;
  27860. /* Test normal case without escaped characters */
  27861. {
  27862. /* Create name: "/C=US/CN=wolfssl.com" */
  27863. ExpectNotNull(name = X509_NAME_new());
  27864. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  27865. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  27866. WOLFSSL_SUCCESS);
  27867. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  27868. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  27869. WOLFSSL_SUCCESS);
  27870. /* Test without flags */
  27871. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27872. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27873. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27874. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  27875. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  27876. BIO_free(membio);
  27877. membio = NULL;
  27878. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  27879. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27880. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27881. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27882. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27883. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  27884. BIO_free(membio);
  27885. membio = NULL;
  27886. /* Test flags: XN_FLAG_DN_REV - reversed */
  27887. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27888. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27889. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27890. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27891. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  27892. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  27893. BIO_free(membio);
  27894. membio = NULL;
  27895. X509_NAME_free(name);
  27896. name = NULL;
  27897. }
  27898. /* Test RFC2253 characters are escaped with backslashes */
  27899. {
  27900. ExpectNotNull(name = X509_NAME_new());
  27901. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  27902. /* space at beginning and end, and: ,+"\ */
  27903. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  27904. WOLFSSL_SUCCESS);
  27905. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  27906. /* # at beginning, and: <>;*/
  27907. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  27908. WOLFSSL_SUCCESS);
  27909. /* Test without flags */
  27910. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27911. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  27912. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27913. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  27914. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  27915. XSTRLEN(expNotEscaped)), 0);
  27916. BIO_free(membio);
  27917. membio = NULL;
  27918. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  27919. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27920. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27921. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  27922. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27923. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  27924. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  27925. BIO_free(membio);
  27926. membio = NULL;
  27927. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  27928. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  27929. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  27930. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  27931. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  27932. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  27933. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  27934. XSTRLEN(expNotEscapedRev)), 0);
  27935. BIO_free(membio);
  27936. X509_NAME_free(name);
  27937. }
  27938. #endif
  27939. return EXPECT_RESULT();
  27940. }
  27941. #ifndef NO_BIO
  27942. static int test_wolfSSL_X509_INFO_multiple_info(void)
  27943. {
  27944. EXPECT_DECLS;
  27945. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  27946. STACK_OF(X509_INFO) *info_stack = NULL;
  27947. X509_INFO *info = NULL;
  27948. int len;
  27949. int i;
  27950. const char* files[] = {
  27951. cliCertFile,
  27952. cliKeyFile,
  27953. /* This needs to be the order as svrCertFile contains the
  27954. * intermediate cert as well. */
  27955. svrKeyFile,
  27956. svrCertFile,
  27957. NULL,
  27958. };
  27959. const char** curFile;
  27960. BIO *fileBIO = NULL;
  27961. BIO *concatBIO = NULL;
  27962. byte tmp[FOURK_BUF];
  27963. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  27964. * to group objects together. */
  27965. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  27966. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  27967. int fileLen;
  27968. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  27969. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  27970. if (EXPECT_SUCCESS()) {
  27971. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  27972. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  27973. fileLen -= len;
  27974. if (EXPECT_FAIL())
  27975. break;
  27976. }
  27977. /* Make sure we read the entire file */
  27978. ExpectIntEQ(fileLen, 0);
  27979. }
  27980. BIO_free(fileBIO);
  27981. fileBIO = NULL;
  27982. }
  27983. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  27984. NULL));
  27985. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  27986. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  27987. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  27988. ExpectNotNull(info->x509);
  27989. ExpectNull(info->crl);
  27990. if (i != 0) {
  27991. ExpectNotNull(info->x_pkey);
  27992. ExpectIntEQ(X509_check_private_key(info->x509,
  27993. info->x_pkey->dec_pkey), 1);
  27994. }
  27995. else {
  27996. ExpectNull(info->x_pkey);
  27997. }
  27998. }
  27999. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28000. BIO_free(concatBIO);
  28001. #endif
  28002. return EXPECT_RESULT();
  28003. }
  28004. #endif
  28005. #ifndef NO_BIO
  28006. static int test_wolfSSL_X509_INFO(void)
  28007. {
  28008. EXPECT_DECLS;
  28009. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  28010. STACK_OF(X509_INFO) *info_stack = NULL;
  28011. X509_INFO *info = NULL;
  28012. BIO *cert = NULL;
  28013. int i;
  28014. /* PEM in hex format to avoid null terminator */
  28015. byte data[] = {
  28016. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  28017. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  28018. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  28019. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  28020. 0x2d, 0x2d, 0x2d
  28021. };
  28022. /* PEM in hex format to avoid null terminator */
  28023. byte data2[] = {
  28024. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  28025. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  28026. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  28027. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  28028. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  28029. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  28030. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  28031. };
  28032. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28033. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28034. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  28035. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  28036. ExpectNotNull(info->x509);
  28037. ExpectNull(info->crl);
  28038. ExpectNull(info->x_pkey);
  28039. }
  28040. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28041. info_stack = NULL;
  28042. BIO_free(cert);
  28043. cert = NULL;
  28044. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  28045. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28046. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28047. info_stack = NULL;
  28048. BIO_free(cert);
  28049. cert = NULL;
  28050. /* This case should fail due to invalid input. */
  28051. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28052. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  28053. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28054. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28055. info_stack = NULL;
  28056. BIO_free(cert);
  28057. cert = NULL;
  28058. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  28059. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  28060. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  28061. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  28062. BIO_free(cert);
  28063. #endif
  28064. return EXPECT_RESULT();
  28065. }
  28066. #endif
  28067. static int test_wolfSSL_X509_subject_name_hash(void)
  28068. {
  28069. EXPECT_DECLS;
  28070. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28071. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28072. X509* x509 = NULL;
  28073. X509_NAME* subjectName = NULL;
  28074. unsigned long ret1 = 0;
  28075. unsigned long ret2 = 0;
  28076. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28077. SSL_FILETYPE_PEM));
  28078. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  28079. /* These two
  28080. * - X509_subject_name_hash(x509)
  28081. * - X509_NAME_hash(X509_get_subject_name(x509))
  28082. * should give the same hash, if !defined(NO_SHA) is true. */
  28083. ret1 = X509_subject_name_hash(x509);
  28084. ExpectIntNE(ret1, 0);
  28085. #if !defined(NO_SHA)
  28086. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  28087. ExpectIntNE(ret2, 0);
  28088. ExpectIntEQ(ret1, ret2);
  28089. #else
  28090. (void) ret2;
  28091. #endif
  28092. X509_free(x509);
  28093. #endif
  28094. return EXPECT_RESULT();
  28095. }
  28096. static int test_wolfSSL_X509_issuer_name_hash(void)
  28097. {
  28098. EXPECT_DECLS;
  28099. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28100. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  28101. X509* x509 = NULL;
  28102. X509_NAME* issuertName = NULL;
  28103. unsigned long ret1 = 0;
  28104. unsigned long ret2 = 0;
  28105. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28106. SSL_FILETYPE_PEM));
  28107. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  28108. /* These two
  28109. * - X509_issuer_name_hash(x509)
  28110. * - X509_NAME_hash(X509_get_issuer_name(x509))
  28111. * should give the same hash, if !defined(NO_SHA) is true. */
  28112. ret1 = X509_issuer_name_hash(x509);
  28113. ExpectIntNE(ret1, 0);
  28114. #if !defined(NO_SHA)
  28115. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  28116. ExpectIntNE(ret2, 0);
  28117. ExpectIntEQ(ret1, ret2);
  28118. #else
  28119. (void) ret2;
  28120. #endif
  28121. X509_free(x509);
  28122. #endif
  28123. return EXPECT_RESULT();
  28124. }
  28125. static int test_wolfSSL_X509_check_host(void)
  28126. {
  28127. EXPECT_DECLS;
  28128. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  28129. && !defined(NO_SHA) && !defined(NO_RSA)
  28130. X509* x509 = NULL;
  28131. const char altName[] = "example.com";
  28132. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28133. SSL_FILETYPE_PEM));
  28134. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  28135. WOLFSSL_SUCCESS);
  28136. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  28137. WOLFSSL_FAILURE);
  28138. X509_free(x509);
  28139. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  28140. WOLFSSL_FAILURE);
  28141. #endif
  28142. return EXPECT_RESULT();
  28143. }
  28144. static int test_wolfSSL_X509_check_email(void)
  28145. {
  28146. EXPECT_DECLS;
  28147. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  28148. X509* x509 = NULL;
  28149. const char goodEmail[] = "info@wolfssl.com";
  28150. const char badEmail[] = "disinfo@wolfssl.com";
  28151. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28152. SSL_FILETYPE_PEM));
  28153. /* Should fail on non-matching email address */
  28154. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  28155. WOLFSSL_FAILURE);
  28156. /* Should succeed on matching email address */
  28157. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  28158. WOLFSSL_SUCCESS);
  28159. /* Should compute length internally when not provided */
  28160. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  28161. WOLFSSL_SUCCESS);
  28162. /* Should fail when email address is NULL */
  28163. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  28164. WOLFSSL_FAILURE);
  28165. X509_free(x509);
  28166. /* Should fail when x509 is NULL */
  28167. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  28168. WOLFSSL_FAILURE);
  28169. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  28170. return EXPECT_RESULT();
  28171. }
  28172. static int test_wolfSSL_DES(void)
  28173. {
  28174. EXPECT_DECLS;
  28175. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  28176. const_DES_cblock myDes;
  28177. DES_cblock iv;
  28178. DES_key_schedule key;
  28179. word32 i;
  28180. DES_LONG dl;
  28181. unsigned char msg[] = "hello wolfssl";
  28182. DES_check_key(1);
  28183. DES_set_key(&myDes, &key);
  28184. /* check, check of odd parity */
  28185. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  28186. myDes[0] = 6; /* set even parity */
  28187. XMEMSET(key, 5, sizeof(DES_key_schedule));
  28188. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  28189. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  28190. /* set odd parity for success case */
  28191. DES_set_odd_parity(&myDes);
  28192. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  28193. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  28194. myDes[3]);
  28195. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  28196. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  28197. ExpectIntEQ(key[i], myDes[i]);
  28198. }
  28199. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  28200. /* check weak key */
  28201. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  28202. XMEMSET(key, 5, sizeof(DES_key_schedule));
  28203. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  28204. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  28205. /* now do unchecked copy of a weak key over */
  28206. DES_set_key_unchecked(&myDes, &key);
  28207. /* compare arrays, should be the same */
  28208. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  28209. ExpectIntEQ(key[i], myDes[i]);
  28210. }
  28211. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  28212. /* check DES_key_sched API */
  28213. XMEMSET(key, 1, sizeof(DES_key_schedule));
  28214. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  28215. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  28216. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  28217. /* compare arrays, should be the same */
  28218. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  28219. ExpectIntEQ(key[i], myDes[i]);
  28220. }
  28221. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  28222. * DES_cbc_encrypt on the input */
  28223. XMEMSET(iv, 0, sizeof(DES_cblock));
  28224. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  28225. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  28226. ExpectIntEQ(dl, 480052723);
  28227. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  28228. return EXPECT_RESULT();
  28229. }
  28230. static int test_wc_PemToDer(void)
  28231. {
  28232. EXPECT_DECLS;
  28233. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28234. int ret;
  28235. DerBuffer* pDer = NULL;
  28236. const char* ca_cert = "./certs/server-cert.pem";
  28237. byte* cert_buf = NULL;
  28238. size_t cert_sz = 0;
  28239. int eccKey = 0;
  28240. EncryptedInfo info;
  28241. XMEMSET(&info, 0, sizeof(info));
  28242. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28243. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  28244. &info, &eccKey), 0);
  28245. wc_FreeDer(&pDer);
  28246. pDer = NULL;
  28247. if (cert_buf != NULL) {
  28248. free(cert_buf);
  28249. cert_buf = NULL;
  28250. }
  28251. #ifdef HAVE_ECC
  28252. {
  28253. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  28254. byte key_buf[256] = {0};
  28255. /* Test fail of loading a key with cert type */
  28256. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  28257. key_buf[0] = '\n';
  28258. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  28259. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  28260. &pDer, NULL, &info, &eccKey)), 0);
  28261. #ifdef OPENSSL_EXTRA
  28262. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  28263. &pDer, NULL, &info, &eccKey)), 0);
  28264. #endif
  28265. wc_FreeDer(&pDer);
  28266. if (cert_buf != NULL)
  28267. free(cert_buf);
  28268. }
  28269. #endif
  28270. #endif
  28271. return EXPECT_RESULT();
  28272. }
  28273. static int test_wc_AllocDer(void)
  28274. {
  28275. EXPECT_DECLS;
  28276. #if !defined(NO_CERTS)
  28277. DerBuffer* pDer = NULL;
  28278. word32 testSize = 1024;
  28279. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  28280. BAD_FUNC_ARG);
  28281. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  28282. ExpectNotNull(pDer);
  28283. wc_FreeDer(&pDer);
  28284. #endif
  28285. return EXPECT_RESULT();
  28286. }
  28287. static int test_wc_CertPemToDer(void)
  28288. {
  28289. EXPECT_DECLS;
  28290. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  28291. const char* ca_cert = "./certs/ca-cert.pem";
  28292. byte* cert_buf = NULL;
  28293. size_t cert_sz = 0;
  28294. size_t cert_dersz = 0;
  28295. byte* cert_der = NULL;
  28296. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28297. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28298. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28299. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28300. (int)cert_dersz, CERT_TYPE), 0);
  28301. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  28302. BAD_FUNC_ARG);
  28303. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  28304. BAD_FUNC_ARG);
  28305. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  28306. BAD_FUNC_ARG);
  28307. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  28308. CERT_TYPE), BAD_FUNC_ARG);
  28309. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  28310. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28311. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  28312. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  28313. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  28314. CERT_TYPE), BAD_FUNC_ARG);
  28315. if (cert_der != NULL)
  28316. free(cert_der);
  28317. if (cert_buf != NULL)
  28318. free(cert_buf);
  28319. #endif
  28320. return EXPECT_RESULT();
  28321. }
  28322. static int test_wc_KeyPemToDer(void)
  28323. {
  28324. EXPECT_DECLS;
  28325. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28326. int ret;
  28327. const byte cert_buf[] = \
  28328. "-----BEGIN PRIVATE KEY-----\n"
  28329. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  28330. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  28331. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  28332. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  28333. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  28334. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  28335. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  28336. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  28337. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  28338. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  28339. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  28340. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  28341. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  28342. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  28343. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  28344. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  28345. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  28346. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  28347. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  28348. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  28349. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  28350. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  28351. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  28352. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  28353. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  28354. "AQue5o6oVfhgLiJzMdo/77gw\n"
  28355. "-----END PRIVATE KEY-----\n";
  28356. const int cert_sz = sizeof(cert_buf);
  28357. const char cert_pw[] = "password";
  28358. int cert_dersz = 0;
  28359. byte* cert_der = NULL;
  28360. /* Bad arg: Cert buffer is NULL */
  28361. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  28362. BAD_FUNC_ARG);
  28363. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  28364. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  28365. BAD_FUNC_ARG);
  28366. /* Test normal operation */
  28367. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28368. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28369. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28370. cert_pw), 0);
  28371. ExpectIntLE(ret, cert_sz);
  28372. if (cert_der != NULL) {
  28373. free(cert_der);
  28374. cert_der = NULL;
  28375. }
  28376. /* Test NULL for DER buffer to return needed DER buffer size */
  28377. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  28378. ExpectIntLE(ret, cert_sz);
  28379. if (EXPECT_SUCCESS())
  28380. cert_dersz = ret;
  28381. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28382. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  28383. cert_pw), 0);
  28384. ExpectIntLE(ret, cert_sz);
  28385. if (cert_der != NULL)
  28386. free(cert_der);
  28387. #endif
  28388. return EXPECT_RESULT();
  28389. }
  28390. static int test_wc_PubKeyPemToDer(void)
  28391. {
  28392. EXPECT_DECLS;
  28393. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  28394. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28395. int ret = 0;
  28396. const char* key = "./certs/ecc-client-keyPub.pem";
  28397. byte* cert_buf = NULL;
  28398. size_t cert_sz = 0, cert_dersz = 0;
  28399. byte* cert_der = NULL;
  28400. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  28401. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  28402. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  28403. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  28404. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28405. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28406. (int)cert_dersz), 0);
  28407. if (cert_der != NULL) {
  28408. free(cert_der);
  28409. cert_der = NULL;
  28410. }
  28411. /* Test NULL for DER buffer to return needed DER buffer size */
  28412. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  28413. ExpectIntLE(ret, cert_sz);
  28414. cert_dersz = ret;
  28415. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28416. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  28417. (int)cert_dersz), 0);
  28418. if (cert_der != NULL) {
  28419. free(cert_der);
  28420. }
  28421. if (cert_buf != NULL) {
  28422. free(cert_buf);
  28423. }
  28424. #endif
  28425. return EXPECT_RESULT();
  28426. }
  28427. static int test_wc_PemPubKeyToDer(void)
  28428. {
  28429. EXPECT_DECLS;
  28430. #if !defined(NO_FILESYSTEM) && \
  28431. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  28432. const char* key = "./certs/ecc-client-keyPub.pem";
  28433. size_t cert_dersz = 1024;
  28434. byte* cert_der = NULL;
  28435. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  28436. BAD_FUNC_ARG);
  28437. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28438. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  28439. if (cert_der != NULL) {
  28440. free(cert_der);
  28441. }
  28442. #endif
  28443. return EXPECT_RESULT();
  28444. }
  28445. static int test_wc_GetPubKeyDerFromCert(void)
  28446. {
  28447. EXPECT_DECLS;
  28448. #if !defined(NO_RSA) || defined(HAVE_ECC)
  28449. int ret;
  28450. word32 idx = 0;
  28451. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28452. word32 keyDerSz = (word32)sizeof(keyDer);
  28453. DecodedCert decoded;
  28454. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28455. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  28456. word32 certBufSz = sizeof(certBuf);
  28457. #endif
  28458. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  28459. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28460. XFILE fp = XBADFILE;
  28461. #endif
  28462. #ifndef NO_RSA
  28463. RsaKey rsaKey;
  28464. #if defined(USE_CERT_BUFFERS_2048)
  28465. byte* rsaCertDer = (byte*)client_cert_der_2048;
  28466. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  28467. #elif defined(USE_CERT_BUFFERS_1024)
  28468. byte* rsaCertDer = (byte*)client_cert_der_1024;
  28469. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  28470. #else
  28471. unsigned char rsaCertDer[TWOK_BUF];
  28472. word32 rsaCertDerSz;
  28473. #endif
  28474. #endif
  28475. #ifdef HAVE_ECC
  28476. ecc_key eccKey;
  28477. #if defined(USE_CERT_BUFFERS_256)
  28478. byte* eccCert = (byte*)cliecc_cert_der_256;
  28479. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  28480. #else
  28481. unsigned char eccCert[ONEK_BUF];
  28482. word32 eccCertSz;
  28483. XFILE fp2 = XBADFILE;
  28484. #endif
  28485. #endif
  28486. #ifndef NO_RSA
  28487. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  28488. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  28489. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  28490. fp), 0);
  28491. if (fp != XBADFILE) {
  28492. XFCLOSE(fp);
  28493. fp = XBADFILE;
  28494. }
  28495. #endif
  28496. /* good test case - RSA DER cert */
  28497. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  28498. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28499. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28500. ExpectIntGT(keyDerSz, 0);
  28501. /* sanity check, verify we can import DER public key */
  28502. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28503. ExpectIntEQ(ret, 0);
  28504. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28505. if (ret == 0) {
  28506. wc_FreeRsaKey(&rsaKey);
  28507. }
  28508. /* test LENGTH_ONLY_E case */
  28509. keyDerSz = 0;
  28510. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28511. LENGTH_ONLY_E);
  28512. ExpectIntGT(keyDerSz, 0);
  28513. /* bad args: DecodedCert NULL */
  28514. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  28515. /* bad args: output key buff size */
  28516. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  28517. /* bad args: zero size output key buffer */
  28518. keyDerSz = 0;
  28519. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28520. BAD_FUNC_ARG);
  28521. wc_FreeDecodedCert(&decoded);
  28522. /* Certificate Request Tests */
  28523. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  28524. {
  28525. XMEMSET(certBuf, 0, sizeof(certBuf));
  28526. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  28527. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  28528. if (fp != XBADFILE) {
  28529. XFCLOSE(fp);
  28530. }
  28531. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  28532. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  28533. /* good test case - RSA DER certificate request */
  28534. keyDerSz = sizeof(keyDer);
  28535. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  28536. 0);
  28537. ExpectIntGT(keyDerSz, 0);
  28538. /* sanity check, verify we can import DER public key */
  28539. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  28540. ExpectIntEQ(ret, 0);
  28541. idx = 0;
  28542. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  28543. if (ret == 0) {
  28544. wc_FreeRsaKey(&rsaKey);
  28545. }
  28546. wc_FreeDecodedCert(&decoded);
  28547. }
  28548. #endif /* WOLFSSL_CERT_REQ */
  28549. #endif /* NO_RSA */
  28550. #ifdef HAVE_ECC
  28551. #ifndef USE_CERT_BUFFERS_256
  28552. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  28553. XBADFILE);
  28554. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  28555. if (fp2 != XBADFILE) {
  28556. XFCLOSE(fp2);
  28557. }
  28558. #endif
  28559. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  28560. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28561. /* good test case - ECC */
  28562. XMEMSET(keyDer, 0, sizeof(keyDer));
  28563. keyDerSz = sizeof(keyDer);
  28564. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28565. ExpectIntGT(keyDerSz, 0);
  28566. /* sanity check, verify we can import DER public key */
  28567. ret = wc_ecc_init(&eccKey);
  28568. ExpectIntEQ(ret, 0);
  28569. idx = 0; /* reset idx to 0, used above in RSA case */
  28570. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  28571. if (ret == 0) {
  28572. wc_ecc_free(&eccKey);
  28573. }
  28574. /* test LENGTH_ONLY_E case */
  28575. keyDerSz = 0;
  28576. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  28577. LENGTH_ONLY_E);
  28578. ExpectIntGT(keyDerSz, 0);
  28579. wc_FreeDecodedCert(&decoded);
  28580. #endif
  28581. #endif /* !NO_RSA || HAVE_ECC */
  28582. return EXPECT_RESULT();
  28583. }
  28584. static int test_wc_CheckCertSigPubKey(void)
  28585. {
  28586. EXPECT_DECLS;
  28587. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28588. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  28589. int ret;
  28590. const char* ca_cert = "./certs/ca-cert.pem";
  28591. byte* cert_buf = NULL;
  28592. size_t cert_sz = 0;
  28593. byte* cert_der = NULL;
  28594. word32 cert_dersz = 0;
  28595. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  28596. word32 keyDerSz = (word32)sizeof(keyDer);
  28597. DecodedCert decoded;
  28598. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  28599. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  28600. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  28601. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  28602. (int)cert_dersz, CERT_TYPE), 0);
  28603. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  28604. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  28605. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  28606. ExpectIntGT(keyDerSz, 0);
  28607. /* Good test case. */
  28608. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28609. keyDerSz, RSAk), 0);
  28610. /* No certificate. */
  28611. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  28612. ECDSAk), BAD_FUNC_ARG);
  28613. /* Bad cert size. */
  28614. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  28615. RSAk), 0);
  28616. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  28617. /* No public key. */
  28618. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  28619. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  28620. /* Bad public key size. */
  28621. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  28622. RSAk), BAD_FUNC_ARG);
  28623. /* Wrong aglo. */
  28624. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  28625. keyDerSz, ECDSAk), ASN_PARSE_E);
  28626. wc_FreeDecodedCert(&decoded);
  28627. if (cert_der != NULL)
  28628. free(cert_der);
  28629. if (cert_buf != NULL)
  28630. free(cert_buf);
  28631. #endif
  28632. return EXPECT_RESULT();
  28633. }
  28634. static int test_wolfSSL_certs(void)
  28635. {
  28636. EXPECT_DECLS;
  28637. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28638. !defined(NO_RSA)
  28639. X509* x509ext = NULL;
  28640. #ifdef OPENSSL_ALL
  28641. X509* x509 = NULL;
  28642. WOLFSSL_X509_EXTENSION* ext = NULL;
  28643. ASN1_OBJECT* obj = NULL;
  28644. #endif
  28645. WOLFSSL* ssl = NULL;
  28646. WOLFSSL_CTX* ctx = NULL;
  28647. STACK_OF(ASN1_OBJECT)* sk = NULL;
  28648. ASN1_STRING* asn1_str = NULL;
  28649. AUTHORITY_KEYID* akey = NULL;
  28650. BASIC_CONSTRAINTS* bc = NULL;
  28651. int crit;
  28652. #ifndef NO_WOLFSSL_SERVER
  28653. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  28654. #else
  28655. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  28656. #endif
  28657. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  28658. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28659. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  28660. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28661. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  28662. #endif
  28663. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  28664. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28665. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  28666. #endif
  28667. ExpectNotNull(ssl = SSL_new(ctx));
  28668. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28669. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28670. #endif
  28671. #ifdef HAVE_PK_CALLBACKS
  28672. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  28673. #endif /* HAVE_PK_CALLBACKS */
  28674. /* create and use x509 */
  28675. #ifdef OPENSSL_ALL
  28676. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  28677. WOLFSSL_FILETYPE_PEM));
  28678. #endif
  28679. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  28680. WOLFSSL_FILETYPE_PEM));
  28681. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  28682. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28683. /* with loading in a new cert the check on private key should now fail */
  28684. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28685. #endif
  28686. #if defined(USE_CERT_BUFFERS_2048)
  28687. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  28688. (unsigned char*)server_cert_der_2048,
  28689. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  28690. #endif
  28691. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28692. /************* Get Digest of Certificate ******************/
  28693. {
  28694. byte digest[64]; /* max digest size */
  28695. word32 digestSz;
  28696. XMEMSET(digest, 0, sizeof(digest));
  28697. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  28698. WOLFSSL_SUCCESS);
  28699. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  28700. WOLFSSL_SUCCESS);
  28701. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  28702. WOLFSSL_FAILURE);
  28703. }
  28704. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  28705. /* test and checkout X509 extensions */
  28706. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  28707. NID_basic_constraints, &crit, NULL));
  28708. ExpectIntEQ(crit, 0);
  28709. #ifdef OPENSSL_ALL
  28710. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  28711. X509_EXTENSION_free(ext);
  28712. ext = NULL;
  28713. ExpectNotNull(ext = X509_EXTENSION_new());
  28714. X509_EXTENSION_set_critical(ext, 1);
  28715. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  28716. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  28717. ASN1_OBJECT_free(obj);
  28718. obj = NULL;
  28719. X509_EXTENSION_free(ext);
  28720. ext = NULL;
  28721. ExpectNotNull(ext = X509_EXTENSION_new());
  28722. X509_EXTENSION_set_critical(ext, 0);
  28723. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  28724. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  28725. NULL);
  28726. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  28727. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  28728. * and X509_get_ext_d2i has created new */
  28729. asn1_str = NULL;
  28730. X509_EXTENSION_free(ext);
  28731. ext = NULL;
  28732. #endif
  28733. BASIC_CONSTRAINTS_free(bc);
  28734. bc = NULL;
  28735. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  28736. NID_key_usage, &crit, NULL));
  28737. ExpectIntEQ(crit, 1);
  28738. ExpectIntEQ(asn1_str->type, NID_key_usage);
  28739. #ifdef OPENSSL_ALL
  28740. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  28741. X509_EXTENSION_free(ext);
  28742. ext = NULL;
  28743. #endif
  28744. ASN1_STRING_free(asn1_str);
  28745. asn1_str = NULL;
  28746. #ifdef OPENSSL_ALL
  28747. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  28748. NID_ext_key_usage, &crit, NULL));
  28749. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  28750. X509_EXTENSION_free(ext);
  28751. ext = NULL;
  28752. sk_ASN1_OBJECT_pop_free(sk, NULL);
  28753. sk = NULL;
  28754. #else
  28755. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  28756. &crit, NULL);
  28757. ExpectNull(sk);
  28758. #endif
  28759. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  28760. NID_authority_key_identifier, &crit, NULL));
  28761. #ifdef OPENSSL_ALL
  28762. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  28763. akey));
  28764. X509_EXTENSION_free(ext);
  28765. ext = NULL;
  28766. #endif
  28767. wolfSSL_AUTHORITY_KEYID_free(akey);
  28768. akey = NULL;
  28769. /* NID not yet supported */
  28770. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28771. NID_private_key_usage_period, &crit, NULL));
  28772. ExpectIntEQ(crit, -1);
  28773. sk_ASN1_OBJECT_free(sk);
  28774. sk = NULL;
  28775. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  28776. NID_subject_alt_name, &crit, NULL));
  28777. {
  28778. int i;
  28779. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  28780. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  28781. ExpectIntEQ(gen->type, GEN_DNS);
  28782. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  28783. }
  28784. }
  28785. sk_GENERAL_NAME_free(sk);
  28786. sk = NULL;
  28787. /* NID not yet supported */
  28788. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28789. NID_issuer_alt_name, &crit, NULL));
  28790. ExpectIntEQ(crit, -1);
  28791. sk_ASN1_OBJECT_free(sk);
  28792. sk = NULL;
  28793. /* NID not yet supported */
  28794. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28795. NID_info_access, &crit, NULL));
  28796. sk_ASN1_OBJECT_free(sk);
  28797. sk = NULL;
  28798. /* NID not yet supported */
  28799. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28800. NID_sinfo_access, &crit, NULL));
  28801. ExpectIntEQ(crit, -1);
  28802. sk_ASN1_OBJECT_free(sk);
  28803. sk = NULL;
  28804. /* NID not yet supported */
  28805. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28806. NID_name_constraints, &crit, NULL));
  28807. ExpectIntEQ(crit, -1);
  28808. sk_ASN1_OBJECT_free(sk);
  28809. sk = NULL;
  28810. /* no cert policy set */
  28811. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28812. NID_certificate_policies, &crit, NULL));
  28813. sk_ASN1_OBJECT_free(sk);
  28814. sk = NULL;
  28815. /* NID not yet supported */
  28816. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28817. NID_policy_mappings, &crit, NULL));
  28818. ExpectIntEQ(crit, -1);
  28819. sk_ASN1_OBJECT_free(sk);
  28820. sk = NULL;
  28821. /* NID not yet supported */
  28822. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28823. NID_policy_constraints, &crit, NULL));
  28824. ExpectIntEQ(crit, -1);
  28825. sk_ASN1_OBJECT_free(sk);
  28826. sk = NULL;
  28827. /* NID not yet supported */
  28828. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28829. NID_inhibit_any_policy, &crit, NULL));
  28830. ExpectIntEQ(crit, -1);
  28831. sk_ASN1_OBJECT_free(sk);
  28832. sk = NULL;
  28833. /* NID not yet supported */
  28834. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  28835. NID_tlsfeature, &crit, NULL));
  28836. ExpectIntEQ(crit, -1);
  28837. sk_ASN1_OBJECT_free(sk);
  28838. sk = NULL;
  28839. /* test invalid cases */
  28840. crit = 0;
  28841. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  28842. NULL));
  28843. ExpectIntEQ(crit, -1);
  28844. /* NULL passed for criticality. */
  28845. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  28846. NID_tlsfeature, NULL, NULL));
  28847. ExpectIntEQ(SSL_get_hit(ssl), 0);
  28848. #ifdef OPENSSL_ALL
  28849. X509_free(x509);
  28850. #endif
  28851. X509_free(x509ext);
  28852. SSL_free(ssl);
  28853. SSL_CTX_free(ctx);
  28854. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  28855. return EXPECT_RESULT();
  28856. }
  28857. static int test_wolfSSL_X509_check_private_key(void)
  28858. {
  28859. EXPECT_DECLS;
  28860. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  28861. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  28862. X509* x509 = NULL;
  28863. EVP_PKEY* pkey = NULL;
  28864. const byte* key;
  28865. /* Check with correct key */
  28866. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  28867. SSL_FILETYPE_PEM)));
  28868. key = client_key_der_2048;
  28869. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  28870. (long)sizeof_client_key_der_2048));
  28871. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  28872. EVP_PKEY_free(pkey);
  28873. pkey = NULL;
  28874. /* Check with wrong key */
  28875. key = server_key_der_2048;
  28876. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  28877. (long)sizeof_server_key_der_2048));
  28878. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  28879. /* test for incorrect parameter */
  28880. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  28881. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  28882. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  28883. EVP_PKEY_free(pkey);
  28884. X509_free(x509);
  28885. #endif
  28886. return EXPECT_RESULT();
  28887. }
  28888. static int test_wolfSSL_private_keys(void)
  28889. {
  28890. EXPECT_DECLS;
  28891. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28892. !defined(NO_FILESYSTEM)
  28893. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28894. WOLFSSL* ssl = NULL;
  28895. WOLFSSL_CTX* ctx = NULL;
  28896. EVP_PKEY* pkey = NULL;
  28897. OpenSSL_add_all_digests();
  28898. OpenSSL_add_all_algorithms();
  28899. #ifndef NO_RSA
  28900. #ifndef NO_WOLFSSL_SERVER
  28901. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28902. #else
  28903. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28904. #endif
  28905. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28906. /* Have to load a cert before you can check the private key against that
  28907. * certificates public key! */
  28908. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28909. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  28910. #endif
  28911. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28912. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28913. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28914. #endif
  28915. ExpectNotNull(ssl = SSL_new(ctx));
  28916. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28917. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28918. #endif
  28919. #ifdef USE_CERT_BUFFERS_2048
  28920. {
  28921. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28922. unsigned char buf[FOURK_BUF];
  28923. word32 bufSz;
  28924. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  28925. (unsigned char*)client_key_der_2048,
  28926. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28927. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28928. /* Should mismatch now that a different private key loaded */
  28929. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28930. #endif
  28931. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  28932. (unsigned char*)server_key,
  28933. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28934. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28935. /* After loading back in DER format of original key, should match */
  28936. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28937. #endif
  28938. /* test loading private key to the WOLFSSL_CTX */
  28939. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28940. (unsigned char*)client_key_der_2048,
  28941. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  28942. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28943. /* Should mismatch now that a different private key loaded */
  28944. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28945. #endif
  28946. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  28947. (unsigned char*)server_key,
  28948. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  28949. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28950. /* After loading back in DER format of original key, should match */
  28951. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  28952. #endif
  28953. /* pkey not set yet, expecting to fail */
  28954. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  28955. /* set PKEY and test again */
  28956. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28957. &server_key, (long)sizeof_server_key_der_2048));
  28958. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28959. /* reuse PKEY structure and test
  28960. * this should be checked with a memory management sanity checker */
  28961. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  28962. server_key = (const unsigned char*)server_key_der_2048;
  28963. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28964. &server_key, (long)sizeof_server_key_der_2048));
  28965. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  28966. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  28967. bufSz = FOURK_BUF;
  28968. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  28969. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  28970. RSAk, NULL, 0)), 0);
  28971. server_key = (const unsigned char*)buf;
  28972. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  28973. (long)bufSz));
  28974. }
  28975. #endif
  28976. EVP_PKEY_free(pkey);
  28977. pkey = NULL;
  28978. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  28979. ssl = NULL;
  28980. SSL_CTX_free(ctx);
  28981. ctx = NULL;
  28982. #endif /* end of RSA private key match tests */
  28983. #ifdef HAVE_ECC
  28984. #ifndef NO_WOLFSSL_SERVER
  28985. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28986. #else
  28987. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28988. #endif
  28989. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  28990. WOLFSSL_FILETYPE_PEM));
  28991. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  28992. WOLFSSL_FILETYPE_PEM));
  28993. ExpectNotNull(ssl = SSL_new(ctx));
  28994. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28995. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28996. #endif
  28997. SSL_free(ssl);
  28998. ssl = NULL;
  28999. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  29000. WOLFSSL_FILETYPE_PEM));
  29001. ExpectNotNull(ssl = SSL_new(ctx));
  29002. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  29003. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29004. #endif
  29005. SSL_free(ssl);
  29006. ssl = NULL;
  29007. SSL_CTX_free(ctx);
  29008. ctx = NULL;
  29009. #endif /* end of ECC private key match tests */
  29010. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  29011. #ifndef NO_WOLFSSL_SERVER
  29012. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29013. #else
  29014. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29015. #endif
  29016. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  29017. WOLFSSL_FILETYPE_PEM));
  29018. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  29019. WOLFSSL_FILETYPE_PEM));
  29020. ExpectNotNull(ssl = SSL_new(ctx));
  29021. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29022. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29023. #endif
  29024. SSL_free(ssl);
  29025. ssl = NULL;
  29026. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  29027. WOLFSSL_FILETYPE_PEM));
  29028. ExpectNotNull(ssl = SSL_new(ctx));
  29029. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29030. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29031. #endif
  29032. SSL_free(ssl);
  29033. ssl = NULL;
  29034. SSL_CTX_free(ctx);
  29035. ctx = NULL;
  29036. #endif /* end of Ed25519 private key match tests */
  29037. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  29038. #ifndef NO_WOLFSSL_SERVER
  29039. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29040. #else
  29041. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29042. #endif
  29043. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  29044. WOLFSSL_FILETYPE_PEM));
  29045. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  29046. WOLFSSL_FILETYPE_PEM));
  29047. ExpectNotNull(ssl = SSL_new(ctx));
  29048. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29049. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29050. #endif
  29051. SSL_free(ssl);
  29052. ssl = NULL;
  29053. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  29054. WOLFSSL_FILETYPE_PEM));
  29055. ExpectNotNull(ssl = SSL_new(ctx));
  29056. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  29057. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  29058. #endif
  29059. SSL_free(ssl);
  29060. ssl = NULL;
  29061. SSL_CTX_free(ctx);
  29062. ctx = NULL;
  29063. #endif /* end of Ed448 private key match tests */
  29064. EVP_cleanup();
  29065. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  29066. CONF_modules_free();
  29067. ENGINE_cleanup();
  29068. CONF_modules_unload();
  29069. (void)ssl;
  29070. (void)ctx;
  29071. (void)pkey;
  29072. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29073. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29074. return EXPECT_RESULT();
  29075. }
  29076. static int test_wolfSSL_PEM_read_PrivateKey(void)
  29077. {
  29078. EXPECT_DECLS;
  29079. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29080. && !defined(NO_FILESYSTEM)
  29081. XFILE file = XBADFILE;
  29082. const char* fname = "./certs/server-key.pem";
  29083. EVP_PKEY* pkey = NULL;
  29084. RSA* rsa = NULL;
  29085. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  29086. unsigned char* sig = NULL;
  29087. size_t sigLen;
  29088. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  29089. size_t tbsLen = sizeof(tbs);
  29090. /* Check error case. */
  29091. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  29092. /* Read in an RSA key. */
  29093. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29094. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  29095. if (file != XBADFILE)
  29096. XFCLOSE(file);
  29097. /* Make sure the key is usable by signing some data with it. */
  29098. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  29099. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  29100. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  29101. DYNAMIC_TYPE_TMP_BUFFER));
  29102. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  29103. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  29104. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  29105. WOLFSSL_SUCCESS);
  29106. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29107. EVP_PKEY_CTX_free(ctx);
  29108. EVP_PKEY_free(pkey);
  29109. #endif
  29110. return EXPECT_RESULT();
  29111. }
  29112. static int test_wolfSSL_PEM_read_PUBKEY(void)
  29113. {
  29114. EXPECT_DECLS;
  29115. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  29116. && !defined(NO_FILESYSTEM)
  29117. XFILE file = XBADFILE;
  29118. const char* fname = "./certs/client-keyPub.pem";
  29119. EVP_PKEY* pkey = NULL;
  29120. /* Check error case. */
  29121. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  29122. /* Read in an RSA key. */
  29123. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29124. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  29125. EVP_PKEY_free(pkey);
  29126. if (file != XBADFILE)
  29127. XFCLOSE(file);
  29128. #endif
  29129. return EXPECT_RESULT();
  29130. }
  29131. static int test_wolfSSL_PEM_PrivateKey(void)
  29132. {
  29133. EXPECT_DECLS;
  29134. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29135. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  29136. #ifndef NO_BIO
  29137. BIO* bio = NULL;
  29138. #endif
  29139. EVP_PKEY* pkey = NULL;
  29140. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  29141. #ifndef NO_BIO
  29142. /* test creating new EVP_PKEY with bad arg */
  29143. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  29144. /* test loading RSA key using BIO */
  29145. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29146. {
  29147. XFILE file = XBADFILE;
  29148. const char* fname = "./certs/server-key.pem";
  29149. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  29150. size_t sz;
  29151. byte* buf = NULL;
  29152. EVP_PKEY* pkey2 = NULL;
  29153. EVP_PKEY* pkey3 = NULL;
  29154. RSA* rsa_key = NULL;
  29155. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29156. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29157. ExpectIntGT(sz = XFTELL(file), 0);
  29158. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29159. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29160. if (buf != NULL) {
  29161. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29162. }
  29163. if (file != XBADFILE) {
  29164. XFCLOSE(file);
  29165. file = XBADFILE;
  29166. }
  29167. /* Test using BIO new mem and loading PEM private key */
  29168. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29169. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29170. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29171. buf = NULL;
  29172. BIO_free(bio);
  29173. bio = NULL;
  29174. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29175. if (pkey2 != NULL) {
  29176. pkey2->type = EVP_PKEY_RSA;
  29177. }
  29178. /* Test parameter copy */
  29179. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  29180. EVP_PKEY_free(pkey2);
  29181. EVP_PKEY_free(pkey);
  29182. pkey = NULL;
  29183. /* Qt unit test case : rsa pkcs8 key */
  29184. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  29185. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29186. ExpectIntGT(sz = XFTELL(file), 0);
  29187. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29188. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29189. if (buf) {
  29190. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29191. }
  29192. if (file != XBADFILE) {
  29193. XFCLOSE(file);
  29194. file = XBADFILE;
  29195. }
  29196. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29197. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29198. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29199. buf = NULL;
  29200. BIO_free(bio);
  29201. bio = NULL;
  29202. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29203. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  29204. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  29205. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29206. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29207. #else
  29208. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29209. #endif
  29210. RSA_free(rsa_key);
  29211. EVP_PKEY_free(pkey3);
  29212. EVP_PKEY_free(pkey);
  29213. pkey = NULL;
  29214. }
  29215. #endif
  29216. /* test loading ECC key using BIO */
  29217. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29218. {
  29219. XFILE file = XBADFILE;
  29220. const char* fname = "./certs/ecc-key.pem";
  29221. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  29222. size_t sz = 0;
  29223. byte* buf = NULL;
  29224. EVP_PKEY* pkey2 = NULL;
  29225. EVP_PKEY* pkey3 = NULL;
  29226. EC_KEY* ec_key = NULL;
  29227. int nid = 0;
  29228. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29229. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29230. ExpectIntGT(sz = XFTELL(file), 0);
  29231. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29232. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29233. if (buf) {
  29234. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29235. }
  29236. if (file != XBADFILE) {
  29237. XFCLOSE(file);
  29238. file = XBADFILE;
  29239. }
  29240. /* Test using BIO new mem and loading PEM private key */
  29241. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29242. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29243. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29244. buf = NULL;
  29245. BIO_free(bio);
  29246. bio = NULL;
  29247. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29248. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29249. if (pkey2 != NULL) {
  29250. pkey2->type = EVP_PKEY_EC;
  29251. }
  29252. /* Test parameter copy */
  29253. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  29254. /* Qt unit test case 1*/
  29255. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29256. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29257. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29258. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29259. #else
  29260. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29261. #endif
  29262. /* Test default digest */
  29263. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  29264. ExpectIntEQ(nid, NID_sha256);
  29265. EC_KEY_free(ec_key);
  29266. ec_key = NULL;
  29267. EVP_PKEY_free(pkey3);
  29268. pkey3 = NULL;
  29269. EVP_PKEY_free(pkey2);
  29270. pkey2 = NULL;
  29271. EVP_PKEY_free(pkey);
  29272. pkey = NULL;
  29273. /* Qt unit test case ec pkcs8 key */
  29274. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  29275. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  29276. ExpectIntGT(sz = XFTELL(file), 0);
  29277. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  29278. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29279. if (buf) {
  29280. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29281. }
  29282. if (file != XBADFILE) {
  29283. XFCLOSE(file);
  29284. file = XBADFILE;
  29285. }
  29286. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29287. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29288. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29289. buf = NULL;
  29290. BIO_free(bio);
  29291. bio = NULL;
  29292. ExpectNotNull(pkey3 = EVP_PKEY_new());
  29293. /* Qt unit test case */
  29294. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29295. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  29296. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29297. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  29298. #else
  29299. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  29300. #endif
  29301. EC_KEY_free(ec_key);
  29302. EVP_PKEY_free(pkey3);
  29303. EVP_PKEY_free(pkey);
  29304. pkey = NULL;
  29305. }
  29306. #endif
  29307. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  29308. defined(WOLFSSL_CERT_GEN))
  29309. {
  29310. #define BIO_PEM_TEST_CHAR 'a'
  29311. EVP_PKEY* pkey2 = NULL;
  29312. unsigned char extra[10];
  29313. int i;
  29314. BIO* pub_bio = NULL;
  29315. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  29316. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29317. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  29318. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29319. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  29320. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  29321. &server_key, (long)sizeof_server_key_der_2048));
  29322. ExpectNull(pkey);
  29323. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  29324. &server_key, (long)sizeof_server_key_der_2048));
  29325. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  29326. NULL), WOLFSSL_FAILURE);
  29327. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29328. NULL), WOLFSSL_FAILURE);
  29329. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  29330. NULL), WOLFSSL_SUCCESS);
  29331. ExpectIntGT(BIO_pending(bio), 0);
  29332. ExpectIntEQ(BIO_pending(bio), 1679);
  29333. /* Check if the pubkey API writes only the public key */
  29334. #ifdef WOLFSSL_KEY_GEN
  29335. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  29336. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  29337. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  29338. ExpectIntGT(BIO_pending(pub_bio), 0);
  29339. /* Previously both the private key and the pubkey calls would write
  29340. * out the private key and the PEM header was the only difference.
  29341. * The public PEM should be significantly shorter than the
  29342. * private key versison. */
  29343. ExpectIntEQ(BIO_pending(pub_bio), 451);
  29344. #endif
  29345. /* test creating new EVP_PKEY with good args */
  29346. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  29347. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  29348. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  29349. /* test of reuse of EVP_PKEY */
  29350. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29351. ExpectIntEQ(BIO_pending(bio), 0);
  29352. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  29353. SSL_SUCCESS);
  29354. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  29355. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  29356. ExpectNotNull(pkey);
  29357. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  29358. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  29359. }
  29360. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  29361. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  29362. for (i = 0; i < 10; i++) {
  29363. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  29364. }
  29365. BIO_free(pub_bio);
  29366. BIO_free(bio);
  29367. bio = NULL;
  29368. EVP_PKEY_free(pkey);
  29369. pkey = NULL;
  29370. EVP_PKEY_free(pkey2);
  29371. }
  29372. #endif
  29373. /* key is DES encrypted */
  29374. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  29375. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  29376. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  29377. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  29378. {
  29379. XFILE f = XBADFILE;
  29380. wc_pem_password_cb* passwd_cb = NULL;
  29381. void* passwd_cb_userdata;
  29382. SSL_CTX* ctx = NULL;
  29383. char passwd[] = "bad password";
  29384. #ifndef WOLFSSL_NO_TLS12
  29385. #ifndef NO_WOLFSSL_SERVER
  29386. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29387. #else
  29388. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29389. #endif
  29390. #else
  29391. #ifndef NO_WOLFSSL_SERVER
  29392. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29393. #else
  29394. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29395. #endif
  29396. #endif
  29397. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29398. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  29399. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  29400. ExpectNull(passwd_cb_userdata =
  29401. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29402. /* fail case with password call back */
  29403. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  29404. (void*)passwd));
  29405. BIO_free(bio);
  29406. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  29407. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29408. (void*)passwd));
  29409. BIO_free(bio);
  29410. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  29411. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  29412. if ((bio == NULL) && (f != XBADFILE)) {
  29413. XFCLOSE(f);
  29414. }
  29415. /* use callback that works */
  29416. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  29417. (void*)"yassl123"));
  29418. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29419. EVP_PKEY_free(pkey);
  29420. pkey = NULL;
  29421. BIO_free(bio);
  29422. bio = NULL;
  29423. SSL_CTX_free(ctx);
  29424. }
  29425. #endif /* !defined(NO_DES3) */
  29426. #endif /* !NO_BIO */
  29427. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29428. {
  29429. unsigned char buf[2048];
  29430. size_t bytes = 0;
  29431. XFILE f = XBADFILE;
  29432. SSL_CTX* ctx = NULL;
  29433. #ifndef WOLFSSL_NO_TLS12
  29434. #ifndef NO_WOLFSSL_SERVER
  29435. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  29436. #else
  29437. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  29438. #endif
  29439. #else
  29440. #ifndef NO_WOLFSSL_SERVER
  29441. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  29442. #else
  29443. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  29444. #endif
  29445. #endif
  29446. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  29447. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  29448. if (f != XBADFILE)
  29449. XFCLOSE(f);
  29450. server_key = buf;
  29451. pkey = NULL;
  29452. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  29453. ExpectNull(pkey);
  29454. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  29455. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  29456. EVP_PKEY_free(pkey);
  29457. pkey = NULL;
  29458. SSL_CTX_free(ctx);
  29459. server_key = NULL;
  29460. }
  29461. #endif
  29462. #ifndef NO_BIO
  29463. (void)bio;
  29464. #endif
  29465. (void)pkey;
  29466. (void)server_key;
  29467. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  29468. return EXPECT_RESULT();
  29469. }
  29470. static int test_wolfSSL_PEM_file_RSAKey(void)
  29471. {
  29472. EXPECT_DECLS;
  29473. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29474. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29475. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29476. RSA* rsa = NULL;
  29477. XFILE fp = XBADFILE;
  29478. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  29479. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  29480. if (fp != XBADFILE)
  29481. XFCLOSE(fp);
  29482. ExpectIntEQ(RSA_size(rsa), 256);
  29483. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  29484. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  29485. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  29486. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  29487. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29488. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  29489. RSA_free(rsa);
  29490. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29491. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29492. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29493. return EXPECT_RESULT();
  29494. }
  29495. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  29496. {
  29497. EXPECT_DECLS;
  29498. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  29499. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  29500. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  29501. RSA* rsa = NULL;
  29502. XFILE f = NULL;
  29503. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  29504. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29505. ExpectIntEQ(RSA_size(rsa), 256);
  29506. if (f != XBADFILE) {
  29507. XFCLOSE(f);
  29508. f = XBADFILE;
  29509. }
  29510. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  29511. NULL), WOLFSSL_FAILURE);
  29512. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  29513. NULL), WOLFSSL_FAILURE);
  29514. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  29515. WOLFSSL_SUCCESS);
  29516. RSA_free(rsa);
  29517. #ifdef HAVE_ECC
  29518. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  29519. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  29520. if (f != XBADFILE)
  29521. XFCLOSE(f);
  29522. #endif /* HAVE_ECC */
  29523. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29524. return EXPECT_RESULT();
  29525. }
  29526. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  29527. {
  29528. EXPECT_DECLS;
  29529. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29530. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29531. XFILE file = XBADFILE;
  29532. const char* fname = "./certs/client-keyPub.pem";
  29533. RSA *rsa = NULL;
  29534. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  29535. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29536. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  29537. ExpectIntEQ(RSA_size(rsa), 256);
  29538. RSA_free(rsa);
  29539. if (file != XBADFILE)
  29540. XFCLOSE(file);
  29541. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29542. return EXPECT_RESULT();
  29543. }
  29544. #ifndef NO_BIO
  29545. static int test_wolfSSL_PEM_bio_RSAKey(void)
  29546. {
  29547. EXPECT_DECLS;
  29548. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29549. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  29550. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  29551. RSA* rsa = NULL;
  29552. BIO* bio = NULL;
  29553. /* PrivateKey */
  29554. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29555. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  29556. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  29557. ExpectNotNull(rsa);
  29558. ExpectIntEQ(RSA_size(rsa), 256);
  29559. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  29560. NULL), WOLFSSL_FAILURE);
  29561. BIO_free(bio);
  29562. bio = NULL;
  29563. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29564. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  29565. NULL), WOLFSSL_SUCCESS);
  29566. BIO_free(bio);
  29567. bio = NULL;
  29568. RSA_free(rsa);
  29569. rsa = NULL;
  29570. /* PUBKEY */
  29571. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  29572. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29573. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29574. ExpectIntEQ(RSA_size(rsa), 256);
  29575. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29576. BIO_free(bio);
  29577. bio = NULL;
  29578. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29579. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  29580. BIO_free(bio);
  29581. bio = NULL;
  29582. RSA_free(rsa);
  29583. rsa = NULL;
  29584. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  29585. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  29586. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29587. BIO_free(bio);
  29588. bio = NULL;
  29589. RSA_free(rsa);
  29590. rsa = NULL;
  29591. #ifdef HAVE_ECC
  29592. /* ensure that non-rsa keys do not work */
  29593. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29594. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29595. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  29596. BIO_free(bio);
  29597. bio = NULL;
  29598. RSA_free(rsa);
  29599. rsa = NULL;
  29600. #endif /* HAVE_ECC */
  29601. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29602. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  29603. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  29604. return EXPECT_RESULT();
  29605. }
  29606. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  29607. {
  29608. EXPECT_DECLS;
  29609. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29610. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29611. RSA* rsa = NULL;
  29612. RSA* rsa_dup = NULL;
  29613. BIO* bio = NULL;
  29614. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  29615. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29616. ExpectIntEQ(RSA_size(rsa), 256);
  29617. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  29618. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  29619. /* Test duplicating empty key. */
  29620. ExpectNotNull(rsa_dup = RSA_new());
  29621. ExpectNull(RSAPublicKey_dup(rsa_dup));
  29622. RSA_free(rsa_dup);
  29623. rsa_dup = NULL;
  29624. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  29625. ExpectPtrNE(rsa_dup, rsa);
  29626. #endif
  29627. /* test if valgrind complains about unreleased memory */
  29628. RSA_up_ref(rsa);
  29629. RSA_free(rsa);
  29630. BIO_free(bio);
  29631. bio = NULL;
  29632. RSA_free(rsa);
  29633. rsa = NULL;
  29634. RSA_free(rsa_dup);
  29635. rsa_dup = NULL;
  29636. #ifdef HAVE_ECC
  29637. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  29638. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  29639. BIO_free(bio);
  29640. #endif /* HAVE_ECC */
  29641. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29642. return EXPECT_RESULT();
  29643. }
  29644. static int test_wolfSSL_PEM_bio_DSAKey(void)
  29645. {
  29646. EXPECT_DECLS;
  29647. #ifndef HAVE_SELFTEST
  29648. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  29649. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  29650. DSA* dsa = NULL;
  29651. BIO* bio = NULL;
  29652. /* PrivateKey */
  29653. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  29654. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  29655. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29656. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29657. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29658. NULL), WOLFSSL_FAILURE);
  29659. BIO_free(bio);
  29660. bio = NULL;
  29661. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29662. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  29663. NULL), WOLFSSL_SUCCESS);
  29664. BIO_free(bio);
  29665. bio = NULL;
  29666. DSA_free(dsa);
  29667. dsa = NULL;
  29668. /* PUBKEY */
  29669. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  29670. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  29671. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29672. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  29673. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29674. BIO_free(bio);
  29675. bio = NULL;
  29676. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29677. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  29678. BIO_free(bio);
  29679. bio = NULL;
  29680. DSA_free(dsa);
  29681. dsa = NULL;
  29682. #ifdef HAVE_ECC
  29683. /* ensure that non-dsa keys do not work */
  29684. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  29685. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  29686. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  29687. BIO_free(bio);
  29688. bio = NULL;
  29689. DSA_free(dsa);
  29690. dsa = NULL;
  29691. #endif /* HAVE_ECC */
  29692. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  29693. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  29694. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  29695. #endif /* HAVE_SELFTEST */
  29696. return EXPECT_RESULT();
  29697. }
  29698. static int test_wolfSSL_PEM_bio_ECKey(void)
  29699. {
  29700. EXPECT_DECLS;
  29701. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  29702. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  29703. EC_KEY* ec = NULL;
  29704. EC_KEY* ec2;
  29705. BIO* bio = NULL;
  29706. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29707. unsigned char* pem = NULL;
  29708. int pLen;
  29709. #endif
  29710. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  29711. "MAA=\n"
  29712. "-----END PUBLIC KEY-----";
  29713. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  29714. "MAA=\n"
  29715. "-----END EC PRIVATE KEY-----";
  29716. /* PrivateKey */
  29717. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29718. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  29719. ec2 = NULL;
  29720. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  29721. ExpectIntEQ(ec == ec2, 1);
  29722. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29723. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  29724. NULL), WOLFSSL_FAILURE);
  29725. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  29726. NULL), WOLFSSL_FAILURE);
  29727. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  29728. WOLFSSL_FAILURE);
  29729. BIO_free(bio);
  29730. bio = NULL;
  29731. /* Public key data - fail. */
  29732. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29733. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29734. BIO_free(bio);
  29735. bio = NULL;
  29736. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29737. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  29738. NULL), WOLFSSL_SUCCESS);
  29739. BIO_free(bio);
  29740. bio = NULL;
  29741. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  29742. NULL),WOLFSSL_FAILURE);
  29743. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  29744. WOLFSSL_FAILURE);
  29745. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  29746. WOLFSSL_FAILURE);
  29747. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  29748. WOLFSSL_SUCCESS);
  29749. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29750. NULL), 0);
  29751. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  29752. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29753. NULL), 0);
  29754. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29755. NULL), 0);
  29756. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  29757. &pLen), 0);
  29758. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  29759. &pLen), 0);
  29760. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  29761. &pLen), 0);
  29762. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29763. NULL), 0);
  29764. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  29765. &pLen), 1);
  29766. ExpectIntGT(pLen, 0);
  29767. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29768. #endif
  29769. EC_KEY_free(ec);
  29770. ec = NULL;
  29771. /* PUBKEY */
  29772. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  29773. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  29774. ec2 = NULL;
  29775. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  29776. ExpectIntEQ(ec == ec2, 1);
  29777. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  29778. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29779. BIO_free(bio);
  29780. bio = NULL;
  29781. /* Test 0x30, 0x00 fails. */
  29782. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  29783. sizeof(ec_key_bad_1)));
  29784. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29785. BIO_free(bio);
  29786. bio = NULL;
  29787. /* Private key data - fail. */
  29788. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  29789. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  29790. BIO_free(bio);
  29791. bio = NULL;
  29792. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  29793. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  29794. BIO_free(bio);
  29795. bio = NULL;
  29796. /* Same test as above, but with a file pointer rather than a BIO. */
  29797. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  29798. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  29799. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  29800. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  29801. EC_KEY_free(ec);
  29802. ec = NULL;
  29803. #ifndef NO_RSA
  29804. /* ensure that non-ec keys do not work */
  29805. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  29806. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  29807. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  29808. BIO_free(bio);
  29809. bio = NULL;
  29810. EC_KEY_free(ec);
  29811. ec = NULL;
  29812. #endif /* !NO_RSA */
  29813. /* Test 0x30, 0x00 fails. */
  29814. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  29815. sizeof(ec_priv_key_bad_1)));
  29816. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  29817. BIO_free(bio);
  29818. bio = NULL;
  29819. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  29820. return EXPECT_RESULT();
  29821. }
  29822. static int test_wolfSSL_PEM_PUBKEY(void)
  29823. {
  29824. EXPECT_DECLS;
  29825. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  29826. BIO* bio = NULL;
  29827. EVP_PKEY* pkey = NULL;
  29828. /* test creating new EVP_PKEY with bad arg */
  29829. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  29830. /* test loading ECC key using BIO */
  29831. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  29832. {
  29833. XFILE file = XBADFILE;
  29834. const char* fname = "./certs/ecc-client-keyPub.pem";
  29835. size_t sz;
  29836. byte* buf = NULL;
  29837. EVP_PKEY* pkey2 = NULL;
  29838. EC_KEY* ec_key = NULL;
  29839. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  29840. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  29841. ExpectIntGT(sz = XFTELL(file), 0);
  29842. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  29843. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  29844. if (buf != NULL) {
  29845. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  29846. }
  29847. if (file != XBADFILE) {
  29848. XFCLOSE(file);
  29849. }
  29850. /* Test using BIO new mem and loading PEM private key */
  29851. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  29852. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  29853. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  29854. BIO_free(bio);
  29855. bio = NULL;
  29856. /* Qt unit test case*/
  29857. ExpectNotNull(pkey2 = EVP_PKEY_new());
  29858. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  29859. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  29860. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  29861. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  29862. #else
  29863. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  29864. #endif
  29865. EC_KEY_free(ec_key);
  29866. EVP_PKEY_free(pkey2);
  29867. EVP_PKEY_free(pkey);
  29868. pkey = NULL;
  29869. }
  29870. #endif
  29871. (void)bio;
  29872. (void)pkey;
  29873. #endif
  29874. return EXPECT_RESULT();
  29875. }
  29876. #endif /* !NO_BIO */
  29877. static int test_DSA_do_sign_verify(void)
  29878. {
  29879. EXPECT_DECLS;
  29880. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  29881. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29882. !defined(NO_DSA)
  29883. unsigned char digest[WC_SHA_DIGEST_SIZE];
  29884. DSA_SIG* sig = NULL;
  29885. DSA* dsa = NULL;
  29886. word32 bytes;
  29887. byte sigBin[DSA_SIG_SIZE];
  29888. int dsacheck;
  29889. #ifdef USE_CERT_BUFFERS_1024
  29890. byte tmp[ONEK_BUF];
  29891. XMEMSET(tmp, 0, sizeof(tmp));
  29892. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  29893. bytes = sizeof_dsa_key_der_1024;
  29894. #elif defined(USE_CERT_BUFFERS_2048)
  29895. byte tmp[TWOK_BUF];
  29896. XMEMSET(tmp, 0, sizeof(tmp));
  29897. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  29898. bytes = sizeof_dsa_key_der_2048;
  29899. #else
  29900. byte tmp[TWOK_BUF];
  29901. XFILE fp = XBADFILE;
  29902. XMEMSET(tmp, 0, sizeof(tmp));
  29903. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  29904. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  29905. if (fp != XBADFILE)
  29906. XFCLOSE(fp);
  29907. #endif /* END USE_CERT_BUFFERS_1024 */
  29908. XMEMSET(digest, 202, sizeof(digest));
  29909. ExpectNotNull(dsa = DSA_new());
  29910. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  29911. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  29912. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  29913. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  29914. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  29915. DSA_SIG_free(sig);
  29916. DSA_free(dsa);
  29917. #endif
  29918. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  29919. return EXPECT_RESULT();
  29920. }
  29921. static int test_wolfSSL_tmp_dh(void)
  29922. {
  29923. EXPECT_DECLS;
  29924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29925. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  29926. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29927. byte buff[6000];
  29928. char file[] = "./certs/dsaparams.pem";
  29929. XFILE f = XBADFILE;
  29930. int bytes = 0;
  29931. DSA* dsa = NULL;
  29932. DH* dh = NULL;
  29933. #if defined(WOLFSSL_DH_EXTRA) && \
  29934. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29935. DH* dh2 = NULL;
  29936. #endif
  29937. BIO* bio = NULL;
  29938. SSL* ssl = NULL;
  29939. SSL_CTX* ctx = NULL;
  29940. #ifndef NO_WOLFSSL_SERVER
  29941. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29942. #else
  29943. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29944. #endif
  29945. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29946. WOLFSSL_FILETYPE_PEM));
  29947. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29948. WOLFSSL_FILETYPE_PEM));
  29949. ExpectNotNull(ssl = SSL_new(ctx));
  29950. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29951. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  29952. if (f != XBADFILE)
  29953. XFCLOSE(f);
  29954. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29955. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  29956. ExpectNotNull(dsa);
  29957. dh = wolfSSL_DSA_dup_DH(dsa);
  29958. ExpectNotNull(dh);
  29959. #if defined(WOLFSSL_DH_EXTRA) && \
  29960. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29961. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  29962. #endif
  29963. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  29964. #ifndef NO_WOLFSSL_SERVER
  29965. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  29966. #else
  29967. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  29968. #endif
  29969. BIO_free(bio);
  29970. DSA_free(dsa);
  29971. DH_free(dh);
  29972. #if defined(WOLFSSL_DH_EXTRA) && \
  29973. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  29974. DH_free(dh2);
  29975. #endif
  29976. SSL_free(ssl);
  29977. SSL_CTX_free(ctx);
  29978. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29979. #endif
  29980. return EXPECT_RESULT();
  29981. }
  29982. static int test_wolfSSL_ctrl(void)
  29983. {
  29984. EXPECT_DECLS;
  29985. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  29986. byte buff[6000];
  29987. BIO* bio = NULL;
  29988. int bytes;
  29989. BUF_MEM* ptr = NULL;
  29990. XMEMSET(buff, 0, sizeof(buff));
  29991. bytes = sizeof(buff);
  29992. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  29993. ExpectNotNull(BIO_s_socket());
  29994. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  29995. /* needs tested after stubs filled out @TODO
  29996. SSL_ctrl
  29997. SSL_CTX_ctrl
  29998. */
  29999. BIO_free(bio);
  30000. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  30001. return EXPECT_RESULT();
  30002. }
  30003. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  30004. {
  30005. EXPECT_DECLS;
  30006. #ifdef OPENSSL_EXTRA
  30007. static const unsigned char pw[] = "password";
  30008. static const int pwSz = sizeof(pw) - 1;
  30009. size_t checkPwSz = 0;
  30010. const unsigned char* checkPw = NULL;
  30011. WOLFSSL_EVP_PKEY* key = NULL;
  30012. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  30013. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  30014. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30015. pwSz));
  30016. if (key != NULL) {
  30017. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  30018. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  30019. ExpectIntEQ(key->pkey_sz, pwSz);
  30020. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  30021. }
  30022. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  30023. ExpectIntEQ((int)checkPwSz, pwSz);
  30024. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  30025. wolfSSL_EVP_PKEY_free(key);
  30026. key = NULL;
  30027. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  30028. 0));
  30029. ExpectIntEQ(key->pkey_sz, 0);
  30030. if (EXPECT_SUCCESS()) {
  30031. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30032. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30033. }
  30034. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30035. ExpectIntEQ((int)checkPwSz, 0);
  30036. wolfSSL_EVP_PKEY_free(key);
  30037. key = NULL;
  30038. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  30039. 0));
  30040. ExpectIntEQ(key->pkey_sz, 0);
  30041. if (EXPECT_SUCCESS()) {
  30042. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  30043. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  30044. }
  30045. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  30046. ExpectIntEQ((int)checkPwSz, 0);
  30047. wolfSSL_EVP_PKEY_free(key);
  30048. key = NULL;
  30049. #endif /* OPENSSL_EXTRA */
  30050. return EXPECT_RESULT();
  30051. }
  30052. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  30053. {
  30054. EXPECT_DECLS;
  30055. #ifdef OPENSSL_EXTRA
  30056. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  30057. const char *priv = "ABCDEFGHIJKLMNOP";
  30058. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  30059. WOLFSSL_EVP_PKEY* key = NULL;
  30060. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30061. NULL, NULL, AES_128_KEY_SIZE, cipher));
  30062. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30063. NULL, (const unsigned char *)priv, 0, cipher));
  30064. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30065. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  30066. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  30067. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  30068. wolfSSL_EVP_PKEY_free(key);
  30069. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  30070. #endif /* OPENSSL_EXTRA */
  30071. return EXPECT_RESULT();
  30072. }
  30073. static int test_wolfSSL_EVP_Digest(void)
  30074. {
  30075. EXPECT_DECLS;
  30076. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30077. const char* in = "abc";
  30078. int inLen = (int)XSTRLEN(in);
  30079. byte out[WC_SHA256_DIGEST_SIZE];
  30080. unsigned int outLen;
  30081. const char* expOut =
  30082. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  30083. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  30084. "\x15\xAD";
  30085. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  30086. "SHA256", NULL), 1);
  30087. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  30088. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  30089. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  30090. return EXPECT_RESULT();
  30091. }
  30092. static int test_wolfSSL_EVP_Digest_all(void)
  30093. {
  30094. EXPECT_DECLS;
  30095. #ifdef OPENSSL_EXTRA
  30096. const char* digests[] = {
  30097. #ifndef NO_MD5
  30098. "MD5",
  30099. #endif
  30100. #ifndef NO_SHA
  30101. "SHA",
  30102. #endif
  30103. #ifdef WOLFSSL_SHA224
  30104. "SHA224",
  30105. #endif
  30106. #ifndef NO_SHA256
  30107. "SHA256",
  30108. #endif
  30109. #ifdef WOLFSSL_SHA384
  30110. "SHA384",
  30111. #endif
  30112. #ifdef WOLFSSL_SHA512
  30113. "SHA512",
  30114. #endif
  30115. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  30116. "SHA512_224",
  30117. #endif
  30118. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  30119. "SHA512_256",
  30120. #endif
  30121. #ifdef WOLFSSL_SHA3
  30122. #ifndef WOLFSSL_NOSHA3_224
  30123. "SHA3_224",
  30124. #endif
  30125. #ifndef WOLFSSL_NOSHA3_256
  30126. "SHA3_256",
  30127. #endif
  30128. "SHA3_384",
  30129. #ifndef WOLFSSL_NOSHA3_512
  30130. "SHA3_512",
  30131. #endif
  30132. #endif /* WOLFSSL_SHA3 */
  30133. NULL
  30134. };
  30135. const char** d;
  30136. const unsigned char in[] = "abc";
  30137. int inLen = XSTR_SIZEOF(in);
  30138. byte out[WC_MAX_DIGEST_SIZE];
  30139. unsigned int outLen;
  30140. for (d = digests; *d != NULL; d++) {
  30141. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  30142. ExpectIntGT(outLen, 0);
  30143. ExpectIntEQ(EVP_MD_size(*d), outLen);
  30144. }
  30145. #endif
  30146. return EXPECT_RESULT();
  30147. }
  30148. static int test_wolfSSL_EVP_MD_size(void)
  30149. {
  30150. EXPECT_DECLS;
  30151. #ifdef OPENSSL_EXTRA
  30152. WOLFSSL_EVP_MD_CTX mdCtx;
  30153. #ifdef WOLFSSL_SHA3
  30154. #ifndef WOLFSSL_NOSHA3_224
  30155. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30156. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  30157. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  30158. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  30159. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30160. #endif
  30161. #ifndef WOLFSSL_NOSHA3_256
  30162. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30163. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  30164. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  30165. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  30166. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30167. #endif
  30168. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30169. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  30170. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  30171. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  30172. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30173. #ifndef WOLFSSL_NOSHA3_512
  30174. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30175. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  30176. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  30177. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  30178. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30179. #endif
  30180. #endif /* WOLFSSL_SHA3 */
  30181. #ifndef NO_SHA256
  30182. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30183. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  30184. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30185. WC_SHA256_DIGEST_SIZE);
  30186. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30187. WC_SHA256_BLOCK_SIZE);
  30188. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  30189. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  30190. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30191. #endif
  30192. #ifndef NO_MD5
  30193. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30194. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  30195. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30196. WC_MD5_DIGEST_SIZE);
  30197. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30198. WC_MD5_BLOCK_SIZE);
  30199. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  30200. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  30201. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30202. #endif
  30203. #ifdef WOLFSSL_SHA224
  30204. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30205. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  30206. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30207. WC_SHA224_DIGEST_SIZE);
  30208. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30209. WC_SHA224_BLOCK_SIZE);
  30210. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  30211. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  30212. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30213. #endif
  30214. #ifdef WOLFSSL_SHA384
  30215. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30216. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  30217. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30218. WC_SHA384_DIGEST_SIZE);
  30219. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30220. WC_SHA384_BLOCK_SIZE);
  30221. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  30222. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  30223. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30224. #endif
  30225. #ifdef WOLFSSL_SHA512
  30226. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30227. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  30228. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30229. WC_SHA512_DIGEST_SIZE);
  30230. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30231. WC_SHA512_BLOCK_SIZE);
  30232. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  30233. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  30234. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30235. #endif
  30236. #ifndef NO_SHA
  30237. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30238. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  30239. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30240. WC_SHA_DIGEST_SIZE);
  30241. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30242. WC_SHA_BLOCK_SIZE);
  30243. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30244. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30245. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30246. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30247. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  30248. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30249. WC_SHA_DIGEST_SIZE);
  30250. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30251. WC_SHA_BLOCK_SIZE);
  30252. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  30253. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  30254. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30255. #endif
  30256. /* error case */
  30257. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30258. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  30259. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  30260. BAD_FUNC_ARG);
  30261. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  30262. /* Cleanup is valid on uninit'ed struct */
  30263. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30264. #endif /* OPENSSL_EXTRA */
  30265. return EXPECT_RESULT();
  30266. }
  30267. static int test_wolfSSL_EVP_MD_pkey_type(void)
  30268. {
  30269. EXPECT_DECLS;
  30270. #ifdef OPENSSL_EXTRA
  30271. const WOLFSSL_EVP_MD* md;
  30272. #ifndef NO_MD5
  30273. ExpectNotNull(md = EVP_md5());
  30274. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  30275. #endif
  30276. #ifndef NO_SHA
  30277. ExpectNotNull(md = EVP_sha1());
  30278. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  30279. #endif
  30280. #ifdef WOLFSSL_SHA224
  30281. ExpectNotNull(md = EVP_sha224());
  30282. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  30283. #endif
  30284. ExpectNotNull(md = EVP_sha256());
  30285. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  30286. #ifdef WOLFSSL_SHA384
  30287. ExpectNotNull(md = EVP_sha384());
  30288. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  30289. #endif
  30290. #ifdef WOLFSSL_SHA512
  30291. ExpectNotNull(md = EVP_sha512());
  30292. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  30293. #endif
  30294. #endif
  30295. return EXPECT_RESULT();
  30296. }
  30297. #ifdef OPENSSL_EXTRA
  30298. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  30299. size_t testKeySz, const char* testData, size_t testDataSz,
  30300. const byte* testResult, size_t testResultSz)
  30301. {
  30302. EXPECT_DECLS;
  30303. unsigned char check[WC_MAX_DIGEST_SIZE];
  30304. size_t checkSz = -1;
  30305. WOLFSSL_EVP_PKEY* key = NULL;
  30306. WOLFSSL_EVP_MD_CTX mdCtx;
  30307. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  30308. testKey, (int)testKeySz));
  30309. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30310. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30311. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30312. (unsigned int)testDataSz), 1);
  30313. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30314. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30315. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30316. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30317. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30318. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30319. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30320. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30321. (unsigned int)testDataSz), 1);
  30322. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30323. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30324. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30325. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  30326. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30327. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30328. ExpectIntEQ((int)checkSz, (int)testResultSz);
  30329. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30330. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30331. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30332. (unsigned int)testDataSz - 4), 1);
  30333. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30334. ExpectIntEQ((int)checkSz,(int)testResultSz);
  30335. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  30336. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30337. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  30338. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30339. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30340. (unsigned int)testDataSz - 4), 1);
  30341. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  30342. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  30343. wolfSSL_EVP_PKEY_free(key);
  30344. return EXPECT_RESULT();
  30345. }
  30346. #endif
  30347. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  30348. {
  30349. EXPECT_DECLS;
  30350. #ifdef OPENSSL_EXTRA
  30351. static const unsigned char testKey[] =
  30352. {
  30353. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30354. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  30355. 0x0b, 0x0b, 0x0b, 0x0b
  30356. };
  30357. static const char testData[] = "Hi There";
  30358. #ifdef WOLFSSL_SHA224
  30359. static const unsigned char testResultSha224[] =
  30360. {
  30361. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  30362. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  30363. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  30364. 0x53, 0x68, 0x4b, 0x22
  30365. };
  30366. #endif
  30367. #ifndef NO_SHA256
  30368. static const unsigned char testResultSha256[] =
  30369. {
  30370. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  30371. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  30372. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  30373. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  30374. };
  30375. #endif
  30376. #ifdef WOLFSSL_SHA384
  30377. static const unsigned char testResultSha384[] =
  30378. {
  30379. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  30380. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  30381. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  30382. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  30383. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  30384. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  30385. };
  30386. #endif
  30387. #ifdef WOLFSSL_SHA512
  30388. static const unsigned char testResultSha512[] =
  30389. {
  30390. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  30391. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  30392. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  30393. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  30394. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  30395. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  30396. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  30397. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  30398. };
  30399. #endif
  30400. #ifdef WOLFSSL_SHA3
  30401. #ifndef WOLFSSL_NOSHA3_224
  30402. static const unsigned char testResultSha3_224[] =
  30403. {
  30404. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  30405. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  30406. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  30407. 0xf3, 0xc8, 0x60, 0xf7
  30408. };
  30409. #endif
  30410. #ifndef WOLFSSL_NOSHA3_256
  30411. static const unsigned char testResultSha3_256[] =
  30412. {
  30413. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  30414. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  30415. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  30416. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  30417. };
  30418. #endif
  30419. #ifndef WOLFSSL_NOSHA3_384
  30420. static const unsigned char testResultSha3_384[] =
  30421. {
  30422. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  30423. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  30424. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  30425. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  30426. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  30427. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  30428. };
  30429. #endif
  30430. #ifndef WOLFSSL_NOSHA3_512
  30431. static const unsigned char testResultSha3_512[] =
  30432. {
  30433. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  30434. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  30435. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  30436. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  30437. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  30438. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  30439. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  30440. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  30441. };
  30442. #endif
  30443. #endif
  30444. #ifndef NO_SHA256
  30445. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  30446. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  30447. sizeof(testResultSha256)), TEST_SUCCESS);
  30448. #endif
  30449. #ifdef WOLFSSL_SHA224
  30450. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  30451. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  30452. sizeof(testResultSha224)), TEST_SUCCESS);
  30453. #endif
  30454. #ifdef WOLFSSL_SHA384
  30455. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  30456. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  30457. sizeof(testResultSha384)), TEST_SUCCESS);
  30458. #endif
  30459. #ifdef WOLFSSL_SHA512
  30460. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  30461. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  30462. sizeof(testResultSha512)), TEST_SUCCESS);
  30463. #endif
  30464. #ifdef WOLFSSL_SHA3
  30465. #ifndef WOLFSSL_NOSHA3_224
  30466. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  30467. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  30468. sizeof(testResultSha3_224)), TEST_SUCCESS);
  30469. #endif
  30470. #ifndef WOLFSSL_NOSHA3_256
  30471. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  30472. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  30473. sizeof(testResultSha3_256)), TEST_SUCCESS);
  30474. #endif
  30475. #ifndef WOLFSSL_NOSHA3_384
  30476. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  30477. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  30478. sizeof(testResultSha3_384)), TEST_SUCCESS);
  30479. #endif
  30480. #ifndef WOLFSSL_NOSHA3_512
  30481. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  30482. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  30483. sizeof(testResultSha3_512)), TEST_SUCCESS);
  30484. #endif
  30485. #endif
  30486. #endif /* OPENSSL_EXTRA */
  30487. return EXPECT_RESULT();
  30488. }
  30489. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  30490. {
  30491. EXPECT_DECLS;
  30492. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  30493. defined(USE_CERT_BUFFERS_2048)
  30494. WOLFSSL_EVP_PKEY* privKey = NULL;
  30495. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30496. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  30497. const char testData[] = "Hi There";
  30498. WOLFSSL_EVP_MD_CTX mdCtx;
  30499. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  30500. int ret;
  30501. size_t checkSz = -1;
  30502. int sz = 2048 / 8;
  30503. const unsigned char* cp;
  30504. const unsigned char* p;
  30505. unsigned char check[2048/8];
  30506. size_t i;
  30507. int paddings[] = {
  30508. RSA_PKCS1_PADDING,
  30509. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  30510. RSA_PKCS1_PSS_PADDING,
  30511. #endif
  30512. };
  30513. cp = client_key_der_2048;
  30514. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  30515. sizeof_client_key_der_2048)));
  30516. p = client_keypub_der_2048;
  30517. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30518. sizeof_client_keypub_der_2048)));
  30519. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30520. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  30521. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30522. NULL, privKey), 1);
  30523. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30524. (unsigned int)XSTRLEN(testData)), 1);
  30525. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30526. ExpectIntEQ((int)checkSz, sz);
  30527. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30528. ExpectIntEQ((int)checkSz,sz);
  30529. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30530. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  30531. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  30532. ExpectIntEQ(ret, 1);
  30533. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30534. ExpectIntEQ(ret, 1);
  30535. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30536. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30537. NULL, pubKey), 1);
  30538. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30539. (unsigned int)XSTRLEN(testData)),
  30540. 1);
  30541. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30542. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30543. ExpectIntEQ(ret, 1);
  30544. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30545. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30546. NULL, privKey), 1);
  30547. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30548. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30549. ExpectIntEQ((int)checkSz, sz);
  30550. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30551. ExpectIntEQ((int)checkSz, sz);
  30552. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30553. (unsigned int)XSTRLEN(testData) - 4), 1);
  30554. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30555. ExpectIntEQ((int)checkSz, sz);
  30556. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30557. ExpectIntEQ(ret, 1);
  30558. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30559. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30560. NULL, pubKey), 1);
  30561. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30562. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30563. (unsigned int)XSTRLEN(testData) - 4),
  30564. 1);
  30565. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30566. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30567. ExpectIntEQ(ret, 1);
  30568. /* Check all signing padding types */
  30569. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  30570. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30571. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  30572. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  30573. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30574. paddings[i]), 1);
  30575. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30576. (unsigned int)XSTRLEN(testData)), 1);
  30577. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30578. ExpectIntEQ((int)checkSz, sz);
  30579. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30580. ExpectIntEQ((int)checkSz,sz);
  30581. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30582. ExpectIntEQ(ret, 1);
  30583. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30584. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  30585. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  30586. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  30587. paddings[i]), 1);
  30588. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30589. (unsigned int)XSTRLEN(testData)), 1);
  30590. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30591. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30592. ExpectIntEQ(ret, 1);
  30593. }
  30594. wolfSSL_EVP_PKEY_free(pubKey);
  30595. wolfSSL_EVP_PKEY_free(privKey);
  30596. #endif
  30597. return EXPECT_RESULT();
  30598. }
  30599. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  30600. {
  30601. EXPECT_DECLS;
  30602. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30603. WOLFSSL_EVP_PKEY* privKey = NULL;
  30604. WOLFSSL_EVP_PKEY* pubKey = NULL;
  30605. const char testData[] = "Hi There";
  30606. WOLFSSL_EVP_MD_CTX mdCtx;
  30607. int ret;
  30608. size_t checkSz = -1;
  30609. const unsigned char* cp;
  30610. const unsigned char* p;
  30611. unsigned char check[2048/8];
  30612. cp = ecc_clikey_der_256;
  30613. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  30614. sizeof_ecc_clikey_der_256));
  30615. p = ecc_clikeypub_der_256;
  30616. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  30617. sizeof_ecc_clikeypub_der_256)));
  30618. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30619. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30620. NULL, privKey), 1);
  30621. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  30622. (unsigned int)XSTRLEN(testData)), 1);
  30623. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30624. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30625. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30626. ExpectIntEQ(ret, 1);
  30627. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30628. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30629. NULL, pubKey), 1);
  30630. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  30631. (unsigned int)XSTRLEN(testData)),
  30632. 1);
  30633. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30634. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30635. ExpectIntEQ(ret, 1);
  30636. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30637. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30638. NULL, privKey), 1);
  30639. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  30640. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  30641. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30642. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  30643. (unsigned int)XSTRLEN(testData) - 4), 1);
  30644. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  30645. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30646. ExpectIntEQ(ret, 1);
  30647. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  30648. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  30649. NULL, pubKey), 1);
  30650. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  30651. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  30652. (unsigned int)XSTRLEN(testData) - 4),
  30653. 1);
  30654. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  30655. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  30656. ExpectIntEQ(ret, 1);
  30657. wolfSSL_EVP_PKEY_free(pubKey);
  30658. wolfSSL_EVP_PKEY_free(privKey);
  30659. #endif
  30660. return EXPECT_RESULT();
  30661. }
  30662. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  30663. {
  30664. EXPECT_DECLS;
  30665. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30666. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  30667. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30668. char caFile[] = "./certs/client-ca.pem";
  30669. char clientFile[] = "./certs/client-cert.pem";
  30670. SSL_CTX* ctx = NULL;
  30671. X509* x509 = NULL;
  30672. BIO *bio = NULL;
  30673. X509 *cert = NULL;
  30674. X509 *ca = NULL;
  30675. STACK_OF(X509) *chain = NULL;
  30676. STACK_OF(X509) *chain2 = NULL;
  30677. #ifndef NO_WOLFSSL_SERVER
  30678. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30679. #else
  30680. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30681. #endif
  30682. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  30683. WOLFSSL_FILETYPE_PEM));
  30684. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  30685. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  30686. WOLFSSL_FILETYPE_PEM));
  30687. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  30688. /* additional test of getting EVP_PKEY key size from X509
  30689. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  30690. * allowed with user RSA */
  30691. {
  30692. EVP_PKEY* pkey = NULL;
  30693. #if defined(HAVE_ECC)
  30694. X509* ecX509 = NULL;
  30695. #endif /* HAVE_ECC */
  30696. ExpectNotNull(pkey = X509_get_pubkey(x509));
  30697. /* current RSA key is 2048 bit (256 bytes) */
  30698. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  30699. EVP_PKEY_free(pkey);
  30700. pkey = NULL;
  30701. #if defined(HAVE_ECC)
  30702. #if defined(USE_CERT_BUFFERS_256)
  30703. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  30704. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  30705. SSL_FILETYPE_ASN1));
  30706. #else
  30707. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  30708. cliEccCertFile, SSL_FILETYPE_PEM));
  30709. #endif
  30710. pkey = X509_get_pubkey(ecX509);
  30711. ExpectNotNull(pkey);
  30712. /* current ECC key is 256 bit (32 bytes) */
  30713. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  30714. X509_free(ecX509);
  30715. ecX509 = NULL;
  30716. EVP_PKEY_free(pkey);
  30717. pkey = NULL;
  30718. #endif /* HAVE_ECC */
  30719. }
  30720. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  30721. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  30722. if (EXPECT_SUCCESS()) {
  30723. x509 = NULL;
  30724. }
  30725. #ifdef WOLFSSL_ENCRYPTED_KEYS
  30726. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  30727. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  30728. #endif
  30729. SSL_CTX_free(ctx);
  30730. ctx = NULL;
  30731. #ifndef NO_WOLFSSL_SERVER
  30732. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30733. #else
  30734. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30735. #endif
  30736. /* Test haproxy use case */
  30737. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  30738. /* Read Certificate */
  30739. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  30740. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  30741. ExpectNotNull(chain = sk_X509_new_null());
  30742. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  30743. if (EXPECT_SUCCESS()) {
  30744. ca = NULL;
  30745. }
  30746. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  30747. ExpectNotNull(ca = sk_X509_shift(chain2));
  30748. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  30749. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  30750. if (EXPECT_SUCCESS()) {
  30751. ca = NULL;
  30752. }
  30753. BIO_free(bio);
  30754. X509_free(cert);
  30755. X509_free(ca);
  30756. X509_free(x509);
  30757. sk_X509_pop_free(chain, X509_free);
  30758. sk_X509_pop_free(chain2, X509_free);
  30759. SSL_CTX_free(ctx);
  30760. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30761. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30762. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  30763. return EXPECT_RESULT();
  30764. }
  30765. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30766. static int test_wolfSSL_ERR_peek_last_error_line(void)
  30767. {
  30768. EXPECT_DECLS;
  30769. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30770. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30771. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  30772. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  30773. callback_functions client_cb;
  30774. callback_functions server_cb;
  30775. int line = 0;
  30776. int flag = ERR_TXT_STRING;
  30777. const char* file = NULL;
  30778. const char* data = NULL;
  30779. /* create a failed connection and inspect the error */
  30780. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30781. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30782. client_cb.method = wolfTLSv1_1_client_method;
  30783. server_cb.method = wolfTLSv1_2_server_method;
  30784. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30785. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  30786. ExpectNotNull(data);
  30787. /* check clearing error state */
  30788. ERR_remove_state(0);
  30789. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30790. ERR_peek_last_error_line(NULL, &line);
  30791. ExpectIntEQ(line, 0);
  30792. ERR_peek_last_error_line(&file, NULL);
  30793. ExpectNull(file);
  30794. /* retry connection to fill error queue */
  30795. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30796. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30797. client_cb.method = wolfTLSv1_1_client_method;
  30798. server_cb.method = wolfTLSv1_2_server_method;
  30799. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  30800. /* check that error code was stored */
  30801. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  30802. ERR_peek_last_error_line(NULL, &line);
  30803. ExpectIntNE(line, 0);
  30804. ERR_peek_last_error_line(&file, NULL);
  30805. ExpectNotNull(file);
  30806. fprintf(stderr, "\nTesting error print out\n");
  30807. ERR_print_errors_fp(stderr);
  30808. fprintf(stderr, "Done testing print out\n\n");
  30809. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  30810. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  30811. return EXPECT_RESULT();
  30812. }
  30813. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  30814. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30815. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30816. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  30817. {
  30818. (void) ok;
  30819. (void) ctx;
  30820. fprintf(stderr, "ENTER verify_cb\n");
  30821. return SSL_SUCCESS;
  30822. }
  30823. #endif
  30824. static int test_wolfSSL_X509_Name_canon(void)
  30825. {
  30826. EXPECT_DECLS;
  30827. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30828. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  30829. defined(WOLFSSL_CERT_GEN) && \
  30830. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  30831. const long ex_hash1 = 0x0fdb2da4;
  30832. const long ex_hash2 = 0x9f3e8c9e;
  30833. X509_NAME *name = NULL;
  30834. X509 *x509 = NULL;
  30835. XFILE file = XBADFILE;
  30836. unsigned long hash = 0;
  30837. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  30838. byte *pbuf = NULL;
  30839. word32 len = 0;
  30840. (void) ex_hash2;
  30841. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  30842. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  30843. ExpectNotNull(name = X509_get_issuer_name(x509));
  30844. /* When output buffer is NULL, should return necessary output buffer
  30845. * length.*/
  30846. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  30847. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  30848. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  30849. hash = (((unsigned long)digest[3] << 24) |
  30850. ((unsigned long)digest[2] << 16) |
  30851. ((unsigned long)digest[1] << 8) |
  30852. ((unsigned long)digest[0]));
  30853. ExpectIntEQ(hash, ex_hash1);
  30854. if (file != XBADFILE) {
  30855. XFCLOSE(file);
  30856. file = XBADFILE;
  30857. }
  30858. X509_free(x509);
  30859. x509 = NULL;
  30860. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  30861. pbuf = NULL;
  30862. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  30863. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  30864. ExpectNotNull(name = X509_get_issuer_name(x509));
  30865. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  30866. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  30867. hash = (((unsigned long)digest[3] << 24) |
  30868. ((unsigned long)digest[2] << 16) |
  30869. ((unsigned long)digest[1] << 8) |
  30870. ((unsigned long)digest[0]));
  30871. ExpectIntEQ(hash, ex_hash2);
  30872. if (file != XBADFILE)
  30873. XFCLOSE(file);
  30874. X509_free(x509);
  30875. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  30876. #endif
  30877. return EXPECT_RESULT();
  30878. }
  30879. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  30880. {
  30881. EXPECT_DECLS;
  30882. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  30883. const int MAX_DIR = 4;
  30884. const char paths[][32] = {
  30885. "./certs/ed25519",
  30886. "./certs/ecc",
  30887. "./certs/crl",
  30888. "./certs/",
  30889. };
  30890. char CertCrl_path[MAX_FILENAME_SZ];
  30891. char *p;
  30892. X509_STORE* str = NULL;
  30893. X509_LOOKUP* lookup = NULL;
  30894. WOLFSSL_STACK* sk = NULL;
  30895. int len, total_len, i;
  30896. (void)sk;
  30897. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  30898. /* illegal string */
  30899. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30900. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30901. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  30902. SSL_FILETYPE_PEM,NULL), 0);
  30903. /* free store */
  30904. X509_STORE_free(str);
  30905. str = NULL;
  30906. /* short folder string */
  30907. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30908. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30909. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  30910. SSL_FILETYPE_PEM,NULL), 1);
  30911. #if defined(WOLFSSL_INT_H)
  30912. /* only available when including internal.h */
  30913. ExpectNotNull(sk = lookup->dirs->dir_entry);
  30914. #endif
  30915. /* free store */
  30916. X509_STORE_free(str);
  30917. str = NULL;
  30918. /* typical function check */
  30919. p = &CertCrl_path[0];
  30920. total_len = 0;
  30921. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  30922. len = (int)XSTRLEN((const char*)&paths[i]);
  30923. total_len += len;
  30924. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  30925. p += len;
  30926. if (i != 0) *(p++) = SEPARATOR_CHAR;
  30927. }
  30928. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30929. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30930. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  30931. SSL_FILETYPE_PEM,NULL), 1);
  30932. #if defined(WOLFSSL_INT_H)
  30933. /* only available when including internal.h */
  30934. ExpectNotNull(sk = lookup->dirs->dir_entry);
  30935. #endif
  30936. X509_STORE_free(str);
  30937. #endif
  30938. return EXPECT_RESULT();
  30939. }
  30940. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  30941. {
  30942. EXPECT_DECLS;
  30943. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30944. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30945. defined(WOLFSSL_SIGNER_DER_CERT)
  30946. X509_STORE_CTX* ctx = NULL;
  30947. X509_STORE* str = NULL;
  30948. X509_LOOKUP* lookup = NULL;
  30949. X509* cert1 = NULL;
  30950. X509* x509Ca = NULL;
  30951. X509* x509Svr = NULL;
  30952. X509* issuer = NULL;
  30953. WOLFSSL_STACK* sk = NULL;
  30954. X509_NAME* caName = NULL;
  30955. X509_NAME* issuerName = NULL;
  30956. XFILE file1 = XBADFILE;
  30957. int i;
  30958. int cert_count = 0;
  30959. int cmp;
  30960. char der[] = "certs/ca-cert.der";
  30961. #ifdef HAVE_CRL
  30962. char pem[][100] = {
  30963. "./certs/crl/crl.pem",
  30964. "./certs/crl/crl2.pem",
  30965. "./certs/crl/caEccCrl.pem",
  30966. "./certs/crl/eccCliCRL.pem",
  30967. "./certs/crl/eccSrvCRL.pem",
  30968. ""
  30969. };
  30970. #endif
  30971. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  30972. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  30973. if (file1 != XBADFILE)
  30974. XFCLOSE(file1);
  30975. ExpectNotNull(ctx = X509_STORE_CTX_new());
  30976. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  30977. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  30978. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  30979. SSL_FILETYPE_PEM,NULL), 1);
  30980. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  30981. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  30982. /* check if CA cert is loaded into the store */
  30983. for (i = 0; i < cert_count; i++) {
  30984. x509Ca = sk_X509_value(sk, i);
  30985. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  30986. }
  30987. ExpectNotNull((x509Svr =
  30988. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30989. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  30990. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  30991. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  30992. ExpectNotNull(issuer);
  30993. caName = X509_get_subject_name(x509Ca);
  30994. ExpectNotNull(caName);
  30995. issuerName = X509_get_subject_name(issuer);
  30996. ExpectNotNull(issuerName);
  30997. cmp = X509_NAME_cmp(caName, issuerName);
  30998. ExpectIntEQ(cmp, 0);
  30999. /* load der format */
  31000. X509_free(issuer);
  31001. issuer = NULL;
  31002. X509_STORE_CTX_free(ctx);
  31003. ctx = NULL;
  31004. X509_STORE_free(str);
  31005. str = NULL;
  31006. sk_X509_pop_free(sk, NULL);
  31007. sk = NULL;
  31008. X509_free(x509Svr);
  31009. x509Svr = NULL;
  31010. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31011. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31012. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  31013. SSL_FILETYPE_ASN1,NULL), 1);
  31014. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  31015. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  31016. /* check if CA cert is loaded into the store */
  31017. for (i = 0; i < cert_count; i++) {
  31018. x509Ca = sk_X509_value(sk, i);
  31019. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  31020. }
  31021. X509_STORE_free(str);
  31022. str = NULL;
  31023. sk_X509_pop_free(sk, NULL);
  31024. sk = NULL;
  31025. X509_free(cert1);
  31026. cert1 = NULL;
  31027. #ifdef HAVE_CRL
  31028. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  31029. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  31030. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  31031. SSL_FILETYPE_PEM,NULL), 1);
  31032. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  31033. "certs/server-revoked-cert.pem",
  31034. SSL_FILETYPE_PEM,NULL), 1);
  31035. if (str) {
  31036. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  31037. WOLFSSL_FILETYPE_PEM), 1);
  31038. /* since store hasn't yet known the revoked cert*/
  31039. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31040. "certs/server-revoked-cert.pem",
  31041. WOLFSSL_FILETYPE_PEM), 1);
  31042. }
  31043. for (i = 0; pem[i][0] != '\0'; i++)
  31044. {
  31045. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  31046. SSL_FILETYPE_PEM, NULL), 1);
  31047. }
  31048. if (str) {
  31049. /* since store knows crl list */
  31050. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  31051. "certs/server-revoked-cert.pem",
  31052. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  31053. }
  31054. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  31055. X509_STORE_free(str);
  31056. #endif
  31057. #endif
  31058. return EXPECT_RESULT();
  31059. }
  31060. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  31061. {
  31062. int res = TEST_SKIPPED;
  31063. #if defined(OPENSSL_EXTRA)
  31064. X509_STORE_CTX_cleanup(NULL);
  31065. X509_STORE_CTX_trusted_stack(NULL, NULL);
  31066. res = TEST_SUCCESS;
  31067. #endif
  31068. return res;
  31069. }
  31070. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  31071. {
  31072. EXPECT_DECLS;
  31073. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31074. X509_STORE_CTX* ctx = NULL;
  31075. X509_STORE* str = NULL;
  31076. X509* x509Ca = NULL;
  31077. X509* x509Svr = NULL;
  31078. X509* issuer = NULL;
  31079. X509_NAME* caName = NULL;
  31080. X509_NAME* issuerName = NULL;
  31081. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31082. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31083. ExpectNotNull((x509Ca =
  31084. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  31085. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  31086. ExpectNotNull((x509Svr =
  31087. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31088. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  31089. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  31090. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  31091. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  31092. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  31093. #ifdef WOLFSSL_SIGNER_DER_CERT
  31094. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  31095. #endif
  31096. X509_free(issuer);
  31097. X509_STORE_CTX_free(ctx);
  31098. X509_free(x509Svr);
  31099. X509_STORE_free(str);
  31100. X509_free(x509Ca);
  31101. #endif
  31102. return EXPECT_RESULT();
  31103. }
  31104. static int test_wolfSSL_PKCS7_certs(void)
  31105. {
  31106. EXPECT_DECLS;
  31107. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  31108. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  31109. STACK_OF(X509)* sk = NULL;
  31110. STACK_OF(X509_INFO)* info_sk = NULL;
  31111. PKCS7 *p7 = NULL;
  31112. BIO* bio = NULL;
  31113. const byte* p = NULL;
  31114. int buflen = 0;
  31115. int i;
  31116. /* Test twice. Once with d2i and once without to test
  31117. * that everything is free'd correctly. */
  31118. for (i = 0; i < 2; i++) {
  31119. ExpectNotNull(p7 = PKCS7_new());
  31120. if (p7 != NULL) {
  31121. p7->version = 1;
  31122. #ifdef NO_SHA
  31123. p7->hashOID = SHA256h;
  31124. #else
  31125. p7->hashOID = SHAh;
  31126. #endif
  31127. }
  31128. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  31129. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  31130. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  31131. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  31132. ExpectNotNull(sk = sk_X509_new_null());
  31133. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  31134. X509_INFO* info = NULL;
  31135. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  31136. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  31137. if (EXPECT_SUCCESS() && (info != NULL)) {
  31138. info->x509 = NULL;
  31139. }
  31140. X509_INFO_free(info);
  31141. }
  31142. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  31143. info_sk = NULL;
  31144. BIO_free(bio);
  31145. bio = NULL;
  31146. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  31147. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  31148. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  31149. sk_X509_pop_free(sk, X509_free);
  31150. }
  31151. sk = NULL;
  31152. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  31153. if (i == 0) {
  31154. PKCS7_free(p7);
  31155. p7 = NULL;
  31156. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  31157. if (p7 != NULL) {
  31158. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  31159. * them */
  31160. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  31161. }
  31162. /* PKCS7_free free's the certs */
  31163. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  31164. }
  31165. BIO_free(bio);
  31166. bio = NULL;
  31167. PKCS7_free(p7);
  31168. p7 = NULL;
  31169. }
  31170. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31171. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  31172. return EXPECT_RESULT();
  31173. }
  31174. static int test_wolfSSL_X509_STORE_CTX(void)
  31175. {
  31176. EXPECT_DECLS;
  31177. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31178. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31179. X509_STORE_CTX* ctx = NULL;
  31180. X509_STORE* str = NULL;
  31181. X509* x509 = NULL;
  31182. #ifdef OPENSSL_ALL
  31183. X509* x5092 = NULL;
  31184. STACK_OF(X509) *sk = NULL;
  31185. STACK_OF(X509) *sk2 = NULL;
  31186. STACK_OF(X509) *sk3 = NULL;
  31187. #endif
  31188. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31189. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  31190. ExpectNotNull((x509 =
  31191. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  31192. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  31193. #ifdef OPENSSL_ALL
  31194. /* sk_X509_new only in OPENSSL_ALL */
  31195. sk = sk_X509_new_null();
  31196. ExpectNotNull(sk);
  31197. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  31198. #else
  31199. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  31200. #endif
  31201. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  31202. X509_STORE_CTX_set_error(ctx, -5);
  31203. X509_STORE_CTX_set_error(NULL, -5);
  31204. X509_STORE_CTX_free(ctx);
  31205. ctx = NULL;
  31206. #ifdef OPENSSL_ALL
  31207. sk_X509_pop_free(sk, NULL);
  31208. sk = NULL;
  31209. #endif
  31210. X509_STORE_free(str);
  31211. str = NULL;
  31212. X509_free(x509);
  31213. x509 = NULL;
  31214. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31215. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  31216. X509_STORE_CTX_free(ctx);
  31217. ctx = NULL;
  31218. #ifdef OPENSSL_ALL
  31219. /* test X509_STORE_CTX_get(1)_chain */
  31220. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  31221. SSL_FILETYPE_PEM)));
  31222. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  31223. SSL_FILETYPE_PEM)));
  31224. ExpectNotNull((sk = sk_X509_new_null()));
  31225. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  31226. if (EXPECT_FAIL()) {
  31227. X509_free(x509);
  31228. x509 = NULL;
  31229. }
  31230. ExpectNotNull((str = X509_STORE_new()));
  31231. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  31232. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  31233. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  31234. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  31235. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  31236. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  31237. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  31238. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  31239. X509_STORE_CTX_free(ctx);
  31240. ctx = NULL;
  31241. X509_STORE_free(str);
  31242. str = NULL;
  31243. /* CTX certs not freed yet */
  31244. X509_free(x5092);
  31245. x5092 = NULL;
  31246. sk_X509_pop_free(sk, NULL);
  31247. sk = NULL;
  31248. /* sk3 is dup so free here */
  31249. sk_X509_pop_free(sk3, NULL);
  31250. sk3 = NULL;
  31251. #endif
  31252. /* test X509_STORE_CTX_get/set_ex_data */
  31253. {
  31254. int i = 0, tmpData = 5;
  31255. void* tmpDataRet;
  31256. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31257. #ifdef HAVE_EX_DATA
  31258. for (i = 0; i < MAX_EX_DATA; i++) {
  31259. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31260. WOLFSSL_SUCCESS);
  31261. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31262. ExpectNotNull(tmpDataRet);
  31263. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31264. }
  31265. #else
  31266. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  31267. WOLFSSL_FAILURE);
  31268. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  31269. ExpectNull(tmpDataRet);
  31270. #endif
  31271. X509_STORE_CTX_free(ctx);
  31272. ctx = NULL;
  31273. }
  31274. /* test X509_STORE_get/set_ex_data */
  31275. {
  31276. int i = 0, tmpData = 99;
  31277. void* tmpDataRet;
  31278. ExpectNotNull(str = X509_STORE_new());
  31279. #ifdef HAVE_EX_DATA
  31280. for (i = 0; i < MAX_EX_DATA; i++) {
  31281. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31282. WOLFSSL_SUCCESS);
  31283. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31284. ExpectNotNull(tmpDataRet);
  31285. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  31286. }
  31287. #else
  31288. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  31289. WOLFSSL_FAILURE);
  31290. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  31291. ExpectNull(tmpDataRet);
  31292. #endif
  31293. X509_STORE_free(str);
  31294. str = NULL;
  31295. }
  31296. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31297. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31298. return EXPECT_RESULT();
  31299. }
  31300. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31301. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  31302. STACK_OF(X509)* chain)
  31303. {
  31304. EXPECT_DECLS;
  31305. XFILE fp = XBADFILE;
  31306. X509* cert = NULL;
  31307. ExpectTrue((fp = XFOPEN(filename, "rb"))
  31308. != XBADFILE);
  31309. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31310. if (fp != XBADFILE) {
  31311. XFCLOSE(fp);
  31312. fp = XBADFILE;
  31313. }
  31314. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  31315. if (EXPECT_FAIL())
  31316. X509_free(cert);
  31317. return EXPECT_RESULT();
  31318. }
  31319. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  31320. int err, int loadCA)
  31321. {
  31322. EXPECT_DECLS;
  31323. X509_STORE_CTX* ctx = NULL;
  31324. X509_STORE* str = NULL;
  31325. XFILE fp = XBADFILE;
  31326. X509* cert = NULL;
  31327. STACK_OF(X509)* untrusted = NULL;
  31328. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  31329. != XBADFILE);
  31330. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  31331. if (fp != XBADFILE) {
  31332. XFCLOSE(fp);
  31333. fp = XBADFILE;
  31334. }
  31335. ExpectNotNull(str = X509_STORE_new());
  31336. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31337. ExpectNotNull(untrusted = sk_X509_new_null());
  31338. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  31339. if (loadCA) {
  31340. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  31341. 1);
  31342. }
  31343. for (; *filenames; filenames++) {
  31344. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  31345. untrusted), TEST_SUCCESS);
  31346. }
  31347. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  31348. ExpectIntEQ(X509_verify_cert(ctx), ret);
  31349. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  31350. X509_free(cert);
  31351. X509_STORE_free(str);
  31352. X509_STORE_CTX_free(ctx);
  31353. sk_X509_pop_free(untrusted, NULL);
  31354. return EXPECT_RESULT();
  31355. }
  31356. #endif
  31357. static int test_X509_STORE_untrusted(void)
  31358. {
  31359. EXPECT_DECLS;
  31360. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31361. const char* untrusted1[] = {
  31362. "./certs/intermediate/ca-int2-cert.pem",
  31363. NULL
  31364. };
  31365. const char* untrusted2[] = {
  31366. "./certs/intermediate/ca-int-cert.pem",
  31367. "./certs/intermediate/ca-int2-cert.pem",
  31368. NULL
  31369. };
  31370. const char* untrusted3[] = {
  31371. "./certs/intermediate/ca-int-cert.pem",
  31372. "./certs/intermediate/ca-int2-cert.pem",
  31373. "./certs/ca-cert.pem",
  31374. NULL
  31375. };
  31376. /* Adding unrelated certs that should be ignored */
  31377. const char* untrusted4[] = {
  31378. "./certs/client-ca.pem",
  31379. "./certs/intermediate/ca-int-cert.pem",
  31380. "./certs/server-cert.pem",
  31381. "./certs/intermediate/ca-int2-cert.pem",
  31382. NULL
  31383. };
  31384. /* Only immediate issuer in untrusted chaing. Fails since can't build chain
  31385. * to loaded CA. */
  31386. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  31387. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  31388. /* Succeeds because path to loaded CA is available. */
  31389. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  31390. TEST_SUCCESS);
  31391. /* Fails because root CA is in the untrusted stack */
  31392. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  31393. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  31394. /* Succeeds because path to loaded CA is available. */
  31395. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  31396. TEST_SUCCESS);
  31397. #endif
  31398. return EXPECT_RESULT();
  31399. }
  31400. static int test_wolfSSL_X509_STORE_set_flags(void)
  31401. {
  31402. EXPECT_DECLS;
  31403. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31404. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31405. X509_STORE* store = NULL;
  31406. X509* x509 = NULL;
  31407. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  31408. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31409. WOLFSSL_FILETYPE_PEM)));
  31410. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  31411. #ifdef HAVE_CRL
  31412. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31413. WOLFSSL_SUCCESS);
  31414. #else
  31415. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  31416. NOT_COMPILED_IN);
  31417. #endif
  31418. wolfSSL_X509_free(x509);
  31419. wolfSSL_X509_STORE_free(store);
  31420. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  31421. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31422. return EXPECT_RESULT();
  31423. }
  31424. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  31425. {
  31426. EXPECT_DECLS;
  31427. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  31428. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  31429. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  31430. WOLFSSL_X509_STORE* store = NULL;
  31431. WOLFSSL_X509_LOOKUP* lookup = NULL;
  31432. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  31433. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  31434. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  31435. X509_FILETYPE_PEM), 1);
  31436. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  31437. X509_FILETYPE_PEM), 1);
  31438. if (store != NULL) {
  31439. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  31440. WOLFSSL_FILETYPE_PEM), 1);
  31441. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31442. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  31443. }
  31444. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  31445. X509_FILETYPE_PEM), 1);
  31446. if (store != NULL) {
  31447. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  31448. WOLFSSL_FILETYPE_PEM), 1);
  31449. }
  31450. wolfSSL_X509_STORE_free(store);
  31451. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  31452. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31453. return EXPECT_RESULT();
  31454. }
  31455. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  31456. {
  31457. EXPECT_DECLS;
  31458. #if defined(OPENSSL_EXTRA)
  31459. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  31460. time_t c_time;
  31461. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  31462. c_time = 365*24*60*60;
  31463. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  31464. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  31465. WOLFSSL_USE_CHECK_TIME);
  31466. ExpectTrue(ctx->param->check_time == c_time);
  31467. wolfSSL_X509_STORE_CTX_free(ctx);
  31468. #endif /* OPENSSL_EXTRA */
  31469. return EXPECT_RESULT();
  31470. }
  31471. static int test_wolfSSL_CTX_get0_set1_param(void)
  31472. {
  31473. EXPECT_DECLS;
  31474. #if defined(OPENSSL_EXTRA)
  31475. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31476. SSL_CTX* ctx = NULL;
  31477. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31478. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  31479. char testIPv4[] = "127.0.0.1";
  31480. char testhostName[] = "foo.hoge.com";
  31481. #ifndef NO_WOLFSSL_SERVER
  31482. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31483. #else
  31484. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31485. #endif
  31486. ExpectNull(SSL_CTX_get0_param(NULL));
  31487. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  31488. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  31489. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  31490. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  31491. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  31492. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  31493. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  31494. WOLFSSL_SUCCESS);
  31495. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  31496. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  31497. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  31498. (int)XSTRLEN(testhostName)));
  31499. ExpectIntEQ(0x01, pParam->hostFlags);
  31500. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  31501. /* test for incorrect parameter */
  31502. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  31503. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  31504. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  31505. SSL_CTX_free(ctx);
  31506. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  31507. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31508. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31509. return EXPECT_RESULT();
  31510. }
  31511. static int test_wolfSSL_get0_param(void)
  31512. {
  31513. EXPECT_DECLS;
  31514. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31515. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31516. SSL_CTX* ctx = NULL;
  31517. SSL* ssl = NULL;
  31518. #ifndef NO_WOLFSSL_SERVER
  31519. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31520. #else
  31521. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31522. #endif
  31523. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31524. SSL_FILETYPE_PEM));
  31525. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31526. ExpectNotNull(ssl = SSL_new(ctx));
  31527. ExpectNotNull(SSL_get0_param(ssl));
  31528. SSL_free(ssl);
  31529. SSL_CTX_free(ctx);
  31530. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31531. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  31532. return EXPECT_RESULT();
  31533. }
  31534. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  31535. {
  31536. EXPECT_DECLS;
  31537. #if defined(OPENSSL_EXTRA)
  31538. const char host[] = "www.example.com";
  31539. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31540. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  31541. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  31542. if (pParam != NULL) {
  31543. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31544. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  31545. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31546. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  31547. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31548. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31549. }
  31550. #endif /* OPENSSL_EXTRA */
  31551. return EXPECT_RESULT();
  31552. }
  31553. static int test_wolfSSL_set1_host(void)
  31554. {
  31555. EXPECT_DECLS;
  31556. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31557. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31558. const char host[] = "www.test_wolfSSL_set1_host.com";
  31559. const char emptyStr[] = "";
  31560. SSL_CTX* ctx = NULL;
  31561. SSL* ssl = NULL;
  31562. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  31563. #ifndef NO_WOLFSSL_SERVER
  31564. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31565. #else
  31566. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31567. #endif
  31568. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31569. SSL_FILETYPE_PEM));
  31570. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31571. ExpectNotNull(ssl = SSL_new(ctx));
  31572. pParam = SSL_get0_param(ssl);
  31573. /* we should get back host string */
  31574. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31575. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31576. /* we should get back empty string */
  31577. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  31578. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31579. /* we should get back host string */
  31580. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  31581. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  31582. /* we should get back empty string */
  31583. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  31584. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  31585. SSL_free(ssl);
  31586. SSL_CTX_free(ctx);
  31587. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31588. #endif /* OPENSSL_EXTRA */
  31589. return EXPECT_RESULT();
  31590. }
  31591. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  31592. {
  31593. EXPECT_DECLS;
  31594. #if defined(OPENSSL_EXTRA)
  31595. unsigned char buf[16] = {0};
  31596. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  31597. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  31598. /* test 127.0.0.1 */
  31599. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  31600. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  31601. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  31602. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  31603. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31604. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  31605. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  31606. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  31607. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31608. ExpectIntEQ(XSTRNCMP(param->ipasc,
  31609. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  31610. /* test 2001:db8:: */
  31611. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31612. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31613. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31614. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  31615. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31616. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  31617. /* test ::1234:5678 */
  31618. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  31619. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31620. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31621. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31622. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31623. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  31624. /* test 2001:db8::1234:5678 */
  31625. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31626. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  31627. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  31628. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  31629. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31630. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  31631. sizeof(param->ipasc)), 0);
  31632. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  31633. /* 2001:db8:1::ab9:c0a8:102 */
  31634. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  31635. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  31636. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  31637. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  31638. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  31639. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  31640. sizeof(param->ipasc)), 0);
  31641. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  31642. #endif /* OPENSSL_EXTRA */
  31643. return EXPECT_RESULT();
  31644. }
  31645. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  31646. {
  31647. EXPECT_DECLS;
  31648. #if defined(OPENSSL_EXTRA)
  31649. X509_STORE* store = NULL;
  31650. X509_STORE_CTX* ctx = NULL;
  31651. X509_STORE_CTX* ctx_no_init = NULL;
  31652. ExpectNotNull((store = X509_STORE_new()));
  31653. ExpectNotNull(ctx = X509_STORE_CTX_new());
  31654. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  31655. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  31656. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  31657. /* should return NULL if ctx has not bee initialized */
  31658. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  31659. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  31660. wolfSSL_X509_STORE_CTX_free(ctx);
  31661. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  31662. X509_STORE_free(store);
  31663. #endif /* OPENSSL_EXTRA */
  31664. return EXPECT_RESULT();
  31665. }
  31666. static int test_wolfSSL_CTX_set_client_CA_list(void)
  31667. {
  31668. EXPECT_DECLS;
  31669. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31670. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  31671. WOLFSSL_CTX* ctx = NULL;
  31672. WOLFSSL* ssl = NULL;
  31673. X509_NAME* name = NULL;
  31674. STACK_OF(X509_NAME)* names = NULL;
  31675. STACK_OF(X509_NAME)* ca_list = NULL;
  31676. int names_len = 0;
  31677. int i;
  31678. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31679. /* Send two X501 names in cert request */
  31680. names = SSL_load_client_CA_file(cliCertFile);
  31681. ExpectNotNull(names);
  31682. ca_list = SSL_load_client_CA_file(caCertFile);
  31683. ExpectNotNull(ca_list);
  31684. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  31685. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  31686. if (EXPECT_FAIL()) {
  31687. wolfSSL_X509_NAME_free(name);
  31688. name = NULL;
  31689. }
  31690. SSL_CTX_set_client_CA_list(ctx, names);
  31691. /* This should only free the stack structure */
  31692. sk_X509_NAME_free(ca_list);
  31693. ca_list = NULL;
  31694. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31695. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31696. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31697. for (i = 0; i < names_len; i++) {
  31698. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31699. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31700. }
  31701. /* Needed to be able to create ssl object */
  31702. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31703. SSL_FILETYPE_PEM));
  31704. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31705. ExpectNotNull(ssl = wolfSSL_new(ctx));
  31706. /* load again as old names are responsibility of ctx to free*/
  31707. names = SSL_load_client_CA_file(cliCertFile);
  31708. ExpectNotNull(names);
  31709. SSL_set_client_CA_list(ssl, names);
  31710. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  31711. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  31712. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  31713. for (i = 0; i < names_len; i++) {
  31714. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  31715. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  31716. }
  31717. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  31718. {
  31719. tcp_ready ready;
  31720. func_args server_args;
  31721. callback_functions server_cb;
  31722. THREAD_TYPE serverThread;
  31723. WOLFSSL* ssl_client = NULL;
  31724. WOLFSSL_CTX* ctx_client = NULL;
  31725. SOCKET_T sockfd = 0;
  31726. /* wolfSSL_get_client_CA_list() with handshake */
  31727. StartTCP();
  31728. InitTcpReady(&ready);
  31729. XMEMSET(&server_args, 0, sizeof(func_args));
  31730. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31731. server_args.signal = &ready;
  31732. server_args.callbacks = &server_cb;
  31733. /* we are responsible for free'ing WOLFSSL_CTX */
  31734. server_cb.ctx = ctx;
  31735. server_cb.isSharedCtx = 1;
  31736. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  31737. cliCertFile, 0));
  31738. start_thread(test_server_nofail, &server_args, &serverThread);
  31739. wait_tcp_ready(&server_args);
  31740. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  31741. ExpectNotNull(ctx_client =
  31742. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  31743. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  31744. ctx_client, caCertFile, 0));
  31745. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  31746. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  31747. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  31748. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  31749. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  31750. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  31751. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  31752. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  31753. /* We are expecting two cert names to be sent */
  31754. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  31755. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  31756. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  31757. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  31758. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  31759. }
  31760. wolfSSL_shutdown(ssl_client);
  31761. wolfSSL_free(ssl_client);
  31762. wolfSSL_CTX_free(ctx_client);
  31763. CloseSocket(sockfd);
  31764. join_thread(serverThread);
  31765. FreeTcpReady(&ready);
  31766. }
  31767. #endif
  31768. wolfSSL_free(ssl);
  31769. wolfSSL_CTX_free(ctx);
  31770. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  31771. * !NO_BIO */
  31772. return EXPECT_RESULT();
  31773. }
  31774. static int test_wolfSSL_CTX_add_client_CA(void)
  31775. {
  31776. EXPECT_DECLS;
  31777. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31778. !defined(NO_WOLFSSL_CLIENT)
  31779. WOLFSSL_CTX* ctx = NULL;
  31780. WOLFSSL_X509* x509 = NULL;
  31781. WOLFSSL_X509* x509_a = NULL;
  31782. STACK_OF(X509_NAME)* ca_list = NULL;
  31783. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31784. /* Add client cert */
  31785. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  31786. SSL_FILETYPE_PEM));
  31787. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  31788. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  31789. /* Add another client cert */
  31790. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  31791. SSL_FILETYPE_PEM));
  31792. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  31793. /* test for incorrect parameter */
  31794. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  31795. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  31796. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  31797. X509_free(x509);
  31798. X509_free(x509_a);
  31799. SSL_CTX_free(ctx);
  31800. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  31801. return EXPECT_RESULT();
  31802. }
  31803. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  31804. defined(HAVE_IO_TESTS_DEPENDENCIES)
  31805. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  31806. {
  31807. callback_functions* callbacks = ((func_args*)args)->callbacks;
  31808. WOLFSSL_CTX* ctx = callbacks->ctx;
  31809. WOLFSSL* ssl = NULL;
  31810. SOCKET_T sfd = 0;
  31811. SOCKET_T cfd = 0;
  31812. word16 port;
  31813. char input[1024];
  31814. int idx;
  31815. int ret, err = 0;
  31816. const char* privateName = "ech-private-name.com";
  31817. int privateNameLen = (int)XSTRLEN(privateName);
  31818. ((func_args*)args)->return_code = TEST_FAIL;
  31819. port = ((func_args*)args)->signal->port;
  31820. AssertIntEQ(WOLFSSL_SUCCESS,
  31821. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  31822. AssertIntEQ(WOLFSSL_SUCCESS,
  31823. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31824. WOLFSSL_FILETYPE_PEM));
  31825. AssertIntEQ(WOLFSSL_SUCCESS,
  31826. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31827. WOLFSSL_FILETYPE_PEM));
  31828. if (callbacks->ctx_ready)
  31829. callbacks->ctx_ready(ctx);
  31830. ssl = wolfSSL_new(ctx);
  31831. /* set the sni for the server */
  31832. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  31833. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  31834. CloseSocket(sfd);
  31835. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  31836. if (callbacks->ssl_ready)
  31837. callbacks->ssl_ready(ssl);
  31838. do {
  31839. err = 0; /* Reset error */
  31840. ret = wolfSSL_accept(ssl);
  31841. if (ret != WOLFSSL_SUCCESS) {
  31842. err = wolfSSL_get_error(ssl, 0);
  31843. }
  31844. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  31845. if (ret != WOLFSSL_SUCCESS) {
  31846. char buff[WOLFSSL_MAX_ERROR_SZ];
  31847. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  31848. }
  31849. else {
  31850. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  31851. input[idx] = 0;
  31852. fprintf(stderr, "Client message: %s\n", input);
  31853. }
  31854. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  31855. privateNameLen));
  31856. ((func_args*)args)->return_code = TEST_SUCCESS;
  31857. }
  31858. if (callbacks->on_result)
  31859. callbacks->on_result(ssl);
  31860. wolfSSL_shutdown(ssl);
  31861. wolfSSL_free(ssl);
  31862. wolfSSL_CTX_free(ctx);
  31863. CloseSocket(cfd);
  31864. #ifdef FP_ECC
  31865. wc_ecc_fp_free();
  31866. #endif
  31867. WOLFSSL_RETURN_FROM_THREAD(0);
  31868. }
  31869. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  31870. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31871. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  31872. {
  31873. AssertNotNull(ssl);
  31874. AssertNotNull(line);
  31875. XFILE fp;
  31876. const byte lf = '\n';
  31877. fp = XFOPEN("./MyKeyLog.txt", "a");
  31878. XFWRITE( line, 1, strlen(line),fp);
  31879. XFWRITE( (void*)&lf,1,1,fp);
  31880. XFFLUSH(fp);
  31881. XFCLOSE(fp);
  31882. }
  31883. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  31884. static int test_wolfSSL_CTX_set_keylog_callback(void)
  31885. {
  31886. EXPECT_DECLS;
  31887. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  31888. !defined(NO_WOLFSSL_CLIENT)
  31889. SSL_CTX* ctx = NULL;
  31890. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31891. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  31892. SSL_CTX_free(ctx);
  31893. SSL_CTX_set_keylog_callback(NULL, NULL);
  31894. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  31895. return EXPECT_RESULT();
  31896. }
  31897. static int test_wolfSSL_CTX_get_keylog_callback(void)
  31898. {
  31899. EXPECT_DECLS;
  31900. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  31901. !defined(NO_WOLFSSL_CLIENT)
  31902. SSL_CTX* ctx = NULL;
  31903. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31904. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  31905. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  31906. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  31907. SSL_CTX_set_keylog_callback(ctx, NULL );
  31908. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  31909. SSL_CTX_free(ctx);
  31910. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  31911. return EXPECT_RESULT();
  31912. }
  31913. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31914. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  31915. {
  31916. /* set keylog callback */
  31917. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  31918. return TEST_SUCCESS;
  31919. }
  31920. #endif
  31921. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  31922. {
  31923. EXPECT_DECLS;
  31924. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  31925. /* This test is intended for checking whether keylog callback is called
  31926. * in client during TLS handshake between the client and a server.
  31927. */
  31928. test_ssl_cbf server_cbf;
  31929. test_ssl_cbf client_cbf;
  31930. XFILE fp = XBADFILE;
  31931. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  31932. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  31933. server_cbf.method = wolfTLSv1_2_server_method;
  31934. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  31935. /* clean up keylog file */
  31936. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  31937. if (fp != XBADFILE) {
  31938. XFCLOSE(fp);
  31939. fp = XBADFILE;
  31940. }
  31941. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  31942. &server_cbf, NULL), TEST_SUCCESS);
  31943. /* check if the keylog file exists */
  31944. char buff[300] = {0};
  31945. int found = 0;
  31946. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  31947. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  31948. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  31949. found = 1;
  31950. break;
  31951. }
  31952. }
  31953. if (fp != XBADFILE) {
  31954. XFCLOSE(fp);
  31955. }
  31956. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  31957. ExpectIntEQ(found, 1);
  31958. /* clean up */
  31959. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  31960. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  31961. return EXPECT_RESULT();
  31962. }
  31963. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  31964. defined(HAVE_SECRET_CALLBACK)
  31965. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  31966. {
  31967. /* set keylog callback */
  31968. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  31969. return TEST_SUCCESS;
  31970. }
  31971. #endif
  31972. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  31973. {
  31974. EXPECT_DECLS;
  31975. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  31976. defined(HAVE_SECRET_CALLBACK)
  31977. /* This test is intended for checking whether keylog callback is called
  31978. * in client during TLS handshake between the client and a server.
  31979. */
  31980. test_ssl_cbf server_cbf;
  31981. test_ssl_cbf client_cbf;
  31982. XFILE fp = XBADFILE;
  31983. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  31984. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  31985. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  31986. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  31987. /* clean up keylog file */
  31988. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  31989. if (fp != XBADFILE) {
  31990. XFCLOSE(fp);
  31991. fp = XBADFILE;
  31992. }
  31993. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  31994. &server_cbf, NULL), TEST_SUCCESS);
  31995. /* check if the keylog file exists */
  31996. {
  31997. char buff[300] = {0};
  31998. int found[4] = {0};
  31999. int numfnd = 0;
  32000. int i;
  32001. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  32002. while (EXPECT_SUCCESS() &&
  32003. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  32004. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  32005. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  32006. found[0] = 1;
  32007. continue;
  32008. }
  32009. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  32010. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  32011. found[1] = 1;
  32012. continue;
  32013. }
  32014. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  32015. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  32016. found[2] = 1;
  32017. continue;
  32018. }
  32019. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  32020. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  32021. found[3] = 1;
  32022. continue;
  32023. }
  32024. }
  32025. if (fp != XBADFILE)
  32026. XFCLOSE(fp);
  32027. for (i = 0; i < 4; i++) {
  32028. if (found[i] != 0)
  32029. numfnd++;
  32030. }
  32031. ExpectIntEQ(numfnd, 4);
  32032. }
  32033. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  32034. return EXPECT_RESULT();
  32035. }
  32036. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  32037. defined(HAVE_IO_TESTS_DEPENDENCIES)
  32038. static int test_wolfSSL_Tls13_ECH_params(void)
  32039. {
  32040. EXPECT_DECLS;
  32041. #if !defined(NO_WOLFSSL_CLIENT)
  32042. word32 outputLen = 0;
  32043. byte testBuf[72];
  32044. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  32045. WOLFSSL *ssl = wolfSSL_new(ctx);
  32046. ExpectNotNull(ctx);
  32047. ExpectNotNull(ssl);
  32048. /* invalid ctx */
  32049. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  32050. "ech-public-name.com", 0, 0, 0));
  32051. /* invalid public name */
  32052. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  32053. 0, 0));
  32054. /* invalid algorithms */
  32055. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  32056. "ech-public-name.com", 1000, 1000, 1000));
  32057. /* invalid ctx */
  32058. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  32059. &outputLen));
  32060. /* invalid output len */
  32061. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  32062. /* invalid ssl */
  32063. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  32064. (char*)testBuf, sizeof(testBuf)));
  32065. /* invalid configs64 */
  32066. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  32067. sizeof(testBuf)));
  32068. /* invalid size */
  32069. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  32070. (char*)testBuf, 0));
  32071. /* invalid ssl */
  32072. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  32073. sizeof(testBuf)));
  32074. /* invalid configs */
  32075. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  32076. sizeof(testBuf)));
  32077. /* invalid size */
  32078. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  32079. /* invalid ssl */
  32080. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  32081. /* invalid size */
  32082. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  32083. wolfSSL_free(ssl);
  32084. wolfSSL_CTX_free(ctx);
  32085. #endif /* !NO_WOLFSSL_CLIENT */
  32086. return EXPECT_RESULT();
  32087. }
  32088. static int test_wolfSSL_Tls13_ECH(void)
  32089. {
  32090. EXPECT_DECLS;
  32091. tcp_ready ready;
  32092. func_args client_args;
  32093. func_args server_args;
  32094. THREAD_TYPE serverThread;
  32095. callback_functions server_cbf;
  32096. callback_functions client_cbf;
  32097. SOCKET_T sockfd = 0;
  32098. WOLFSSL_CTX* ctx = NULL;
  32099. WOLFSSL* ssl = NULL;
  32100. const char* publicName = "ech-public-name.com";
  32101. const char* privateName = "ech-private-name.com";
  32102. int privateNameLen = 20;
  32103. char reply[1024];
  32104. int replyLen = 0;
  32105. byte rawEchConfig[128];
  32106. word32 rawEchConfigLen = sizeof(rawEchConfig);
  32107. InitTcpReady(&ready);
  32108. ready.port = 22222;
  32109. XMEMSET(&client_args, 0, sizeof(func_args));
  32110. XMEMSET(&server_args, 0, sizeof(func_args));
  32111. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  32112. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  32113. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  32114. /* create the server context here so we can get the ech config */
  32115. ExpectNotNull(server_cbf.ctx =
  32116. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  32117. /* generate ech config */
  32118. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  32119. publicName, 0, 0, 0));
  32120. /* get the config for the client to use */
  32121. ExpectIntEQ(WOLFSSL_SUCCESS,
  32122. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  32123. &rawEchConfigLen));
  32124. server_args.callbacks = &server_cbf;
  32125. server_args.signal = &ready;
  32126. /* start server task */
  32127. start_thread(server_task_ech, &server_args, &serverThread);
  32128. wait_tcp_ready(&server_args);
  32129. /* run as a TLS1.3 client */
  32130. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  32131. ExpectIntEQ(WOLFSSL_SUCCESS,
  32132. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  32133. ExpectIntEQ(WOLFSSL_SUCCESS,
  32134. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  32135. ExpectIntEQ(WOLFSSL_SUCCESS,
  32136. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  32137. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  32138. /* get connected the server task */
  32139. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32140. /* set the ech configs for the client */
  32141. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  32142. rawEchConfigLen));
  32143. /* set the sni for the client */
  32144. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  32145. privateName, privateNameLen));
  32146. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  32147. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32148. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  32149. privateNameLen);
  32150. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  32151. /* add th null terminator for string compare */
  32152. reply[replyLen] = 0;
  32153. /* check that the server replied with the private name */
  32154. ExpectStrEQ(privateName, reply);
  32155. wolfSSL_free(ssl);
  32156. wolfSSL_CTX_free(ctx);
  32157. CloseSocket(sockfd);
  32158. join_thread(serverThread);
  32159. FreeTcpReady(&ready);
  32160. return EXPECT_RESULT();
  32161. }
  32162. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  32163. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32164. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32165. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32166. static int post_auth_version_cb(WOLFSSL* ssl)
  32167. {
  32168. EXPECT_DECLS;
  32169. /* do handshake and then test version error */
  32170. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32171. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32172. return EXPECT_RESULT();
  32173. }
  32174. static int post_auth_version_client_cb(WOLFSSL* ssl)
  32175. {
  32176. EXPECT_DECLS;
  32177. /* do handshake and then test version error */
  32178. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  32179. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  32180. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  32181. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  32182. /* check was added to error queue */
  32183. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  32184. /* check the string matches expected string */
  32185. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  32186. "WRONG_SSL_VERSION");
  32187. #endif
  32188. return EXPECT_RESULT();
  32189. }
  32190. static int post_auth_cb(WOLFSSL* ssl)
  32191. {
  32192. EXPECT_DECLS;
  32193. WOLFSSL_X509* x509 = NULL;
  32194. /* do handshake and then test version error */
  32195. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  32196. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  32197. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  32198. wolfSSL_X509_free(x509);
  32199. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  32200. return EXPECT_RESULT();
  32201. }
  32202. static int set_post_auth_cb(WOLFSSL* ssl)
  32203. {
  32204. if (!wolfSSL_is_server(ssl)) {
  32205. EXPECT_DECLS;
  32206. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  32207. return EXPECT_RESULT();
  32208. }
  32209. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  32210. return TEST_SUCCESS;
  32211. }
  32212. #endif
  32213. static int test_wolfSSL_Tls13_postauth(void)
  32214. {
  32215. EXPECT_DECLS;
  32216. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  32217. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32218. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  32219. test_ssl_cbf server_cbf;
  32220. test_ssl_cbf client_cbf;
  32221. /* test version failure doing post auth with TLS 1.2 connection */
  32222. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32223. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32224. server_cbf.method = wolfTLSv1_2_server_method;
  32225. server_cbf.ssl_ready = set_post_auth_cb;
  32226. server_cbf.on_result = post_auth_version_cb;
  32227. client_cbf.ssl_ready = set_post_auth_cb;
  32228. client_cbf.on_result = post_auth_version_client_cb;
  32229. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32230. &server_cbf, NULL), TEST_SUCCESS);
  32231. /* tests on post auth with TLS 1.3 */
  32232. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  32233. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  32234. server_cbf.method = wolfTLSv1_3_server_method;
  32235. server_cbf.ssl_ready = set_post_auth_cb;
  32236. client_cbf.ssl_ready = set_post_auth_cb;
  32237. server_cbf.on_result = post_auth_cb;
  32238. client_cbf.on_result = NULL;
  32239. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  32240. &server_cbf, NULL), TEST_SUCCESS);
  32241. #endif
  32242. return EXPECT_RESULT();
  32243. }
  32244. static int test_wolfSSL_X509_NID(void)
  32245. {
  32246. EXPECT_DECLS;
  32247. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  32248. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  32249. int sigType;
  32250. int nameSz;
  32251. X509* cert = NULL;
  32252. EVP_PKEY* pubKeyTmp = NULL;
  32253. X509_NAME* name = NULL;
  32254. char commonName[80];
  32255. char countryName[80];
  32256. char localityName[80];
  32257. char stateName[80];
  32258. char orgName[80];
  32259. char orgUnit[80];
  32260. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  32261. /* convert cert from DER to internal WOLFSSL_X509 struct */
  32262. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  32263. sizeof_client_cert_der_2048));
  32264. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  32265. /* extract PUBLIC KEY from cert */
  32266. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  32267. /* extract signatureType */
  32268. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  32269. /* extract subjectName info */
  32270. ExpectNotNull(name = X509_get_subject_name(cert));
  32271. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  32272. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32273. NULL, 0)), 0);
  32274. ExpectIntEQ(nameSz, 15);
  32275. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32276. commonName, sizeof(commonName))), 0);
  32277. ExpectIntEQ(nameSz, 15);
  32278. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  32279. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  32280. commonName, 9)), 0);
  32281. ExpectIntEQ(nameSz, 8);
  32282. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  32283. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  32284. countryName, sizeof(countryName))), 0);
  32285. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  32286. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  32287. localityName, sizeof(localityName))), 0);
  32288. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  32289. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32290. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  32291. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  32292. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  32293. orgName, sizeof(orgName))), 0);
  32294. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  32295. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  32296. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  32297. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  32298. EVP_PKEY_free(pubKeyTmp);
  32299. X509_free(cert);
  32300. #endif
  32301. return EXPECT_RESULT();
  32302. }
  32303. static int test_wolfSSL_CTX_set_srp_username(void)
  32304. {
  32305. EXPECT_DECLS;
  32306. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  32307. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32308. WOLFSSL_CTX* ctx = NULL;
  32309. WOLFSSL* ssl = NULL;
  32310. const char *username = "TESTUSER";
  32311. const char *password = "TESTPASSWORD";
  32312. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32313. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32314. SSL_SUCCESS);
  32315. wolfSSL_CTX_free(ctx);
  32316. ctx = NULL;
  32317. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32318. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32319. SSL_SUCCESS);
  32320. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32321. SSL_SUCCESS);
  32322. ExpectNotNull(ssl = SSL_new(ctx));
  32323. ExpectNotNull(SSL_get_srp_username(ssl));
  32324. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  32325. wolfSSL_free(ssl);
  32326. wolfSSL_CTX_free(ctx);
  32327. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32328. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32329. return EXPECT_RESULT();
  32330. }
  32331. static int test_wolfSSL_CTX_set_srp_password(void)
  32332. {
  32333. EXPECT_DECLS;
  32334. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  32335. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  32336. WOLFSSL_CTX* ctx = NULL;
  32337. const char *username = "TESTUSER";
  32338. const char *password = "TESTPASSWORD";
  32339. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32340. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32341. SSL_SUCCESS);
  32342. wolfSSL_CTX_free(ctx);
  32343. ctx = NULL;
  32344. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  32345. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  32346. SSL_SUCCESS);
  32347. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  32348. SSL_SUCCESS);
  32349. wolfSSL_CTX_free(ctx);
  32350. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  32351. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  32352. return EXPECT_RESULT();
  32353. }
  32354. static int test_wolfSSL_X509_STORE(void)
  32355. {
  32356. EXPECT_DECLS;
  32357. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32358. X509_STORE *store = NULL;
  32359. #ifdef HAVE_CRL
  32360. X509_STORE_CTX *storeCtx = NULL;
  32361. X509_CRL *crl = NULL;
  32362. X509 *ca = NULL;
  32363. X509 *cert = NULL;
  32364. const char crlPem[] = "./certs/crl/crl.revoked";
  32365. const char srvCert[] = "./certs/server-revoked-cert.pem";
  32366. const char caCert[] = "./certs/ca-cert.pem";
  32367. XFILE fp = XBADFILE;
  32368. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32369. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32370. SSL_FILETYPE_PEM)));
  32371. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32372. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32373. SSL_FILETYPE_PEM)));
  32374. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32375. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32376. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32377. X509_STORE_free(store);
  32378. store = NULL;
  32379. X509_STORE_CTX_free(storeCtx);
  32380. storeCtx = NULL;
  32381. X509_free(cert);
  32382. cert = NULL;
  32383. X509_free(ca);
  32384. ca = NULL;
  32385. /* should fail to verify now after adding in CRL */
  32386. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32387. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  32388. SSL_FILETYPE_PEM)));
  32389. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  32390. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  32391. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  32392. NULL, NULL));
  32393. if (fp != XBADFILE)
  32394. XFCLOSE(fp);
  32395. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  32396. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  32397. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  32398. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  32399. SSL_FILETYPE_PEM)));
  32400. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  32401. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  32402. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  32403. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  32404. X509_CRL_free(crl);
  32405. crl = NULL;
  32406. X509_STORE_free(store);
  32407. store = NULL;
  32408. X509_STORE_CTX_free(storeCtx);
  32409. storeCtx = NULL;
  32410. X509_free(cert);
  32411. cert = NULL;
  32412. X509_free(ca);
  32413. ca = NULL;
  32414. #endif /* HAVE_CRL */
  32415. #ifndef WOLFCRYPT_ONLY
  32416. {
  32417. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32418. SSL_CTX* ctx = NULL;
  32419. SSL* ssl = NULL;
  32420. int i;
  32421. for (i = 0; i < 2; i++) {
  32422. #ifndef NO_WOLFSSL_SERVER
  32423. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32424. #else
  32425. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32426. #endif
  32427. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32428. SSL_CTX_set_cert_store(ctx, store);
  32429. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32430. SSL_CTX_set_cert_store(ctx, store);
  32431. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  32432. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32433. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32434. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32435. SSL_FILETYPE_PEM), SSL_SUCCESS);
  32436. ExpectNotNull(ssl = SSL_new(ctx));
  32437. if (i == 0) {
  32438. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  32439. SSL_SUCCESS);
  32440. }
  32441. else {
  32442. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  32443. #ifdef OPENSSL_ALL
  32444. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  32445. #endif
  32446. }
  32447. if (EXPECT_FAIL() || (i == 1)) {
  32448. X509_STORE_free(store);
  32449. store = NULL;
  32450. }
  32451. SSL_free(ssl);
  32452. ssl = NULL;
  32453. SSL_CTX_free(ctx);
  32454. ctx = NULL;
  32455. }
  32456. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32457. }
  32458. #endif
  32459. #endif
  32460. return EXPECT_RESULT();
  32461. }
  32462. static int test_wolfSSL_X509_STORE_load_locations(void)
  32463. {
  32464. EXPECT_DECLS;
  32465. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32466. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32467. SSL_CTX *ctx = NULL;
  32468. X509_STORE *store = NULL;
  32469. const char ca_file[] = "./certs/ca-cert.pem";
  32470. const char client_pem_file[] = "./certs/client-cert.pem";
  32471. const char client_der_file[] = "./certs/client-cert.der";
  32472. const char ecc_file[] = "./certs/ecc-key.pem";
  32473. const char certs_path[] = "./certs/";
  32474. const char bad_path[] = "./bad-path/";
  32475. #ifdef HAVE_CRL
  32476. const char crl_path[] = "./certs/crl/";
  32477. const char crl_file[] = "./certs/crl/crl.pem";
  32478. #endif
  32479. #ifndef NO_WOLFSSL_SERVER
  32480. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32481. #else
  32482. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32483. #endif
  32484. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32485. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  32486. WOLFSSL_SUCCESS);
  32487. /* Test bad arguments */
  32488. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  32489. WOLFSSL_FAILURE);
  32490. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  32491. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  32492. WOLFSSL_FAILURE);
  32493. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  32494. WOLFSSL_FAILURE);
  32495. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  32496. WOLFSSL_FAILURE);
  32497. #ifdef HAVE_CRL
  32498. /* Test with CRL */
  32499. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  32500. WOLFSSL_SUCCESS);
  32501. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  32502. WOLFSSL_SUCCESS);
  32503. #endif
  32504. /* Test with CA */
  32505. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  32506. WOLFSSL_SUCCESS);
  32507. /* Test with client_cert and certs path */
  32508. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  32509. WOLFSSL_SUCCESS);
  32510. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  32511. WOLFSSL_SUCCESS);
  32512. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  32513. /* Clear nodes */
  32514. ERR_clear_error();
  32515. #endif
  32516. SSL_CTX_free(ctx);
  32517. #endif
  32518. return EXPECT_RESULT();
  32519. }
  32520. static int test_X509_STORE_get0_objects(void)
  32521. {
  32522. EXPECT_DECLS;
  32523. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  32524. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  32525. X509_STORE *store = NULL;
  32526. X509_STORE *store_cpy = NULL;
  32527. SSL_CTX *ctx = NULL;
  32528. X509_OBJECT *obj = NULL;
  32529. STACK_OF(X509_OBJECT) *objs = NULL;
  32530. int i;
  32531. /* Setup store */
  32532. #ifndef NO_WOLFSSL_SERVER
  32533. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  32534. #else
  32535. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  32536. #endif
  32537. ExpectNotNull(store_cpy = X509_STORE_new());
  32538. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  32539. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  32540. WOLFSSL_SUCCESS);
  32541. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  32542. WOLFSSL_SUCCESS);
  32543. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  32544. WOLFSSL_SUCCESS);
  32545. #ifdef HAVE_CRL
  32546. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  32547. WOLFSSL_SUCCESS);
  32548. #endif
  32549. /* Store ready */
  32550. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  32551. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  32552. #ifdef HAVE_CRL
  32553. #ifdef WOLFSSL_SIGNER_DER_CERT
  32554. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  32555. #else
  32556. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  32557. #endif
  32558. #else
  32559. #ifdef WOLFSSL_SIGNER_DER_CERT
  32560. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  32561. #else
  32562. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  32563. #endif
  32564. #endif
  32565. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  32566. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  32567. switch (X509_OBJECT_get_type(obj)) {
  32568. case X509_LU_X509:
  32569. {
  32570. WOLFSSL_X509* x509;
  32571. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  32572. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  32573. break;
  32574. }
  32575. case X509_LU_CRL:
  32576. #ifdef HAVE_CRL
  32577. {
  32578. WOLFSSL_CRL* crl = NULL;
  32579. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  32580. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  32581. break;
  32582. }
  32583. #endif
  32584. case X509_LU_NONE:
  32585. default:
  32586. Fail(("X509_OBJECT_get_type should return x509 or crl "
  32587. "(when built with crl support)"),
  32588. ("Unrecognized X509_OBJECT type or none"));
  32589. }
  32590. }
  32591. X509_STORE_free(store_cpy);
  32592. SSL_CTX_free(ctx);
  32593. #endif
  32594. return EXPECT_RESULT();
  32595. }
  32596. static int test_wolfSSL_BN_CTX(void)
  32597. {
  32598. EXPECT_DECLS;
  32599. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32600. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32601. WOLFSSL_BN_CTX* bn_ctx = NULL;
  32602. WOLFSSL_BIGNUM* t = NULL;
  32603. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  32604. /* No implementation. */
  32605. BN_CTX_init(NULL);
  32606. ExpectNotNull(t = BN_CTX_get(NULL));
  32607. BN_free(t);
  32608. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  32609. BN_free(t);
  32610. #ifndef NO_WOLFSSL_STUB
  32611. /* No implementation. */
  32612. BN_CTX_start(NULL);
  32613. BN_CTX_start(bn_ctx);
  32614. #endif
  32615. BN_CTX_free(NULL);
  32616. BN_CTX_free(bn_ctx);
  32617. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32618. return EXPECT_RESULT();
  32619. }
  32620. static int test_wolfSSL_BN(void)
  32621. {
  32622. EXPECT_DECLS;
  32623. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32624. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32625. BIGNUM* a = NULL;
  32626. BIGNUM* b = NULL;
  32627. BIGNUM* c = NULL;
  32628. BIGNUM* d = NULL;
  32629. BIGNUM emptyBN;
  32630. /* Setup */
  32631. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32632. /* internal not set emptyBN. */
  32633. ExpectNotNull(a = BN_new());
  32634. ExpectNotNull(b = BN_new());
  32635. ExpectNotNull(c = BN_dup(b));
  32636. ExpectNotNull(d = BN_new());
  32637. /* Invalid parameter testing. */
  32638. BN_free(NULL);
  32639. ExpectNull(BN_dup(NULL));
  32640. ExpectNull(BN_dup(&emptyBN));
  32641. ExpectNull(BN_copy(NULL, NULL));
  32642. ExpectNull(BN_copy(b, NULL));
  32643. ExpectNull(BN_copy(NULL, c));
  32644. ExpectNull(BN_copy(b, &emptyBN));
  32645. ExpectNull(BN_copy(&emptyBN, c));
  32646. BN_clear(NULL);
  32647. BN_clear(&emptyBN);
  32648. ExpectIntEQ(BN_num_bytes(NULL), 0);
  32649. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  32650. ExpectIntEQ(BN_num_bits(NULL), 0);
  32651. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  32652. ExpectIntEQ(BN_is_negative(NULL), 0);
  32653. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  32654. /* END Invalid Parameters */
  32655. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  32656. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  32657. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  32658. ExpectIntEQ(BN_num_bits(a), 2);
  32659. ExpectIntEQ(BN_num_bytes(a), 1);
  32660. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  32661. defined(WOLFSSL_SP_INT_NEGATIVE))
  32662. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  32663. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  32664. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  32665. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  32666. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  32667. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32668. {
  32669. /* Do additional tests on negative BN conversions. */
  32670. char* ret = NULL;
  32671. ASN1_INTEGER* asn1 = NULL;
  32672. BIGNUM* tmp = NULL;
  32673. /* Sanity check we have a negative BN. */
  32674. ExpectIntEQ(BN_is_negative(c), 1);
  32675. ExpectNotNull(ret = BN_bn2dec(c));
  32676. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32677. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32678. ret = NULL;
  32679. /* Convert to ASN1_INTEGER and back to BN. */
  32680. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  32681. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  32682. /* After converting back BN should be negative and correct. */
  32683. ExpectIntEQ(BN_is_negative(tmp), 1);
  32684. ExpectNotNull(ret = BN_bn2dec(tmp));
  32685. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  32686. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  32687. ASN1_INTEGER_free(asn1);
  32688. BN_free(tmp);
  32689. }
  32690. #endif
  32691. ExpectIntEQ(BN_get_word(c), 4);
  32692. #endif
  32693. ExpectIntEQ(BN_set_word(a, 3), 1);
  32694. ExpectIntEQ(BN_set_word(b, 3), 1);
  32695. ExpectIntEQ(BN_set_word(c, 4), 1);
  32696. /* NULL == NULL, NULL < num, num > NULL */
  32697. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  32698. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  32699. ExpectIntLT(BN_cmp(NULL, b), 0);
  32700. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  32701. ExpectIntGT(BN_cmp(a, NULL), 0);
  32702. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  32703. ExpectIntEQ(BN_cmp(a, b), 0);
  32704. ExpectIntLT(BN_cmp(a, c), 0);
  32705. ExpectIntGT(BN_cmp(c, b), 0);
  32706. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  32707. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  32708. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  32709. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  32710. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  32711. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  32712. BN_clear(a);
  32713. BN_free(a);
  32714. BN_free(b);
  32715. BN_free(c);
  32716. BN_clear_free(d);
  32717. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32718. return EXPECT_RESULT();
  32719. }
  32720. static int test_wolfSSL_BN_init(void)
  32721. {
  32722. EXPECT_DECLS;
  32723. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32724. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32725. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  32726. BIGNUM* ap = NULL;
  32727. BIGNUM bv;
  32728. BIGNUM cv;
  32729. BIGNUM dv;
  32730. ExpectNotNull(ap = BN_new());
  32731. BN_init(NULL);
  32732. XMEMSET(&bv, 0, sizeof(bv));
  32733. ExpectNull(BN_dup(&bv));
  32734. BN_init(&bv);
  32735. BN_init(&cv);
  32736. BN_init(&dv);
  32737. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  32738. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  32739. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  32740. /* a^b mod c = */
  32741. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  32742. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  32743. /* check result 3^2 mod 5 */
  32744. ExpectIntEQ(BN_get_word(&dv), 4);
  32745. /* a*b mod c = */
  32746. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  32747. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  32748. /* check result 3*2 mod 5 */
  32749. ExpectIntEQ(BN_get_word(&dv), 1);
  32750. BN_free(ap);
  32751. #endif
  32752. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32753. return EXPECT_RESULT();
  32754. }
  32755. static int test_wolfSSL_BN_enc_dec(void)
  32756. {
  32757. EXPECT_DECLS;
  32758. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32759. BIGNUM* a = NULL;
  32760. BIGNUM* b = NULL;
  32761. BIGNUM* c = NULL;
  32762. BIGNUM emptyBN;
  32763. char* str = NULL;
  32764. const char* emptyStr = "";
  32765. const char* numberStr = "12345";
  32766. const char* badStr = "g12345";
  32767. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32768. const char* twoStr = "2";
  32769. #endif
  32770. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  32771. unsigned char outNum[5];
  32772. /* Setup */
  32773. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32774. ExpectNotNull(a = BN_new());
  32775. ExpectNotNull(b = BN_new());
  32776. ExpectIntEQ(BN_set_word(a, 2), 1);
  32777. /* Invalid parameters */
  32778. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  32779. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  32780. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  32781. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  32782. ExpectNull(BN_bn2hex(NULL));
  32783. ExpectNull(BN_bn2hex(&emptyBN));
  32784. ExpectNull(BN_bn2dec(NULL));
  32785. ExpectNull(BN_bn2dec(&emptyBN));
  32786. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  32787. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  32788. ExpectNull(BN_bin2bn(binNum, -1, a));
  32789. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  32790. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  32791. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  32792. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  32793. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  32794. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  32795. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  32796. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  32797. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  32798. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  32799. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  32800. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  32801. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  32802. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  32803. ExpectIntEQ(BN_set_word(a, 2), 1);
  32804. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  32805. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  32806. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  32807. ExpectIntEQ(BN_cmp(a, b), 0);
  32808. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  32809. ExpectIntEQ(BN_cmp(a, b), -1);
  32810. ExpectNotNull(str = BN_bn2hex(a));
  32811. ExpectNotNull(BN_hex2bn(&b, str));
  32812. ExpectIntEQ(BN_cmp(a, b), 0);
  32813. ExpectNotNull(BN_hex2bn(&b, numberStr));
  32814. ExpectIntEQ(BN_cmp(a, b), -1);
  32815. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32816. str = NULL;
  32817. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32818. ExpectNotNull(str = BN_bn2dec(a));
  32819. ExpectStrEQ(str, twoStr);
  32820. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32821. str = NULL;
  32822. #ifndef NO_RSA
  32823. ExpectNotNull(str = BN_bn2dec(a));
  32824. ExpectNotNull(BN_dec2bn(&b, str));
  32825. ExpectIntEQ(BN_cmp(a, b), 0);
  32826. ExpectNotNull(BN_dec2bn(&b, numberStr));
  32827. ExpectIntEQ(BN_cmp(a, b), -1);
  32828. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  32829. str = NULL;
  32830. #else
  32831. /* No implementation - fail with good parameters. */
  32832. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  32833. #endif
  32834. #endif
  32835. BN_free(b);
  32836. BN_free(a);
  32837. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32838. return EXPECT_RESULT();
  32839. }
  32840. static int test_wolfSSL_BN_word(void)
  32841. {
  32842. EXPECT_DECLS;
  32843. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  32844. BIGNUM* a = NULL;
  32845. BIGNUM* b = NULL;
  32846. BIGNUM* c = NULL;
  32847. BIGNUM av;
  32848. ExpectNotNull(a = BN_new());
  32849. ExpectNotNull(b = BN_new());
  32850. ExpectNotNull(c = BN_new());
  32851. XMEMSET(&av, 0, sizeof(av));
  32852. /* Invalid parameter. */
  32853. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  32854. ExpectIntEQ(BN_add_word(&av, 3), 0);
  32855. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  32856. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  32857. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  32858. ExpectIntEQ(BN_set_word(&av, 3), 0);
  32859. ExpectIntEQ(BN_get_word(NULL), 0);
  32860. ExpectIntEQ(BN_get_word(&av), 0);
  32861. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  32862. ExpectIntEQ(BN_is_word(&av, 3), 0);
  32863. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  32864. !defined(NO_DSA))
  32865. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  32866. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  32867. #endif
  32868. ExpectIntEQ(BN_one(NULL), 0);
  32869. ExpectIntEQ(BN_one(&av), 0);
  32870. BN_zero(NULL);
  32871. BN_zero(&av);
  32872. ExpectIntEQ(BN_is_one(NULL), 0);
  32873. ExpectIntEQ(BN_is_one(&av), 0);
  32874. ExpectIntEQ(BN_is_zero(NULL), 0);
  32875. ExpectIntEQ(BN_is_zero(&av), 0);
  32876. ExpectIntEQ(BN_set_word(a, 3), 1);
  32877. ExpectIntEQ(BN_set_word(b, 2), 1);
  32878. ExpectIntEQ(BN_set_word(c, 5), 1);
  32879. /* a + 3 = */
  32880. ExpectIntEQ(BN_add_word(a, 3), 1);
  32881. /* check result 3 + 3*/
  32882. ExpectIntEQ(BN_get_word(a), 6);
  32883. ExpectIntEQ(BN_is_word(a, 6), 1);
  32884. ExpectIntEQ(BN_is_word(a, 5), 0);
  32885. /* set a back to 3 */
  32886. ExpectIntEQ(BN_set_word(a, 3), 1);
  32887. /* a - 3 = */
  32888. ExpectIntEQ(BN_sub_word(a, 3), 1);
  32889. /* check result 3 - 3*/
  32890. ExpectIntEQ(BN_get_word(a), 0);
  32891. ExpectIntEQ(BN_one(a), 1);
  32892. ExpectIntEQ(BN_is_word(a, 1), 1);
  32893. ExpectIntEQ(BN_is_word(a, 0), 0);
  32894. ExpectIntEQ(BN_is_one(a), 1);
  32895. ExpectIntEQ(BN_is_zero(a), 0);
  32896. BN_zero(a);
  32897. ExpectIntEQ(BN_is_word(a, 0), 1);
  32898. ExpectIntEQ(BN_is_word(a, 1), 0);
  32899. ExpectIntEQ(BN_is_zero(a), 1);
  32900. ExpectIntEQ(BN_is_one(a), 0);
  32901. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  32902. !defined(NO_DSA))
  32903. ExpectIntEQ(BN_set_word(a, 5), 1);
  32904. ExpectIntEQ(BN_mod_word(a, 3), 2);
  32905. ExpectIntEQ(BN_mod_word(a, 0), -1);
  32906. #endif
  32907. BN_free(c);
  32908. BN_free(b);
  32909. BN_free(a);
  32910. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  32911. return EXPECT_RESULT();
  32912. }
  32913. static int test_wolfSSL_BN_bits(void)
  32914. {
  32915. EXPECT_DECLS;
  32916. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32917. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32918. BIGNUM* a = NULL;
  32919. BIGNUM emptyBN;
  32920. /* Setup */
  32921. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32922. ExpectNotNull(a = BN_new());
  32923. /* Invalid parameters. */
  32924. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  32925. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  32926. ExpectIntEQ(BN_set_bit(a, -1), 0);
  32927. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  32928. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  32929. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  32930. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  32931. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  32932. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  32933. ExpectIntEQ(BN_is_odd(NULL), 0);
  32934. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  32935. ExpectIntEQ(BN_set_word(a, 0), 1);
  32936. ExpectIntEQ(BN_is_zero(a), 1);
  32937. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  32938. ExpectIntEQ(BN_is_zero(a), 0);
  32939. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  32940. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  32941. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  32942. ExpectIntEQ(BN_is_zero(a), 1);
  32943. ExpectIntEQ(BN_set_bit(a, 0), 1);
  32944. ExpectIntEQ(BN_is_odd(a), 1);
  32945. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  32946. ExpectIntEQ(BN_is_odd(a), 0);
  32947. ExpectIntEQ(BN_set_bit(a, 1), 1);
  32948. ExpectIntEQ(BN_is_odd(a), 0);
  32949. ExpectIntEQ(BN_set_bit(a, 129), 1);
  32950. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  32951. #ifndef NO_WOLFSSL_STUB
  32952. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  32953. #endif
  32954. BN_free(a);
  32955. #endif
  32956. return EXPECT_RESULT();
  32957. }
  32958. static int test_wolfSSL_BN_shift(void)
  32959. {
  32960. EXPECT_DECLS;
  32961. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  32962. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  32963. BIGNUM* a = NULL;
  32964. BIGNUM* b = NULL;
  32965. BIGNUM emptyBN;
  32966. /* Setup */
  32967. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  32968. ExpectNotNull(a = BN_new());
  32969. ExpectNotNull(b = BN_new());
  32970. /* Invalid parameters. */
  32971. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  32972. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  32973. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  32974. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  32975. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  32976. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  32977. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  32978. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  32979. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  32980. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  32981. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  32982. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  32983. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  32984. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  32985. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  32986. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  32987. ExpectIntEQ(BN_set_word(a, 1), 1);
  32988. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  32989. ExpectIntEQ(BN_is_word(b, 2), 1);
  32990. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  32991. ExpectIntEQ(BN_is_word(a, 2), 1);
  32992. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  32993. ExpectIntEQ(BN_is_word(b, 1), 1);
  32994. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  32995. ExpectIntEQ(BN_is_word(a, 1), 1);
  32996. BN_free(b);
  32997. BN_free(a);
  32998. #endif
  32999. return EXPECT_RESULT();
  33000. }
  33001. static int test_wolfSSL_BN_math(void)
  33002. {
  33003. EXPECT_DECLS;
  33004. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33005. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33006. BIGNUM* a = NULL;
  33007. BIGNUM* b = NULL;
  33008. BIGNUM* r = NULL;
  33009. BIGNUM* rem = NULL;
  33010. BIGNUM emptyBN;
  33011. BN_ULONG val1;
  33012. BN_ULONG val2;
  33013. /* Setup */
  33014. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33015. ExpectNotNull(a = BN_new());
  33016. ExpectNotNull(b = BN_new());
  33017. ExpectNotNull(r = BN_new());
  33018. ExpectNotNull(rem = BN_new());
  33019. /* Invalid parameters. */
  33020. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  33021. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  33022. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  33023. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  33024. ExpectIntEQ(BN_add(r, a, NULL), 0);
  33025. ExpectIntEQ(BN_add(r, NULL, b), 0);
  33026. ExpectIntEQ(BN_add(NULL, a, b), 0);
  33027. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  33028. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  33029. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  33030. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  33031. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  33032. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  33033. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  33034. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  33035. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  33036. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  33037. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  33038. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  33039. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  33040. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  33041. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  33042. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  33043. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  33044. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  33045. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  33046. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  33047. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  33048. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  33049. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  33050. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  33051. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  33052. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  33053. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  33054. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  33055. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33056. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  33057. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  33058. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  33059. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  33060. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  33061. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  33062. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  33063. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  33064. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  33065. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  33066. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  33067. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  33068. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  33069. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  33070. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  33071. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33072. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33073. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  33074. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  33075. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  33076. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  33077. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  33078. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  33079. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  33080. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  33081. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  33082. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  33083. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  33084. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  33085. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  33086. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  33087. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33088. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  33089. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  33090. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  33091. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  33092. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  33093. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  33094. /* END Invalid parameters. */
  33095. val1 = 8;
  33096. val2 = 3;
  33097. ExpectIntEQ(BN_set_word(a, val1), 1);
  33098. ExpectIntEQ(BN_set_word(b, val2), 1);
  33099. ExpectIntEQ(BN_add(r, a, b), 1);
  33100. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  33101. ExpectIntEQ(BN_sub(r, a, b), 1);
  33102. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  33103. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  33104. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  33105. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  33106. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  33107. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  33108. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  33109. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  33110. BN_free(rem);
  33111. BN_free(r);
  33112. BN_free(b);
  33113. BN_free(a);
  33114. #endif
  33115. return EXPECT_RESULT();
  33116. }
  33117. static int test_wolfSSL_BN_math_mod(void)
  33118. {
  33119. EXPECT_DECLS;
  33120. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33121. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33122. BIGNUM* a = NULL;
  33123. BIGNUM* b = NULL;
  33124. BIGNUM* m = NULL;
  33125. BIGNUM* r = NULL;
  33126. BIGNUM* t = NULL;
  33127. BIGNUM emptyBN;
  33128. BN_ULONG val1;
  33129. BN_ULONG val2;
  33130. BN_ULONG val3;
  33131. /* Setup */
  33132. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33133. ExpectNotNull(a = BN_new());
  33134. ExpectNotNull(b = BN_new());
  33135. ExpectNotNull(m = BN_new());
  33136. ExpectNotNull(r = BN_new());
  33137. /* Invalid parameters. */
  33138. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  33139. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  33140. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  33141. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  33142. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  33143. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  33144. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  33145. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  33146. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  33147. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33148. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33149. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33150. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33151. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33152. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  33153. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  33154. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  33155. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  33156. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  33157. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  33158. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  33159. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  33160. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  33161. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  33162. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  33163. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  33164. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  33165. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33166. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33167. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33168. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33169. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33170. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  33171. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  33172. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  33173. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  33174. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  33175. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  33176. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  33177. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  33178. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  33179. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  33180. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  33181. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  33182. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  33183. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33184. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33185. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  33186. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  33187. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  33188. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  33189. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  33190. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  33191. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  33192. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  33193. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33194. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33195. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  33196. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  33197. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  33198. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  33199. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  33200. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  33201. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  33202. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  33203. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  33204. /* END Invalid parameters. */
  33205. val1 = 9;
  33206. val2 = 13;
  33207. val3 = 5;
  33208. ExpectIntEQ(BN_set_word(a, val1), 1);
  33209. ExpectIntEQ(BN_set_word(b, val2), 1);
  33210. ExpectIntEQ(BN_set_word(m, val3), 1);
  33211. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  33212. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  33213. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  33214. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  33215. ExpectIntEQ(BN_set_word(a, 2), 1);
  33216. ExpectIntEQ(BN_set_word(b, 3), 1);
  33217. ExpectIntEQ(BN_set_word(m, 5), 1);
  33218. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  33219. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  33220. ExpectIntEQ(BN_is_word(r, 3), 1);
  33221. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  33222. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  33223. ExpectIntEQ(BN_is_word(r, 3), 1);
  33224. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  33225. ExpectIntEQ(BN_is_word(t, 3), 1);
  33226. BN_free(t);
  33227. /* No inverse case. No inverse when a divides b. */
  33228. ExpectIntEQ(BN_set_word(a, 3), 1);
  33229. ExpectIntEQ(BN_set_word(m, 9), 1);
  33230. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  33231. BN_free(r);
  33232. BN_free(m);
  33233. BN_free(b);
  33234. BN_free(a);
  33235. #endif
  33236. return EXPECT_RESULT();
  33237. }
  33238. static int test_wolfSSL_BN_math_other(void)
  33239. {
  33240. EXPECT_DECLS;
  33241. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33242. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33243. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  33244. BIGNUM* a = NULL;
  33245. BIGNUM* b = NULL;
  33246. BIGNUM* r = NULL;
  33247. BIGNUM emptyBN;
  33248. /* Setup */
  33249. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33250. ExpectNotNull(a = BN_new());
  33251. ExpectNotNull(b = BN_new());
  33252. ExpectNotNull(r = BN_new());
  33253. /* Invalid parameters. */
  33254. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  33255. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  33256. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  33257. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  33258. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  33259. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  33260. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  33261. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  33262. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  33263. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  33264. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  33265. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  33266. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  33267. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  33268. /* END Invalid parameters. */
  33269. /* No common factors between 2 and 3. */
  33270. ExpectIntEQ(BN_set_word(a, 2), 1);
  33271. ExpectIntEQ(BN_set_word(b, 3), 1);
  33272. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33273. ExpectIntEQ(BN_is_word(r, 1), 1);
  33274. /* 3 is largest value that divides both 6 and 9. */
  33275. ExpectIntEQ(BN_set_word(a, 6), 1);
  33276. ExpectIntEQ(BN_set_word(b, 9), 1);
  33277. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  33278. ExpectIntEQ(BN_is_word(r, 3), 1);
  33279. /* GCD of 0 and 0 is undefined. */
  33280. ExpectIntEQ(BN_set_word(a, 0), 1);
  33281. ExpectIntEQ(BN_set_word(b, 0), 1);
  33282. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  33283. /* Teardown */
  33284. BN_free(r);
  33285. BN_free(b);
  33286. BN_free(a);
  33287. #endif
  33288. #endif
  33289. return EXPECT_RESULT();
  33290. }
  33291. static int test_wolfSSL_BN_rand(void)
  33292. {
  33293. EXPECT_DECLS;
  33294. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  33295. BIGNUM* bn = NULL;
  33296. BIGNUM* range = NULL;
  33297. BIGNUM emptyBN;
  33298. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33299. ExpectNotNull(bn = BN_new());
  33300. ExpectNotNull(range = BN_new());
  33301. /* Invalid parameters. */
  33302. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  33303. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33304. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  33305. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  33306. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  33307. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  33308. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  33309. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33310. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  33311. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  33312. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  33313. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  33314. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  33315. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  33316. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  33317. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  33318. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  33319. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  33320. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  33321. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33322. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33323. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33324. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33325. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33326. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33327. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33328. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33329. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33330. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33331. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33332. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33333. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33334. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33335. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33336. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33337. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  33338. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33339. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  33340. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33341. /* 1 bit random value must have no more than one top bit set. */
  33342. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33343. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33344. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33345. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33346. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33347. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  33348. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  33349. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  33350. /* END Invalid parameters. */
  33351. /* 0 bit random: 0. */
  33352. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33353. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33354. ExpectIntEQ(BN_is_zero(bn), 1);
  33355. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  33356. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  33357. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33358. ExpectIntEQ(BN_is_zero(bn), 1);
  33359. /* 1 bit random: 0 or 1. */
  33360. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33361. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33362. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33363. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33364. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33365. ExpectIntEQ(BN_get_word(bn), 1);
  33366. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33367. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33368. ExpectIntEQ(BN_get_word(bn), 1);
  33369. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  33370. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33371. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  33372. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33373. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33374. ExpectIntEQ(BN_get_word(bn), 1);
  33375. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  33376. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33377. ExpectIntEQ(BN_get_word(bn), 1);
  33378. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33379. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33380. ExpectIntEQ(BN_num_bits(bn), 8);
  33381. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33382. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33383. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33384. ExpectIntEQ(BN_num_bits(bn), 8);
  33385. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33386. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33387. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33388. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33389. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33390. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  33391. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33392. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  33393. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  33394. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33395. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33396. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33397. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  33398. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  33399. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  33400. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33401. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33402. * case, requesting a 13-bit random number would actually return a 16-bit
  33403. * random number. */
  33404. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  33405. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33406. ExpectIntEQ(BN_num_bits(bn), 13);
  33407. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  33408. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  33409. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33410. ExpectIntEQ(BN_set_word(range, 0), 1);
  33411. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33412. ExpectIntEQ(BN_set_word(range, 1), 1);
  33413. ExpectIntEQ(BN_rand_range(bn, range), 1);
  33414. BN_free(bn);
  33415. BN_free(range);
  33416. #endif
  33417. return EXPECT_RESULT();
  33418. }
  33419. static int test_wolfSSL_BN_prime(void)
  33420. {
  33421. EXPECT_DECLS;
  33422. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  33423. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  33424. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  33425. BIGNUM* a = NULL;
  33426. BIGNUM* add = NULL;
  33427. BIGNUM* rem = NULL;
  33428. BIGNUM emptyBN;
  33429. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  33430. ExpectNotNull(a = BN_new());
  33431. ExpectNotNull(add = BN_new());
  33432. ExpectNotNull(rem = BN_new());
  33433. /* Invalid parameters. */
  33434. /* BN_generate_prime_ex()
  33435. * prime - must have valid BIGNUM
  33436. * bits - Greater then 0
  33437. * safe - not supported, must be 0
  33438. * add - not supported, must be NULL
  33439. * rem - not supported, must be NULL
  33440. * cb - anything
  33441. */
  33442. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  33443. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  33444. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  33445. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  33446. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  33447. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  33448. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  33449. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  33450. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  33451. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  33452. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  33453. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  33454. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  33455. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  33456. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  33457. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  33458. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  33459. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  33460. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  33461. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  33462. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  33463. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  33464. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  33465. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  33466. /* END Invalid parameters. */
  33467. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  33468. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  33469. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  33470. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  33471. BN_free(rem);
  33472. BN_free(add);
  33473. BN_free(a);
  33474. #endif
  33475. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  33476. return EXPECT_RESULT();
  33477. }
  33478. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33479. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33480. #define TEST_ARG 0x1234
  33481. static void msg_cb(int write_p, int version, int content_type,
  33482. const void *buf, size_t len, SSL *ssl, void *arg)
  33483. {
  33484. (void)write_p;
  33485. (void)version;
  33486. (void)content_type;
  33487. (void)buf;
  33488. (void)len;
  33489. (void)ssl;
  33490. AssertTrue(arg == (void*)TEST_ARG);
  33491. }
  33492. #endif
  33493. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33494. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33495. #if defined(SESSION_CERTS)
  33496. #include "wolfssl/internal.h"
  33497. #endif
  33498. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  33499. {
  33500. EXPECT_DECLS;
  33501. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33502. STACK_OF(X509)* sk = NULL;
  33503. X509* x509 = NULL;
  33504. int i, num;
  33505. BIO* bio = NULL;
  33506. #endif
  33507. ExpectNotNull(ctx);
  33508. ExpectNotNull(ssl);
  33509. fprintf(stderr, "\n===== msgcb called ====\n");
  33510. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  33511. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  33512. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  33513. ExpectNotNull(SSL_get0_verified_chain(ssl));
  33514. #endif
  33515. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  33516. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  33517. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  33518. if (sk == NULL) {
  33519. BIO_free(bio);
  33520. return TEST_FAIL;
  33521. }
  33522. num = sk_X509_num(sk);
  33523. ExpectTrue(num > 0);
  33524. for (i = 0; i < num; i++) {
  33525. ExpectNotNull(x509 = sk_X509_value(sk,i));
  33526. if (x509 == NULL)
  33527. break;
  33528. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  33529. X509_print(bio,x509);
  33530. fprintf(stderr, "\n\n");
  33531. }
  33532. BIO_free(bio);
  33533. #endif
  33534. return EXPECT_RESULT();
  33535. }
  33536. #endif
  33537. static int test_wolfSSL_msgCb(void)
  33538. {
  33539. EXPECT_DECLS;
  33540. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  33541. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33542. test_ssl_cbf client_cb;
  33543. test_ssl_cbf server_cb;
  33544. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33545. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33546. #ifndef WOLFSSL_NO_TLS12
  33547. client_cb.method = wolfTLSv1_2_client_method;
  33548. server_cb.method = wolfTLSv1_2_server_method;
  33549. #else
  33550. client_cb.method = wolfTLSv1_3_client_method;
  33551. server_cb.method = wolfTLSv1_3_server_method;
  33552. #endif
  33553. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33554. &server_cb, msgCb), TEST_SUCCESS);
  33555. #endif
  33556. return EXPECT_RESULT();
  33557. }
  33558. static int test_wolfSSL_either_side(void)
  33559. {
  33560. EXPECT_DECLS;
  33561. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33562. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33563. test_ssl_cbf client_cb;
  33564. test_ssl_cbf server_cb;
  33565. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33566. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33567. /* Use different CTX for client and server */
  33568. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33569. ExpectNotNull(client_cb.ctx);
  33570. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  33571. ExpectNotNull(server_cb.ctx);
  33572. /* we are responsible for free'ing WOLFSSL_CTX */
  33573. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33574. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33575. &server_cb, NULL), TEST_SUCCESS);
  33576. wolfSSL_CTX_free(client_cb.ctx);
  33577. wolfSSL_CTX_free(server_cb.ctx);
  33578. #endif
  33579. return EXPECT_RESULT();
  33580. }
  33581. static int test_wolfSSL_DTLS_either_side(void)
  33582. {
  33583. EXPECT_DECLS;
  33584. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  33585. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  33586. test_ssl_cbf client_cb;
  33587. test_ssl_cbf server_cb;
  33588. XMEMSET(&client_cb, 0, sizeof(client_cb));
  33589. XMEMSET(&server_cb, 0, sizeof(server_cb));
  33590. /* Use different CTX for client and server */
  33591. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33592. ExpectNotNull(client_cb.ctx);
  33593. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  33594. ExpectNotNull(server_cb.ctx);
  33595. /* we are responsible for free'ing WOLFSSL_CTX */
  33596. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  33597. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  33598. &server_cb, NULL), TEST_SUCCESS);
  33599. wolfSSL_CTX_free(client_cb.ctx);
  33600. wolfSSL_CTX_free(server_cb.ctx);
  33601. #endif
  33602. return EXPECT_RESULT();
  33603. }
  33604. static int test_generate_cookie(void)
  33605. {
  33606. EXPECT_DECLS;
  33607. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  33608. SSL_CTX* ctx = NULL;
  33609. SSL* ssl = NULL;
  33610. byte buf[FOURK_BUF] = {0};
  33611. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  33612. ExpectNotNull(ssl = SSL_new(ctx));
  33613. /* Test unconnected */
  33614. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  33615. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  33616. wolfSSL_SetCookieCtx(ssl, ctx);
  33617. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  33618. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  33619. SSL_free(ssl);
  33620. SSL_CTX_free(ctx);
  33621. #endif
  33622. return EXPECT_RESULT();
  33623. }
  33624. static int test_wolfSSL_set_options(void)
  33625. {
  33626. EXPECT_DECLS;
  33627. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33628. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33629. WOLFSSL* ssl = NULL;
  33630. WOLFSSL_CTX* ctx = NULL;
  33631. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33632. char appData[] = "extra msg";
  33633. #endif
  33634. #ifdef OPENSSL_EXTRA
  33635. unsigned char protos[] = {
  33636. 7, 't', 'l', 's', '/', '1', '.', '2',
  33637. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  33638. };
  33639. unsigned int len = sizeof(protos);
  33640. void *arg = (void *)TEST_ARG;
  33641. #endif
  33642. #ifndef NO_WOLFSSL_SERVER
  33643. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33644. #else
  33645. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33646. #endif
  33647. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33648. WOLFSSL_FILETYPE_PEM));
  33649. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33650. WOLFSSL_FILETYPE_PEM));
  33651. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  33652. == WOLFSSL_OP_NO_TLSv1);
  33653. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  33654. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33655. WOLFSSL_OP_NO_SSLv2)), 0);
  33656. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33657. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33658. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  33659. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33660. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33661. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33662. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  33663. WOLFSSL_OP_NO_COMPRESSION));
  33664. wolfSSL_CTX_free(ctx);
  33665. ctx = NULL;
  33666. #ifndef NO_WOLFSSL_SERVER
  33667. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33668. #else
  33669. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33670. #endif
  33671. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33672. WOLFSSL_FILETYPE_PEM));
  33673. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33674. WOLFSSL_FILETYPE_PEM));
  33675. #ifdef OPENSSL_EXTRA
  33676. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  33677. #endif
  33678. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33679. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  33680. #ifdef HAVE_EX_DATA
  33681. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  33682. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33683. if (ssl != NULL) {
  33684. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  33685. appData, sizeof(appData)), 0);
  33686. }
  33687. #else
  33688. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  33689. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  33690. #endif
  33691. #endif
  33692. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  33693. WOLFSSL_OP_NO_TLSv1);
  33694. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  33695. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  33696. WOLFSSL_OP_NO_SSLv2)), 0);
  33697. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  33698. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  33699. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  33700. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  33701. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33702. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  33703. #ifdef OPENSSL_EXTRA
  33704. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  33705. WOLFSSL_OP_NO_COMPRESSION));
  33706. #endif
  33707. #ifdef OPENSSL_EXTRA
  33708. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  33709. wolfSSL_set_msg_callback_arg(ssl, arg);
  33710. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33711. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  33712. #else
  33713. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  33714. #endif
  33715. #endif
  33716. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  33717. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  33718. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  33719. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  33720. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33721. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  33722. #else
  33723. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  33724. #endif
  33725. #endif /* HAVE_ALPN && !NO_BIO */
  33726. #endif
  33727. wolfSSL_free(ssl);
  33728. wolfSSL_CTX_free(ctx);
  33729. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33730. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33731. return EXPECT_RESULT();
  33732. }
  33733. static int test_wolfSSL_sk_SSL_CIPHER(void)
  33734. {
  33735. EXPECT_DECLS;
  33736. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  33737. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33738. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33739. SSL* ssl = NULL;
  33740. SSL_CTX* ctx = NULL;
  33741. STACK_OF(SSL_CIPHER) *sk = NULL;
  33742. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  33743. #ifndef NO_WOLFSSL_SERVER
  33744. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33745. #else
  33746. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33747. #endif
  33748. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  33749. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33750. ExpectNotNull(ssl = SSL_new(ctx));
  33751. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  33752. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  33753. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  33754. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  33755. /* error case because connection has not been established yet */
  33756. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  33757. sk_SSL_CIPHER_free(dupSk);
  33758. /* sk is pointer to internal struct that should be free'd in SSL_free */
  33759. SSL_free(ssl);
  33760. SSL_CTX_free(ctx);
  33761. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33762. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33763. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33764. return EXPECT_RESULT();
  33765. }
  33766. static int test_wolfSSL_set1_curves_list(void)
  33767. {
  33768. EXPECT_DECLS;
  33769. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  33770. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33771. SSL* ssl = NULL;
  33772. SSL_CTX* ctx = NULL;
  33773. #ifndef NO_WOLFSSL_SERVER
  33774. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33775. #else
  33776. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33777. #endif
  33778. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  33779. SSL_FILETYPE_PEM));
  33780. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  33781. ExpectNotNull(ssl = SSL_new(ctx));
  33782. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  33783. #ifdef HAVE_ECC
  33784. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  33785. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  33786. #endif
  33787. #ifdef HAVE_CURVE25519
  33788. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  33789. #else
  33790. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  33791. #endif
  33792. #ifdef HAVE_CURVE448
  33793. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  33794. #else
  33795. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  33796. #endif
  33797. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  33798. #ifdef HAVE_ECC
  33799. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  33800. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  33801. #endif
  33802. #ifdef HAVE_CURVE25519
  33803. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  33804. #else
  33805. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  33806. #endif
  33807. #ifdef HAVE_CURVE448
  33808. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  33809. #else
  33810. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  33811. #endif
  33812. SSL_free(ssl);
  33813. SSL_CTX_free(ctx);
  33814. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33815. #endif
  33816. return EXPECT_RESULT();
  33817. }
  33818. static int test_wolfSSL_set1_sigalgs_list(void)
  33819. {
  33820. EXPECT_DECLS;
  33821. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  33822. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33823. SSL* ssl = NULL;
  33824. SSL_CTX* ctx = NULL;
  33825. #ifndef NO_WOLFSSL_SERVER
  33826. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33827. #else
  33828. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33829. #endif
  33830. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33831. SSL_FILETYPE_PEM));
  33832. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33833. ExpectNotNull(ssl = SSL_new(ctx));
  33834. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33835. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  33836. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  33837. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  33838. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  33839. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  33840. #ifndef NO_RSA
  33841. #ifndef NO_SHA256
  33842. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  33843. WOLFSSL_FAILURE);
  33844. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  33845. WOLFSSL_FAILURE);
  33846. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  33847. WOLFSSL_SUCCESS);
  33848. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  33849. WOLFSSL_SUCCESS);
  33850. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  33851. WOLFSSL_FAILURE);
  33852. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  33853. WOLFSSL_FAILURE);
  33854. #ifdef WC_RSA_PSS
  33855. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  33856. WOLFSSL_SUCCESS);
  33857. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  33858. WOLFSSL_SUCCESS);
  33859. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  33860. WOLFSSL_SUCCESS);
  33861. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  33862. WOLFSSL_SUCCESS);
  33863. #endif
  33864. #ifdef WOLFSSL_SHA512
  33865. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33866. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  33867. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33868. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  33869. #elif defined(WOLFSSL_SHA384)
  33870. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33871. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  33872. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33873. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  33874. #endif
  33875. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  33876. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  33877. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  33878. WOLFSSL_FAILURE);
  33879. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  33880. WOLFSSL_FAILURE);
  33881. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  33882. WOLFSSL_FAILURE);
  33883. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  33884. WOLFSSL_FAILURE);
  33885. #endif
  33886. #endif
  33887. #ifdef HAVE_ECC
  33888. #ifndef NO_SHA256
  33889. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  33890. WOLFSSL_SUCCESS);
  33891. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  33892. WOLFSSL_SUCCESS);
  33893. #ifdef WOLFSSL_SHA512
  33894. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33895. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  33896. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33897. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  33898. #elif defined(WOLFSSL_SHA384)
  33899. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  33900. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  33901. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  33902. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  33903. #endif
  33904. #endif
  33905. #endif
  33906. #ifdef HAVE_ED25519
  33907. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  33908. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  33909. #endif
  33910. #ifdef HAVE_ED448
  33911. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  33912. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  33913. #endif
  33914. #ifndef NO_DSA
  33915. #ifndef NO_SHA256
  33916. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  33917. WOLFSSL_SUCCESS);
  33918. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  33919. WOLFSSL_SUCCESS);
  33920. #endif
  33921. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  33922. defined(WOLFSSL_ALLOW_TLS_SHA1))
  33923. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  33924. WOLFSSL_SUCCESS);
  33925. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  33926. WOLFSSL_SUCCESS);
  33927. #endif
  33928. #endif
  33929. SSL_free(ssl);
  33930. SSL_CTX_free(ctx);
  33931. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33932. #endif
  33933. return EXPECT_RESULT();
  33934. }
  33935. /* Testing wolfSSL_set_tlsext_status_type function.
  33936. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  33937. */
  33938. static int test_wolfSSL_set_tlsext_status_type(void)
  33939. {
  33940. EXPECT_DECLS;
  33941. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  33942. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  33943. SSL* ssl = NULL;
  33944. SSL_CTX* ctx = NULL;
  33945. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33946. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33947. SSL_FILETYPE_PEM));
  33948. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33949. ExpectNotNull(ssl = SSL_new(ctx));
  33950. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  33951. SSL_SUCCESS);
  33952. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  33953. SSL_free(ssl);
  33954. SSL_CTX_free(ctx);
  33955. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  33956. return EXPECT_RESULT();
  33957. }
  33958. #ifndef NO_BIO
  33959. static int test_wolfSSL_PEM_read_bio(void)
  33960. {
  33961. EXPECT_DECLS;
  33962. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33963. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33964. byte buff[6000];
  33965. XFILE f = XBADFILE;
  33966. int bytes;
  33967. X509* x509 = NULL;
  33968. BIO* bio = NULL;
  33969. BUF_MEM* buf = NULL;
  33970. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  33971. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  33972. if (f != XBADFILE)
  33973. XFCLOSE(f);
  33974. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33975. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33976. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  33977. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  33978. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  33979. /* BIO should return the set EOF value */
  33980. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  33981. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  33982. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  33983. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  33984. BIO_free(bio);
  33985. BUF_MEM_free(buf);
  33986. X509_free(x509);
  33987. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33988. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33989. return EXPECT_RESULT();
  33990. }
  33991. #if defined(OPENSSL_EXTRA)
  33992. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  33993. long argl, long ret)
  33994. {
  33995. (void)bio;
  33996. (void)cmd;
  33997. (void)argp;
  33998. (void)argi;
  33999. (void)argl;
  34000. return ret;
  34001. }
  34002. #endif
  34003. static int test_wolfSSL_BIO(void)
  34004. {
  34005. EXPECT_DECLS;
  34006. #if defined(OPENSSL_EXTRA)
  34007. const unsigned char* p = NULL;
  34008. byte buff[20];
  34009. BIO* bio1 = NULL;
  34010. BIO* bio2 = NULL;
  34011. BIO* bio3 = NULL;
  34012. char* bufPt = NULL;
  34013. int i;
  34014. for (i = 0; i < 20; i++) {
  34015. buff[i] = i;
  34016. }
  34017. /* test BIO_free with NULL */
  34018. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  34019. /* Creating and testing type BIO_s_bio */
  34020. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34021. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  34022. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  34023. /* read/write before set up */
  34024. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34025. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  34026. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  34027. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  34028. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  34029. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  34030. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  34031. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  34032. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  34033. /* write buffer full */
  34034. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  34035. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  34036. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  34037. /* write the other direction with pair */
  34038. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  34039. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  34040. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  34041. /* try read */
  34042. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  34043. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  34044. /* try read using ctrl function */
  34045. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  34046. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  34047. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  34048. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  34049. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  34050. for (i = 0; i < 20; i++) {
  34051. ExpectIntEQ((int)bufPt[i], i);
  34052. }
  34053. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34054. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  34055. for (i = 0; i < 8; i++) {
  34056. ExpectIntEQ((int)bufPt[i], i);
  34057. }
  34058. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  34059. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  34060. /* new pair */
  34061. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  34062. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  34063. bio2 = NULL;
  34064. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  34065. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34066. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  34067. /* test wrap around... */
  34068. ExpectIntEQ(BIO_reset(bio1), 0);
  34069. ExpectIntEQ(BIO_reset(bio3), 0);
  34070. /* fill write buffer, read only small amount then write again */
  34071. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34072. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34073. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  34074. for (i = 0; i < 4; i++) {
  34075. ExpectIntEQ(bufPt[i], i);
  34076. }
  34077. /* try writing over read index */
  34078. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  34079. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  34080. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  34081. /* read and write 0 bytes */
  34082. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34083. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  34084. /* should read only to end of write buffer then need to read again */
  34085. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  34086. for (i = 0; i < 16; i++) {
  34087. ExpectIntEQ(bufPt[i], buff[4 + i]);
  34088. }
  34089. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  34090. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  34091. for (i = 0; i < 4; i++) {
  34092. ExpectIntEQ(bufPt[i], 0);
  34093. }
  34094. /* read index should not have advanced with nread0 */
  34095. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  34096. for (i = 0; i < 4; i++) {
  34097. ExpectIntEQ(bufPt[i], 0);
  34098. }
  34099. /* write and fill up buffer checking reset of index state */
  34100. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34101. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34102. /* test reset on data in bio1 write buffer */
  34103. ExpectIntEQ(BIO_reset(bio1), 0);
  34104. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  34105. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  34106. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  34107. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  34108. ExpectNotNull(p);
  34109. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  34110. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  34111. for (i = 0; i < 6; i++) {
  34112. ExpectIntEQ(bufPt[i], i);
  34113. }
  34114. /* test case of writing twice with offset read index */
  34115. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  34116. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  34117. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34118. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  34119. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34120. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  34121. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  34122. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  34123. BIO_free(bio1);
  34124. bio1 = NULL;
  34125. BIO_free(bio3);
  34126. bio3 = NULL;
  34127. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  34128. {
  34129. BIO* bioA = NULL;
  34130. BIO* bioB = NULL;
  34131. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  34132. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  34133. BIO_free(bioA);
  34134. bioA = NULL;
  34135. BIO_free(bioB);
  34136. bioB = NULL;
  34137. }
  34138. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  34139. /* BIOs with file pointers */
  34140. #if !defined(NO_FILESYSTEM)
  34141. {
  34142. XFILE f1 = XBADFILE;
  34143. XFILE f2 = XBADFILE;
  34144. BIO* f_bio1 = NULL;
  34145. BIO* f_bio2 = NULL;
  34146. unsigned char cert[300];
  34147. char testFile[] = "tests/bio_write_test.txt";
  34148. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  34149. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  34150. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  34151. /* Failure due to wrong BIO type */
  34152. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34153. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  34154. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  34155. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  34156. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  34157. WOLFSSL_SUCCESS);
  34158. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34159. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  34160. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  34161. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  34162. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  34163. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  34164. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  34165. ExpectIntEQ(BIO_reset(f_bio2), 0);
  34166. ExpectIntEQ(BIO_tell(NULL),-1);
  34167. ExpectIntEQ(BIO_tell(f_bio2),0);
  34168. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  34169. ExpectIntEQ(BIO_tell(f_bio2),4);
  34170. BIO_free(f_bio1);
  34171. f_bio1 = NULL;
  34172. BIO_free(f_bio2);
  34173. f_bio2 = NULL;
  34174. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  34175. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  34176. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  34177. BIO_free(f_bio1);
  34178. f_bio1 = NULL;
  34179. }
  34180. #endif /* !defined(NO_FILESYSTEM) */
  34181. /* BIO info callback */
  34182. {
  34183. const char* testArg = "test";
  34184. BIO* cb_bio = NULL;
  34185. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  34186. BIO_set_callback(cb_bio, bioCallback);
  34187. ExpectNotNull(BIO_get_callback(cb_bio));
  34188. BIO_set_callback(cb_bio, NULL);
  34189. ExpectNull(BIO_get_callback(cb_bio));
  34190. BIO_set_callback_arg(cb_bio, (char*)testArg);
  34191. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  34192. ExpectNull(BIO_get_callback_arg(NULL));
  34193. BIO_free(cb_bio);
  34194. cb_bio = NULL;
  34195. }
  34196. /* BIO_vfree */
  34197. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  34198. BIO_vfree(NULL);
  34199. BIO_vfree(bio1);
  34200. #endif
  34201. return EXPECT_RESULT();
  34202. }
  34203. #endif /* !NO_BIO */
  34204. static int test_wolfSSL_a2i_IPADDRESS(void)
  34205. {
  34206. EXPECT_DECLS;
  34207. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  34208. const unsigned char* data = NULL;
  34209. int dataSz = 0;
  34210. ASN1_OCTET_STRING *st = NULL;
  34211. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  34212. const unsigned char ipv6_exp[] = {
  34213. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  34214. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  34215. };
  34216. const unsigned char ipv6_home[] = {
  34217. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34218. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  34219. };
  34220. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  34221. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  34222. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34223. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  34224. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  34225. ASN1_STRING_free(st);
  34226. st = NULL;
  34227. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  34228. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34229. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34230. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  34231. ASN1_STRING_free(st);
  34232. st = NULL;
  34233. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  34234. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  34235. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  34236. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  34237. ASN1_STRING_free(st);
  34238. #endif
  34239. return EXPECT_RESULT();
  34240. }
  34241. static int test_wolfSSL_DES_ecb_encrypt(void)
  34242. {
  34243. EXPECT_DECLS;
  34244. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  34245. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  34246. WOLFSSL_DES_key_schedule key;
  34247. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  34248. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  34249. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  34250. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  34251. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  34252. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  34253. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  34254. /* Encrypt messages */
  34255. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  34256. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  34257. {
  34258. /* Decrypt messages */
  34259. int ret1 = 0;
  34260. int ret2 = 0;
  34261. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  34262. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  34263. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  34264. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  34265. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  34266. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  34267. }
  34268. #endif
  34269. return EXPECT_RESULT();
  34270. }
  34271. static int test_wolfSSL_X509_cmp_time(void)
  34272. {
  34273. EXPECT_DECLS;
  34274. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  34275. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  34276. WOLFSSL_ASN1_TIME asn_time;
  34277. time_t t;
  34278. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  34279. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  34280. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  34281. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  34282. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  34283. #endif
  34284. return EXPECT_RESULT();
  34285. }
  34286. static int test_wolfSSL_X509_time_adj(void)
  34287. {
  34288. EXPECT_DECLS;
  34289. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  34290. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  34291. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  34292. !defined(NO_ASN_TIME)
  34293. X509* x509 = NULL;
  34294. time_t t;
  34295. time_t not_before;
  34296. time_t not_after;
  34297. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  34298. client_cert_der_2048, sizeof_client_cert_der_2048,
  34299. WOLFSSL_FILETYPE_ASN1));
  34300. t = 0;
  34301. not_before = wc_Time(0);
  34302. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  34303. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  34304. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  34305. /* Check X509_gmtime_adj, too. */
  34306. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  34307. X509_free(x509);
  34308. #endif
  34309. return EXPECT_RESULT();
  34310. }
  34311. static int test_wolfSSL_X509(void)
  34312. {
  34313. EXPECT_DECLS;
  34314. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34315. !defined(NO_RSA)
  34316. X509* x509 = NULL;
  34317. #ifndef NO_BIO
  34318. BIO* bio = NULL;
  34319. X509_STORE_CTX* ctx = NULL;
  34320. X509_STORE* store = NULL;
  34321. #endif
  34322. char der[] = "certs/ca-cert.der";
  34323. XFILE fp = XBADFILE;
  34324. ExpectNotNull(x509 = X509_new());
  34325. X509_free(x509);
  34326. x509 = NULL;
  34327. #ifndef NO_BIO
  34328. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34329. SSL_FILETYPE_PEM));
  34330. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34331. #ifdef WOLFSSL_CERT_GEN
  34332. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  34333. #endif
  34334. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34335. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  34336. ExpectNotNull(store = X509_STORE_new());
  34337. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  34338. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  34339. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  34340. X509_STORE_CTX_free(ctx);
  34341. X509_STORE_free(store);
  34342. X509_free(x509);
  34343. x509 = NULL;
  34344. BIO_free(bio);
  34345. #endif
  34346. /** d2i_X509_fp test **/
  34347. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34348. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  34349. ExpectNotNull(x509);
  34350. X509_free(x509);
  34351. x509 = NULL;
  34352. if (fp != XBADFILE) {
  34353. XFCLOSE(fp);
  34354. fp = XBADFILE;
  34355. }
  34356. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  34357. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  34358. ExpectNotNull(x509);
  34359. X509_free(x509);
  34360. if (fp != XBADFILE)
  34361. XFCLOSE(fp);
  34362. /* X509_up_ref test */
  34363. ExpectIntEQ(X509_up_ref(NULL), 0);
  34364. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  34365. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  34366. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  34367. X509_free(x509); /* refCount = 2 */
  34368. X509_free(x509); /* refCount = 1 */
  34369. X509_free(x509); /* refCount = 0, free */
  34370. #endif
  34371. return EXPECT_RESULT();
  34372. }
  34373. static int test_wolfSSL_X509_get_ext_count(void)
  34374. {
  34375. EXPECT_DECLS;
  34376. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34377. !defined(NO_RSA)
  34378. int ret = 0;
  34379. WOLFSSL_X509* x509 = NULL;
  34380. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  34381. XFILE f = XBADFILE;
  34382. /* NULL parameter check */
  34383. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  34384. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34385. SSL_FILETYPE_PEM));
  34386. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34387. wolfSSL_X509_free(x509);
  34388. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  34389. SSL_FILETYPE_PEM));
  34390. ExpectIntEQ(X509_get_ext_count(x509), 5);
  34391. wolfSSL_X509_free(x509);
  34392. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  34393. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  34394. if (f != XBADFILE)
  34395. XFCLOSE(f);
  34396. /* wolfSSL_X509_get_ext_count() valid input */
  34397. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  34398. /* wolfSSL_X509_get_ext_count() NULL argument */
  34399. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  34400. wolfSSL_X509_free(x509);
  34401. #endif
  34402. return EXPECT_RESULT();
  34403. }
  34404. static int test_wolfSSL_X509_sign2(void)
  34405. {
  34406. EXPECT_DECLS;
  34407. /* test requires WOLFSSL_AKID_NAME to match expected output */
  34408. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34409. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  34410. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  34411. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  34412. defined(WOLFSSL_IP_ALT_NAME))
  34413. WOLFSSL_X509 *x509 = NULL;
  34414. WOLFSSL_X509 *ca = NULL;
  34415. const unsigned char *der = NULL;
  34416. const unsigned char *pt = NULL;
  34417. WOLFSSL_EVP_PKEY *priv = NULL;
  34418. WOLFSSL_X509_NAME *name = NULL;
  34419. int derSz;
  34420. #ifndef NO_ASN_TIME
  34421. WOLFSSL_ASN1_TIME *notBefore = NULL;
  34422. WOLFSSL_ASN1_TIME *notAfter = NULL;
  34423. const int year = 365*24*60*60;
  34424. const int day = 24*60*60;
  34425. const int hour = 60*60;
  34426. const int mini = 60;
  34427. time_t t;
  34428. #endif
  34429. const unsigned char expected[] = {
  34430. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  34431. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  34432. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  34433. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  34434. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  34435. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  34436. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  34437. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  34438. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  34439. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  34440. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  34441. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  34442. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  34443. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  34444. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34445. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  34446. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  34447. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  34448. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  34449. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  34450. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  34451. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  34452. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  34453. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  34454. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  34455. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  34456. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  34457. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  34458. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  34459. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  34460. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  34461. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  34462. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  34463. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34464. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  34465. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  34466. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  34467. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  34468. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  34469. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  34470. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  34471. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  34472. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  34473. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  34474. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  34475. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  34476. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  34477. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  34478. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  34479. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  34480. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  34481. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  34482. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  34483. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  34484. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  34485. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  34486. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  34487. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  34488. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  34489. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  34490. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  34491. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  34492. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34493. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34494. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  34495. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  34496. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  34497. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  34498. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  34499. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  34500. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  34501. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  34502. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  34503. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  34504. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  34505. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  34506. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  34507. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34508. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  34509. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  34510. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  34511. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  34512. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  34513. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  34514. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  34515. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  34516. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  34517. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  34518. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  34519. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  34520. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  34521. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  34522. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  34523. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  34524. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  34525. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  34526. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  34527. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  34528. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  34529. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  34530. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  34531. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  34532. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  34533. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  34534. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  34535. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  34536. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  34537. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  34538. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  34539. };
  34540. pt = ca_key_der_2048;
  34541. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  34542. sizeof_ca_key_der_2048));
  34543. pt = client_cert_der_2048;
  34544. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  34545. sizeof_client_cert_der_2048));
  34546. pt = ca_cert_der_2048;
  34547. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  34548. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  34549. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34550. #ifndef NO_ASN_TIME
  34551. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  34552. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  34553. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  34554. ExpectIntEQ(notAfter->length, 13);
  34555. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  34556. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  34557. #endif
  34558. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  34559. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  34560. ExpectIntEQ(derSz, sizeof(expected));
  34561. #ifndef NO_ASN_TIME
  34562. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  34563. #endif
  34564. wolfSSL_X509_free(ca);
  34565. wolfSSL_X509_free(x509);
  34566. wolfSSL_EVP_PKEY_free(priv);
  34567. #ifndef NO_ASN_TIME
  34568. wolfSSL_ASN1_TIME_free(notBefore);
  34569. wolfSSL_ASN1_TIME_free(notAfter);
  34570. #endif
  34571. #endif
  34572. return EXPECT_RESULT();
  34573. }
  34574. static int test_wolfSSL_X509_sign(void)
  34575. {
  34576. EXPECT_DECLS;
  34577. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  34578. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  34579. int ret;
  34580. char *cn = NULL;
  34581. word32 cnSz;
  34582. X509_NAME *name = NULL;
  34583. X509 *x509 = NULL;
  34584. X509 *ca = NULL;
  34585. DecodedCert dCert;
  34586. EVP_PKEY *pub = NULL;
  34587. EVP_PKEY *priv = NULL;
  34588. EVP_MD_CTX *mctx = NULL;
  34589. #if defined(USE_CERT_BUFFERS_1024)
  34590. const unsigned char* rsaPriv = client_key_der_1024;
  34591. const unsigned char* rsaPub = client_keypub_der_1024;
  34592. const unsigned char* certIssuer = client_cert_der_1024;
  34593. long clientKeySz = (long)sizeof_client_key_der_1024;
  34594. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  34595. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  34596. #elif defined(USE_CERT_BUFFERS_2048)
  34597. const unsigned char* rsaPriv = client_key_der_2048;
  34598. const unsigned char* rsaPub = client_keypub_der_2048;
  34599. const unsigned char* certIssuer = client_cert_der_2048;
  34600. long clientKeySz = (long)sizeof_client_key_der_2048;
  34601. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  34602. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  34603. #endif
  34604. byte sn[16];
  34605. int snSz = sizeof(sn);
  34606. /* Set X509_NAME fields */
  34607. ExpectNotNull(name = X509_NAME_new());
  34608. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  34609. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  34610. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34611. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  34612. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34613. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  34614. /* Get private and public keys */
  34615. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  34616. clientKeySz));
  34617. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  34618. ExpectNotNull(x509 = X509_new());
  34619. /* Set version 3 */
  34620. ExpectIntNE(X509_set_version(x509, 2L), 0);
  34621. /* Set subject name, add pubkey, and sign certificate */
  34622. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  34623. X509_NAME_free(name);
  34624. name = NULL;
  34625. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  34626. #ifdef WOLFSSL_ALT_NAMES
  34627. /* Add some subject alt names */
  34628. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  34629. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  34630. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34631. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  34632. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34633. "sphygmomanometer",
  34634. ASN_DNS_TYPE), SSL_SUCCESS);
  34635. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34636. "supercalifragilisticexpialidocious",
  34637. ASN_DNS_TYPE), SSL_SUCCESS);
  34638. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  34639. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  34640. ASN_DNS_TYPE), SSL_SUCCESS);
  34641. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34642. {
  34643. unsigned char ip4_type[] = {127,128,0,255};
  34644. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  34645. 0xff, 0xee, 0x99, 0x88,
  34646. 0x77, 0x66, 0x55, 0x44,
  34647. 0x00, 0x33, 0x22, 0x11};
  34648. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  34649. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  34650. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  34651. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  34652. }
  34653. #endif
  34654. #endif /* WOLFSSL_ALT_NAMES */
  34655. /* test valid sign case */
  34656. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  34657. /* test valid X509_sign_ctx case */
  34658. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34659. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  34660. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  34661. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  34662. ExpectIntEQ(X509_get_ext_count(x509), 1);
  34663. #endif
  34664. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  34665. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  34666. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  34667. #endif
  34668. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  34669. WOLFSSL_SUCCESS);
  34670. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  34671. /* Variation in size depends on ASN.1 encoding when MSB is set.
  34672. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  34673. * with the MSB set. See GenerateInteger in asn.c */
  34674. #ifndef USE_CERT_BUFFERS_1024
  34675. #ifndef WOLFSSL_ALT_NAMES
  34676. /* Valid case - size should be 781-786 with 16 byte serial number */
  34677. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  34678. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34679. /* Valid case - size should be 955-960 with 16 byte serial number */
  34680. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  34681. #else
  34682. /* Valid case - size should be 926-931 with 16 byte serial number */
  34683. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  34684. #endif
  34685. #else
  34686. #ifndef WOLFSSL_ALT_NAMES
  34687. /* Valid case - size should be 537-542 with 16 byte serial number */
  34688. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  34689. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  34690. /* Valid case - size should be 695-670 with 16 byte serial number */
  34691. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  34692. #else
  34693. /* Valid case - size should be 666-671 with 16 byte serial number */
  34694. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  34695. #endif
  34696. #endif
  34697. /* check that issuer name is as expected after signature */
  34698. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  34699. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  34700. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  34701. ExpectNotNull(name = X509_get_subject_name(ca));
  34702. cnSz = X509_NAME_get_sz(name);
  34703. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34704. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34705. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  34706. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34707. cn = NULL;
  34708. #ifdef WOLFSSL_MULTI_ATTRIB
  34709. /* test adding multiple OU's to the signer */
  34710. ExpectNotNull(name = X509_get_subject_name(ca));
  34711. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34712. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  34713. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  34714. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  34715. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  34716. #endif
  34717. ExpectNotNull(name = X509_get_subject_name(ca));
  34718. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  34719. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34720. ExpectNotNull(name = X509_get_issuer_name(x509));
  34721. cnSz = X509_NAME_get_sz(name);
  34722. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34723. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  34724. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  34725. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  34726. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34727. cn = NULL;
  34728. FreeDecodedCert(&dCert);
  34729. /* Test invalid parameters */
  34730. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  34731. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  34732. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  34733. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  34734. EVP_MD_CTX_free(mctx);
  34735. mctx = NULL;
  34736. ExpectNotNull(mctx = EVP_MD_CTX_new());
  34737. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  34738. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  34739. /* test invalid version number */
  34740. #if defined(OPENSSL_ALL)
  34741. ExpectIntNE(X509_set_version(x509, 6L), 0);
  34742. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34743. /* uses ParseCert which fails on bad version number */
  34744. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  34745. #endif
  34746. EVP_MD_CTX_free(mctx);
  34747. EVP_PKEY_free(priv);
  34748. EVP_PKEY_free(pub);
  34749. X509_free(x509);
  34750. X509_free(ca);
  34751. #endif
  34752. return EXPECT_RESULT();
  34753. }
  34754. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  34755. {
  34756. EXPECT_DECLS;
  34757. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34758. X509* x509 = NULL;
  34759. const X509_ALGOR* alg;
  34760. ExpectNotNull(x509 = X509_new());
  34761. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  34762. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34763. X509_free(x509);
  34764. #endif
  34765. return EXPECT_RESULT();
  34766. }
  34767. static int test_wolfSSL_X509_ALGOR_get0(void)
  34768. {
  34769. EXPECT_DECLS;
  34770. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34771. !defined(NO_SHA256) && !defined(NO_RSA)
  34772. X509* x509 = NULL;
  34773. const ASN1_OBJECT* obj = NULL;
  34774. const X509_ALGOR* alg = NULL;
  34775. int pptype = 0;
  34776. const void *ppval = NULL;
  34777. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  34778. SSL_FILETYPE_PEM));
  34779. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  34780. /* Invalid case */
  34781. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  34782. ExpectNull(obj);
  34783. /* Valid case */
  34784. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  34785. ExpectNotNull(obj);
  34786. ExpectNull(ppval);
  34787. ExpectIntNE(pptype, 0);
  34788. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  34789. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  34790. X509_free(x509);
  34791. #endif
  34792. return EXPECT_RESULT();
  34793. }
  34794. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  34795. {
  34796. EXPECT_DECLS;
  34797. #if defined(OPENSSL_EXTRA)
  34798. X509_VERIFY_PARAM *paramTo = NULL;
  34799. X509_VERIFY_PARAM *paramFrom = NULL;
  34800. char testIPv4[] = "127.0.0.1";
  34801. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  34802. char testhostName1[] = "foo.hoge.com";
  34803. char testhostName2[] = "foobar.hoge.com";
  34804. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  34805. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  34806. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  34807. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  34808. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  34809. (int)XSTRLEN(testhostName1)), 1);
  34810. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  34811. (int)XSTRLEN(testhostName1)));
  34812. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  34813. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  34814. ExpectIntEQ(0x01, paramFrom->hostFlags);
  34815. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  34816. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  34817. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34818. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  34819. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  34820. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34821. /* null pointer */
  34822. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  34823. /* in the case of "from" null, returns success */
  34824. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  34825. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  34826. /* inherit flags test : VPARAM_DEFAULT */
  34827. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34828. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34829. (int)XSTRLEN(testhostName1)));
  34830. ExpectIntEQ(0x01, paramTo->hostFlags);
  34831. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34832. /* inherit flags test : VPARAM OVERWRITE */
  34833. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34834. (int)XSTRLEN(testhostName2)), 1);
  34835. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34836. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34837. if (paramTo != NULL) {
  34838. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  34839. }
  34840. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34841. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34842. (int)XSTRLEN(testhostName1)));
  34843. ExpectIntEQ(0x01, paramTo->hostFlags);
  34844. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34845. /* inherit flags test : VPARAM_RESET_FLAGS */
  34846. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34847. (int)XSTRLEN(testhostName2)), 1);
  34848. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34849. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  34850. if (paramTo != NULL) {
  34851. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  34852. }
  34853. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34854. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  34855. (int)XSTRLEN(testhostName1)));
  34856. ExpectIntEQ(0x01, paramTo->hostFlags);
  34857. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  34858. /* inherit flags test : VPARAM_LOCKED */
  34859. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  34860. (int)XSTRLEN(testhostName2)), 1);
  34861. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  34862. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  34863. if (paramTo != NULL) {
  34864. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  34865. }
  34866. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  34867. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  34868. (int)XSTRLEN(testhostName2)));
  34869. ExpectIntEQ(0x00, paramTo->hostFlags);
  34870. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34871. /* test for incorrect parameters */
  34872. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  34873. 0);
  34874. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  34875. /* inherit flags test : VPARAM_ONCE, not testable yet */
  34876. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  34877. 1);
  34878. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  34879. X509_V_FLAG_CRL_CHECK_ALL);
  34880. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  34881. X509_V_FLAG_CRL_CHECK_ALL), 1);
  34882. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  34883. X509_VERIFY_PARAM_free(paramTo);
  34884. X509_VERIFY_PARAM_free(paramFrom);
  34885. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  34886. #endif
  34887. return EXPECT_RESULT();
  34888. }
  34889. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  34890. static int test_wolfSSL_check_domain_verify_count = 0;
  34891. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  34892. WOLFSSL_X509_STORE_CTX* store)
  34893. {
  34894. EXPECT_DECLS;
  34895. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  34896. ExpectIntEQ(preverify, 1);
  34897. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  34898. return EXPECT_SUCCESS();
  34899. }
  34900. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  34901. {
  34902. EXPECT_DECLS;
  34903. X509_VERIFY_PARAM *param = NULL;
  34904. ExpectNotNull(param = SSL_get0_param(ssl));
  34905. /* Domain check should only be done on the leaf cert */
  34906. X509_VERIFY_PARAM_set_hostflags(param,
  34907. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  34908. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  34909. "wolfSSL Server Chain", 0), 1);
  34910. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  34911. test_wolfSSL_check_domain_verify_cb);
  34912. return EXPECT_RESULT();
  34913. }
  34914. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  34915. {
  34916. EXPECT_DECLS;
  34917. /* Use a cert with different domains in chain */
  34918. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  34919. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  34920. return EXPECT_RESULT();
  34921. }
  34922. static int test_wolfSSL_check_domain(void)
  34923. {
  34924. EXPECT_DECLS;
  34925. test_ssl_cbf func_cb_client;
  34926. test_ssl_cbf func_cb_server;
  34927. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  34928. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  34929. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  34930. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  34931. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  34932. &func_cb_server, NULL), TEST_SUCCESS);
  34933. /* Should have been called once for each cert in sent chain */
  34934. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  34935. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  34936. #else
  34937. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  34938. #endif
  34939. return EXPECT_RESULT();
  34940. }
  34941. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  34942. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  34943. {
  34944. EXPECT_DECLS;
  34945. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  34946. X509* x509 = NULL;
  34947. X509_PUBKEY* pubKey;
  34948. ExpectNotNull(x509 = X509_new());
  34949. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  34950. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  34951. X509_free(x509);
  34952. #endif
  34953. return EXPECT_RESULT();
  34954. }
  34955. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  34956. {
  34957. EXPECT_DECLS;
  34958. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34959. !defined(NO_SHA256) && !defined(NO_RSA)
  34960. X509* x509 = NULL;
  34961. ASN1_OBJECT* obj = NULL;
  34962. const ASN1_OBJECT* pa_oid = NULL;
  34963. X509_PUBKEY* pubKey = NULL;
  34964. X509_PUBKEY* pubKey2 = NULL;
  34965. EVP_PKEY* evpKey = NULL;
  34966. const unsigned char *pk = NULL;
  34967. int ppklen;
  34968. int pptype;
  34969. X509_ALGOR *pa = NULL;
  34970. const void *pval;
  34971. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  34972. SSL_FILETYPE_PEM));
  34973. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  34974. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  34975. ExpectNotNull(pk);
  34976. ExpectNotNull(pa);
  34977. ExpectNotNull(pubKey);
  34978. ExpectIntGT(ppklen, 0);
  34979. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  34980. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  34981. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  34982. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  34983. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  34984. ExpectNotNull(pk);
  34985. ExpectNotNull(pa);
  34986. ExpectIntGT(ppklen, 0);
  34987. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  34988. ExpectNotNull(pa_oid);
  34989. ExpectNull(pval);
  34990. ExpectIntEQ(pptype, V_ASN1_NULL);
  34991. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  34992. X509_PUBKEY_free(pubKey2);
  34993. X509_free(x509);
  34994. EVP_PKEY_free(evpKey);
  34995. #endif
  34996. return EXPECT_RESULT();
  34997. }
  34998. static int test_wolfSSL_X509_PUBKEY_EC(void)
  34999. {
  35000. EXPECT_DECLS;
  35001. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  35002. X509* x509 = NULL;
  35003. ASN1_OBJECT* obj = NULL;
  35004. ASN1_OBJECT* poid = NULL;
  35005. const ASN1_OBJECT* pa_oid = NULL;
  35006. X509_PUBKEY* pubKey = NULL;
  35007. X509_PUBKEY* pubKey2 = NULL;
  35008. EVP_PKEY* evpKey = NULL;
  35009. const unsigned char *pk = NULL;
  35010. int ppklen;
  35011. int pptype;
  35012. X509_ALGOR *pa = NULL;
  35013. const void *pval;
  35014. char buf[50];
  35015. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  35016. SSL_FILETYPE_PEM));
  35017. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  35018. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  35019. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  35020. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  35021. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  35022. ExpectNotNull(pk);
  35023. ExpectNotNull(pa);
  35024. ExpectIntGT(ppklen, 0);
  35025. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35026. ExpectNotNull(pa_oid);
  35027. ExpectNotNull(pval);
  35028. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  35029. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  35030. poid = (ASN1_OBJECT *)pval;
  35031. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  35032. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  35033. X509_PUBKEY_free(pubKey2);
  35034. X509_free(x509);
  35035. EVP_PKEY_free(evpKey);
  35036. #endif
  35037. return EXPECT_RESULT();
  35038. }
  35039. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  35040. {
  35041. EXPECT_DECLS;
  35042. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  35043. word32 bytes;
  35044. #ifdef USE_CERT_BUFFERS_1024
  35045. byte tmp[ONEK_BUF];
  35046. #elif defined(USE_CERT_BUFFERS_2048)
  35047. byte tmp[TWOK_BUF];
  35048. #else
  35049. byte tmp[TWOK_BUF];
  35050. #endif /* END USE_CERT_BUFFERS_1024 */
  35051. const unsigned char* dsaKeyDer = tmp;
  35052. ASN1_OBJECT* obj = NULL;
  35053. ASN1_STRING* str;
  35054. const ASN1_OBJECT* pa_oid = NULL;
  35055. X509_PUBKEY* pubKey = NULL;
  35056. EVP_PKEY* evpKey = NULL;
  35057. const unsigned char *pk = NULL;
  35058. int ppklen, pptype;
  35059. X509_ALGOR *pa = NULL;
  35060. const void *pval;
  35061. #ifdef USE_CERT_BUFFERS_1024
  35062. XMEMSET(tmp, 0, sizeof(tmp));
  35063. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  35064. bytes = sizeof_dsa_key_der_1024;
  35065. #elif defined(USE_CERT_BUFFERS_2048)
  35066. XMEMSET(tmp, 0, sizeof(tmp));
  35067. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  35068. bytes = sizeof_dsa_key_der_2048;
  35069. #else
  35070. {
  35071. XFILE fp = XBADFILE;
  35072. XMEMSET(tmp, 0, sizeof(tmp));
  35073. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  35074. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  35075. if (fp != XBADFILE)
  35076. XFCLOSE(fp);
  35077. }
  35078. #endif
  35079. /* Initialize pkey with der format dsa key */
  35080. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  35081. ExpectNotNull(pubKey = X509_PUBKEY_new());
  35082. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  35083. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  35084. ExpectNotNull(pk);
  35085. ExpectNotNull(pa);
  35086. ExpectIntGT(ppklen, 0);
  35087. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  35088. ExpectNotNull(pa_oid);
  35089. ExpectNotNull(pval);
  35090. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  35091. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  35092. str = (ASN1_STRING *)pval;
  35093. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  35094. #ifdef USE_CERT_BUFFERS_1024
  35095. ExpectIntEQ(ASN1_STRING_length(str), 291);
  35096. #else
  35097. ExpectIntEQ(ASN1_STRING_length(str), 549);
  35098. #endif /* END USE_CERT_BUFFERS_1024 */
  35099. X509_PUBKEY_free(pubKey);
  35100. EVP_PKEY_free(evpKey);
  35101. #endif
  35102. return EXPECT_RESULT();
  35103. }
  35104. static int test_wolfSSL_BUF(void)
  35105. {
  35106. EXPECT_DECLS;
  35107. #if defined(OPENSSL_EXTRA)
  35108. BUF_MEM* buf = NULL;
  35109. ExpectNotNull(buf = BUF_MEM_new());
  35110. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  35111. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  35112. BUF_MEM_free(buf);
  35113. #endif
  35114. return EXPECT_RESULT();
  35115. }
  35116. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35117. static int stub_rand_seed(const void *buf, int num)
  35118. {
  35119. (void)buf;
  35120. (void)num;
  35121. return 123;
  35122. }
  35123. static int stub_rand_bytes(unsigned char *buf, int num)
  35124. {
  35125. (void)buf;
  35126. (void)num;
  35127. return 456;
  35128. }
  35129. static byte* was_stub_rand_cleanup_called(void)
  35130. {
  35131. static byte was_called = 0;
  35132. return &was_called;
  35133. }
  35134. static void stub_rand_cleanup(void)
  35135. {
  35136. byte* was_called = was_stub_rand_cleanup_called();
  35137. *was_called = 1;
  35138. return;
  35139. }
  35140. static byte* was_stub_rand_add_called(void)
  35141. {
  35142. static byte was_called = 0;
  35143. return &was_called;
  35144. }
  35145. static int stub_rand_add(const void *buf, int num, double entropy)
  35146. {
  35147. byte* was_called = was_stub_rand_add_called();
  35148. (void)buf;
  35149. (void)num;
  35150. (void)entropy;
  35151. *was_called = 1;
  35152. return 0;
  35153. }
  35154. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  35155. {
  35156. (void)buf;
  35157. (void)num;
  35158. return 9876;
  35159. }
  35160. static int stub_rand_status(void)
  35161. {
  35162. return 5432;
  35163. }
  35164. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35165. static int test_wolfSSL_RAND_set_rand_method(void)
  35166. {
  35167. EXPECT_DECLS;
  35168. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  35169. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  35170. unsigned char* buf = NULL;
  35171. int num = 0;
  35172. double entropy = 0;
  35173. int ret;
  35174. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  35175. byte* was_add_called = was_stub_rand_add_called();
  35176. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  35177. DYNAMIC_TYPE_TMP_BUFFER));
  35178. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  35179. ExpectIntEQ(*was_cleanup_called, 0);
  35180. RAND_cleanup();
  35181. ExpectIntEQ(*was_cleanup_called, 0);
  35182. rand_methods.seed = &stub_rand_seed;
  35183. rand_methods.bytes = &stub_rand_bytes;
  35184. rand_methods.cleanup = &stub_rand_cleanup;
  35185. rand_methods.add = &stub_rand_add;
  35186. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  35187. rand_methods.status = &stub_rand_status;
  35188. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  35189. ExpectIntEQ(RAND_seed(buf, num), 123);
  35190. ExpectIntEQ(RAND_bytes(buf, num), 456);
  35191. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  35192. ExpectIntEQ(RAND_status(), 5432);
  35193. ExpectIntEQ(*was_add_called, 0);
  35194. /* The function pointer for RAND_add returns int, but RAND_add itself
  35195. * returns void. */
  35196. RAND_add(buf, num, entropy);
  35197. ExpectIntEQ(*was_add_called, 1);
  35198. was_add_called = 0;
  35199. ExpectIntEQ(*was_cleanup_called, 0);
  35200. RAND_cleanup();
  35201. ExpectIntEQ(*was_cleanup_called, 1);
  35202. *was_cleanup_called = 0;
  35203. ret = RAND_set_rand_method(NULL);
  35204. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  35205. ExpectIntNE(RAND_status(), 5432);
  35206. ExpectIntEQ(*was_cleanup_called, 0);
  35207. RAND_cleanup();
  35208. ExpectIntEQ(*was_cleanup_called, 0);
  35209. RAND_set_rand_method(NULL);
  35210. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35211. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  35212. return EXPECT_RESULT();
  35213. }
  35214. static int test_wolfSSL_RAND_bytes(void)
  35215. {
  35216. EXPECT_DECLS;
  35217. #if defined(OPENSSL_EXTRA)
  35218. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  35219. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  35220. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  35221. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  35222. int max_bufsize;
  35223. byte *my_buf = NULL;
  35224. /* sanity check */
  35225. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  35226. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  35227. max_bufsize = size4;
  35228. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  35229. DYNAMIC_TYPE_TMP_BUFFER));
  35230. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  35231. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  35232. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  35233. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  35234. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  35235. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  35236. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  35237. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  35238. #endif
  35239. return EXPECT_RESULT();
  35240. }
  35241. static int test_wolfSSL_RAND(void)
  35242. {
  35243. EXPECT_DECLS;
  35244. #if defined(OPENSSL_EXTRA)
  35245. byte seed[16];
  35246. XMEMSET(seed, 0, sizeof(seed));
  35247. /* No global methods set. */
  35248. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  35249. ExpectIntEQ(RAND_poll(), 1);
  35250. RAND_cleanup();
  35251. ExpectIntEQ(RAND_egd(NULL), -1);
  35252. #ifndef NO_FILESYSTEM
  35253. {
  35254. char fname[100];
  35255. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  35256. ExpectIntEQ(RAND_write_file(NULL), 0);
  35257. }
  35258. #endif
  35259. #endif
  35260. return EXPECT_RESULT();
  35261. }
  35262. static int test_wolfSSL_PKCS8_Compat(void)
  35263. {
  35264. EXPECT_DECLS;
  35265. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  35266. !defined(NO_BIO)
  35267. PKCS8_PRIV_KEY_INFO* pt = NULL;
  35268. BIO* bio = NULL;
  35269. XFILE f = XBADFILE;
  35270. int bytes;
  35271. char pkcs8_buffer[512];
  35272. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35273. EVP_PKEY *pkey = NULL;
  35274. #endif
  35275. /* file from wolfssl/certs/ directory */
  35276. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  35277. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  35278. 0);
  35279. if (f != XBADFILE)
  35280. XFCLOSE(f);
  35281. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35282. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  35283. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  35284. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  35285. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  35286. /* gets PKCS8 pointer to pkey */
  35287. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  35288. EVP_PKEY_free(pkey);
  35289. #endif
  35290. BIO_free(bio);
  35291. PKCS8_PRIV_KEY_INFO_free(pt);
  35292. #endif
  35293. return EXPECT_RESULT();
  35294. }
  35295. static int test_wolfSSL_PKCS8_d2i(void)
  35296. {
  35297. EXPECT_DECLS;
  35298. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  35299. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  35300. * requires a 12 byte password in FIPS mode. This test ends up
  35301. * trying to use an 8 byte password. */
  35302. #ifndef NO_FILESYSTEM
  35303. unsigned char pkcs8_buffer[2048];
  35304. const unsigned char* p;
  35305. int bytes;
  35306. XFILE file = XBADFILE;
  35307. WOLFSSL_EVP_PKEY* pkey = NULL;
  35308. #ifndef NO_BIO
  35309. BIO* bio = NULL;
  35310. #if defined(OPENSSL_ALL) && \
  35311. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  35312. defined(HAVE_ECC)) && \
  35313. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35314. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  35315. #endif
  35316. #endif
  35317. #ifndef NO_RSA
  35318. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  35319. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  35320. #ifndef NO_DES3
  35321. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  35322. #endif
  35323. #endif /* NO_RSA */
  35324. #ifdef HAVE_ECC
  35325. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  35326. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  35327. #ifndef NO_DES3
  35328. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  35329. #endif
  35330. #endif /* HAVE_ECC */
  35331. #endif /* !NO_FILESYSTEM */
  35332. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  35333. #ifndef NO_RSA
  35334. #ifdef USE_CERT_BUFFERS_1024
  35335. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  35336. int rsaSz = sizeof_server_key_der_1024;
  35337. #else
  35338. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  35339. int rsaSz = sizeof_server_key_der_2048;
  35340. #endif
  35341. #endif
  35342. #ifdef HAVE_ECC
  35343. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  35344. int ecSz = sizeof_ecc_key_der_256;
  35345. #endif
  35346. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  35347. #ifndef NO_FILESYSTEM
  35348. (void)pkcs8_buffer;
  35349. (void)p;
  35350. (void)bytes;
  35351. (void)file;
  35352. #ifndef NO_BIO
  35353. (void)bio;
  35354. #endif
  35355. #endif
  35356. #ifdef OPENSSL_ALL
  35357. #ifndef NO_RSA
  35358. /* Try to auto-detect normal RSA private key */
  35359. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  35360. EVP_PKEY_free(pkey);
  35361. pkey = NULL;
  35362. #endif
  35363. #ifdef HAVE_ECC
  35364. /* Try to auto-detect normal EC private key */
  35365. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  35366. EVP_PKEY_free(pkey);
  35367. pkey = NULL;
  35368. #endif
  35369. #endif /* OPENSSL_ALL */
  35370. #ifndef NO_FILESYSTEM
  35371. #ifndef NO_RSA
  35372. /* Get DER encoded RSA PKCS#8 data. */
  35373. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  35374. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35375. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35376. file)), 0);
  35377. if (file != XBADFILE) {
  35378. XFCLOSE(file);
  35379. file = XBADFILE;
  35380. }
  35381. p = pkcs8_buffer;
  35382. #ifdef OPENSSL_ALL
  35383. /* Try to decode - auto-detect key type. */
  35384. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35385. #else
  35386. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  35387. #endif
  35388. /* Get PEM encoded RSA PKCS#8 data. */
  35389. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  35390. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35391. file)), 0);
  35392. if (file != XBADFILE) {
  35393. XFCLOSE(file);
  35394. file = XBADFILE;
  35395. }
  35396. #if defined(OPENSSL_ALL) && \
  35397. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35398. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35399. /* Write PKCS#8 PEM to BIO. */
  35400. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35401. NULL), bytes);
  35402. /* Compare file and written data */
  35403. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35404. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35405. BIO_free(bio);
  35406. bio = NULL;
  35407. #if !defined(NO_DES3) && !defined(NO_SHA)
  35408. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35409. /* Write Encrypted PKCS#8 PEM to BIO. */
  35410. bytes = 1834;
  35411. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  35412. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35413. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35414. (void*)"yassl123"));
  35415. EVP_PKEY_free(evpPkey);
  35416. evpPkey = NULL;
  35417. BIO_free(bio);
  35418. bio = NULL;
  35419. #endif /* !NO_DES3 && !NO_SHA */
  35420. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35421. EVP_PKEY_free(pkey);
  35422. pkey = NULL;
  35423. /* PKCS#8 encrypted RSA key */
  35424. #ifndef NO_DES3
  35425. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  35426. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35427. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35428. file)), 0);
  35429. if (file != XBADFILE) {
  35430. XFCLOSE(file);
  35431. file = XBADFILE;
  35432. }
  35433. #if defined(OPENSSL_ALL) && \
  35434. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35435. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35436. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35437. (void*)"yassl123"));
  35438. EVP_PKEY_free(pkey);
  35439. pkey = NULL;
  35440. BIO_free(bio);
  35441. bio = NULL;
  35442. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35443. #endif /* !NO_DES3 */
  35444. #endif /* NO_RSA */
  35445. #ifdef HAVE_ECC
  35446. /* PKCS#8 encode EC key */
  35447. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  35448. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35449. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35450. file)), 0);
  35451. if (file != XBADFILE) {
  35452. XFCLOSE(file);
  35453. file = XBADFILE;
  35454. }
  35455. p = pkcs8_buffer;
  35456. #ifdef OPENSSL_ALL
  35457. /* Try to decode - auto-detect key type. */
  35458. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  35459. #else
  35460. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  35461. #endif
  35462. /* Get PEM encoded RSA PKCS#8 data. */
  35463. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  35464. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35465. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35466. file)), 0);
  35467. if (file != XBADFILE) {
  35468. XFCLOSE(file);
  35469. file = XBADFILE;
  35470. }
  35471. #if defined(OPENSSL_ALL) && \
  35472. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  35473. defined(HAVE_AES_CBC)
  35474. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35475. /* Write PKCS#8 PEM to BIO. */
  35476. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  35477. NULL), bytes);
  35478. /* Compare file and written data */
  35479. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  35480. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  35481. BIO_free(bio);
  35482. bio = NULL;
  35483. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35484. /* Write Encrypted PKCS#8 PEM to BIO. */
  35485. bytes = 379;
  35486. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  35487. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  35488. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  35489. (void*)"yassl123"));
  35490. EVP_PKEY_free(evpPkey);
  35491. evpPkey = NULL;
  35492. BIO_free(bio);
  35493. bio = NULL;
  35494. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  35495. EVP_PKEY_free(pkey);
  35496. pkey = NULL;
  35497. /* PKCS#8 encrypted EC key */
  35498. #ifndef NO_DES3
  35499. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  35500. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  35501. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  35502. file)), 0);
  35503. if (file != XBADFILE) {
  35504. XFCLOSE(file);
  35505. file = XBADFILE;
  35506. }
  35507. #if defined(OPENSSL_ALL) && \
  35508. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  35509. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  35510. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  35511. (void*)"yassl123"));
  35512. EVP_PKEY_free(pkey);
  35513. pkey = NULL;
  35514. BIO_free(bio);
  35515. bio = NULL;
  35516. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  35517. #endif /* !NO_DES3 */
  35518. #endif /* HAVE_ECC */
  35519. #endif /* !NO_FILESYSTEM */
  35520. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  35521. return EXPECT_RESULT();
  35522. }
  35523. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35524. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35525. #define LOGGING_THREADS 5
  35526. #define ERROR_COUNT 10
  35527. /* copied from logging.c since this is not exposed otherwise */
  35528. #ifndef ERROR_QUEUE_MAX
  35529. #ifdef ERROR_QUEUE_PER_THREAD
  35530. #define ERROR_QUEUE_MAX 16
  35531. #else
  35532. /* this breaks from compat of unlimited error queue size */
  35533. #define ERROR_QUEUE_MAX 100
  35534. #endif
  35535. #endif
  35536. static volatile int loggingThreadsReady;
  35537. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  35538. {
  35539. const char* file;
  35540. int line;
  35541. unsigned long err;
  35542. int errorCount = 0;
  35543. int i;
  35544. (void)args;
  35545. while (!loggingThreadsReady);
  35546. for (i = 0; i < ERROR_COUNT; i++)
  35547. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35548. while ((err = ERR_get_error_line(&file, &line))) {
  35549. AssertIntEQ(err, 990 + errorCount);
  35550. errorCount++;
  35551. }
  35552. AssertIntEQ(errorCount, ERROR_COUNT);
  35553. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  35554. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  35555. errorCount = 0;
  35556. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  35557. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  35558. while ((err = ERR_get_error_line(&file, &line))) {
  35559. AssertIntEQ(err, 990 + errorCount);
  35560. errorCount++;
  35561. }
  35562. /* test that the 3 errors over the max were dropped */
  35563. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  35564. WOLFSSL_RETURN_FROM_THREAD(0);
  35565. }
  35566. #endif
  35567. static int test_error_queue_per_thread(void)
  35568. {
  35569. int res = TEST_SKIPPED;
  35570. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  35571. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  35572. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  35573. int i;
  35574. ERR_clear_error(); /* clear out any error nodes */
  35575. loggingThreadsReady = 0;
  35576. for (i = 0; i < LOGGING_THREADS; i++)
  35577. start_thread(test_logging, NULL, &loggingThreads[i]);
  35578. loggingThreadsReady = 1;
  35579. for (i = 0; i < LOGGING_THREADS; i++)
  35580. join_thread(loggingThreads[i]);
  35581. res = TEST_SUCCESS;
  35582. #endif
  35583. return res;
  35584. }
  35585. static int test_wolfSSL_ERR_put_error(void)
  35586. {
  35587. EXPECT_DECLS;
  35588. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35589. defined(DEBUG_WOLFSSL)
  35590. const char* file;
  35591. int line;
  35592. ERR_clear_error(); /* clear out any error nodes */
  35593. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35594. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35595. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  35596. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  35597. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  35598. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  35599. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  35600. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  35601. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  35602. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  35603. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  35604. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  35605. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  35606. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  35607. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  35608. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  35609. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  35610. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  35611. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  35612. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  35613. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  35614. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  35615. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  35616. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  35617. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  35618. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  35619. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  35620. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  35621. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  35622. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  35623. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  35624. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  35625. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  35626. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  35627. "this file", 100);
  35628. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  35629. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35630. ExpectIntEQ(line, 100);
  35631. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  35632. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  35633. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  35634. #endif
  35635. /* try reading past end of error queue */
  35636. file = NULL;
  35637. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35638. ExpectNull(file);
  35639. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  35640. PEMerr(4,4);
  35641. ExpectIntEQ(ERR_get_error(), 4);
  35642. /* Empty and free up all error nodes */
  35643. ERR_clear_error();
  35644. /* Verify all nodes are cleared */
  35645. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  35646. ERR_clear_error();
  35647. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  35648. #endif
  35649. return EXPECT_RESULT();
  35650. }
  35651. /*
  35652. * This is a regression test for a bug where the peek/get error functions were
  35653. * drawing from the end of the queue rather than the front.
  35654. */
  35655. static int test_wolfSSL_ERR_get_error_order(void)
  35656. {
  35657. EXPECT_DECLS;
  35658. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  35659. /* Empty the queue. */
  35660. wolfSSL_ERR_clear_error();
  35661. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  35662. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  35663. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  35664. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  35665. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  35666. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  35667. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  35668. return EXPECT_RESULT();
  35669. }
  35670. #ifndef NO_BIO
  35671. static int test_wolfSSL_ERR_print_errors(void)
  35672. {
  35673. EXPECT_DECLS;
  35674. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35675. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  35676. BIO* bio = NULL;
  35677. char buf[1024];
  35678. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35679. ERR_clear_error(); /* clear out any error nodes */
  35680. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35681. /* Choosing -600 as an unused errno. */
  35682. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  35683. ERR_print_errors(bio);
  35684. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  35685. ExpectIntEQ(XSTRNCMP(
  35686. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  35687. buf, 55), 0);
  35688. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  35689. ExpectIntEQ(XSTRNCMP(
  35690. "error:600:wolfSSL library:unknown error number:asn.c:100",
  35691. buf, 56), 0);
  35692. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  35693. ExpectIntEQ(buf[0], '\0');
  35694. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  35695. BIO_free(bio);
  35696. #endif
  35697. return EXPECT_RESULT();
  35698. }
  35699. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35700. defined(DEBUG_WOLFSSL)
  35701. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  35702. {
  35703. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  35704. return 0;
  35705. }
  35706. #endif
  35707. static int test_wolfSSL_ERR_print_errors_cb(void)
  35708. {
  35709. EXPECT_DECLS;
  35710. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  35711. defined(DEBUG_WOLFSSL)
  35712. BIO* bio = NULL;
  35713. char buf[1024];
  35714. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35715. ERR_clear_error(); /* clear out any error nodes */
  35716. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  35717. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  35718. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  35719. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  35720. ExpectIntEQ(XSTRNCMP(
  35721. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  35722. buf, 53), 0);
  35723. ExpectIntEQ(XSTRNCMP(
  35724. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  35725. buf + 53, 55), 0);
  35726. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  35727. BIO_free(bio);
  35728. #endif
  35729. return EXPECT_RESULT();
  35730. }
  35731. /*
  35732. * Testing WOLFSSL_ERROR_MSG
  35733. */
  35734. static int test_WOLFSSL_ERROR_MSG(void)
  35735. {
  35736. int res = TEST_SKIPPED;
  35737. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  35738. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  35739. const char* msg = TEST_STRING;
  35740. WOLFSSL_ERROR_MSG(msg);
  35741. res = TEST_SUCCESS;
  35742. #endif
  35743. return res;
  35744. } /* End test_WOLFSSL_ERROR_MSG */
  35745. /*
  35746. * Testing wc_ERR_remove_state
  35747. */
  35748. static int test_wc_ERR_remove_state(void)
  35749. {
  35750. int res = TEST_SKIPPED;
  35751. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35752. wc_ERR_remove_state();
  35753. res = TEST_SUCCESS;
  35754. #endif
  35755. return res;
  35756. } /* End test_wc_ERR_remove_state */
  35757. /*
  35758. * Testing wc_ERR_print_errors_fp
  35759. */
  35760. static int test_wc_ERR_print_errors_fp(void)
  35761. {
  35762. EXPECT_DECLS;
  35763. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  35764. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  35765. long sz;
  35766. XFILE fp = XBADFILE;
  35767. WOLFSSL_ERROR(BAD_FUNC_ARG);
  35768. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  35769. XBADFILE);
  35770. wc_ERR_print_errors_fp(fp);
  35771. #if defined(DEBUG_WOLFSSL)
  35772. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  35773. #ifdef NO_ERROR_QUEUE
  35774. ExpectIntEQ(sz = XFTELL(fp), 0);
  35775. #else
  35776. ExpectIntNE(sz = XFTELL(fp), 0);
  35777. #endif
  35778. #endif
  35779. if (fp != XBADFILE)
  35780. XFCLOSE(fp);
  35781. (void)sz;
  35782. #endif
  35783. return EXPECT_RESULT();
  35784. } /* End test_wc_ERR_print_errors_fp */
  35785. #ifdef DEBUG_WOLFSSL
  35786. static void Logging_cb(const int logLevel, const char *const logMessage)
  35787. {
  35788. (void)logLevel;
  35789. (void)logMessage;
  35790. }
  35791. #endif
  35792. /*
  35793. * Testing wolfSSL_GetLoggingCb
  35794. */
  35795. static int test_wolfSSL_GetLoggingCb(void)
  35796. {
  35797. EXPECT_DECLS;
  35798. #ifdef DEBUG_WOLFSSL
  35799. /* Testing without wolfSSL_SetLoggingCb() */
  35800. ExpectNull(wolfSSL_GetLoggingCb());
  35801. /* Testing with wolfSSL_SetLoggingCb() */
  35802. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  35803. ExpectNotNull(wolfSSL_GetLoggingCb());
  35804. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  35805. #endif
  35806. ExpectNull(wolfSSL_GetLoggingCb());
  35807. return EXPECT_RESULT();
  35808. } /* End test_wolfSSL_GetLoggingCb */
  35809. #endif /* !NO_BIO */
  35810. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  35811. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  35812. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  35813. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  35814. {
  35815. EXPECT_DECLS;
  35816. static const unsigned char key[] = "simple test key";
  35817. HMAC_CTX* hmac = NULL;
  35818. ENGINE* e = NULL;
  35819. unsigned char hash[WC_MAX_DIGEST_SIZE];
  35820. unsigned int len;
  35821. ExpectNotNull(hmac = HMAC_CTX_new());
  35822. HMAC_CTX_init(hmac);
  35823. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  35824. SSL_SUCCESS);
  35825. /* re-using test key as data to hash */
  35826. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  35827. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  35828. ExpectIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  35829. ExpectIntEQ(len, md_len);
  35830. ExpectIntEQ(HMAC_size(hmac), md_len);
  35831. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  35832. HMAC_cleanup(hmac);
  35833. HMAC_CTX_free(hmac);
  35834. len = 0;
  35835. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  35836. ExpectIntEQ(len, md_len);
  35837. return EXPECT_RESULT();
  35838. }
  35839. #endif
  35840. static int test_wolfSSL_HMAC(void)
  35841. {
  35842. EXPECT_DECLS;
  35843. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  35844. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  35845. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  35846. #ifndef NO_SHA256
  35847. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  35848. TEST_SUCCESS);
  35849. #endif
  35850. #ifdef WOLFSSL_SHA224
  35851. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  35852. TEST_SUCCESS);
  35853. #endif
  35854. #ifdef WOLFSSL_SHA384
  35855. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  35856. TEST_SUCCESS);
  35857. #endif
  35858. #ifdef WOLFSSL_SHA512
  35859. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  35860. TEST_SUCCESS);
  35861. #endif
  35862. #ifdef WOLFSSL_SHA3
  35863. #ifndef WOLFSSL_NOSHA3_224
  35864. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  35865. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  35866. #endif
  35867. #ifndef WOLFSSL_NOSHA3_256
  35868. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  35869. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  35870. #endif
  35871. #ifndef WOLFSSL_NOSHA3_384
  35872. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  35873. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  35874. #endif
  35875. #ifndef WOLFSSL_NOSHA3_512
  35876. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  35877. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  35878. #endif
  35879. #endif
  35880. #ifndef NO_SHA
  35881. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  35882. TEST_SUCCESS);
  35883. #endif
  35884. #endif
  35885. return EXPECT_RESULT();
  35886. }
  35887. static int test_wolfSSL_CMAC(void)
  35888. {
  35889. EXPECT_DECLS;
  35890. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  35891. defined(WOLFSSL_AES_DIRECT)
  35892. int i;
  35893. byte key[AES_128_KEY_SIZE];
  35894. CMAC_CTX* cmacCtx = NULL;
  35895. byte out[AES_BLOCK_SIZE];
  35896. size_t outLen = AES_BLOCK_SIZE;
  35897. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  35898. key[i] = i;
  35899. }
  35900. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  35901. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  35902. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  35903. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  35904. NULL), SSL_SUCCESS);
  35905. /* re-using test key as data to hash */
  35906. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  35907. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  35908. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  35909. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  35910. CMAC_CTX_free(cmacCtx);
  35911. /* give a key too small for the cipher, verify we get failure */
  35912. cmacCtx = NULL;
  35913. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  35914. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  35915. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  35916. NULL), SSL_FAILURE);
  35917. CMAC_CTX_free(cmacCtx);
  35918. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  35919. return EXPECT_RESULT();
  35920. }
  35921. static int test_wolfSSL_OBJ(void)
  35922. {
  35923. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  35924. * mode
  35925. */
  35926. EXPECT_DECLS;
  35927. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  35928. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  35929. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  35930. ASN1_OBJECT *obj = NULL;
  35931. ASN1_OBJECT *obj2 = NULL;
  35932. char buf[50];
  35933. XFILE fp = XBADFILE;
  35934. X509 *x509 = NULL;
  35935. X509_NAME *x509Name = NULL;
  35936. X509_NAME_ENTRY *x509NameEntry = NULL;
  35937. ASN1_OBJECT *asn1Name = NULL;
  35938. int numNames = 0;
  35939. BIO *bio = NULL;
  35940. int nid;
  35941. int i, j;
  35942. const char *f[] = {
  35943. #ifndef NO_RSA
  35944. "./certs/ca-cert.der",
  35945. #endif
  35946. #ifdef HAVE_ECC
  35947. "./certs/ca-ecc-cert.der",
  35948. "./certs/ca-ecc384-cert.der",
  35949. #endif
  35950. NULL};
  35951. ASN1_OBJECT *field_name_obj = NULL;
  35952. int lastpos = -1;
  35953. int tmp = -1;
  35954. ASN1_STRING *asn1 = NULL;
  35955. unsigned char *buf_dyn = NULL;
  35956. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  35957. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  35958. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  35959. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  35960. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  35961. ASN1_OBJECT_free(obj);
  35962. obj = NULL;
  35963. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  35964. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  35965. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  35966. #ifdef WOLFSSL_CERT_EXT
  35967. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  35968. #endif
  35969. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  35970. ExpectNotNull(obj2 = OBJ_dup(obj));
  35971. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  35972. ASN1_OBJECT_free(obj);
  35973. obj = NULL;
  35974. ASN1_OBJECT_free(obj2);
  35975. obj2 = NULL;
  35976. for (i = 0; f[i] != NULL; i++)
  35977. {
  35978. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  35979. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  35980. if (fp != XBADFILE) {
  35981. XFCLOSE(fp);
  35982. fp = XBADFILE;
  35983. }
  35984. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  35985. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  35986. /* Get the Common Name by using OBJ_txt2obj */
  35987. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  35988. do
  35989. {
  35990. lastpos = tmp;
  35991. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  35992. } while (tmp > -1);
  35993. ExpectIntNE(lastpos, -1);
  35994. ASN1_OBJECT_free(field_name_obj);
  35995. field_name_obj = NULL;
  35996. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  35997. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  35998. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  35999. /*
  36000. * All Common Names should be www.wolfssl.com
  36001. * This makes testing easier as we can test for the expected value.
  36002. */
  36003. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  36004. OPENSSL_free(buf_dyn);
  36005. buf_dyn = NULL;
  36006. bio = BIO_new(BIO_s_mem());
  36007. ExpectTrue(bio != NULL);
  36008. for (j = 0; j < numNames; j++)
  36009. {
  36010. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  36011. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  36012. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  36013. }
  36014. BIO_free(bio);
  36015. bio = NULL;
  36016. X509_free(x509);
  36017. x509 = NULL;
  36018. }
  36019. #ifdef HAVE_PKCS12
  36020. {
  36021. PKCS12 *p12 = NULL;
  36022. int boolRet;
  36023. EVP_PKEY *pkey = NULL;
  36024. const char *p12_f[] = {
  36025. #if !defined(NO_DES3) && !defined(NO_RSA)
  36026. "./certs/test-servercert.p12",
  36027. #endif
  36028. NULL};
  36029. for (i = 0; p12_f[i] != NULL; i++)
  36030. {
  36031. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  36032. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  36033. if (fp != XBADFILE) {
  36034. XFCLOSE(fp);
  36035. fp = XBADFILE;
  36036. }
  36037. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  36038. &pkey, &x509, NULL)) > 0);
  36039. wc_PKCS12_free(p12);
  36040. p12 = NULL;
  36041. EVP_PKEY_free(pkey);
  36042. x509Name = X509_get_issuer_name(x509);
  36043. ExpectNotNull(x509Name);
  36044. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  36045. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  36046. for (j = 0; j < numNames; j++)
  36047. {
  36048. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  36049. ExpectNotNull(asn1Name =
  36050. X509_NAME_ENTRY_get_object(x509NameEntry));
  36051. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  36052. }
  36053. BIO_free(bio);
  36054. bio = NULL;
  36055. X509_free(x509);
  36056. x509 = NULL;
  36057. }
  36058. }
  36059. #endif /* HAVE_PKCS12 */
  36060. #endif
  36061. return EXPECT_RESULT();
  36062. }
  36063. static int test_wolfSSL_OBJ_cmp(void)
  36064. {
  36065. EXPECT_DECLS;
  36066. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36067. ASN1_OBJECT *obj = NULL;
  36068. ASN1_OBJECT *obj2 = NULL;
  36069. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  36070. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  36071. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  36072. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  36073. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  36074. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  36075. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  36076. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  36077. ASN1_OBJECT_free(obj);
  36078. ASN1_OBJECT_free(obj2);
  36079. #endif
  36080. return EXPECT_RESULT();
  36081. }
  36082. static int test_wolfSSL_OBJ_txt2nid(void)
  36083. {
  36084. EXPECT_DECLS;
  36085. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  36086. defined(WOLFSSL_APACHE_HTTPD)
  36087. int i;
  36088. static const struct {
  36089. const char* sn;
  36090. const char* ln;
  36091. const char* oid;
  36092. int nid;
  36093. } testVals[] = {
  36094. #ifdef WOLFSSL_APACHE_HTTPD
  36095. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  36096. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  36097. NID_id_on_dnsSRV },
  36098. { "msUPN", "Microsoft User Principal Name",
  36099. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  36100. #endif
  36101. { NULL, NULL, NULL, NID_undef }
  36102. };
  36103. /* Invalid cases */
  36104. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  36105. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  36106. /* Valid cases */
  36107. for (i = 0; testVals[i].sn != NULL; i++) {
  36108. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  36109. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  36110. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  36111. }
  36112. #endif
  36113. return EXPECT_RESULT();
  36114. }
  36115. static int test_wolfSSL_OBJ_txt2obj(void)
  36116. {
  36117. EXPECT_DECLS;
  36118. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  36119. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  36120. int i;
  36121. char buf[50];
  36122. ASN1_OBJECT* obj = NULL;
  36123. static const struct {
  36124. const char* oidStr;
  36125. const char* sn;
  36126. const char* ln;
  36127. } objs_list[] = {
  36128. #if defined(WOLFSSL_APACHE_HTTPD)
  36129. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  36130. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  36131. #endif
  36132. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  36133. { NULL, NULL, NULL }
  36134. };
  36135. static const struct {
  36136. const char* numeric;
  36137. const char* name;
  36138. } objs_named[] = {
  36139. /* In dictionary but not in normal list. */
  36140. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  36141. /* Made up OID. */
  36142. { "1.3.5.7", "1.3.5.7" },
  36143. { NULL, NULL }
  36144. };
  36145. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  36146. ASN1_OBJECT_free(obj);
  36147. obj = NULL;
  36148. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  36149. ASN1_OBJECT_free(obj);
  36150. obj = NULL;
  36151. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  36152. /* Test numerical value of oid (oidStr) */
  36153. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  36154. /* Convert object back to text to confirm oid is correct */
  36155. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  36156. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  36157. ASN1_OBJECT_free(obj);
  36158. obj = NULL;
  36159. XMEMSET(buf, 0, sizeof(buf));
  36160. /* Test short name (sn) */
  36161. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  36162. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  36163. /* Convert object back to text to confirm oid is correct */
  36164. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  36165. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  36166. ASN1_OBJECT_free(obj);
  36167. obj = NULL;
  36168. XMEMSET(buf, 0, sizeof(buf));
  36169. /* Test long name (ln) - should fail when no_name = 1 */
  36170. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  36171. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  36172. /* Convert object back to text to confirm oid is correct */
  36173. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  36174. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  36175. ASN1_OBJECT_free(obj);
  36176. obj = NULL;
  36177. XMEMSET(buf, 0, sizeof(buf));
  36178. }
  36179. for (i = 0; objs_named[i].numeric != NULL; i++) {
  36180. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  36181. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  36182. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  36183. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  36184. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  36185. ASN1_OBJECT_free(obj);
  36186. obj = NULL;
  36187. }
  36188. #endif
  36189. return EXPECT_RESULT();
  36190. }
  36191. static int test_wolfSSL_PEM_write_bio_X509(void)
  36192. {
  36193. EXPECT_DECLS;
  36194. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  36195. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  36196. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  36197. !defined(NO_FILESYSTEM)
  36198. /* This test contains the hard coded expected
  36199. * lengths. Update if necessary */
  36200. XFILE fp = XBADFILE;
  36201. WOLFSSL_EVP_PKEY *priv = NULL;
  36202. BIO* input = NULL;
  36203. BIO* output = NULL;
  36204. X509* x509a = NULL;
  36205. X509* x509b = NULL;
  36206. ASN1_TIME* notBeforeA = NULL;
  36207. ASN1_TIME* notAfterA = NULL;
  36208. #ifndef NO_ASN_TIME
  36209. ASN1_TIME* notBeforeB = NULL;
  36210. ASN1_TIME* notAfterB = NULL;
  36211. #endif
  36212. int expectedLen;
  36213. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  36214. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  36215. if (fp != XBADFILE) {
  36216. XFCLOSE(fp);
  36217. fp = XBADFILE;
  36218. }
  36219. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  36220. "rb"));
  36221. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  36222. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  36223. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  36224. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  36225. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  36226. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  36227. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  36228. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  36229. /* compare length against expected */
  36230. expectedLen = 2000;
  36231. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  36232. #ifndef NO_ASN_TIME
  36233. /* read exported X509 PEM back into struct, sanity check on export,
  36234. * make sure notBefore/notAfter are the same and certs are identical. */
  36235. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  36236. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  36237. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  36238. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  36239. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  36240. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  36241. X509_free(x509b);
  36242. x509b = NULL;
  36243. #endif
  36244. /* Reset output buffer */
  36245. BIO_free(output);
  36246. output = NULL;
  36247. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  36248. /* Test forcing the AKID to be generated just from KeyIdentifier */
  36249. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  36250. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  36251. x509a->authKeyId = x509a->authKeyIdSrc;
  36252. x509a->authKeyIdSrc = NULL;
  36253. x509a->authKeyIdSrcSz = 0;
  36254. }
  36255. /* Resign to re-generate the der */
  36256. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  36257. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  36258. /* Check that we generate a smaller output since the AKID will
  36259. * only contain the KeyIdentifier without any additional
  36260. * information */
  36261. /* Here we copy the validity struct from the original */
  36262. expectedLen = 1688;
  36263. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  36264. /* Reset buffers and x509 */
  36265. BIO_free(input);
  36266. input = NULL;
  36267. BIO_free(output);
  36268. output = NULL;
  36269. X509_free(x509a);
  36270. x509a = NULL;
  36271. /* test CA and basicConstSet values are encoded when
  36272. * the cert is a CA */
  36273. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  36274. /* read PEM into X509 struct */
  36275. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  36276. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  36277. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  36278. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  36279. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  36280. * values are maintained and certs are identical.*/
  36281. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  36282. ExpectIntEQ(x509b->isCa, 1);
  36283. ExpectIntEQ(x509b->basicConstSet, 1);
  36284. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  36285. X509_free(x509a);
  36286. x509a = NULL;
  36287. X509_free(x509b);
  36288. x509b = NULL;
  36289. BIO_free(input);
  36290. input = NULL;
  36291. BIO_free(output);
  36292. output = NULL;
  36293. /* test CA and basicConstSet values are encoded when
  36294. * the cert is not CA */
  36295. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  36296. /* read PEM into X509 struct */
  36297. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  36298. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  36299. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  36300. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  36301. /* read exported X509 PEM back into struct, ensure isCa and
  36302. * basicConstSet values are maintained and certs are identical */
  36303. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  36304. ExpectIntEQ(x509b->isCa, 0);
  36305. ExpectIntEQ(x509b->basicConstSet, 1);
  36306. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  36307. wolfSSL_EVP_PKEY_free(priv);
  36308. X509_free(x509a);
  36309. X509_free(x509b);
  36310. BIO_free(input);
  36311. BIO_free(output);
  36312. #endif
  36313. return EXPECT_RESULT();
  36314. }
  36315. static int test_wolfSSL_X509_NAME_ENTRY(void)
  36316. {
  36317. EXPECT_DECLS;
  36318. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36319. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  36320. X509* x509 = NULL;
  36321. #ifndef NO_BIO
  36322. BIO* bio = NULL;
  36323. #endif
  36324. X509_NAME* nm = NULL;
  36325. X509_NAME_ENTRY* entry = NULL;
  36326. unsigned char cn[] = "another name to add";
  36327. #ifdef OPENSSL_ALL
  36328. int i;
  36329. int names_len = 0;
  36330. #endif
  36331. ExpectNotNull(x509 =
  36332. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  36333. #ifndef NO_BIO
  36334. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36335. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  36336. #endif
  36337. #ifdef WOLFSSL_CERT_REQ
  36338. {
  36339. X509_REQ* req = NULL;
  36340. #ifndef NO_BIO
  36341. BIO* bReq = NULL;
  36342. #endif
  36343. ExpectNotNull(req =
  36344. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  36345. #ifndef NO_BIO
  36346. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  36347. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  36348. BIO_free(bReq);
  36349. #endif
  36350. X509_free(req);
  36351. }
  36352. #endif
  36353. ExpectNotNull(nm = X509_get_subject_name(x509));
  36354. /* Test add entry */
  36355. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  36356. 0x0c, cn, (int)sizeof(cn)));
  36357. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  36358. #ifdef WOLFSSL_CERT_EXT
  36359. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  36360. (byte*)"support@wolfssl.com", 19, -1,
  36361. 1), WOLFSSL_SUCCESS);
  36362. #endif
  36363. X509_NAME_ENTRY_free(entry);
  36364. entry = NULL;
  36365. #ifdef WOLFSSL_CERT_REQ
  36366. {
  36367. unsigned char srv_pkcs9p[] = "Server";
  36368. unsigned char fvrtDrnk[] = "tequila";
  36369. unsigned char* der = NULL;
  36370. char* subject = NULL;
  36371. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  36372. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  36373. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  36374. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  36375. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  36376. ExpectNotNull(der);
  36377. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  36378. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  36379. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  36380. #ifdef DEBUG_WOLFSSL
  36381. if (subject != NULL) {
  36382. fprintf(stderr, "\n\t%s\n", subject);
  36383. }
  36384. #endif
  36385. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  36386. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  36387. }
  36388. #endif
  36389. /* Test add entry by text */
  36390. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  36391. 0x0c, cn, (int)sizeof(cn)));
  36392. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  36393. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  36394. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  36395. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  36396. #endif
  36397. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  36398. X509_NAME_ENTRY_free(entry);
  36399. entry = NULL;
  36400. /* Test add entry by NID */
  36401. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  36402. cn, -1, -1, 0), SSL_SUCCESS);
  36403. #ifdef OPENSSL_ALL
  36404. /* stack of name entry */
  36405. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  36406. for (i = 0; i < names_len; i++) {
  36407. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  36408. }
  36409. #endif
  36410. #ifndef NO_BIO
  36411. BIO_free(bio);
  36412. #endif
  36413. X509_free(x509); /* free's nm */
  36414. #endif
  36415. return EXPECT_RESULT();
  36416. }
  36417. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  36418. static int test_GENERAL_NAME_set0_othername(void) {
  36419. EXPECT_DECLS;
  36420. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36421. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  36422. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  36423. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  36424. defined(WOLFSSL_FPKI)
  36425. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  36426. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  36427. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  36428. const char * cert_fname = "./certs/server-cert.der";
  36429. const char * key_fname = "./certs/server-key.der";
  36430. X509* x509 = NULL;
  36431. GENERAL_NAME* gn = NULL;
  36432. GENERAL_NAMES* gns = NULL;
  36433. ASN1_OBJECT* upn_oid = NULL;
  36434. ASN1_UTF8STRING *utf8str = NULL;
  36435. ASN1_TYPE *value = NULL;
  36436. X509_EXTENSION * ext = NULL;
  36437. byte* pt = NULL;
  36438. byte der[4096];
  36439. int derSz = 0;
  36440. EVP_PKEY* priv = NULL;
  36441. XFILE f = XBADFILE;
  36442. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  36443. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  36444. if (f != XBADFILE) {
  36445. XFCLOSE(f);
  36446. f = XBADFILE;
  36447. }
  36448. ExpectNotNull(gn = GENERAL_NAME_new());
  36449. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  36450. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  36451. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  36452. ExpectNotNull(value = ASN1_TYPE_new());
  36453. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  36454. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  36455. wolfSSL_ASN1_STRING_free(utf8str);
  36456. }
  36457. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  36458. if (EXPECT_FAIL()) {
  36459. ASN1_TYPE_free(value);
  36460. }
  36461. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  36462. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  36463. if (EXPECT_FAIL()) {
  36464. GENERAL_NAME_free(gn);
  36465. gn = NULL;
  36466. }
  36467. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  36468. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  36469. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  36470. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  36471. if (f != XBADFILE) {
  36472. XFCLOSE(f);
  36473. f = XBADFILE;
  36474. }
  36475. pt = der;
  36476. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  36477. (const unsigned char**)&pt, derSz));
  36478. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36479. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  36480. gns = NULL;
  36481. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  36482. NULL));
  36483. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  36484. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  36485. ExpectIntEQ(gn->type, 0);
  36486. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  36487. ASN1_OBJECT_free(upn_oid);
  36488. X509_EXTENSION_free(ext);
  36489. X509_free(x509);
  36490. EVP_PKEY_free(priv);
  36491. #endif
  36492. return EXPECT_RESULT();
  36493. }
  36494. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  36495. static int test_othername_and_SID_ext(void) {
  36496. EXPECT_DECLS;
  36497. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36498. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  36499. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  36500. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  36501. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  36502. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  36503. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  36504. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  36505. const char* csr_fname = "./certs/csr.signed.der";
  36506. const char* key_fname = "./certs/server-key.der";
  36507. byte der[4096];
  36508. int derSz = 0;
  36509. X509_REQ* x509 = NULL;
  36510. STACK_OF(X509_EXTENSION) *exts = NULL;
  36511. X509_EXTENSION * san_ext = NULL;
  36512. X509_EXTENSION * ext = NULL;
  36513. GENERAL_NAME* gn = NULL;
  36514. GENERAL_NAMES* gns = NULL;
  36515. ASN1_OBJECT* upn_oid = NULL;
  36516. ASN1_UTF8STRING *utf8str = NULL;
  36517. ASN1_TYPE *value = NULL;
  36518. ASN1_STRING *extval = NULL;
  36519. /* SID extension. SID data format explained here:
  36520. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  36521. */
  36522. uint8_t SidExtension[] = {
  36523. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  36524. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  36525. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  36526. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  36527. uint8_t expectedAltName[] = {
  36528. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  36529. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  36530. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36531. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  36532. X509_EXTENSION *sid_ext = NULL;
  36533. ASN1_OBJECT* sid_oid = NULL;
  36534. ASN1_OCTET_STRING *sid_data = NULL;
  36535. EVP_PKEY* priv = NULL;
  36536. XFILE f = XBADFILE;
  36537. byte* pt = NULL;
  36538. BIO* bio = NULL;
  36539. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  36540. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  36541. if (f != XBADFILE) {
  36542. XFCLOSE(f);
  36543. f = XBADFILE;
  36544. }
  36545. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  36546. ExpectNotNull(gn = GENERAL_NAME_new());
  36547. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  36548. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  36549. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  36550. ExpectNotNull(value = ASN1_TYPE_new());
  36551. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  36552. if (EXPECT_FAIL()) {
  36553. ASN1_UTF8STRING_free(utf8str);
  36554. }
  36555. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  36556. if (EXPECT_FAIL()) {
  36557. ASN1_TYPE_free(value);
  36558. GENERAL_NAME_free(gn);
  36559. gn = NULL;
  36560. }
  36561. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  36562. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  36563. if (EXPECT_FAIL()) {
  36564. GENERAL_NAME_free(gn);
  36565. }
  36566. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  36567. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  36568. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  36569. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  36570. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  36571. sid_data));
  36572. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  36573. /* Ensure an empty stack doesn't raise an error. */
  36574. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  36575. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  36576. if (EXPECT_FAIL()) {
  36577. X509_EXTENSION_free(san_ext);
  36578. }
  36579. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  36580. if (EXPECT_FAIL()) {
  36581. X509_EXTENSION_free(sid_ext);
  36582. }
  36583. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  36584. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  36585. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  36586. if (f != XBADFILE)
  36587. XFCLOSE(f);
  36588. pt = der;
  36589. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  36590. (const unsigned char**)&pt, derSz));
  36591. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  36592. pt = der;
  36593. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  36594. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  36595. gns = NULL;
  36596. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  36597. exts = NULL;
  36598. ASN1_OBJECT_free(upn_oid);
  36599. ASN1_OBJECT_free(sid_oid);
  36600. ASN1_OCTET_STRING_free(sid_data);
  36601. X509_REQ_free(x509);
  36602. x509 = NULL;
  36603. EVP_PKEY_free(priv);
  36604. /* At this point everything used to generate what is in der is cleaned up.
  36605. * We now read back from der to confirm the extensions were inserted
  36606. * correctly. */
  36607. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  36608. ExpectNotNull(bio);
  36609. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  36610. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  36611. ExpectNotNull(x509);
  36612. BIO_free(bio);
  36613. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  36614. x509));
  36615. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  36616. /* Check the SID extension. */
  36617. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  36618. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  36619. ExpectIntEQ(extval->length, sizeof(SidExtension));
  36620. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  36621. /* Check the AltNames extension. */
  36622. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  36623. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  36624. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  36625. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  36626. 0);
  36627. /* Cleanup */
  36628. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  36629. NULL));
  36630. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36631. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  36632. ExpectIntEQ(gn->type, 0);
  36633. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  36634. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  36635. X509_REQ_free(x509);
  36636. #endif
  36637. return EXPECT_RESULT();
  36638. }
  36639. static int test_wolfSSL_X509_set_name(void)
  36640. {
  36641. EXPECT_DECLS;
  36642. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36643. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  36644. X509* x509 = NULL;
  36645. X509_NAME* name = NULL;
  36646. ExpectNotNull(name = X509_NAME_new());
  36647. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36648. (byte*)"wolfssl.com", 11, 0, 1),
  36649. WOLFSSL_SUCCESS);
  36650. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36651. (byte*)"support@wolfssl.com", 19, -1,
  36652. 1), WOLFSSL_SUCCESS);
  36653. ExpectNotNull(x509 = X509_new());
  36654. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  36655. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  36656. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  36657. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  36658. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  36659. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  36660. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  36661. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36662. X509_free(x509);
  36663. X509_NAME_free(name);
  36664. #endif /* OPENSSL_ALL && !NO_CERTS */
  36665. return EXPECT_RESULT();
  36666. }
  36667. static int test_wolfSSL_X509_set_notAfter(void)
  36668. {
  36669. EXPECT_DECLS;
  36670. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  36671. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  36672. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  36673. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  36674. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  36675. /* Generalized time will overflow time_t if not long */
  36676. X509* x = NULL;
  36677. BIO* bio = NULL;
  36678. ASN1_TIME *asn_time = NULL;
  36679. ASN1_TIME *time_check = NULL;
  36680. const int year = 365*24*60*60;
  36681. const int day = 24*60*60;
  36682. const int hour = 60*60;
  36683. const int mini = 60;
  36684. int offset_day;
  36685. unsigned char buf[25];
  36686. time_t t;
  36687. /*
  36688. * Setup asn_time. APACHE HTTPD uses time(NULL)
  36689. */
  36690. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  36691. offset_day = 7;
  36692. /*
  36693. * Free these.
  36694. */
  36695. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  36696. ExpectNotNull(asn_time);
  36697. ExpectNotNull(x = X509_new());
  36698. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36699. /*
  36700. * Tests
  36701. */
  36702. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  36703. /* time_check is simply (ANS1_TIME*)x->notAfter */
  36704. ExpectNotNull(time_check = X509_get_notAfter(x));
  36705. /* ANS1_TIME_check validates by checking if argument can be parsed */
  36706. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  36707. /* Convert to human readable format and compare to intended date */
  36708. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  36709. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  36710. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  36711. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  36712. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  36713. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  36714. /*
  36715. * Cleanup
  36716. */
  36717. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  36718. X509_free(x);
  36719. BIO_free(bio);
  36720. #endif
  36721. return EXPECT_RESULT();
  36722. }
  36723. static int test_wolfSSL_X509_set_notBefore(void)
  36724. {
  36725. EXPECT_DECLS;
  36726. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  36727. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  36728. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  36729. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  36730. X509* x = NULL;
  36731. BIO* bio = NULL;
  36732. ASN1_TIME *asn_time = NULL;
  36733. ASN1_TIME *time_check = NULL;
  36734. const int year = 365*24*60*60;
  36735. const int day = 24*60*60;
  36736. const int hour = 60*60;
  36737. const int mini = 60;
  36738. int offset_day;
  36739. unsigned char buf[25];
  36740. time_t t;
  36741. /*
  36742. * Setup asn_time. APACHE HTTPD uses time(NULL)
  36743. */
  36744. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  36745. offset_day = 7;
  36746. /*
  36747. * Free these.
  36748. */
  36749. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  36750. ExpectNotNull(asn_time);
  36751. ExpectNotNull(x = X509_new());
  36752. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36753. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  36754. /*
  36755. * Main Tests
  36756. */
  36757. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  36758. /* time_check == (ANS1_TIME*)x->notBefore */
  36759. ExpectNotNull(time_check = X509_get_notBefore(x));
  36760. /* ANS1_TIME_check validates by checking if argument can be parsed */
  36761. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  36762. /* Convert to human readable format and compare to intended date */
  36763. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  36764. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  36765. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  36766. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  36767. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  36768. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  36769. /*
  36770. * Cleanup
  36771. */
  36772. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  36773. X509_free(x);
  36774. BIO_free(bio);
  36775. #endif
  36776. return EXPECT_RESULT();
  36777. }
  36778. static int test_wolfSSL_X509_set_version(void)
  36779. {
  36780. EXPECT_DECLS;
  36781. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36782. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  36783. X509* x509 = NULL;
  36784. long v = 2L;
  36785. long maxInt = INT_MAX;
  36786. ExpectNotNull(x509 = X509_new());
  36787. /* These should pass. */
  36788. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  36789. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  36790. /* Fail Case: When v(long) is greater than x509->version(int). */
  36791. v = maxInt+1;
  36792. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  36793. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  36794. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  36795. /* Cleanup */
  36796. X509_free(x509);
  36797. #endif
  36798. return EXPECT_RESULT();
  36799. }
  36800. #ifndef NO_BIO
  36801. static int test_wolfSSL_BIO_gets(void)
  36802. {
  36803. EXPECT_DECLS;
  36804. #if defined(OPENSSL_EXTRA)
  36805. BIO* bio = NULL;
  36806. BIO* bio2 = NULL;
  36807. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  36808. char emp[] = "";
  36809. char bio_buffer[20];
  36810. int bufferSz = 20;
  36811. #ifdef OPENSSL_ALL
  36812. BUF_MEM* emp_bm = NULL;
  36813. BUF_MEM* msg_bm = NULL;
  36814. #endif
  36815. /* try with bad args */
  36816. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  36817. #ifdef OPENSSL_ALL
  36818. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  36819. #endif
  36820. /* try with real msg */
  36821. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  36822. XMEMSET(bio_buffer, 0, bufferSz);
  36823. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  36824. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  36825. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  36826. ExpectFalse(bio2 != BIO_next(bio));
  36827. /* make buffer filled with no terminating characters */
  36828. XMEMSET(bio_buffer, 1, bufferSz);
  36829. /* BIO_gets reads a line of data */
  36830. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  36831. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  36832. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  36833. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  36834. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  36835. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  36836. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  36837. #ifdef OPENSSL_ALL
  36838. /* test setting the mem_buf manually */
  36839. BIO_free(bio);
  36840. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  36841. ExpectNotNull(emp_bm = BUF_MEM_new());
  36842. ExpectNotNull(msg_bm = BUF_MEM_new());
  36843. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  36844. if (EXPECT_SUCCESS()) {
  36845. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  36846. msg_bm->data = NULL;
  36847. }
  36848. /* emp size is 1 for terminator */
  36849. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  36850. if (EXPECT_SUCCESS()) {
  36851. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  36852. emp_bm->data = emp;
  36853. msg_bm->data = msg;
  36854. }
  36855. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  36856. /* check reading an empty string */
  36857. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  36858. ExpectStrEQ(emp, bio_buffer);
  36859. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  36860. /* BIO_gets reads a line of data */
  36861. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  36862. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  36863. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  36864. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  36865. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  36866. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  36867. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  36868. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  36869. if (EXPECT_SUCCESS())
  36870. emp_bm->data = NULL;
  36871. BUF_MEM_free(emp_bm);
  36872. if (EXPECT_SUCCESS())
  36873. msg_bm->data = NULL;
  36874. BUF_MEM_free(msg_bm);
  36875. #endif
  36876. /* check not null terminated string */
  36877. BIO_free(bio);
  36878. bio = NULL;
  36879. msg[0] = 0x33;
  36880. msg[1] = 0x33;
  36881. msg[2] = 0x33;
  36882. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  36883. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  36884. ExpectIntEQ(bio_buffer[0], msg[0]);
  36885. ExpectIntEQ(bio_buffer[1], msg[1]);
  36886. ExpectIntNE(bio_buffer[2], msg[2]);
  36887. BIO_free(bio);
  36888. bio = NULL;
  36889. msg[3] = 0x33;
  36890. bio_buffer[3] = 0x33;
  36891. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  36892. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  36893. ExpectIntEQ(bio_buffer[0], msg[0]);
  36894. ExpectIntEQ(bio_buffer[1], msg[1]);
  36895. ExpectIntEQ(bio_buffer[2], msg[2]);
  36896. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  36897. /* check reading an empty string */
  36898. BIO_free(bio);
  36899. bio = NULL;
  36900. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  36901. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  36902. ExpectStrEQ(emp, bio_buffer);
  36903. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  36904. /* check error cases */
  36905. BIO_free(bio);
  36906. bio = NULL;
  36907. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  36908. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36909. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  36910. #if !defined(NO_FILESYSTEM)
  36911. {
  36912. BIO* f_bio = NULL;
  36913. XFILE f = XBADFILE;
  36914. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  36915. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  36916. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  36917. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  36918. if (EXPECT_FAIL() && (f != XBADFILE)) {
  36919. XFCLOSE(f);
  36920. }
  36921. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  36922. BIO_free(f_bio);
  36923. f_bio = NULL;
  36924. }
  36925. #endif /* NO_FILESYSTEM */
  36926. BIO_free(bio);
  36927. bio = NULL;
  36928. BIO_free(bio2);
  36929. bio2 = NULL;
  36930. /* try with type BIO */
  36931. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  36932. sizeof(msg));
  36933. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  36934. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  36935. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  36936. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  36937. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  36938. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  36939. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  36940. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  36941. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  36942. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  36943. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  36944. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  36945. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  36946. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  36947. BIO_free(bio);
  36948. bio = NULL;
  36949. BIO_free(bio2);
  36950. bio2 = NULL;
  36951. /* check reading an empty string */
  36952. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  36953. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  36954. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  36955. ExpectStrEQ(emp, bio_buffer);
  36956. BIO_free(bio);
  36957. bio = NULL;
  36958. #endif
  36959. return EXPECT_RESULT();
  36960. }
  36961. static int test_wolfSSL_BIO_puts(void)
  36962. {
  36963. EXPECT_DECLS;
  36964. #if defined(OPENSSL_EXTRA)
  36965. BIO* bio = NULL;
  36966. char input[] = "hello\0world\n.....ok\n\0";
  36967. char output[128];
  36968. XMEMSET(output, 0, sizeof(output));
  36969. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36970. ExpectIntEQ(BIO_puts(bio, input), 5);
  36971. ExpectIntEQ(BIO_pending(bio), 5);
  36972. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  36973. ExpectIntEQ(BIO_pending(bio), 19);
  36974. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  36975. ExpectStrEQ(output, "helloworld\n");
  36976. ExpectIntEQ(BIO_pending(bio), 8);
  36977. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  36978. ExpectStrEQ(output, ".....ok\n");
  36979. ExpectIntEQ(BIO_pending(bio), 0);
  36980. ExpectIntEQ(BIO_puts(bio, ""), -1);
  36981. BIO_free(bio);
  36982. #endif
  36983. return EXPECT_RESULT();
  36984. }
  36985. static int test_wolfSSL_BIO_dump(void)
  36986. {
  36987. EXPECT_DECLS;
  36988. #if defined(OPENSSL_EXTRA)
  36989. BIO* bio;
  36990. static const unsigned char data[] = {
  36991. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  36992. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  36993. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  36994. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  36995. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  36996. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  36997. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  36998. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  36999. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  37000. 0xB4
  37001. };
  37002. /* Generated with OpenSSL. */
  37003. static const char expected[] =
  37004. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  37005. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  37006. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  37007. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  37008. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  37009. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  37010. static const char expectedAll[] =
  37011. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  37012. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  37013. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  37014. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  37015. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  37016. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  37017. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  37018. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  37019. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  37020. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  37021. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  37022. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  37023. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  37024. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  37025. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  37026. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  37027. char output[16 * 80];
  37028. int i;
  37029. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37030. /* Example key dumped. */
  37031. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  37032. sizeof(expected) - 1);
  37033. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  37034. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  37035. /* Try every possible value for a character. */
  37036. for (i = 0; i < 256; i++)
  37037. output[i] = i;
  37038. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  37039. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  37040. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  37041. BIO_free(bio);
  37042. #endif
  37043. return EXPECT_RESULT();
  37044. }
  37045. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  37046. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  37047. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  37048. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  37049. {
  37050. (void)ssl;
  37051. (void)buf;
  37052. (void)sz;
  37053. (void)ctx;
  37054. return WOLFSSL_CBIO_ERR_WANT_READ;
  37055. }
  37056. #endif
  37057. static int test_wolfSSL_BIO_should_retry(void)
  37058. {
  37059. EXPECT_DECLS;
  37060. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  37061. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  37062. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  37063. tcp_ready ready;
  37064. func_args server_args;
  37065. THREAD_TYPE serverThread;
  37066. SOCKET_T sockfd = 0;
  37067. WOLFSSL_CTX* ctx = NULL;
  37068. WOLFSSL* ssl = NULL;
  37069. char msg[64] = "hello wolfssl!";
  37070. char reply[1024];
  37071. int msgSz = (int)XSTRLEN(msg);
  37072. int ret;
  37073. BIO* bio = NULL;
  37074. XMEMSET(&server_args, 0, sizeof(func_args));
  37075. #ifdef WOLFSSL_TIRTOS
  37076. fdOpenSession(Task_self());
  37077. #endif
  37078. StartTCP();
  37079. InitTcpReady(&ready);
  37080. #if defined(USE_WINDOWS_API)
  37081. /* use RNG to get random port if using windows */
  37082. ready.port = GetRandomPort();
  37083. #endif
  37084. server_args.signal = &ready;
  37085. start_thread(test_server_nofail, &server_args, &serverThread);
  37086. wait_tcp_ready(&server_args);
  37087. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37088. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  37089. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  37090. #endif
  37091. ExpectIntEQ(WOLFSSL_SUCCESS,
  37092. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  37093. ExpectIntEQ(WOLFSSL_SUCCESS,
  37094. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  37095. ExpectIntEQ(WOLFSSL_SUCCESS,
  37096. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  37097. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  37098. /* force retry */
  37099. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  37100. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  37101. ExpectNotNull(ssl);
  37102. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  37103. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  37104. if (EXPECT_FAIL()) {
  37105. wolfSSL_free(ssl);
  37106. ssl = NULL;
  37107. }
  37108. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  37109. ExpectIntNE(BIO_should_retry(bio), 0);
  37110. ExpectIntEQ(BIO_should_read(bio), 0);
  37111. ExpectIntEQ(BIO_should_write(bio), 0);
  37112. /* now perform successful connection */
  37113. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  37114. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  37115. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  37116. ret = wolfSSL_get_error(ssl, -1);
  37117. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  37118. ExpectIntNE(BIO_should_retry(bio), 0);
  37119. if (ret == WOLFSSL_ERROR_WANT_READ)
  37120. ExpectIntEQ(BIO_should_read(bio), 1);
  37121. else
  37122. ExpectIntEQ(BIO_should_read(bio), 0);
  37123. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  37124. ExpectIntEQ(BIO_should_write(bio), 1);
  37125. else
  37126. ExpectIntEQ(BIO_should_write(bio), 0);
  37127. }
  37128. else {
  37129. ExpectIntEQ(BIO_should_retry(bio), 0);
  37130. ExpectIntEQ(BIO_should_read(bio), 0);
  37131. ExpectIntEQ(BIO_should_write(bio), 0);
  37132. }
  37133. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  37134. XSTRLEN("I hear you fa shizzle!")), 0);
  37135. BIO_free(bio);
  37136. wolfSSL_CTX_free(ctx);
  37137. CloseSocket(sockfd);
  37138. join_thread(serverThread);
  37139. FreeTcpReady(&ready);
  37140. #ifdef WOLFSSL_TIRTOS
  37141. fdOpenSession(Task_self());
  37142. #endif
  37143. #endif
  37144. return EXPECT_RESULT();
  37145. }
  37146. static int test_wolfSSL_BIO_connect(void)
  37147. {
  37148. EXPECT_DECLS;
  37149. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  37150. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  37151. tcp_ready ready;
  37152. func_args server_args;
  37153. THREAD_TYPE serverThread;
  37154. BIO *tcpBio = NULL;
  37155. BIO *sslBio = NULL;
  37156. SSL_CTX* ctx = NULL;
  37157. SSL *ssl = NULL;
  37158. SSL *sslPtr;
  37159. char msg[] = "hello wolfssl!";
  37160. char reply[30];
  37161. char buff[10] = {0};
  37162. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37163. ExpectIntEQ(WOLFSSL_SUCCESS,
  37164. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  37165. ExpectIntEQ(WOLFSSL_SUCCESS,
  37166. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  37167. ExpectIntEQ(WOLFSSL_SUCCESS,
  37168. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  37169. /* Setup server */
  37170. XMEMSET(&server_args, 0, sizeof(func_args));
  37171. StartTCP();
  37172. InitTcpReady(&ready);
  37173. #if defined(USE_WINDOWS_API)
  37174. /* use RNG to get random port if using windows */
  37175. ready.port = GetRandomPort();
  37176. #endif
  37177. server_args.signal = &ready;
  37178. start_thread(test_server_nofail, &server_args, &serverThread);
  37179. wait_tcp_ready(&server_args);
  37180. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  37181. /* Start the test proper */
  37182. /* Setup the TCP BIO */
  37183. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  37184. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  37185. /* Setup the SSL object */
  37186. ExpectNotNull(ssl = SSL_new(ctx));
  37187. SSL_set_connect_state(ssl);
  37188. /* Setup the SSL BIO */
  37189. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  37190. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  37191. if (EXPECT_FAIL()) {
  37192. wolfSSL_free(ssl);
  37193. }
  37194. /* Verify that BIO_get_ssl works. */
  37195. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  37196. ExpectPtrEq(ssl, sslPtr);
  37197. /* Link BIO's so that sslBio uses tcpBio for IO */
  37198. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  37199. /* Do TCP connect */
  37200. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  37201. /* Do TLS handshake */
  37202. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  37203. /* Test writing */
  37204. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  37205. /* Expect length of default wolfSSL reply */
  37206. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  37207. /* Clean it all up */
  37208. BIO_free_all(sslBio);
  37209. /* Server clean up */
  37210. join_thread(serverThread);
  37211. FreeTcpReady(&ready);
  37212. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  37213. * after. */
  37214. XMEMSET(&server_args, 0, sizeof(func_args));
  37215. StartTCP();
  37216. InitTcpReady(&ready);
  37217. #if defined(USE_WINDOWS_API)
  37218. /* use RNG to get random port if using windows */
  37219. ready.port = GetRandomPort();
  37220. #endif
  37221. server_args.signal = &ready;
  37222. start_thread(test_server_nofail, &server_args, &serverThread);
  37223. wait_tcp_ready(&server_args);
  37224. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  37225. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  37226. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  37227. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  37228. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  37229. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  37230. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  37231. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  37232. /* Attempt to close the TLS connection gracefully. */
  37233. BIO_ssl_shutdown(sslBio);
  37234. BIO_free_all(sslBio);
  37235. join_thread(serverThread);
  37236. FreeTcpReady(&ready);
  37237. SSL_CTX_free(ctx);
  37238. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  37239. wc_ecc_fp_free(); /* free per thread cache */
  37240. #endif
  37241. #endif
  37242. return EXPECT_RESULT();
  37243. }
  37244. static int test_wolfSSL_BIO_tls(void)
  37245. {
  37246. EXPECT_DECLS;
  37247. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  37248. SSL_CTX* ctx = NULL;
  37249. SSL *ssl = NULL;
  37250. BIO *readBio = NULL;
  37251. BIO *writeBio = NULL;
  37252. int ret;
  37253. int err = 0;
  37254. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  37255. ExpectNotNull(ssl = SSL_new(ctx));
  37256. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  37257. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  37258. /* Qt reads data from write-bio,
  37259. * then writes the read data into plain packet.
  37260. * Qt reads data from plain packet,
  37261. * then writes the read data into read-bio.
  37262. */
  37263. SSL_set_bio(ssl, readBio, writeBio);
  37264. do {
  37265. #ifdef WOLFSSL_ASYNC_CRYPT
  37266. if (err == WC_PENDING_E) {
  37267. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  37268. if (ret < 0) { break; } else if (ret == 0) { continue; }
  37269. }
  37270. #endif
  37271. ret = SSL_connect(ssl);
  37272. err = SSL_get_error(ssl, 0);
  37273. } while (err == WC_PENDING_E);
  37274. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  37275. /* in this use case, should return WANT READ
  37276. * so that Qt will read the data from plain packet for next state.
  37277. */
  37278. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  37279. SSL_free(ssl);
  37280. SSL_CTX_free(ctx);
  37281. #endif
  37282. return EXPECT_RESULT();
  37283. }
  37284. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  37285. defined(HAVE_HTTP_CLIENT)
  37286. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  37287. {
  37288. BIO* clientBio;
  37289. SSL* sslClient;
  37290. SSL_CTX* ctx;
  37291. char connectAddr[20]; /* IP + port */;
  37292. (void)args;
  37293. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  37294. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  37295. AssertIntEQ(BIO_do_connect(clientBio), 1);
  37296. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  37297. AssertNotNull(sslClient = SSL_new(ctx));
  37298. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  37299. SSL_set_bio(sslClient, clientBio, clientBio);
  37300. AssertIntEQ(SSL_connect(sslClient), 1);
  37301. SSL_free(sslClient);
  37302. SSL_CTX_free(ctx);
  37303. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  37304. wc_ecc_fp_free(); /* free per thread cache */
  37305. #endif
  37306. WOLFSSL_RETURN_FROM_THREAD(0);
  37307. }
  37308. #endif
  37309. static int test_wolfSSL_BIO_accept(void)
  37310. {
  37311. EXPECT_DECLS;
  37312. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  37313. defined(HAVE_HTTP_CLIENT)
  37314. BIO* serverBindBio = NULL;
  37315. BIO* serverAcceptBio = NULL;
  37316. SSL* sslServer = NULL;
  37317. SSL_CTX* ctx = NULL;
  37318. func_args args;
  37319. THREAD_TYPE thread;
  37320. char port[10]; /* 10 bytes should be enough to store the string
  37321. * representation of the port */
  37322. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  37323. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  37324. /* First BIO_do_accept binds the port */
  37325. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  37326. XMEMSET(&args, 0, sizeof(func_args));
  37327. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  37328. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  37329. /* Let's plug it into SSL to test */
  37330. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  37331. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  37332. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  37333. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  37334. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  37335. ExpectNotNull(sslServer = SSL_new(ctx));
  37336. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  37337. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  37338. ExpectIntEQ(SSL_accept(sslServer), 1);
  37339. join_thread(thread);
  37340. BIO_free(serverBindBio);
  37341. SSL_free(sslServer);
  37342. SSL_CTX_free(ctx);
  37343. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  37344. wc_ecc_fp_free(); /* free per thread cache */
  37345. #endif
  37346. #endif
  37347. return EXPECT_RESULT();
  37348. }
  37349. static int test_wolfSSL_BIO_write(void)
  37350. {
  37351. EXPECT_DECLS;
  37352. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  37353. BIO* bio = NULL;
  37354. BIO* bio64 = NULL;
  37355. BIO* bio_mem = NULL;
  37356. BIO* ptr = NULL;
  37357. int sz;
  37358. char msg[] = "conversion test";
  37359. char out[40];
  37360. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  37361. void* bufPtr = NULL;
  37362. BUF_MEM* buf = NULL;
  37363. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  37364. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  37365. if (EXPECT_FAIL()) {
  37366. BIO_free(bio64);
  37367. }
  37368. /* now should convert to base64 then write to memory */
  37369. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  37370. BIO_flush(bio);
  37371. /* test BIO chain */
  37372. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  37373. ExpectNotNull(buf);
  37374. ExpectIntEQ(buf->length, 25);
  37375. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  37376. ExpectPtrEq(buf->data, bufPtr);
  37377. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  37378. sz = sizeof(out);
  37379. XMEMSET(out, 0, sz);
  37380. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  37381. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  37382. /* write then read should return the same message */
  37383. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  37384. sz = sizeof(out);
  37385. XMEMSET(out, 0, sz);
  37386. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  37387. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  37388. /* now try encoding with no line ending */
  37389. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  37390. #ifdef HAVE_EX_DATA
  37391. BIO_set_ex_data(bio64, 0, (void*) "data");
  37392. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  37393. #endif
  37394. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  37395. BIO_flush(bio);
  37396. sz = sizeof(out);
  37397. XMEMSET(out, 0, sz);
  37398. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  37399. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  37400. BIO_free_all(bio); /* frees bio64 also */
  37401. bio = NULL;
  37402. /* test with more than one bio64 in list */
  37403. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  37404. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  37405. if (EXPECT_FAIL()) {
  37406. BIO_free(bio64);
  37407. bio64 = NULL;
  37408. }
  37409. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  37410. ExpectNotNull(BIO_push(bio64, bio_mem));
  37411. if (EXPECT_FAIL()) {
  37412. BIO_free(bio_mem);
  37413. }
  37414. /* now should convert to base64 when stored and then decode with read */
  37415. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  37416. BIO_flush(bio);
  37417. sz = sizeof(out);
  37418. XMEMSET(out, 0, sz);
  37419. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  37420. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  37421. BIO_clear_flags(bio64, ~0);
  37422. BIO_set_retry_read(bio);
  37423. BIO_free_all(bio); /* frees bio64s also */
  37424. bio = NULL;
  37425. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  37426. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  37427. BIO_free(bio);
  37428. #endif
  37429. return EXPECT_RESULT();
  37430. }
  37431. static int test_wolfSSL_BIO_printf(void)
  37432. {
  37433. EXPECT_DECLS;
  37434. #if defined(OPENSSL_ALL)
  37435. BIO* bio = NULL;
  37436. int sz = 7;
  37437. char msg[] = "TLS 1.3 for the world";
  37438. char out[60];
  37439. char expected[] = "TLS 1.3 for the world : sz = 7";
  37440. XMEMSET(out, 0, sizeof(out));
  37441. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37442. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  37443. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  37444. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  37445. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  37446. BIO_free(bio);
  37447. #endif
  37448. return EXPECT_RESULT();
  37449. }
  37450. static int test_wolfSSL_BIO_f_md(void)
  37451. {
  37452. EXPECT_DECLS;
  37453. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  37454. BIO* bio = NULL;
  37455. BIO* mem = NULL;
  37456. char msg[] = "message to hash";
  37457. char out[60];
  37458. EVP_MD_CTX* ctx = NULL;
  37459. const unsigned char testKey[] =
  37460. {
  37461. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  37462. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  37463. 0x0b, 0x0b, 0x0b, 0x0b
  37464. };
  37465. const char testData[] = "Hi There";
  37466. const unsigned char testResult[] =
  37467. {
  37468. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  37469. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  37470. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  37471. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  37472. };
  37473. const unsigned char expectedHash[] =
  37474. {
  37475. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  37476. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  37477. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  37478. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  37479. };
  37480. const unsigned char emptyHash[] =
  37481. {
  37482. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  37483. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  37484. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  37485. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  37486. };
  37487. unsigned char check[sizeof(testResult) + 1];
  37488. size_t checkSz = -1;
  37489. EVP_PKEY* key = NULL;
  37490. XMEMSET(out, 0, sizeof(out));
  37491. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  37492. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  37493. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  37494. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  37495. /* should not be able to write/read yet since just digest wrapper and no
  37496. * data is passing through the bio */
  37497. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  37498. ExpectIntEQ(BIO_pending(bio), 0);
  37499. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  37500. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  37501. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  37502. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  37503. BIO_reset(bio);
  37504. /* append BIO mem to bio in order to read/write */
  37505. ExpectNotNull(bio = BIO_push(bio, mem));
  37506. XMEMSET(out, 0, sizeof(out));
  37507. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  37508. ExpectIntEQ(BIO_pending(bio), 16);
  37509. /* this just reads the message and does not hash it (gets calls final) */
  37510. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  37511. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  37512. /* create a message digest using BIO */
  37513. XMEMSET(out, 0, sizeof(out));
  37514. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  37515. ExpectIntEQ(BIO_pending(mem), 16);
  37516. ExpectIntEQ(BIO_pending(bio), 16);
  37517. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  37518. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  37519. BIO_free(bio);
  37520. bio = NULL;
  37521. BIO_free(mem);
  37522. mem = NULL;
  37523. /* test with HMAC */
  37524. XMEMSET(out, 0, sizeof(out));
  37525. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  37526. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  37527. BIO_get_md_ctx(bio, &ctx);
  37528. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  37529. (int)sizeof(testKey)));
  37530. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  37531. ExpectNotNull(bio = BIO_push(bio, mem));
  37532. BIO_write(bio, testData, (int)strlen(testData));
  37533. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  37534. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  37535. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  37536. EVP_PKEY_free(key);
  37537. BIO_free(bio);
  37538. BIO_free(mem);
  37539. #endif
  37540. return EXPECT_RESULT();
  37541. }
  37542. static int test_wolfSSL_BIO_up_ref(void)
  37543. {
  37544. EXPECT_DECLS;
  37545. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  37546. BIO* bio = NULL;
  37547. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  37548. ExpectIntEQ(BIO_up_ref(NULL), 0);
  37549. ExpectIntEQ(BIO_up_ref(bio), 1);
  37550. BIO_free(bio);
  37551. ExpectIntEQ(BIO_up_ref(bio), 1);
  37552. BIO_free(bio);
  37553. BIO_free(bio);
  37554. #endif
  37555. return EXPECT_RESULT();
  37556. }
  37557. static int test_wolfSSL_BIO_reset(void)
  37558. {
  37559. EXPECT_DECLS;
  37560. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  37561. BIO* bio = NULL;
  37562. byte buf[16];
  37563. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  37564. (word32)XSTRLEN("secure your data")));
  37565. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  37566. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  37567. XMEMSET(buf, 0, 16);
  37568. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  37569. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  37570. /* You cannot write to MEM BIO with read-only mode. */
  37571. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  37572. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  37573. XMEMSET(buf, 0, 16);
  37574. ExpectIntEQ(BIO_reset(bio), 0);
  37575. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  37576. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  37577. BIO_free(bio);
  37578. #endif
  37579. return EXPECT_RESULT();
  37580. }
  37581. #endif /* !NO_BIO */
  37582. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  37583. /* test that the callback arg is correct */
  37584. static int certCbArg = 0;
  37585. static int clientCertCb(WOLFSSL* ssl, void* arg)
  37586. {
  37587. if (ssl == NULL || arg != &certCbArg)
  37588. return 0;
  37589. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  37590. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  37591. return 0;
  37592. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  37593. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  37594. return 0;
  37595. return 1;
  37596. }
  37597. static int clientCertSetupCb(WOLFSSL_CTX* ctx)
  37598. {
  37599. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  37600. return TEST_SUCCESS;
  37601. }
  37602. /**
  37603. * This is only done because test_client_nofail has no way to stop
  37604. * certificate and key loading
  37605. */
  37606. static int clientCertClearCb(WOLFSSL* ssl)
  37607. {
  37608. /* Clear the loaded certs to force the callbacks to set them up */
  37609. SSL_certs_clear(ssl);
  37610. return TEST_SUCCESS;
  37611. }
  37612. static int serverCertCb(WOLFSSL* ssl, void* arg)
  37613. {
  37614. if (ssl == NULL || arg != &certCbArg)
  37615. return 0;
  37616. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  37617. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  37618. return 0;
  37619. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  37620. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  37621. return 0;
  37622. return 1;
  37623. }
  37624. static int serverCertSetupCb(WOLFSSL_CTX* ctx)
  37625. {
  37626. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  37627. return TEST_SUCCESS;
  37628. }
  37629. /**
  37630. * This is only done because test_server_nofail has no way to stop
  37631. * certificate and key loading
  37632. */
  37633. static int serverCertClearCb(WOLFSSL* ssl)
  37634. {
  37635. /* Clear the loaded certs to force the callbacks to set them up */
  37636. SSL_certs_clear(ssl);
  37637. return TEST_SUCCESS;
  37638. }
  37639. #endif
  37640. static int test_wolfSSL_cert_cb(void)
  37641. {
  37642. EXPECT_DECLS;
  37643. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  37644. test_ssl_cbf func_cb_client;
  37645. test_ssl_cbf func_cb_server;
  37646. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  37647. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  37648. func_cb_client.ctx_ready = clientCertSetupCb;
  37649. func_cb_client.ssl_ready = clientCertClearCb;
  37650. func_cb_server.ctx_ready = serverCertSetupCb;
  37651. func_cb_server.ssl_ready = serverCertClearCb;
  37652. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  37653. &func_cb_server, NULL), TEST_SUCCESS);
  37654. #endif
  37655. return EXPECT_RESULT();
  37656. }
  37657. static int test_wolfSSL_SESSION(void)
  37658. {
  37659. EXPECT_DECLS;
  37660. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  37661. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  37662. !defined(NO_SESSION_CACHE)
  37663. WOLFSSL* ssl = NULL;
  37664. WOLFSSL_CTX* ctx = NULL;
  37665. WOLFSSL_SESSION* sess = NULL;
  37666. WOLFSSL_SESSION* sess_copy = NULL;
  37667. #ifdef OPENSSL_EXTRA
  37668. #ifdef HAVE_EXT_CACHE
  37669. unsigned char* sessDer = NULL;
  37670. unsigned char* ptr = NULL;
  37671. int sz;
  37672. #endif
  37673. const unsigned char context[] = "user app context";
  37674. unsigned int contextSz = (unsigned int)sizeof(context);
  37675. #endif
  37676. int ret, err;
  37677. SOCKET_T sockfd;
  37678. tcp_ready ready;
  37679. func_args server_args;
  37680. THREAD_TYPE serverThread;
  37681. char msg[80];
  37682. const char* sendGET = "GET";
  37683. /* TLS v1.3 requires session tickets */
  37684. /* CHACHA and POLY1305 required for myTicketEncCb */
  37685. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  37686. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  37687. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  37688. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  37689. #else
  37690. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37691. #endif
  37692. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  37693. WOLFSSL_FILETYPE_PEM));
  37694. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  37695. WOLFSSL_FILETYPE_PEM));
  37696. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  37697. WOLFSSL_SUCCESS);
  37698. #ifdef WOLFSSL_ENCRYPTED_KEYS
  37699. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  37700. #endif
  37701. #ifdef HAVE_SESSION_TICKET
  37702. /* Use session tickets, for ticket tests below */
  37703. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  37704. #endif
  37705. XMEMSET(&server_args, 0, sizeof(func_args));
  37706. #ifdef WOLFSSL_TIRTOS
  37707. fdOpenSession(Task_self());
  37708. #endif
  37709. StartTCP();
  37710. InitTcpReady(&ready);
  37711. #if defined(USE_WINDOWS_API)
  37712. /* use RNG to get random port if using windows */
  37713. ready.port = GetRandomPort();
  37714. #endif
  37715. server_args.signal = &ready;
  37716. start_thread(test_server_nofail, &server_args, &serverThread);
  37717. wait_tcp_ready(&server_args);
  37718. /* client connection */
  37719. ExpectNotNull(ssl = wolfSSL_new(ctx));
  37720. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  37721. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  37722. #ifdef WOLFSSL_ASYNC_CRYPT
  37723. err = 0; /* Reset error */
  37724. #endif
  37725. do {
  37726. #ifdef WOLFSSL_ASYNC_CRYPT
  37727. if (err == WC_PENDING_E) {
  37728. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  37729. if (ret < 0) { break; } else if (ret == 0) { continue; }
  37730. }
  37731. #endif
  37732. ret = wolfSSL_connect(ssl);
  37733. err = wolfSSL_get_error(ssl, 0);
  37734. } while (err == WC_PENDING_E);
  37735. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  37736. #ifdef WOLFSSL_ASYNC_CRYPT
  37737. err = 0; /* Reset error */
  37738. #endif
  37739. do {
  37740. #ifdef WOLFSSL_ASYNC_CRYPT
  37741. if (err == WC_PENDING_E) {
  37742. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  37743. if (ret < 0) { break; } else if (ret == 0) { continue; }
  37744. }
  37745. #endif
  37746. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  37747. err = wolfSSL_get_error(ssl, 0);
  37748. } while (err == WC_PENDING_E);
  37749. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  37750. #ifdef WOLFSSL_ASYNC_CRYPT
  37751. err = 0; /* Reset error */
  37752. #endif
  37753. do {
  37754. #ifdef WOLFSSL_ASYNC_CRYPT
  37755. if (err == WC_PENDING_E) {
  37756. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  37757. if (ret < 0) { break; } else if (ret == 0) { continue; }
  37758. }
  37759. #endif
  37760. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  37761. err = wolfSSL_get_error(ssl, 0);
  37762. } while (err == WC_PENDING_E);
  37763. ExpectIntEQ(ret, 23);
  37764. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  37765. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  37766. #ifdef HAVE_EXT_CACHE
  37767. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  37768. * HAVE_EXT_CACHE we get new objects each time */
  37769. #endif
  37770. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  37771. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  37772. sess = wolfSSL_get_session(ssl);
  37773. #ifdef OPENSSL_EXTRA
  37774. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  37775. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  37776. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  37777. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  37778. #ifdef HAVE_SESSION_TICKET
  37779. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  37780. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  37781. SESSION_TICKET_HINT_DEFAULT);
  37782. #else
  37783. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  37784. #endif
  37785. #else
  37786. (void)sess;
  37787. #endif /* OPENSSL_EXTRA */
  37788. /* Retain copy of the session for later testing */
  37789. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  37790. wolfSSL_shutdown(ssl);
  37791. wolfSSL_free(ssl); ssl = NULL;
  37792. join_thread(serverThread);
  37793. FreeTcpReady(&ready);
  37794. #ifdef WOLFSSL_TIRTOS
  37795. fdOpenSession(Task_self());
  37796. #endif
  37797. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  37798. {
  37799. X509 *x509 = NULL;
  37800. char buf[30];
  37801. int bufSz = 0;
  37802. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  37803. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  37804. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  37805. sizeof(buf))), 0);
  37806. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  37807. if (bufSz == 7) {
  37808. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  37809. }
  37810. if (bufSz == 16) {
  37811. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  37812. }
  37813. }
  37814. #endif
  37815. #ifdef HAVE_EXT_CACHE
  37816. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  37817. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  37818. sess_copy = NULL;
  37819. #endif
  37820. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  37821. /* get session from DER and update the timeout */
  37822. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  37823. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  37824. wolfSSL_SESSION_free(sess); sess = NULL;
  37825. sess = NULL;
  37826. ptr = sessDer;
  37827. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  37828. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  37829. (const unsigned char**)&ptr, sz));
  37830. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  37831. sessDer = NULL;
  37832. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  37833. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  37834. #endif
  37835. /* successful set session test */
  37836. ExpectNotNull(ssl = wolfSSL_new(ctx));
  37837. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  37838. #ifdef HAVE_SESSION_TICKET
  37839. /* Test set/get session ticket */
  37840. {
  37841. const char* ticket = "This is a session ticket";
  37842. char buf[64] = {0};
  37843. word32 bufSz = (word32)sizeof(buf);
  37844. ExpectIntEQ(SSL_SUCCESS,
  37845. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  37846. (word32)XSTRLEN(ticket)));
  37847. ExpectIntEQ(SSL_SUCCESS,
  37848. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  37849. ExpectStrEQ(ticket, buf);
  37850. }
  37851. #endif
  37852. #ifdef OPENSSL_EXTRA
  37853. /* session timeout case */
  37854. /* make the session to be expired */
  37855. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  37856. XSLEEP_MS(1200);
  37857. /* SSL_set_session should reject specified session but return success
  37858. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  37859. */
  37860. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  37861. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  37862. #else
  37863. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  37864. #endif
  37865. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  37866. /* fail case with miss match session context IDs (use compatibility API) */
  37867. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  37868. SSL_SUCCESS);
  37869. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  37870. wolfSSL_free(ssl); ssl = NULL;
  37871. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  37872. SSL_FAILURE);
  37873. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  37874. SSL_SUCCESS);
  37875. ExpectNotNull(ssl = wolfSSL_new(ctx));
  37876. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  37877. #endif /* OPENSSL_EXTRA */
  37878. wolfSSL_free(ssl);
  37879. wolfSSL_SESSION_free(sess);
  37880. wolfSSL_CTX_free(ctx);
  37881. #endif
  37882. return EXPECT_RESULT();
  37883. }
  37884. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  37885. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  37886. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  37887. !defined(WOLFSSL_NO_TLS12)
  37888. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  37889. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  37890. {
  37891. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  37892. /* returns previous timeout value */
  37893. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  37894. #else
  37895. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  37896. #endif
  37897. }
  37898. /* set the session to timeout in a second */
  37899. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  37900. {
  37901. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  37902. }
  37903. /* store the client side session from the first successful connection */
  37904. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  37905. {
  37906. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  37907. NULL); /* ref count 1 */
  37908. }
  37909. /* wait till session is expired then set it in the WOLFSSL struct for use */
  37910. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  37911. {
  37912. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  37913. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  37914. WOLFSSL_SUCCESS);
  37915. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  37916. }
  37917. /* set expired session in the WOLFSSL struct for use */
  37918. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  37919. {
  37920. XSLEEP_MS(1200); /* wait a second for session to expire */
  37921. /* set the expired session, call to set session fails but continuing on
  37922. after failure should be handled here */
  37923. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  37924. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  37925. WOLFSSL_SUCCESS);
  37926. #else
  37927. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  37928. WOLFSSL_SUCCESS);
  37929. #endif
  37930. }
  37931. /* check that the expired session was not reused */
  37932. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  37933. {
  37934. /* since the session has expired it should not have been reused */
  37935. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  37936. }
  37937. #endif
  37938. static int test_wolfSSL_SESSION_expire_downgrade(void)
  37939. {
  37940. EXPECT_DECLS;
  37941. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  37942. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  37943. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  37944. !defined(WOLFSSL_NO_TLS12)
  37945. WOLFSSL_CTX* ctx = NULL;
  37946. callback_functions server_cbf, client_cbf;
  37947. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  37948. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  37949. /* force server side to use TLS 1.2 */
  37950. server_cbf.ctx = ctx;
  37951. server_cbf.method = wolfTLSv1_2_server_method;
  37952. client_cbf.method = wolfSSLv23_client_method;
  37953. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  37954. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  37955. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  37956. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  37957. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  37958. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  37959. /* set the previously created session and wait till expired */
  37960. server_cbf.ctx = ctx;
  37961. client_cbf.method = wolfSSLv23_client_method;
  37962. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  37963. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  37964. client_cbf.on_result =
  37965. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  37966. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  37967. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  37968. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  37969. /* set the previously created expired session */
  37970. server_cbf.ctx = ctx;
  37971. client_cbf.method = wolfSSLv23_client_method;
  37972. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  37973. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  37974. client_cbf.on_result =
  37975. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  37976. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  37977. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  37978. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  37979. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  37980. wolfSSL_CTX_free(ctx);
  37981. #endif
  37982. return EXPECT_RESULT();
  37983. }
  37984. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37985. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  37986. static int clientSessRemCountMalloc = 0;
  37987. static int serverSessRemCountMalloc = 0;
  37988. static int clientSessRemCountFree = 0;
  37989. static int serverSessRemCountFree = 0;
  37990. static WOLFSSL_CTX* serverSessCtx = NULL;
  37991. static WOLFSSL_SESSION* serverSess = NULL;
  37992. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  37993. !defined(NO_SESSION_CACHE_REF)
  37994. static WOLFSSL_CTX* clientSessCtx = NULL;
  37995. static WOLFSSL_SESSION* clientSess = NULL;
  37996. #endif
  37997. static int serverSessRemIdx = 3;
  37998. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  37999. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  38000. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  38001. {
  38002. int* side;
  38003. (void)ctx;
  38004. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  38005. if (side != NULL) {
  38006. if (*side == WOLFSSL_CLIENT_END)
  38007. clientSessRemCountFree++;
  38008. else
  38009. serverSessRemCountFree++;
  38010. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  38011. }
  38012. }
  38013. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  38014. {
  38015. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  38016. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  38017. !defined(NO_SESSION_CACHE_REF)
  38018. {
  38019. EXPECT_DECLS;
  38020. /* Allow downgrade, set min version, and disable TLS 1.3.
  38021. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  38022. * reference to the session cache. But with WOLFSSL_TLS13 and without
  38023. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  38024. * the session in the cache. In this case we need to downgrade to
  38025. * previous versions to just use the legacy session ID field. */
  38026. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  38027. SSL_SUCCESS);
  38028. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  38029. SSL_SUCCESS);
  38030. return EXPECT_RESULT();
  38031. }
  38032. #else
  38033. return TEST_SUCCESS;
  38034. #endif
  38035. }
  38036. static int SessRemSslSetupCb(WOLFSSL* ssl)
  38037. {
  38038. EXPECT_DECLS;
  38039. int* side;
  38040. if (SSL_is_server(ssl)) {
  38041. side = &sessRemCtx_Server;
  38042. serverSessRemCountMalloc++;
  38043. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  38044. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  38045. SSL_SUCCESS);
  38046. }
  38047. else {
  38048. side = &sessRemCtx_Client;
  38049. clientSessRemCountMalloc++;
  38050. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  38051. !defined(NO_SESSION_CACHE_REF)
  38052. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  38053. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  38054. SSL_SUCCESS);
  38055. #endif
  38056. }
  38057. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  38058. serverSessRemIdx, side), SSL_SUCCESS);
  38059. return EXPECT_RESULT();
  38060. }
  38061. #endif
  38062. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  38063. {
  38064. EXPECT_DECLS;
  38065. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  38066. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  38067. /* Check that the remove callback gets called for external data in a
  38068. * session object */
  38069. test_ssl_cbf func_cb;
  38070. XMEMSET(&func_cb, 0, sizeof(func_cb));
  38071. func_cb.ctx_ready = SessRemCtxSetupCb;
  38072. func_cb.on_result = SessRemSslSetupCb;
  38073. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  38074. NULL), TEST_SUCCESS);
  38075. /* Both should have been allocated */
  38076. ExpectIntEQ(clientSessRemCountMalloc, 1);
  38077. ExpectIntEQ(serverSessRemCountMalloc, 1);
  38078. /* This should not be called yet. Session wasn't evicted from cache yet. */
  38079. ExpectIntEQ(clientSessRemCountFree, 0);
  38080. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  38081. !defined(NO_SESSION_CACHE_REF)
  38082. /* Force a cache lookup */
  38083. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  38084. /* Force a cache update */
  38085. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  38086. /* This should set the timeout to 0 and call the remove callback from within
  38087. * the session cache. */
  38088. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  38089. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  38090. ExpectIntEQ(clientSessRemCountFree, 1);
  38091. #endif
  38092. /* Server session is in the cache so ex_data isn't free'd with the SSL
  38093. * object */
  38094. ExpectIntEQ(serverSessRemCountFree, 0);
  38095. /* Force a cache lookup */
  38096. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  38097. /* Force a cache update */
  38098. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  38099. /* This should set the timeout to 0 and call the remove callback from within
  38100. * the session cache. */
  38101. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  38102. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  38103. ExpectIntEQ(serverSessRemCountFree, 1);
  38104. /* Need to free the references that we kept */
  38105. SSL_CTX_free(serverSessCtx);
  38106. SSL_SESSION_free(serverSess);
  38107. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  38108. !defined(NO_SESSION_CACHE_REF)
  38109. SSL_CTX_free(clientSessCtx);
  38110. SSL_SESSION_free(clientSess);
  38111. #endif
  38112. #endif
  38113. return EXPECT_RESULT();
  38114. }
  38115. static int test_wolfSSL_ticket_keys(void)
  38116. {
  38117. EXPECT_DECLS;
  38118. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  38119. !defined(NO_WOLFSSL_SERVER)
  38120. WOLFSSL_CTX* ctx = NULL;
  38121. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  38122. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  38123. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  38124. WOLFSSL_FAILURE);
  38125. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  38126. WOLFSSL_FAILURE);
  38127. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  38128. WOLFSSL_FAILURE);
  38129. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  38130. WOLFSSL_FAILURE);
  38131. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  38132. WOLFSSL_FAILURE);
  38133. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  38134. WOLFSSL_FAILURE);
  38135. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  38136. WOLFSSL_FAILURE);
  38137. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  38138. WOLFSSL_FAILURE);
  38139. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  38140. WOLFSSL_FAILURE);
  38141. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  38142. WOLFSSL_FAILURE);
  38143. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  38144. WOLFSSL_FAILURE);
  38145. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  38146. WOLFSSL_FAILURE);
  38147. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  38148. WOLFSSL_FAILURE);
  38149. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  38150. WOLFSSL_FAILURE);
  38151. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  38152. WOLFSSL_SUCCESS);
  38153. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  38154. WOLFSSL_SUCCESS);
  38155. wolfSSL_CTX_free(ctx);
  38156. #endif
  38157. return EXPECT_RESULT();
  38158. }
  38159. #ifndef NO_BIO
  38160. static int test_wolfSSL_d2i_PUBKEY(void)
  38161. {
  38162. EXPECT_DECLS;
  38163. #if defined(OPENSSL_EXTRA)
  38164. BIO* bio = NULL;
  38165. EVP_PKEY* pkey = NULL;
  38166. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38167. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  38168. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  38169. /* RSA PUBKEY test */
  38170. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  38171. sizeof_client_keypub_der_2048), 0);
  38172. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  38173. EVP_PKEY_free(pkey);
  38174. pkey = NULL;
  38175. #endif
  38176. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  38177. /* ECC PUBKEY test */
  38178. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  38179. sizeof_ecc_clikeypub_der_256), 0);
  38180. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  38181. EVP_PKEY_free(pkey);
  38182. pkey = NULL;
  38183. #endif
  38184. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  38185. /* DSA PUBKEY test */
  38186. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  38187. sizeof_dsa_pub_key_der_2048), 0);
  38188. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  38189. EVP_PKEY_free(pkey);
  38190. pkey = NULL;
  38191. #endif
  38192. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  38193. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  38194. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  38195. (HAVE_FIPS_VERSION > 2))
  38196. /* DH PUBKEY test */
  38197. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  38198. sizeof_dh_pub_key_der_2048), 0);
  38199. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  38200. EVP_PKEY_free(pkey);
  38201. pkey = NULL;
  38202. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38203. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  38204. BIO_free(bio);
  38205. (void)pkey;
  38206. #endif
  38207. return EXPECT_RESULT();
  38208. }
  38209. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  38210. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  38211. {
  38212. EXPECT_DECLS;
  38213. BIO* bio = NULL;
  38214. EVP_PKEY* pkey = NULL;
  38215. WOLFSSL_CTX* ctx = NULL;
  38216. #if defined(WOLFSSL_KEY_GEN)
  38217. unsigned char buff[4096];
  38218. unsigned char* bufPtr = buff;
  38219. #endif
  38220. /* test creating new EVP_PKEY with bad arg */
  38221. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  38222. /* test loading RSA key using BIO */
  38223. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  38224. {
  38225. XFILE file = XBADFILE;
  38226. const char* fname = "./certs/server-key.der";
  38227. size_t sz;
  38228. byte* buf = NULL;
  38229. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  38230. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  38231. ExpectTrue((sz = XFTELL(file)) != 0);
  38232. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  38233. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  38234. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  38235. if (file != XBADFILE) {
  38236. XFCLOSE(file);
  38237. }
  38238. /* Test using BIO new mem and loading DER private key */
  38239. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  38240. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  38241. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  38242. BIO_free(bio);
  38243. bio = NULL;
  38244. EVP_PKEY_free(pkey);
  38245. pkey = NULL;
  38246. }
  38247. #endif
  38248. /* test loading ECC key using BIO */
  38249. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  38250. {
  38251. XFILE file = XBADFILE;
  38252. const char* fname = "./certs/ecc-key.der";
  38253. size_t sz;
  38254. byte* buf = NULL;
  38255. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  38256. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  38257. ExpectTrue((sz = XFTELL(file)) != 0);
  38258. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  38259. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  38260. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  38261. if (file != XBADFILE)
  38262. XFCLOSE(file);
  38263. /* Test using BIO new mem and loading DER private key */
  38264. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  38265. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  38266. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  38267. BIO_free(bio);
  38268. bio = NULL;
  38269. EVP_PKEY_free(pkey);
  38270. pkey = NULL;
  38271. }
  38272. #endif
  38273. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38274. #ifndef NO_WOLFSSL_SERVER
  38275. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38276. #else
  38277. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  38278. #endif
  38279. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  38280. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  38281. {
  38282. RSA* rsa = NULL;
  38283. /* Tests bad parameters */
  38284. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  38285. /* RSA not set yet, expecting to fail*/
  38286. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  38287. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  38288. /* set RSA using bio*/
  38289. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  38290. sizeof_client_key_der_2048), 0);
  38291. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  38292. ExpectNotNull(rsa);
  38293. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  38294. /* i2d RSAprivate key tests */
  38295. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  38296. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  38297. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  38298. sizeof_client_key_der_2048);
  38299. bufPtr -= sizeof_client_key_der_2048;
  38300. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  38301. sizeof_client_key_der_2048), 0);
  38302. bufPtr = NULL;
  38303. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  38304. sizeof_client_key_der_2048);
  38305. ExpectNotNull(bufPtr);
  38306. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  38307. sizeof_client_key_der_2048), 0);
  38308. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  38309. RSA_free(rsa);
  38310. rsa = RSA_new();
  38311. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  38312. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  38313. RSA_free(rsa);
  38314. }
  38315. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  38316. SSL_CTX_free(ctx);
  38317. ctx = NULL;
  38318. BIO_free(bio);
  38319. bio = NULL;
  38320. return EXPECT_RESULT();
  38321. }
  38322. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  38323. #endif /* !NO_BIO */
  38324. static int test_wolfSSL_sk_GENERAL_NAME(void)
  38325. {
  38326. EXPECT_DECLS;
  38327. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38328. !defined(NO_RSA)
  38329. X509* x509 = NULL;
  38330. GENERAL_NAME* gn = NULL;
  38331. unsigned char buf[4096];
  38332. const unsigned char* bufPt = NULL;
  38333. int bytes = 0;
  38334. int i;
  38335. int j;
  38336. XFILE f = XBADFILE;
  38337. STACK_OF(GENERAL_NAME)* sk = NULL;
  38338. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  38339. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  38340. if (f != XBADFILE)
  38341. XFCLOSE(f);
  38342. for (j = 0; j < 2; ++j) {
  38343. bufPt = buf;
  38344. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  38345. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  38346. NID_subject_alt_name, NULL, NULL));
  38347. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  38348. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  38349. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  38350. if (gn != NULL) {
  38351. switch (gn->type) {
  38352. case GEN_DNS:
  38353. fprintf(stderr, "found type GEN_DNS\n");
  38354. break;
  38355. case GEN_EMAIL:
  38356. fprintf(stderr, "found type GEN_EMAIL\n");
  38357. break;
  38358. case GEN_URI:
  38359. fprintf(stderr, "found type GEN_URI\n");
  38360. break;
  38361. }
  38362. }
  38363. }
  38364. X509_free(x509);
  38365. x509 = NULL;
  38366. if (j == 0) {
  38367. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  38368. }
  38369. else {
  38370. /*
  38371. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  38372. * it was supposed to. This is a regression test for that bug.
  38373. */
  38374. GENERAL_NAMES_free(sk);
  38375. }
  38376. sk = NULL;
  38377. }
  38378. #endif
  38379. return EXPECT_RESULT();
  38380. }
  38381. static int test_wolfSSL_GENERAL_NAME_print(void)
  38382. {
  38383. EXPECT_DECLS;
  38384. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  38385. X509* x509 = NULL;
  38386. GENERAL_NAME* gn = NULL;
  38387. unsigned char buf[4096];
  38388. const unsigned char* bufPt = NULL;
  38389. int bytes;
  38390. XFILE f = XBADFILE;
  38391. STACK_OF(GENERAL_NAME)* sk = NULL;
  38392. BIO* out = NULL;
  38393. unsigned char outbuf[128];
  38394. X509_EXTENSION* ext = NULL;
  38395. AUTHORITY_INFO_ACCESS* aia = NULL;
  38396. ACCESS_DESCRIPTION* ad = NULL;
  38397. ASN1_IA5STRING *dnsname = NULL;
  38398. const unsigned char v4Addr[] = {192,168,53,1};
  38399. const unsigned char v6Addr[] =
  38400. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  38401. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  38402. const unsigned char email[] =
  38403. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  38404. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  38405. const char* dnsStr = "DNS:example.com";
  38406. const char* uriStr = "URI:http://127.0.0.1:22220";
  38407. const char* v4addStr = "IP Address:192.168.53.1";
  38408. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  38409. const char* emailStr = "email:info@wolfssl.com";
  38410. const char* othrStr = "othername:<unsupported>";
  38411. const char* x400Str = "X400Name:<unsupported>";
  38412. const char* ediStr = "EdiPartyName:<unsupported>";
  38413. /* BIO to output */
  38414. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  38415. /* test for NULL param */
  38416. gn = NULL;
  38417. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  38418. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  38419. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  38420. /* test for GEN_DNS */
  38421. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  38422. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  38423. if (f != XBADFILE) {
  38424. XFCLOSE(f);
  38425. f = XBADFILE;
  38426. }
  38427. bufPt = buf;
  38428. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  38429. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  38430. NID_subject_alt_name, NULL, NULL));
  38431. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  38432. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38433. XMEMSET(outbuf, 0, sizeof(outbuf));
  38434. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38435. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  38436. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  38437. gn = NULL;
  38438. sk = NULL;
  38439. X509_free(x509);
  38440. x509 = NULL;
  38441. /* Lets test for setting as well. */
  38442. ExpectNotNull(gn = GENERAL_NAME_new());
  38443. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  38444. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  38445. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  38446. dnsname = NULL;
  38447. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38448. XMEMSET(outbuf, 0, sizeof(outbuf));
  38449. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38450. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  38451. GENERAL_NAME_free(gn);
  38452. /* test for GEN_URI */
  38453. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  38454. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  38455. if (f != XBADFILE) {
  38456. XFCLOSE(f);
  38457. f = XBADFILE;
  38458. }
  38459. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  38460. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  38461. ext));
  38462. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  38463. if (ad != NULL) {
  38464. gn = ad->location;
  38465. }
  38466. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38467. gn = NULL;
  38468. XMEMSET(outbuf,0,sizeof(outbuf));
  38469. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38470. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  38471. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  38472. aia = NULL;
  38473. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  38474. ExpectNotNull(aia);
  38475. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  38476. aia = NULL;
  38477. X509_free(x509);
  38478. x509 = NULL;
  38479. /* test for GEN_IPADD */
  38480. /* ip v4 address */
  38481. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  38482. if (gn != NULL) {
  38483. gn->type = GEN_IPADD;
  38484. if (gn->d.iPAddress != NULL) {
  38485. gn->d.iPAddress->length = sizeof(v4Addr);
  38486. }
  38487. }
  38488. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  38489. sizeof(v4Addr)), 1);
  38490. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38491. XMEMSET(outbuf,0,sizeof(outbuf));
  38492. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38493. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  38494. GENERAL_NAME_free(gn);
  38495. gn = NULL;
  38496. /* ip v6 address */
  38497. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  38498. if (gn != NULL) {
  38499. gn->type = GEN_IPADD;
  38500. if (gn->d.iPAddress != NULL) {
  38501. gn->d.iPAddress->length = sizeof(v6Addr);
  38502. }
  38503. }
  38504. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  38505. sizeof(v6Addr)), 1);
  38506. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38507. XMEMSET(outbuf,0,sizeof(outbuf));
  38508. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38509. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  38510. GENERAL_NAME_free(gn);
  38511. gn = NULL;
  38512. /* test for GEN_EMAIL */
  38513. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  38514. if (gn != NULL) {
  38515. gn->type = GEN_EMAIL;
  38516. if (gn->d.rfc822Name != NULL) {
  38517. gn->d.rfc822Name->length = sizeof(email);
  38518. }
  38519. }
  38520. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  38521. 1);
  38522. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38523. XMEMSET(outbuf,0,sizeof(outbuf));
  38524. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38525. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  38526. GENERAL_NAME_free(gn);
  38527. gn = NULL;
  38528. /* test for GEN_OTHERNAME */
  38529. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  38530. if (gn != NULL) {
  38531. gn->type = GEN_OTHERNAME;
  38532. }
  38533. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38534. XMEMSET(outbuf,0,sizeof(outbuf));
  38535. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38536. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  38537. GENERAL_NAME_free(gn);
  38538. gn = NULL;
  38539. /* test for GEN_X400 */
  38540. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  38541. if (gn != NULL) {
  38542. gn->type = GEN_X400;
  38543. }
  38544. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38545. XMEMSET(outbuf,0,sizeof(outbuf));
  38546. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38547. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  38548. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  38549. if (gn != NULL) {
  38550. gn->type = GEN_IA5;
  38551. }
  38552. GENERAL_NAME_free(gn);
  38553. gn = NULL;
  38554. /* test for GEN_EDIPARTY */
  38555. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  38556. if (gn != NULL) {
  38557. gn->type = GEN_EDIPARTY;
  38558. }
  38559. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  38560. XMEMSET(outbuf,0,sizeof(outbuf));
  38561. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  38562. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  38563. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  38564. if (gn != NULL) {
  38565. gn->type = GEN_IA5;
  38566. }
  38567. GENERAL_NAME_free(gn);
  38568. gn = NULL;
  38569. BIO_free(out);
  38570. #endif /* OPENSSL_ALL */
  38571. return EXPECT_RESULT();
  38572. }
  38573. static int test_wolfSSL_sk_DIST_POINT(void)
  38574. {
  38575. EXPECT_DECLS;
  38576. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  38577. !defined(NO_RSA)
  38578. X509* x509 = NULL;
  38579. unsigned char buf[4096];
  38580. const unsigned char* bufPt;
  38581. int bytes;
  38582. int i;
  38583. int j;
  38584. XFILE f = XBADFILE;
  38585. DIST_POINT* dp;
  38586. DIST_POINT_NAME* dpn;
  38587. GENERAL_NAME* gn;
  38588. ASN1_IA5STRING* uri;
  38589. STACK_OF(DIST_POINT)* dps = NULL;
  38590. STACK_OF(GENERAL_NAME)* gns = NULL;
  38591. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  38592. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  38593. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  38594. if (f != XBADFILE)
  38595. XFCLOSE(f);
  38596. bufPt = buf;
  38597. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  38598. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  38599. NID_crl_distribution_points, NULL, NULL));
  38600. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  38601. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  38602. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  38603. ExpectNotNull(dpn = dp->distpoint);
  38604. /* this should be type 0, fullname */
  38605. ExpectIntEQ(dpn->type, 0);
  38606. ExpectNotNull(gns = dp->distpoint->name.fullname);
  38607. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  38608. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  38609. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  38610. ExpectIntEQ(gn->type, GEN_URI);
  38611. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  38612. ExpectNotNull(uri->data);
  38613. ExpectIntGT(uri->length, 0);
  38614. }
  38615. }
  38616. X509_free(x509);
  38617. CRL_DIST_POINTS_free(dps);
  38618. #endif
  38619. return EXPECT_RESULT();
  38620. }
  38621. static int test_wolfSSL_MD4(void)
  38622. {
  38623. EXPECT_DECLS;
  38624. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  38625. MD4_CTX md4;
  38626. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  38627. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  38628. "789012345678901234567890";
  38629. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  38630. "\xcc\x05\x36";
  38631. int msgSz = (int)XSTRLEN(msg);
  38632. XMEMSET(out, 0, sizeof(out));
  38633. MD4_Init(&md4);
  38634. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  38635. MD4_Final(out, &md4);
  38636. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  38637. #endif
  38638. return EXPECT_RESULT();
  38639. }
  38640. static int test_wolfSSL_verify_mode(void)
  38641. {
  38642. EXPECT_DECLS;
  38643. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  38644. WOLFSSL* ssl = NULL;
  38645. WOLFSSL_CTX* ctx = NULL;
  38646. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38647. ExpectNotNull(ssl = SSL_new(ctx));
  38648. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  38649. SSL_free(ssl);
  38650. ssl = NULL;
  38651. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38652. ExpectNotNull(ssl = SSL_new(ctx));
  38653. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  38654. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  38655. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  38656. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  38657. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  38658. SSL_free(ssl);
  38659. ssl = NULL;
  38660. wolfSSL_CTX_set_verify(ctx,
  38661. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  38662. ExpectNotNull(ssl = SSL_new(ctx));
  38663. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  38664. ExpectIntEQ(SSL_get_verify_mode(ssl),
  38665. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  38666. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  38667. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  38668. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  38669. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  38670. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  38671. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  38672. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  38673. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  38674. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  38675. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  38676. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  38677. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  38678. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  38679. #endif
  38680. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  38681. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  38682. SSL_free(ssl);
  38683. SSL_CTX_free(ctx);
  38684. #endif
  38685. return EXPECT_RESULT();
  38686. }
  38687. static int test_wolfSSL_verify_depth(void)
  38688. {
  38689. EXPECT_DECLS;
  38690. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  38691. WOLFSSL* ssl = NULL;
  38692. WOLFSSL_CTX* ctx = NULL;
  38693. long depth;
  38694. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38695. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  38696. ExpectNotNull(ssl = SSL_new(ctx));
  38697. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  38698. SSL_free(ssl);
  38699. ssl = NULL;
  38700. SSL_CTX_set_verify_depth(ctx, -1);
  38701. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  38702. SSL_CTX_set_verify_depth(ctx, 2);
  38703. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  38704. ExpectNotNull(ssl = SSL_new(ctx));
  38705. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  38706. SSL_free(ssl);
  38707. SSL_CTX_free(ctx);
  38708. #endif
  38709. return EXPECT_RESULT();
  38710. }
  38711. static int test_wolfSSL_verify_result(void)
  38712. {
  38713. EXPECT_DECLS;
  38714. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  38715. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  38716. WOLFSSL* ssl = NULL;
  38717. WOLFSSL_CTX* ctx = NULL;
  38718. long result = 0xDEADBEEF;
  38719. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  38720. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38721. ExpectNotNull(ssl = SSL_new(ctx));
  38722. wolfSSL_set_verify_result(ssl, result);
  38723. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  38724. SSL_free(ssl);
  38725. SSL_CTX_free(ctx);
  38726. #endif
  38727. return EXPECT_RESULT();
  38728. }
  38729. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38730. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38731. * buffer of 64 bytes.
  38732. *
  38733. * returns the size of the digest buffer on success and a negative value on
  38734. * failure.
  38735. */
  38736. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38737. int* sz)
  38738. {
  38739. EXPECT_DECLS;
  38740. HMAC_CTX ctx1;
  38741. HMAC_CTX ctx2;
  38742. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38743. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38744. unsigned char long_key[] =
  38745. "0123456789012345678901234567890123456789"
  38746. "0123456789012345678901234567890123456789"
  38747. "0123456789012345678901234567890123456789"
  38748. "0123456789012345678901234567890123456789";
  38749. unsigned char msg[] = "message to hash";
  38750. unsigned int digestSz = 64;
  38751. int keySz = sizeof(key);
  38752. int long_keySz = sizeof(long_key);
  38753. int msgSz = sizeof(msg);
  38754. unsigned char digest2[64];
  38755. unsigned int digestSz2 = 64;
  38756. HMAC_CTX_init(&ctx1);
  38757. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38758. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38759. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38760. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38761. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38762. HMAC_CTX_cleanup(&ctx1);
  38763. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38764. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38765. HMAC_CTX_cleanup(&ctx2);
  38766. ExpectIntEQ(digestSz, digestSz2);
  38767. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38768. /* test HMAC_Init with NULL key */
  38769. /* init after copy */
  38770. HMAC_CTX_init(&ctx1);
  38771. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38772. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38773. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38774. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38775. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38776. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38777. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38778. HMAC_CTX_cleanup(&ctx1);
  38779. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38780. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38781. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38782. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38783. HMAC_CTX_cleanup(&ctx2);
  38784. ExpectIntEQ(digestSz, digestSz2);
  38785. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38786. /* long key */
  38787. HMAC_CTX_init(&ctx1);
  38788. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  38789. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38790. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38791. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38792. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38793. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38794. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38795. HMAC_CTX_cleanup(&ctx1);
  38796. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38797. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38798. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38799. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38800. HMAC_CTX_cleanup(&ctx2);
  38801. ExpectIntEQ(digestSz, digestSz2);
  38802. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38803. /* init before copy */
  38804. HMAC_CTX_init(&ctx1);
  38805. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38806. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38807. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38808. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38809. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38810. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38811. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38812. HMAC_CTX_cleanup(&ctx1);
  38813. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38814. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38815. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38816. HMAC_CTX_cleanup(&ctx2);
  38817. ExpectIntEQ(digestSz, digestSz2);
  38818. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38819. *sz = digestSz;
  38820. return EXPECT_RESULT();
  38821. }
  38822. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  38823. static int test_wolfSSL_HMAC_CTX(void)
  38824. {
  38825. EXPECT_DECLS;
  38826. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38827. unsigned char digest[64];
  38828. int digestSz;
  38829. #ifndef NO_SHA
  38830. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  38831. TEST_SUCCESS);
  38832. ExpectIntEQ(digestSz, 20);
  38833. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  38834. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  38835. #endif /* !NO_SHA */
  38836. #ifdef WOLFSSL_SHA224
  38837. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  38838. TEST_SUCCESS);
  38839. ExpectIntEQ(digestSz, 28);
  38840. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  38841. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  38842. "\x02\x0E", digest, digestSz), 0);
  38843. #endif /* WOLFSSL_SHA224 */
  38844. #ifndef NO_SHA256
  38845. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  38846. TEST_SUCCESS);
  38847. ExpectIntEQ(digestSz, 32);
  38848. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  38849. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  38850. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  38851. #endif /* !NO_SHA256 */
  38852. #ifdef WOLFSSL_SHA384
  38853. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  38854. TEST_SUCCESS);
  38855. ExpectIntEQ(digestSz, 48);
  38856. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  38857. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  38858. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  38859. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  38860. digest, digestSz), 0);
  38861. #endif /* WOLFSSL_SHA384 */
  38862. #ifdef WOLFSSL_SHA512
  38863. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  38864. TEST_SUCCESS);
  38865. ExpectIntEQ(digestSz, 64);
  38866. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  38867. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  38868. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  38869. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  38870. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  38871. digest, digestSz), 0);
  38872. #endif /* WOLFSSL_SHA512 */
  38873. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  38874. HAVE_FIPS_VERSION <= 2)
  38875. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  38876. TEST_SUCCESS);
  38877. ExpectIntEQ(digestSz, 16);
  38878. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  38879. "\xE4\x98\xDD", digest, digestSz), 0);
  38880. #endif /* !NO_MD5 */
  38881. #endif
  38882. return EXPECT_RESULT();
  38883. }
  38884. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  38885. static void sslMsgCb(int w, int version, int type, const void* buf,
  38886. size_t sz, SSL* ssl, void* arg)
  38887. {
  38888. int i;
  38889. unsigned char* pt = (unsigned char*)buf;
  38890. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  38891. (w)?"Writing":"Reading", (int)sz, version, type);
  38892. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  38893. fprintf(stderr, "\n");
  38894. (void)ssl;
  38895. (void)arg;
  38896. }
  38897. #endif /* OPENSSL_EXTRA */
  38898. static int test_wolfSSL_msg_callback(void)
  38899. {
  38900. EXPECT_DECLS;
  38901. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  38902. WOLFSSL* ssl = NULL;
  38903. WOLFSSL_CTX* ctx = NULL;
  38904. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38905. ExpectNotNull(ssl = SSL_new(ctx));
  38906. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  38907. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  38908. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  38909. SSL_free(ssl);
  38910. SSL_CTX_free(ctx);
  38911. #endif
  38912. return EXPECT_RESULT();
  38913. }
  38914. static int test_wolfSSL_SHA(void)
  38915. {
  38916. EXPECT_DECLS;
  38917. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  38918. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  38919. (!defined(HAVE_FIPS) || \
  38920. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  38921. {
  38922. const unsigned char in[] = "abc";
  38923. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  38924. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  38925. unsigned char out[WC_SHA_DIGEST_SIZE];
  38926. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  38927. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  38928. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38929. /* SHA interface test */
  38930. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  38931. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  38932. ExpectNotNull(SHA(in, 0, out));
  38933. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  38934. ExpectNotNull(SHA(NULL, 0, out));
  38935. ExpectNotNull(SHA(NULL, 0, NULL));
  38936. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  38937. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  38938. }
  38939. #endif
  38940. #if !defined(NO_SHA256)
  38941. {
  38942. const unsigned char in[] = "abc";
  38943. unsigned char expected[] =
  38944. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  38945. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  38946. "\x15\xAD";
  38947. unsigned char out[WC_SHA256_DIGEST_SIZE];
  38948. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  38949. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38950. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  38951. #else
  38952. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  38953. #endif
  38954. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  38955. }
  38956. #endif
  38957. #if defined(WOLFSSL_SHA384)
  38958. {
  38959. const unsigned char in[] = "abc";
  38960. unsigned char expected[] =
  38961. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  38962. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  38963. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  38964. "\xc8\x25\xa7";
  38965. unsigned char out[WC_SHA384_DIGEST_SIZE];
  38966. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  38967. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38968. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  38969. #else
  38970. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  38971. #endif
  38972. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  38973. }
  38974. #endif
  38975. #if defined(WOLFSSL_SHA512)
  38976. {
  38977. const unsigned char in[] = "abc";
  38978. unsigned char expected[] =
  38979. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  38980. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  38981. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  38982. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  38983. "\xa5\x4c\xa4\x9f";
  38984. unsigned char out[WC_SHA512_DIGEST_SIZE];
  38985. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  38986. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38987. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  38988. #else
  38989. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  38990. #endif
  38991. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  38992. }
  38993. #endif
  38994. #endif
  38995. return EXPECT_RESULT();
  38996. }
  38997. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  38998. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  38999. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  39000. static void binary_dump(void *ptr, int size)
  39001. {
  39002. #ifdef WOLFSSL_EVP_PRINT
  39003. int i = 0;
  39004. unsigned char *p = (unsigned char *) ptr;
  39005. fprintf(stderr, "{");
  39006. while ((p != NULL) && (i < size)) {
  39007. if ((i % 8) == 0) {
  39008. fprintf(stderr, "\n");
  39009. fprintf(stderr, " ");
  39010. }
  39011. fprintf(stderr, "0x%02x, ", p[i]);
  39012. i++;
  39013. }
  39014. fprintf(stderr, "\n};\n");
  39015. #else
  39016. (void) ptr;
  39017. (void) size;
  39018. #endif
  39019. }
  39020. static int last_val = 0x0f;
  39021. static int check_result(unsigned char *data, int len)
  39022. {
  39023. int i;
  39024. for ( ; len; ) {
  39025. last_val = (last_val + 1) % 16;
  39026. for (i = 0; i < 16; len--, i++, data++)
  39027. if (*data != last_val) {
  39028. return -1;
  39029. }
  39030. }
  39031. return 0;
  39032. }
  39033. static int r_offset;
  39034. static int w_offset;
  39035. static void init_offset(void)
  39036. {
  39037. r_offset = 0;
  39038. w_offset = 0;
  39039. }
  39040. static void get_record(unsigned char *data, unsigned char *buf, int len)
  39041. {
  39042. XMEMCPY(buf, data+r_offset, len);
  39043. r_offset += len;
  39044. }
  39045. static void set_record(unsigned char *data, unsigned char *buf, int len)
  39046. {
  39047. XMEMCPY(data+w_offset, buf, len);
  39048. w_offset += len;
  39049. }
  39050. static void set_plain(unsigned char *plain, int rec)
  39051. {
  39052. int i, j;
  39053. unsigned char *p = plain;
  39054. #define BLOCKSZ 16
  39055. for (i=0; i<(rec/BLOCKSZ); i++) {
  39056. for (j=0; j<BLOCKSZ; j++)
  39057. *p++ = (i % 16);
  39058. }
  39059. }
  39060. #endif
  39061. static int test_wolfSSL_EVP_Cipher_extra(void)
  39062. {
  39063. EXPECT_DECLS;
  39064. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  39065. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  39066. /* aes128-cbc, keylen=16, ivlen=16 */
  39067. byte aes128_cbc_key[] = {
  39068. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  39069. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  39070. };
  39071. byte aes128_cbc_iv[] = {
  39072. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  39073. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  39074. };
  39075. /* teset data size table */
  39076. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  39077. int test_drive2[] = {8, 3, 8, 512, 0};
  39078. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  39079. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  39080. int test_drive_len[100];
  39081. int ret = 0;
  39082. EVP_CIPHER_CTX *evp = NULL;
  39083. int ilen = 0;
  39084. int klen = 0;
  39085. int i, j;
  39086. const EVP_CIPHER *type;
  39087. byte *iv;
  39088. byte *key;
  39089. int ivlen;
  39090. int keylen;
  39091. #define RECORDS 16
  39092. #define BUFFSZ 512
  39093. byte plain [BUFFSZ * RECORDS];
  39094. byte cipher[BUFFSZ * RECORDS];
  39095. byte inb[BUFFSZ];
  39096. byte outb[BUFFSZ+16];
  39097. int outl = 0;
  39098. int inl;
  39099. iv = aes128_cbc_iv;
  39100. ivlen = sizeof(aes128_cbc_iv);
  39101. key = aes128_cbc_key;
  39102. keylen = sizeof(aes128_cbc_key);
  39103. type = EVP_aes_128_cbc();
  39104. set_plain(plain, BUFFSZ * RECORDS);
  39105. SSL_library_init();
  39106. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  39107. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  39108. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  39109. klen = EVP_CIPHER_CTX_key_length(evp);
  39110. if (klen > 0 && keylen != klen) {
  39111. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  39112. }
  39113. ilen = EVP_CIPHER_CTX_iv_length(evp);
  39114. if (ilen > 0 && ivlen != ilen) {
  39115. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  39116. }
  39117. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  39118. for (j = 0; j<RECORDS; j++)
  39119. {
  39120. inl = BUFFSZ;
  39121. get_record(plain, inb, inl);
  39122. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  39123. set_record(cipher, outb, outl);
  39124. }
  39125. for (i = 0; test_drive[i]; i++) {
  39126. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  39127. init_offset();
  39128. test_drive_len[i] = 0;
  39129. for (j = 0; test_drive[i][j]; j++)
  39130. {
  39131. inl = test_drive[i][j];
  39132. test_drive_len[i] += inl;
  39133. get_record(plain, inb, inl);
  39134. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  39135. 0);
  39136. /* output to cipher buffer, so that following Dec test can detect
  39137. if any error */
  39138. set_record(cipher, outb, outl);
  39139. }
  39140. EVP_CipherFinal(evp, outb, &outl);
  39141. if (outl > 0)
  39142. set_record(cipher, outb, outl);
  39143. }
  39144. for (i = 0; test_drive[i]; i++) {
  39145. last_val = 0x0f;
  39146. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  39147. init_offset();
  39148. for (j = 0; test_drive[i][j]; j++) {
  39149. inl = test_drive[i][j];
  39150. get_record(cipher, inb, inl);
  39151. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  39152. 0);
  39153. binary_dump(outb, outl);
  39154. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  39155. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  39156. }
  39157. ret = EVP_CipherFinal(evp, outb, &outl);
  39158. binary_dump(outb, outl);
  39159. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  39160. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  39161. ExpectTrue(ret);
  39162. }
  39163. EVP_CIPHER_CTX_free(evp);
  39164. evp = NULL;
  39165. /* Do an extra test to verify correct behavior with empty input. */
  39166. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  39167. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  39168. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  39169. klen = EVP_CIPHER_CTX_key_length(evp);
  39170. if (klen > 0 && keylen != klen) {
  39171. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  39172. }
  39173. ilen = EVP_CIPHER_CTX_iv_length(evp);
  39174. if (ilen > 0 && ivlen != ilen) {
  39175. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  39176. }
  39177. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  39178. /* outl should be set to 0 after passing NULL, 0 for input args. */
  39179. outl = -1;
  39180. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  39181. ExpectIntEQ(outl, 0);
  39182. EVP_CIPHER_CTX_free(evp);
  39183. #endif /* test_EVP_Cipher */
  39184. return EXPECT_RESULT();
  39185. }
  39186. static int test_wolfSSL_PEM_read_DHparams(void)
  39187. {
  39188. EXPECT_DECLS;
  39189. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  39190. !defined(NO_FILESYSTEM)
  39191. DH* dh = NULL;
  39192. XFILE fp = XBADFILE;
  39193. unsigned char derOut[300];
  39194. unsigned char* derOutBuf = derOut;
  39195. int derOutSz = 0;
  39196. unsigned char derExpected[300];
  39197. int derExpectedSz = 0;
  39198. XMEMSET(derOut, 0, sizeof(derOut));
  39199. XMEMSET(derExpected, 0, sizeof(derExpected));
  39200. /* open DH param file, read into DH struct */
  39201. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  39202. /* bad args */
  39203. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  39204. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  39205. /* good args */
  39206. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  39207. if (fp != XBADFILE) {
  39208. XFCLOSE(fp);
  39209. fp = XBADFILE;
  39210. }
  39211. /* read in certs/dh2048.der for comparison against exported params */
  39212. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  39213. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  39214. fp), 0);
  39215. if (fp != XBADFILE) {
  39216. XFCLOSE(fp);
  39217. fp = XBADFILE;
  39218. }
  39219. /* export DH back to DER and compare */
  39220. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  39221. ExpectIntEQ(derOutSz, derExpectedSz);
  39222. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  39223. DH_free(dh);
  39224. dh = NULL;
  39225. /* Test parsing with X9.42 header */
  39226. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  39227. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  39228. if (fp != XBADFILE)
  39229. XFCLOSE(fp);
  39230. DH_free(dh);
  39231. #endif
  39232. return EXPECT_RESULT();
  39233. }
  39234. static int test_wolfSSL_AES_ecb_encrypt(void)
  39235. {
  39236. EXPECT_DECLS;
  39237. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  39238. AES_KEY aes;
  39239. const byte msg[] =
  39240. {
  39241. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39242. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  39243. };
  39244. const byte verify[] =
  39245. {
  39246. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  39247. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  39248. };
  39249. const byte key[] =
  39250. {
  39251. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39252. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39253. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39254. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  39255. };
  39256. byte out[AES_BLOCK_SIZE];
  39257. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  39258. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39259. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  39260. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  39261. #ifdef HAVE_AES_DECRYPT
  39262. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  39263. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39264. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  39265. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  39266. #endif
  39267. /* test bad arguments */
  39268. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  39269. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  39270. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  39271. #endif
  39272. return EXPECT_RESULT();
  39273. }
  39274. static int test_wolfSSL_MD5(void)
  39275. {
  39276. EXPECT_DECLS;
  39277. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  39278. byte input1[] = "";
  39279. byte input2[] = "message digest";
  39280. byte hash[WC_MD5_DIGEST_SIZE];
  39281. unsigned char output1[] =
  39282. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  39283. unsigned char output2[] =
  39284. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  39285. WOLFSSL_MD5_CTX md5;
  39286. XMEMSET(&md5, 0, sizeof(md5));
  39287. /* Test cases for illegal parameters */
  39288. ExpectIntEQ(MD5_Init(NULL), 0);
  39289. ExpectIntEQ(MD5_Init(&md5), 1);
  39290. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  39291. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  39292. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  39293. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  39294. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  39295. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  39296. /* Init MD5 CTX */
  39297. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  39298. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  39299. 1);
  39300. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  39301. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  39302. /* Init MD5 CTX */
  39303. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  39304. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  39305. (int)XSTRLEN((const char*)input2)), 1);
  39306. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  39307. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  39308. #if !defined(NO_OLD_NAMES) && \
  39309. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  39310. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  39311. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  39312. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  39313. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  39314. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  39315. &hash);
  39316. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  39317. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  39318. &hash);
  39319. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  39320. {
  39321. byte data[] = "Data to be hashed.";
  39322. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  39323. ExpectNotNull(MD5(data, sizeof(data), NULL));
  39324. ExpectNotNull(MD5(data, sizeof(data), hash));
  39325. ExpectNotNull(MD5(NULL, 0, hash));
  39326. ExpectNull(MD5(NULL, sizeof(data), hash));
  39327. }
  39328. #endif
  39329. #endif
  39330. return EXPECT_RESULT();
  39331. }
  39332. static int test_wolfSSL_MD5_Transform(void)
  39333. {
  39334. EXPECT_DECLS;
  39335. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  39336. byte input1[] = "";
  39337. byte input2[] = "abc";
  39338. byte local[WC_MD5_BLOCK_SIZE];
  39339. word32 sLen = 0;
  39340. #ifdef BIG_ENDIAN_ORDER
  39341. unsigned char output1[] =
  39342. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  39343. unsigned char output2[] =
  39344. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  39345. #else
  39346. unsigned char output1[] =
  39347. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  39348. unsigned char output2[] =
  39349. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  39350. #endif
  39351. union {
  39352. wc_Md5 native;
  39353. MD5_CTX compat;
  39354. } md5;
  39355. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  39356. XMEMSET(&local, 0, sizeof(local));
  39357. /* sanity check */
  39358. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  39359. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  39360. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  39361. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  39362. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39363. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  39364. /* Init MD5 CTX */
  39365. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  39366. /* Do Transform*/
  39367. sLen = (word32)XSTRLEN((char*)input1);
  39368. XMEMCPY(local, input1, sLen);
  39369. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  39370. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  39371. /* Init MD5 CTX */
  39372. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  39373. sLen = (word32)XSTRLEN((char*)input2);
  39374. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  39375. XMEMCPY(local, input2, sLen);
  39376. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  39377. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  39378. #endif
  39379. return EXPECT_RESULT();
  39380. }
  39381. static int test_wolfSSL_SHA224(void)
  39382. {
  39383. EXPECT_DECLS;
  39384. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  39385. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  39386. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  39387. unsigned char input[] =
  39388. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  39389. unsigned char output[] =
  39390. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  39391. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  39392. size_t inLen;
  39393. byte hash[WC_SHA224_DIGEST_SIZE];
  39394. inLen = XSTRLEN((char*)input);
  39395. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  39396. ExpectNull(SHA224(NULL, inLen, hash));
  39397. ExpectNotNull(SHA224(input, 0, hash));
  39398. ExpectNotNull(SHA224(input, inLen, NULL));
  39399. ExpectNotNull(SHA224(NULL, 0, hash));
  39400. ExpectNotNull(SHA224(NULL, 0, NULL));
  39401. ExpectNotNull(SHA224(input, inLen, hash));
  39402. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  39403. #endif
  39404. return EXPECT_RESULT();
  39405. }
  39406. static int test_wolfSSL_SHA_Transform(void)
  39407. {
  39408. EXPECT_DECLS;
  39409. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  39410. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  39411. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  39412. byte input1[] = "";
  39413. byte input2[] = "abc";
  39414. byte local[WC_SHA_BLOCK_SIZE];
  39415. word32 sLen = 0;
  39416. #ifdef BIG_ENDIAN_ORDER
  39417. unsigned char output1[] =
  39418. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  39419. "\x09\xf7\x3a\x93";
  39420. unsigned char output2[] =
  39421. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  39422. "\x7c\x6e\x08\xb8";
  39423. #else
  39424. unsigned char output1[] =
  39425. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  39426. "\x93\x3a\xf7\x09";
  39427. unsigned char output2[] =
  39428. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  39429. "\xb8\x08\x6e\x7c";
  39430. #endif
  39431. union {
  39432. wc_Sha native;
  39433. SHA_CTX compat;
  39434. } sha;
  39435. union {
  39436. wc_Sha native;
  39437. SHA_CTX compat;
  39438. } sha1;
  39439. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  39440. XMEMSET(&local, 0, sizeof(local));
  39441. /* sanity check */
  39442. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  39443. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  39444. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  39445. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  39446. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  39447. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  39448. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  39449. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39450. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  39451. /* Init SHA CTX */
  39452. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  39453. /* Do Transform*/
  39454. sLen = (word32)XSTRLEN((char*)input1);
  39455. XMEMCPY(local, input1, sLen);
  39456. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  39457. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  39458. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  39459. /* Init SHA CTX */
  39460. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  39461. sLen = (word32)XSTRLEN((char*)input2);
  39462. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  39463. XMEMCPY(local, input2, sLen);
  39464. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  39465. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  39466. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  39467. /* SHA1 */
  39468. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  39469. /* Init SHA CTX */
  39470. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  39471. /* Do Transform*/
  39472. sLen = (word32)XSTRLEN((char*)input1);
  39473. XMEMCPY(local, input1, sLen);
  39474. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  39475. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  39476. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  39477. /* Init SHA CTX */
  39478. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  39479. sLen = (word32)XSTRLEN((char*)input2);
  39480. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  39481. XMEMCPY(local, input2, sLen);
  39482. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  39483. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  39484. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  39485. #endif
  39486. #endif
  39487. return EXPECT_RESULT();
  39488. }
  39489. static int test_wolfSSL_SHA256_Transform(void)
  39490. {
  39491. EXPECT_DECLS;
  39492. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  39493. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  39494. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  39495. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  39496. !defined(WOLFSSL_KCAPI_HASH)
  39497. byte input1[] = "";
  39498. byte input2[] = "abc";
  39499. byte local[WC_SHA256_BLOCK_SIZE];
  39500. word32 sLen = 0;
  39501. #ifdef BIG_ENDIAN_ORDER
  39502. unsigned char output1[] =
  39503. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  39504. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  39505. unsigned char output2[] =
  39506. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  39507. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  39508. #else
  39509. unsigned char output1[] =
  39510. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  39511. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  39512. unsigned char output2[] =
  39513. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  39514. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  39515. #endif
  39516. union {
  39517. wc_Sha256 native;
  39518. SHA256_CTX compat;
  39519. } sha256;
  39520. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  39521. XMEMSET(&local, 0, sizeof(local));
  39522. /* sanity check */
  39523. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  39524. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  39525. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  39526. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  39527. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39528. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  39529. /* Init SHA256 CTX */
  39530. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  39531. /* Do Transform*/
  39532. sLen = (word32)XSTRLEN((char*)input1);
  39533. XMEMCPY(local, input1, sLen);
  39534. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  39535. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  39536. 0);
  39537. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  39538. /* Init SHA256 CTX */
  39539. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  39540. sLen = (word32)XSTRLEN((char*)input2);
  39541. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  39542. XMEMCPY(local, input2, sLen);
  39543. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  39544. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  39545. 0);
  39546. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  39547. #endif
  39548. #endif
  39549. return EXPECT_RESULT();
  39550. }
  39551. static int test_wolfSSL_SHA256(void)
  39552. {
  39553. EXPECT_DECLS;
  39554. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  39555. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  39556. unsigned char input[] =
  39557. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  39558. unsigned char output[] =
  39559. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  39560. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  39561. "\x06\xC1";
  39562. size_t inLen;
  39563. byte hash[WC_SHA256_DIGEST_SIZE];
  39564. inLen = XSTRLEN((char*)input);
  39565. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  39566. ExpectNotNull(SHA256(input, inLen, hash));
  39567. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  39568. #endif
  39569. return EXPECT_RESULT();
  39570. }
  39571. static int test_wolfSSL_SHA512_Transform(void)
  39572. {
  39573. EXPECT_DECLS;
  39574. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  39575. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  39576. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  39577. !defined(WOLFSSL_KCAPI_HASH)
  39578. byte input1[] = "";
  39579. byte input2[] = "abc";
  39580. byte local[WC_SHA512_BLOCK_SIZE];
  39581. word32 sLen = 0;
  39582. #ifdef BIG_ENDIAN_ORDER
  39583. unsigned char output1[] =
  39584. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  39585. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  39586. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  39587. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  39588. unsigned char output2[] =
  39589. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  39590. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  39591. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  39592. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  39593. #else
  39594. unsigned char output1[] =
  39595. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  39596. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  39597. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  39598. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  39599. unsigned char output2[] =
  39600. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  39601. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  39602. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  39603. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  39604. #endif
  39605. union {
  39606. wc_Sha512 native;
  39607. SHA512_CTX compat;
  39608. } sha512;
  39609. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  39610. XMEMSET(&local, 0, sizeof(local));
  39611. /* sanity check */
  39612. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  39613. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  39614. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  39615. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  39616. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39617. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  39618. /* Init SHA512 CTX */
  39619. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  39620. /* Do Transform*/
  39621. sLen = (word32)XSTRLEN((char*)input1);
  39622. XMEMCPY(local, input1, sLen);
  39623. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  39624. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  39625. WC_SHA512_DIGEST_SIZE), 0);
  39626. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  39627. /* Init SHA512 CTX */
  39628. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  39629. sLen = (word32)XSTRLEN((char*)input2);
  39630. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  39631. XMEMCPY(local, input2, sLen);
  39632. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  39633. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  39634. WC_SHA512_DIGEST_SIZE), 0);
  39635. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  39636. (void)input1;
  39637. #endif
  39638. #endif
  39639. return EXPECT_RESULT();
  39640. }
  39641. static int test_wolfSSL_X509_get_serialNumber(void)
  39642. {
  39643. EXPECT_DECLS;
  39644. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  39645. ASN1_INTEGER* a = NULL;
  39646. BIGNUM* bn = NULL;
  39647. X509* x509 = NULL;
  39648. char *serialHex = NULL;
  39649. byte serial[3];
  39650. int serialSz;
  39651. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  39652. SSL_FILETYPE_PEM));
  39653. ExpectNotNull(a = X509_get_serialNumber(x509));
  39654. /* check on value of ASN1 Integer */
  39655. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  39656. a = NULL;
  39657. /* test setting serial number and then retrieving it */
  39658. ExpectNotNull(a = ASN1_INTEGER_new());
  39659. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  39660. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  39661. serialSz = sizeof(serial);
  39662. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  39663. WOLFSSL_SUCCESS);
  39664. ExpectIntEQ(serialSz, 1);
  39665. ExpectIntEQ(serial[0], 3);
  39666. ASN1_INTEGER_free(a);
  39667. a = NULL;
  39668. /* test setting serial number with 0's in it */
  39669. serial[0] = 0x01;
  39670. serial[1] = 0x00;
  39671. serial[2] = 0x02;
  39672. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  39673. if (a != NULL) {
  39674. a->data[0] = ASN_INTEGER;
  39675. a->data[1] = sizeof(serial);
  39676. XMEMCPY(&a->data[2], serial, sizeof(serial));
  39677. a->length = sizeof(serial) + 2;
  39678. }
  39679. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  39680. XMEMSET(serial, 0, sizeof(serial));
  39681. serialSz = sizeof(serial);
  39682. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  39683. WOLFSSL_SUCCESS);
  39684. ExpectIntEQ(serialSz, 3);
  39685. ExpectIntEQ(serial[0], 0x01);
  39686. ExpectIntEQ(serial[1], 0x00);
  39687. ExpectIntEQ(serial[2], 0x02);
  39688. ASN1_INTEGER_free(a);
  39689. a = NULL;
  39690. X509_free(x509); /* free's a */
  39691. ExpectNotNull(serialHex = BN_bn2hex(bn));
  39692. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  39693. ExpectStrEQ(serialHex, "01");
  39694. #else
  39695. ExpectStrEQ(serialHex, "1");
  39696. #endif
  39697. OPENSSL_free(serialHex);
  39698. ExpectIntEQ(BN_get_word(bn), 1);
  39699. BN_free(bn);
  39700. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  39701. ExpectNotNull(a = ASN1_INTEGER_new());
  39702. if (a != NULL) {
  39703. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  39704. DYNAMIC_TYPE_OPENSSL));
  39705. a->isDynamic = 1;
  39706. ASN1_INTEGER_free(a);
  39707. }
  39708. #endif
  39709. return EXPECT_RESULT();
  39710. }
  39711. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  39712. {
  39713. EXPECT_DECLS;
  39714. #if defined(OPENSSL_EXTRA)
  39715. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  39716. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  39717. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  39718. #endif
  39719. return EXPECT_RESULT();
  39720. }
  39721. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  39722. {
  39723. EXPECT_DECLS;
  39724. #if defined(OPENSSL_EXTRA)
  39725. #define MAX_HEXSTR_BUFSZ 9
  39726. #define NUM_CASES 5
  39727. struct Output {
  39728. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  39729. long ret;
  39730. };
  39731. int i;
  39732. int j;
  39733. const char* inputs[NUM_CASES] = {
  39734. "aabcd1357e",
  39735. "01:12:23:34:a5:b6:c7:d8:e9",
  39736. ":01:02",
  39737. "012",
  39738. ":ab:ac:d"
  39739. };
  39740. struct Output expectedOutputs[NUM_CASES] = {
  39741. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  39742. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  39743. {{0x01, 0x02}, 2},
  39744. {{0x00}, 0},
  39745. {{0x00}, 0}
  39746. };
  39747. long len = 0;
  39748. unsigned char* returnedBuf = NULL;
  39749. for (i = 0; i < NUM_CASES && EXPECT_SUCCESS(); ++i) {
  39750. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  39751. if (returnedBuf == NULL) {
  39752. ExpectIntEQ(expectedOutputs[i].ret, 0);
  39753. continue;
  39754. }
  39755. ExpectIntEQ(expectedOutputs[i].ret, len);
  39756. for (j = 0; j < len; ++j) {
  39757. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  39758. }
  39759. OPENSSL_free(returnedBuf);
  39760. returnedBuf = NULL;
  39761. }
  39762. #endif
  39763. return EXPECT_RESULT();
  39764. }
  39765. static int test_wolfSSL_X509_CA_num(void)
  39766. {
  39767. EXPECT_DECLS;
  39768. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39769. defined(HAVE_ECC) && !defined(NO_RSA)
  39770. WOLFSSL_X509_STORE *store = NULL;
  39771. WOLFSSL_X509 *x509_1 = NULL;
  39772. WOLFSSL_X509 *x509_2 = NULL;
  39773. int ca_num = 0;
  39774. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  39775. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  39776. WOLFSSL_FILETYPE_PEM));
  39777. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  39778. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  39779. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  39780. WOLFSSL_FILETYPE_PEM));
  39781. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  39782. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  39783. wolfSSL_X509_free(x509_1);
  39784. wolfSSL_X509_free(x509_2);
  39785. wolfSSL_X509_STORE_free(store);
  39786. #endif
  39787. return EXPECT_RESULT();
  39788. }
  39789. static int test_wolfSSL_X509_check_ca(void)
  39790. {
  39791. EXPECT_DECLS;
  39792. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  39793. WOLFSSL_X509 *x509 = NULL;
  39794. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  39795. WOLFSSL_FILETYPE_PEM));
  39796. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  39797. wolfSSL_X509_free(x509);
  39798. #endif
  39799. return EXPECT_RESULT();
  39800. }
  39801. static int test_wolfSSL_X509_check_ip_asc(void)
  39802. {
  39803. EXPECT_DECLS;
  39804. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  39805. WOLFSSL_X509 *x509 = NULL;
  39806. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  39807. WOLFSSL_FILETYPE_PEM));
  39808. #if 0
  39809. /* TODO: add cert gen for testing positive case */
  39810. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  39811. #endif
  39812. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  39813. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  39814. wolfSSL_X509_free(x509);
  39815. #endif
  39816. return EXPECT_RESULT();
  39817. }
  39818. static int test_wolfSSL_make_cert(void)
  39819. {
  39820. EXPECT_DECLS;
  39821. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  39822. defined(WOLFSSL_CERT_EXT)
  39823. int ret;
  39824. Cert cert;
  39825. CertName name;
  39826. RsaKey key;
  39827. WC_RNG rng;
  39828. byte der[FOURK_BUF];
  39829. word32 idx;
  39830. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  39831. #ifdef OPENSSL_EXTRA
  39832. const unsigned char* pt;
  39833. int certSz;
  39834. X509* x509 = NULL;
  39835. X509_NAME* x509name;
  39836. X509_NAME_ENTRY* entry;
  39837. ASN1_STRING* entryValue;
  39838. #endif
  39839. XMEMSET(&name, 0, sizeof(CertName));
  39840. /* set up cert name */
  39841. XMEMCPY(name.country, "US", sizeof("US"));
  39842. name.countryEnc = CTC_PRINTABLE;
  39843. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  39844. name.stateEnc = CTC_UTF8;
  39845. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  39846. name.localityEnc = CTC_UTF8;
  39847. XMEMCPY(name.sur, "Test", sizeof("Test"));
  39848. name.surEnc = CTC_UTF8;
  39849. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  39850. name.orgEnc = CTC_UTF8;
  39851. XMEMCPY(name.unit, "Development", sizeof("Development"));
  39852. name.unitEnc = CTC_UTF8;
  39853. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  39854. name.commonNameEnc = CTC_UTF8;
  39855. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  39856. name.serialDevEnc = CTC_PRINTABLE;
  39857. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  39858. name.userIdEnc = CTC_PRINTABLE;
  39859. #ifdef WOLFSSL_MULTI_ATTRIB
  39860. #if CTC_MAX_ATTRIB > 2
  39861. {
  39862. NameAttrib* n;
  39863. n = &name.name[0];
  39864. n->id = ASN_DOMAIN_COMPONENT;
  39865. n->type = CTC_UTF8;
  39866. n->sz = sizeof("com");
  39867. XMEMCPY(n->value, "com", sizeof("com"));
  39868. n = &name.name[1];
  39869. n->id = ASN_DOMAIN_COMPONENT;
  39870. n->type = CTC_UTF8;
  39871. n->sz = sizeof("wolfssl");
  39872. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  39873. }
  39874. #endif
  39875. #endif /* WOLFSSL_MULTI_ATTRIB */
  39876. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  39877. #ifndef HAVE_FIPS
  39878. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  39879. #else
  39880. ExpectIntEQ(wc_InitRng(&rng), 0);
  39881. #endif
  39882. /* load test RSA key */
  39883. idx = 0;
  39884. #if defined(USE_CERT_BUFFERS_1024)
  39885. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  39886. sizeof_server_key_der_1024), 0);
  39887. #elif defined(USE_CERT_BUFFERS_2048)
  39888. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  39889. sizeof_server_key_der_2048), 0);
  39890. #else
  39891. /* error case, no RSA key loaded, happens later */
  39892. (void)idx;
  39893. #endif
  39894. XMEMSET(&cert, 0 , sizeof(Cert));
  39895. ExpectIntEQ(wc_InitCert(&cert), 0);
  39896. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  39897. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  39898. cert.serialSz = (int)sizeof(mySerial);
  39899. cert.isCA = 1;
  39900. #ifndef NO_SHA256
  39901. cert.sigType = CTC_SHA256wRSA;
  39902. #else
  39903. cert.sigType = CTC_SHAwRSA;
  39904. #endif
  39905. /* add SKID from the Public Key */
  39906. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  39907. /* add AKID from the Public Key */
  39908. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  39909. ret = 0;
  39910. do {
  39911. #if defined(WOLFSSL_ASYNC_CRYPT)
  39912. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  39913. #endif
  39914. if (ret >= 0) {
  39915. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  39916. }
  39917. } while (ret == WC_PENDING_E);
  39918. ExpectIntGT(ret, 0);
  39919. #ifdef OPENSSL_EXTRA
  39920. /* der holds a certificate with DC's now check X509 parsing of it */
  39921. certSz = ret;
  39922. pt = der;
  39923. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  39924. ExpectNotNull(x509name = X509_get_subject_name(x509));
  39925. #ifdef WOLFSSL_MULTI_ATTRIB
  39926. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  39927. -1)), 5);
  39928. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  39929. idx)), 6);
  39930. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  39931. idx)), -1);
  39932. #endif /* WOLFSSL_MULTI_ATTRIB */
  39933. /* compare DN at index 0 */
  39934. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  39935. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  39936. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  39937. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  39938. #ifndef WOLFSSL_MULTI_ATTRIB
  39939. /* compare Serial Number */
  39940. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  39941. -1)), 7);
  39942. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  39943. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  39944. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  39945. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  39946. #endif
  39947. #ifdef WOLFSSL_MULTI_ATTRIB
  39948. /* get first and second DC and compare result */
  39949. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  39950. -1)), 5);
  39951. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  39952. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  39953. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  39954. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  39955. idx)), 6);
  39956. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  39957. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  39958. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  39959. #endif /* WOLFSSL_MULTI_ATTRIB */
  39960. /* try invalid index locations for regression test and sanity check */
  39961. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  39962. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  39963. X509_free(x509);
  39964. #endif /* OPENSSL_EXTRA */
  39965. wc_FreeRsaKey(&key);
  39966. wc_FreeRng(&rng);
  39967. #endif
  39968. return EXPECT_RESULT();
  39969. }
  39970. static int test_wolfSSL_X509_get_version(void)
  39971. {
  39972. EXPECT_DECLS;
  39973. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  39974. WOLFSSL_X509 *x509 = NULL;
  39975. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  39976. WOLFSSL_FILETYPE_PEM));
  39977. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  39978. wolfSSL_X509_free(x509);
  39979. #endif
  39980. return EXPECT_RESULT();
  39981. }
  39982. static int test_wolfSSL_DES_ncbc(void)
  39983. {
  39984. EXPECT_DECLS;
  39985. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39986. const_DES_cblock myDes;
  39987. DES_cblock iv = {1};
  39988. DES_key_schedule key = {0};
  39989. unsigned char msg[] = "hello wolfssl";
  39990. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  39991. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  39992. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  39993. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  39994. /* partial block test */
  39995. DES_set_key(&key, &myDes);
  39996. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  39997. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  39998. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  39999. DES_set_key(&key, &myDes);
  40000. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40001. *((byte*)&iv) = 1;
  40002. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  40003. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  40004. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  40005. /* full block test */
  40006. DES_set_key(&key, &myDes);
  40007. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  40008. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40009. *((byte*)&iv) = 1;
  40010. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  40011. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  40012. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  40013. DES_set_key(&key, &myDes);
  40014. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40015. *((byte*)&iv) = 1;
  40016. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  40017. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  40018. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  40019. #endif
  40020. return EXPECT_RESULT();
  40021. }
  40022. static int test_wolfSSL_AES_cbc_encrypt(void)
  40023. {
  40024. EXPECT_DECLS;
  40025. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  40026. AES_KEY aes;
  40027. AES_KEY* aesN = NULL;
  40028. size_t len = 0;
  40029. size_t lenB = 0;
  40030. int keySz0 = 0;
  40031. int keySzN = -1;
  40032. byte out[AES_BLOCK_SIZE] = {0};
  40033. byte* outN = NULL;
  40034. /* Test vectors retrieved from:
  40035. * <begin URL>
  40036. * https://csrc.nist.gov/
  40037. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  40038. * documents/aes/KAT_AES.zip
  40039. * </end URL>
  40040. */
  40041. const byte* pt128N = NULL;
  40042. byte* key128N = NULL;
  40043. byte* iv128N = NULL;
  40044. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  40045. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  40047. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  40048. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  40049. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  40050. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  40051. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  40052. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  40053. len = sizeof(pt128);
  40054. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  40055. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  40056. ExpectIntNE(XMEMCMP(b, g, h), i)
  40057. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  40058. /* Stressing wolfSSL_AES_cbc_encrypt() */
  40059. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  40060. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  40061. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  40062. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  40063. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  40064. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  40065. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  40066. /* Stressing wolfSSL_AES_set_encrypt_key */
  40067. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  40068. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  40069. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  40070. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  40071. /* Stressing wolfSSL_AES_set_decrypt_key */
  40072. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  40073. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  40074. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  40075. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  40076. #ifdef WOLFSSL_AES_128
  40077. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  40078. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40079. RESET_IV(iv128tmp, iv128);
  40080. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  40081. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  40082. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  40083. wc_AesFree((Aes*)&aes);
  40084. #ifdef HAVE_AES_DECRYPT
  40085. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  40086. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40087. RESET_IV(iv128tmp, iv128);
  40088. len = sizeof(ct128);
  40089. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  40090. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  40091. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  40092. wc_AesFree((Aes*)&aes);
  40093. #endif
  40094. #endif /* WOLFSSL_AES_128 */
  40095. #ifdef WOLFSSL_AES_192
  40096. {
  40097. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  40098. * Appendix F.2.3 */
  40099. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  40100. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  40101. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  40102. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  40103. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  40104. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  40105. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  40106. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  40107. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  40108. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  40109. len = sizeof(pt192);
  40110. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  40111. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40112. RESET_IV(iv192tmp, iv192);
  40113. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  40114. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  40115. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  40116. wc_AesFree((Aes*)&aes);
  40117. #ifdef HAVE_AES_DECRYPT
  40118. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  40119. len = sizeof(ct192);
  40120. RESET_IV(iv192tmp, iv192);
  40121. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40122. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  40123. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  40124. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  40125. wc_AesFree((Aes*)&aes);
  40126. #endif
  40127. }
  40128. #endif /* WOLFSSL_AES_192 */
  40129. #ifdef WOLFSSL_AES_256
  40130. {
  40131. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  40132. * Appendix F.2.5 */
  40133. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  40134. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  40135. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  40136. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  40137. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  40138. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  40139. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  40140. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  40141. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  40142. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  40143. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  40144. len = sizeof(pt256);
  40145. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  40146. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40147. RESET_IV(iv256tmp, iv256);
  40148. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  40149. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  40150. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  40151. wc_AesFree((Aes*)&aes);
  40152. #ifdef HAVE_AES_DECRYPT
  40153. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  40154. len = sizeof(ct256);
  40155. RESET_IV(iv256tmp, iv256);
  40156. XMEMSET(out, 0, AES_BLOCK_SIZE);
  40157. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  40158. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  40159. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  40160. wc_AesFree((Aes*)&aes);
  40161. #endif
  40162. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40163. {
  40164. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  40165. byte wrapPlain[sizeof(key256)] = { 0 };
  40166. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  40167. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  40168. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  40169. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  40170. 15), WOLFSSL_FAILURE);
  40171. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  40172. sizeof(key256)), sizeof(wrapCipher));
  40173. wc_AesFree((Aes*)&aes);
  40174. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  40175. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  40176. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  40177. 23), WOLFSSL_FAILURE);
  40178. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  40179. sizeof(wrapCipher)), sizeof(wrapPlain));
  40180. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  40181. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  40182. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  40183. wc_AesFree((Aes*)&aes);
  40184. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  40185. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  40186. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  40187. sizeof(key256)), sizeof(wrapCipher));
  40188. wc_AesFree((Aes*)&aes);
  40189. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  40190. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  40191. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  40192. sizeof(wrapCipher)), sizeof(wrapPlain));
  40193. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  40194. wc_AesFree((Aes*)&aes);
  40195. }
  40196. #endif /* HAVE_AES_KEYWRAP */
  40197. }
  40198. #endif /* WOLFSSL_AES_256 */
  40199. #endif
  40200. return EXPECT_RESULT();
  40201. }
  40202. static int test_wolfSSL_CRYPTO_cts128(void)
  40203. {
  40204. EXPECT_DECLS;
  40205. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  40206. defined(HAVE_CTS)
  40207. byte tmp[64]; /* Largest vector size */
  40208. /* Test vectors taken form RFC3962 Appendix B */
  40209. const testVector vects[] = {
  40210. {
  40211. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40212. "\x20",
  40213. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  40214. "\x97",
  40215. 17, 17
  40216. },
  40217. {
  40218. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40219. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  40220. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  40221. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  40222. 31, 31
  40223. },
  40224. {
  40225. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40226. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  40227. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  40228. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  40229. 32, 32
  40230. },
  40231. {
  40232. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40233. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40234. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  40235. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40236. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  40237. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  40238. 47, 47
  40239. },
  40240. {
  40241. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40242. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40243. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  40244. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40245. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  40246. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  40247. 48, 48
  40248. },
  40249. {
  40250. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  40251. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  40252. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  40253. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  40254. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  40255. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  40256. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  40257. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  40258. 64, 64
  40259. }
  40260. };
  40261. byte keyBytes[AES_128_KEY_SIZE] = {
  40262. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  40263. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  40264. };
  40265. size_t i;
  40266. XMEMSET(tmp, 0, sizeof(tmp));
  40267. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  40268. AES_KEY encKey;
  40269. AES_KEY decKey;
  40270. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  40271. XMEMSET(iv, 0, sizeof(iv));
  40272. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  40273. &encKey), 0);
  40274. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  40275. &decKey), 0);
  40276. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  40277. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  40278. vects[i].outLen);
  40279. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  40280. XMEMSET(iv, 0, sizeof(iv));
  40281. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  40282. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  40283. vects[i].inLen);
  40284. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  40285. }
  40286. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  40287. return EXPECT_RESULT();
  40288. }
  40289. #if defined(OPENSSL_ALL)
  40290. static int test_wolfSSL_sk_CIPHER_description(void)
  40291. {
  40292. EXPECT_DECLS;
  40293. #if !defined(NO_RSA)
  40294. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  40295. int i;
  40296. int numCiphers = 0;
  40297. const SSL_METHOD *method = NULL;
  40298. const SSL_CIPHER *cipher = NULL;
  40299. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  40300. SSL_CTX *ctx = NULL;
  40301. SSL *ssl = NULL;
  40302. char buf[256];
  40303. char test_str[9] = "0000000";
  40304. const char badStr[] = "unknown";
  40305. const char certPath[] = "./certs/client-cert.pem";
  40306. XMEMSET(buf, 0, sizeof(buf));
  40307. ExpectNotNull(method = TLSv1_2_client_method());
  40308. ExpectNotNull(ctx = SSL_CTX_new(method));
  40309. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40310. SSL_CTX_set_verify_depth(ctx, 4);
  40311. SSL_CTX_set_options(ctx, flags);
  40312. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  40313. WOLFSSL_SUCCESS);
  40314. ExpectNotNull(ssl = SSL_new(ctx));
  40315. /* SSL_get_ciphers returns a stack of all configured ciphers
  40316. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  40317. */
  40318. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  40319. /* loop through the amount of supportedCiphers */
  40320. numCiphers = sk_num(supportedCiphers);
  40321. for (i = 0; i < numCiphers; ++i) {
  40322. int j;
  40323. /* sk_value increments "sk->data.cipher->cipherOffset".
  40324. * wolfSSL_sk_CIPHER_description sets the description for
  40325. * the cipher based on the provided offset.
  40326. */
  40327. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  40328. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  40329. }
  40330. /* Search cipher description string for "unknown" descriptor */
  40331. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  40332. int k = 0;
  40333. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  40334. test_str[k] = badStr[k];
  40335. j++;
  40336. k++;
  40337. }
  40338. }
  40339. /* Fail if test_str == badStr == "unknown" */
  40340. ExpectStrNE(test_str,badStr);
  40341. }
  40342. SSL_free(ssl);
  40343. SSL_CTX_free(ctx);
  40344. #endif
  40345. return EXPECT_RESULT();
  40346. }
  40347. static int test_wolfSSL_get_ciphers_compat(void)
  40348. {
  40349. EXPECT_DECLS;
  40350. #if !defined(NO_RSA)
  40351. const SSL_METHOD *method = NULL;
  40352. const char certPath[] = "./certs/client-cert.pem";
  40353. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  40354. SSL_CTX *ctx = NULL;
  40355. WOLFSSL *ssl = NULL;
  40356. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  40357. ExpectNotNull(method = SSLv23_client_method());
  40358. ExpectNotNull(ctx = SSL_CTX_new(method));
  40359. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  40360. SSL_CTX_set_verify_depth(ctx, 4);
  40361. SSL_CTX_set_options(ctx, flags);
  40362. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  40363. WOLFSSL_SUCCESS);
  40364. ExpectNotNull(ssl = SSL_new(ctx));
  40365. /* Test Bad NULL input */
  40366. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  40367. /* Test for Good input */
  40368. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  40369. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  40370. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  40371. SSL_free(ssl);
  40372. SSL_CTX_free(ctx);
  40373. #endif
  40374. return EXPECT_RESULT();
  40375. }
  40376. static int test_wolfSSL_X509_PUBKEY_get(void)
  40377. {
  40378. EXPECT_DECLS;
  40379. WOLFSSL_X509_PUBKEY pubkey;
  40380. WOLFSSL_X509_PUBKEY* key;
  40381. WOLFSSL_EVP_PKEY evpkey ;
  40382. WOLFSSL_EVP_PKEY* evpPkey;
  40383. WOLFSSL_EVP_PKEY* retEvpPkey;
  40384. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  40385. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  40386. key = &pubkey;
  40387. evpPkey = &evpkey;
  40388. evpPkey->type = WOLFSSL_SUCCESS;
  40389. key->pkey = evpPkey;
  40390. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  40391. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  40392. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  40393. key->pkey = NULL;
  40394. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  40395. return EXPECT_RESULT();
  40396. }
  40397. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  40398. {
  40399. EXPECT_DECLS;
  40400. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  40401. DSA *dsa = NULL;
  40402. DSA *setDsa = NULL;
  40403. EVP_PKEY *pkey = NULL;
  40404. EVP_PKEY *set1Pkey = NULL;
  40405. SHA_CTX sha;
  40406. byte signature[DSA_SIG_SIZE];
  40407. byte hash[WC_SHA_DIGEST_SIZE];
  40408. word32 bytes;
  40409. int answer;
  40410. #ifdef USE_CERT_BUFFERS_1024
  40411. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  40412. int dsaKeySz = sizeof_dsa_key_der_1024;
  40413. byte tmp[ONEK_BUF];
  40414. XMEMSET(tmp, 0, sizeof(tmp));
  40415. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  40416. bytes = dsaKeySz;
  40417. #elif defined(USE_CERT_BUFFERS_2048)
  40418. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  40419. int dsaKeySz = sizeof_dsa_key_der_2048;
  40420. byte tmp[TWOK_BUF];
  40421. XMEMSET(tmp, 0, sizeof(tmp));
  40422. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  40423. bytes = dsaKeySz;
  40424. #else
  40425. byte tmp[TWOK_BUF];
  40426. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  40427. int dsaKeySz;
  40428. XFILE fp = XBADFILE;
  40429. XMEMSET(tmp, 0, sizeof(tmp));
  40430. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  40431. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  40432. if (fp != XBADFILE)
  40433. XFCLOSE(fp);
  40434. #endif /* END USE_CERT_BUFFERS_1024 */
  40435. /* Create hash to later Sign and Verify */
  40436. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  40437. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  40438. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  40439. /* Initialize pkey with der format dsa key */
  40440. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  40441. (long)dsaKeySz));
  40442. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  40443. /* Should Fail: NULL argument */
  40444. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  40445. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  40446. /* Should Pass: Initialized pkey argument */
  40447. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  40448. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  40449. #ifdef USE_CERT_BUFFERS_1024
  40450. ExpectIntEQ(DSA_bits(dsa), 1024);
  40451. #else
  40452. ExpectIntEQ(DSA_bits(dsa), 2048);
  40453. #endif
  40454. /* Sign */
  40455. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  40456. /* Verify. */
  40457. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  40458. WOLFSSL_SUCCESS);
  40459. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  40460. /* Should Fail: set1Pkey not initialized */
  40461. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  40462. /* Initialize set1Pkey */
  40463. set1Pkey = EVP_PKEY_new();
  40464. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  40465. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  40466. WOLFSSL_SUCCESS);
  40467. /* Should Pass: set dsa into set1Pkey */
  40468. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  40469. DSA_free(dsa);
  40470. DSA_free(setDsa);
  40471. EVP_PKEY_free(pkey);
  40472. EVP_PKEY_free(set1Pkey);
  40473. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  40474. return EXPECT_RESULT();
  40475. } /* END test_EVP_PKEY_set1_get1_DSA */
  40476. static int test_wolfSSL_DSA_generate_parameters(void)
  40477. {
  40478. EXPECT_DECLS;
  40479. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  40480. !defined(HAVE_FIPS)
  40481. DSA *dsa = NULL;
  40482. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  40483. NULL));
  40484. DSA_free(dsa);
  40485. #endif
  40486. return EXPECT_RESULT();
  40487. }
  40488. static int test_wolfSSL_DSA_SIG(void)
  40489. {
  40490. EXPECT_DECLS;
  40491. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  40492. !defined(HAVE_FIPS)
  40493. DSA *dsa = NULL;
  40494. DSA *dsa2 = NULL;
  40495. DSA_SIG *sig = NULL;
  40496. const BIGNUM *p = NULL;
  40497. const BIGNUM *q = NULL;
  40498. const BIGNUM *g = NULL;
  40499. const BIGNUM *pub = NULL;
  40500. const BIGNUM *priv = NULL;
  40501. BIGNUM *dup_p = NULL;
  40502. BIGNUM *dup_q = NULL;
  40503. BIGNUM *dup_g = NULL;
  40504. BIGNUM *dup_pub = NULL;
  40505. BIGNUM *dup_priv = NULL;
  40506. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  40507. ExpectNotNull(dsa = DSA_new());
  40508. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  40509. NULL), 1);
  40510. ExpectIntEQ(DSA_generate_key(dsa), 1);
  40511. DSA_get0_pqg(dsa, &p, &q, &g);
  40512. DSA_get0_key(dsa, &pub, &priv);
  40513. ExpectNotNull(dup_p = BN_dup(p));
  40514. ExpectNotNull(dup_q = BN_dup(q));
  40515. ExpectNotNull(dup_g = BN_dup(g));
  40516. ExpectNotNull(dup_pub = BN_dup(pub));
  40517. ExpectNotNull(dup_priv = BN_dup(priv));
  40518. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  40519. ExpectNotNull(dsa2 = DSA_new());
  40520. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  40521. if (EXPECT_FAIL()) {
  40522. BN_free(dup_p);
  40523. BN_free(dup_q);
  40524. BN_free(dup_g);
  40525. }
  40526. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  40527. if (EXPECT_FAIL()) {
  40528. BN_free(dup_pub);
  40529. BN_free(dup_priv);
  40530. }
  40531. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  40532. DSA_free(dsa);
  40533. DSA_free(dsa2);
  40534. DSA_SIG_free(sig);
  40535. #endif
  40536. return EXPECT_RESULT();
  40537. }
  40538. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  40539. {
  40540. EXPECT_DECLS;
  40541. #ifdef HAVE_ECC
  40542. WOLFSSL_EC_KEY* ecKey = NULL;
  40543. WOLFSSL_EC_KEY* ecGet1 = NULL;
  40544. EVP_PKEY* pkey = NULL;
  40545. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  40546. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40547. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  40548. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  40549. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  40550. /* Should fail since ecKey is empty */
  40551. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  40552. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  40553. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  40554. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  40555. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  40556. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  40557. wolfSSL_EC_KEY_free(ecKey);
  40558. wolfSSL_EC_KEY_free(ecGet1);
  40559. EVP_PKEY_free(pkey);
  40560. #endif /* HAVE_ECC */
  40561. return EXPECT_RESULT();
  40562. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  40563. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  40564. {
  40565. EXPECT_DECLS;
  40566. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  40567. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40568. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  40569. DH *dh = NULL;
  40570. DH *setDh = NULL;
  40571. EVP_PKEY *pkey = NULL;
  40572. XFILE f = XBADFILE;
  40573. unsigned char buf[4096];
  40574. const unsigned char* pt = buf;
  40575. const char* dh2048 = "./certs/dh2048.der";
  40576. long len = 0;
  40577. int code = -1;
  40578. XMEMSET(buf, 0, sizeof(buf));
  40579. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  40580. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  40581. if (f != XBADFILE)
  40582. XFCLOSE(f);
  40583. /* Load dh2048.der into DH with internal format */
  40584. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  40585. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  40586. ExpectIntEQ(code, 0);
  40587. code = -1;
  40588. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40589. /* Set DH into PKEY */
  40590. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  40591. /* Get DH from PKEY */
  40592. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  40593. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  40594. ExpectIntEQ(code, 0);
  40595. EVP_PKEY_free(pkey);
  40596. DH_free(setDh);
  40597. DH_free(dh);
  40598. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  40599. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40600. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  40601. return EXPECT_RESULT();
  40602. } /* END test_EVP_PKEY_set1_get1_DH */
  40603. static int test_wolfSSL_CTX_ctrl(void)
  40604. {
  40605. EXPECT_DECLS;
  40606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40607. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40608. char caFile[] = "./certs/client-ca.pem";
  40609. char clientFile[] = "./certs/client-cert.pem";
  40610. SSL_CTX* ctx = NULL;
  40611. X509* x509 = NULL;
  40612. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  40613. byte buf[6000];
  40614. char file[] = "./certs/dsaparams.pem";
  40615. XFILE f = XBADFILE;
  40616. int bytes;
  40617. BIO* bio = NULL;
  40618. DSA* dsa = NULL;
  40619. DH* dh = NULL;
  40620. #endif
  40621. #ifdef HAVE_ECC
  40622. WOLFSSL_EC_KEY* ecKey = NULL;
  40623. #endif
  40624. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  40625. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  40626. WOLFSSL_FILETYPE_PEM));
  40627. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  40628. if (EXPECT_FAIL()) {
  40629. wolfSSL_X509_free(x509);
  40630. }
  40631. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  40632. WOLFSSL_FILETYPE_PEM));
  40633. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  40634. /* Initialize DH */
  40635. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  40636. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  40637. if (f != XBADFILE)
  40638. XFCLOSE(f);
  40639. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  40640. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  40641. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  40642. #endif
  40643. #ifdef HAVE_ECC
  40644. /* Initialize WOLFSSL_EC_KEY */
  40645. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  40646. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  40647. #endif
  40648. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  40649. /* additional test of getting EVP_PKEY key size from X509
  40650. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  40651. * allowed with user RSA */
  40652. {
  40653. EVP_PKEY* pkey = NULL;
  40654. #if defined(HAVE_ECC)
  40655. X509* ecX509 = NULL;
  40656. #endif /* HAVE_ECC */
  40657. ExpectNotNull(pkey = X509_get_pubkey(x509));
  40658. /* current RSA key is 2048 bit (256 bytes) */
  40659. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  40660. EVP_PKEY_free(pkey);
  40661. pkey = NULL;
  40662. #if defined(HAVE_ECC)
  40663. #if defined(USE_CERT_BUFFERS_256)
  40664. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  40665. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  40666. SSL_FILETYPE_ASN1));
  40667. #else
  40668. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  40669. cliEccCertFile, SSL_FILETYPE_PEM));
  40670. #endif
  40671. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  40672. /* current ECC key is 256 bit (32 bytes) */
  40673. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  40674. X509_free(ecX509);
  40675. EVP_PKEY_free(pkey);
  40676. #endif /* HAVE_ECC */
  40677. }
  40678. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  40679. /* Tests should fail with passed in NULL pointer */
  40680. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  40681. SSL_FAILURE);
  40682. #if !defined(NO_DH) && !defined(NO_DSA)
  40683. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  40684. SSL_FAILURE);
  40685. #endif
  40686. #ifdef HAVE_ECC
  40687. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  40688. SSL_FAILURE);
  40689. #endif
  40690. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  40691. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  40692. */
  40693. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  40694. SSL_SUCCESS);
  40695. if (EXPECT_FAIL()) {
  40696. wolfSSL_X509_free(x509);
  40697. }
  40698. /* Test with SSL_CTRL_OPTIONS
  40699. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  40700. */
  40701. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  40702. NULL) == SSL_OP_NO_TLSv1);
  40703. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  40704. /* Test with SSL_CTRL_SET_TMP_DH
  40705. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  40706. */
  40707. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  40708. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  40709. SSL_SUCCESS);
  40710. #endif
  40711. /* Test with SSL_CTRL_SET_TMP_ECDH
  40712. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  40713. */
  40714. #ifdef HAVE_ECC
  40715. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  40716. SSL_SUCCESS);
  40717. #endif
  40718. #ifdef WOLFSSL_ENCRYPTED_KEYS
  40719. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  40720. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  40721. #endif
  40722. /* Test for min/max proto */
  40723. #ifndef WOLFSSL_NO_TLS12
  40724. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  40725. 0, NULL), SSL_SUCCESS);
  40726. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  40727. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  40728. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  40729. #endif
  40730. #ifdef WOLFSSL_TLS13
  40731. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  40732. 0, NULL), SSL_SUCCESS);
  40733. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  40734. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  40735. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  40736. #ifndef WOLFSSL_NO_TLS12
  40737. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  40738. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  40739. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  40740. #endif
  40741. #endif
  40742. /* Cleanup and Pass */
  40743. #if !defined(NO_DH) && !defined(NO_DSA)
  40744. #ifndef NO_BIO
  40745. BIO_free(bio);
  40746. DSA_free(dsa);
  40747. DH_free(dh);
  40748. #endif
  40749. #endif
  40750. #ifdef HAVE_ECC
  40751. wolfSSL_EC_KEY_free(ecKey);
  40752. #endif
  40753. SSL_CTX_free(ctx);
  40754. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40755. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  40756. return EXPECT_RESULT();
  40757. }
  40758. static int test_wolfSSL_EVP_PKEY_assign(void)
  40759. {
  40760. EXPECT_DECLS;
  40761. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  40762. int type;
  40763. WOLFSSL_EVP_PKEY* pkey = NULL;
  40764. #ifndef NO_RSA
  40765. WOLFSSL_RSA* rsa = NULL;
  40766. #endif
  40767. #ifndef NO_DSA
  40768. WOLFSSL_DSA* dsa = NULL;
  40769. #endif
  40770. #ifdef HAVE_ECC
  40771. WOLFSSL_EC_KEY* ecKey = NULL;
  40772. #endif
  40773. #ifndef NO_RSA
  40774. type = EVP_PKEY_RSA;
  40775. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40776. ExpectNotNull(rsa = wolfSSL_RSA_new());
  40777. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  40778. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  40779. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  40780. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  40781. if (EXPECT_FAIL()) {
  40782. wolfSSL_RSA_free(rsa);
  40783. }
  40784. wolfSSL_EVP_PKEY_free(pkey);
  40785. pkey = NULL;
  40786. #endif /* NO_RSA */
  40787. #ifndef NO_DSA
  40788. type = EVP_PKEY_DSA;
  40789. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40790. ExpectNotNull(dsa = wolfSSL_DSA_new());
  40791. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  40792. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  40793. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  40794. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  40795. if (EXPECT_FAIL()) {
  40796. wolfSSL_DSA_free(dsa);
  40797. }
  40798. wolfSSL_EVP_PKEY_free(pkey);
  40799. pkey = NULL;
  40800. #endif /* NO_DSA */
  40801. #ifdef HAVE_ECC
  40802. type = EVP_PKEY_EC;
  40803. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40804. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  40805. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  40806. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  40807. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  40808. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  40809. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  40810. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  40811. if (EXPECT_FAIL()) {
  40812. wolfSSL_EC_KEY_free(ecKey);
  40813. }
  40814. wolfSSL_EVP_PKEY_free(pkey);
  40815. pkey = NULL;
  40816. #endif /* HAVE_ECC */
  40817. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  40818. return EXPECT_RESULT();
  40819. }
  40820. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  40821. {
  40822. EXPECT_DECLS;
  40823. #if !defined(NO_DH) && \
  40824. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  40825. XFILE f = XBADFILE;
  40826. unsigned char buf[4096];
  40827. const unsigned char* pt = buf;
  40828. const char* params1 = "./certs/dh2048.der";
  40829. long len = 0;
  40830. WOLFSSL_DH* dh = NULL;
  40831. WOLFSSL_EVP_PKEY* pkey = NULL;
  40832. XMEMSET(buf, 0, sizeof(buf));
  40833. /* Load DH parameters DER. */
  40834. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  40835. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  40836. if (f != XBADFILE)
  40837. XFCLOSE(f);
  40838. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  40839. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  40840. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40841. /* Bad cases */
  40842. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  40843. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  40844. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  40845. /* Good case */
  40846. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  40847. if (EXPECT_FAIL()) {
  40848. wolfSSL_DH_free(dh);
  40849. }
  40850. EVP_PKEY_free(pkey);
  40851. #endif
  40852. return EXPECT_RESULT();
  40853. }
  40854. static int test_wolfSSL_EVP_PKEY_base_id(void)
  40855. {
  40856. EXPECT_DECLS;
  40857. WOLFSSL_EVP_PKEY* pkey = NULL;
  40858. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40859. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  40860. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  40861. EVP_PKEY_free(pkey);
  40862. return EXPECT_RESULT();
  40863. }
  40864. static int test_wolfSSL_EVP_PKEY_id(void)
  40865. {
  40866. EXPECT_DECLS;
  40867. WOLFSSL_EVP_PKEY* pkey = NULL;
  40868. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40869. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  40870. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  40871. EVP_PKEY_free(pkey);
  40872. return EXPECT_RESULT();
  40873. }
  40874. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  40875. {
  40876. EXPECT_DECLS;
  40877. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  40878. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  40879. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  40880. ECC_MIN_KEY_SZ <= 256)
  40881. EVP_PKEY_CTX* ctx = NULL;
  40882. EVP_PKEY* pkey = NULL;
  40883. /* Test error conditions. */
  40884. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  40885. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  40886. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  40887. #ifndef NO_RSA
  40888. EVP_PKEY_CTX_free(ctx);
  40889. /* Parameter generation for RSA not supported yet. */
  40890. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  40891. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  40892. #endif
  40893. #ifdef HAVE_ECC
  40894. EVP_PKEY_CTX_free(ctx);
  40895. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  40896. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  40897. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  40898. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  40899. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  40900. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  40901. WOLFSSL_SUCCESS);
  40902. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  40903. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  40904. #endif
  40905. EVP_PKEY_CTX_free(ctx);
  40906. EVP_PKEY_free(pkey);
  40907. #endif
  40908. return EXPECT_RESULT();
  40909. }
  40910. static int test_wolfSSL_EVP_PKEY_keygen(void)
  40911. {
  40912. EXPECT_DECLS;
  40913. WOLFSSL_EVP_PKEY* pkey = NULL;
  40914. EVP_PKEY_CTX* ctx = NULL;
  40915. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  40916. WOLFSSL_EVP_PKEY* params = NULL;
  40917. DH* dh = NULL;
  40918. const BIGNUM* pubkey = NULL;
  40919. const BIGNUM* privkey = NULL;
  40920. ASN1_INTEGER* asn1int = NULL;
  40921. unsigned int length = 0;
  40922. byte* derBuffer = NULL;
  40923. #endif
  40924. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40925. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40926. /* Bad cases */
  40927. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  40928. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  40929. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  40930. /* Good case */
  40931. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  40932. EVP_PKEY_CTX_free(ctx);
  40933. ctx = NULL;
  40934. EVP_PKEY_free(pkey);
  40935. pkey = NULL;
  40936. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  40937. /* Test DH keygen */
  40938. {
  40939. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  40940. ExpectNotNull(dh = DH_get_2048_256());
  40941. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  40942. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  40943. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  40944. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  40945. DH_free(dh);
  40946. dh = NULL;
  40947. EVP_PKEY_CTX_free(ctx);
  40948. EVP_PKEY_free(params);
  40949. /* try exporting generated key to DER, to verify */
  40950. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  40951. DH_get0_key(dh, &pubkey, &privkey);
  40952. ExpectNotNull(pubkey);
  40953. ExpectNotNull(privkey);
  40954. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  40955. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  40956. ASN1_INTEGER_free(asn1int);
  40957. DH_free(dh);
  40958. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40959. EVP_PKEY_free(pkey);
  40960. }
  40961. #endif
  40962. return EXPECT_RESULT();
  40963. }
  40964. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  40965. {
  40966. EXPECT_DECLS;
  40967. WOLFSSL_EVP_PKEY* pkey = NULL;
  40968. EVP_PKEY_CTX *ctx = NULL;
  40969. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40970. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40971. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  40972. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  40973. EVP_PKEY_CTX_free(ctx);
  40974. EVP_PKEY_free(pkey);
  40975. return EXPECT_RESULT();
  40976. }
  40977. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  40978. {
  40979. EXPECT_DECLS;
  40980. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  40981. WOLFSSL_EVP_PKEY* pkey = NULL;
  40982. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  40983. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  40984. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  40985. EVP_PKEY_free(pkey);
  40986. #endif
  40987. return EXPECT_RESULT();
  40988. }
  40989. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  40990. {
  40991. EXPECT_DECLS;
  40992. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  40993. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  40994. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  40995. !defined(NO_FILESYSTEM)
  40996. WOLFSSL_EVP_PKEY* params = NULL;
  40997. WOLFSSL_EVP_PKEY* copy = NULL;
  40998. DH* dh = NULL;
  40999. BIGNUM* p1;
  41000. BIGNUM* g1;
  41001. BIGNUM* q1;
  41002. BIGNUM* p2;
  41003. BIGNUM* g2;
  41004. BIGNUM* q2;
  41005. /* create DH with DH_get_2048_256 params */
  41006. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  41007. ExpectNotNull(dh = DH_get_2048_256());
  41008. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  41009. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  41010. (const BIGNUM**)&q1,
  41011. (const BIGNUM**)&g1);
  41012. DH_free(dh);
  41013. dh = NULL;
  41014. /* create DH with random generated DH params */
  41015. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  41016. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  41017. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  41018. DH_free(dh);
  41019. dh = NULL;
  41020. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  41021. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  41022. ExpectNotNull(dh->p);
  41023. ExpectNotNull(dh->g);
  41024. ExpectNotNull(dh->q);
  41025. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  41026. (const BIGNUM**)&q2,
  41027. (const BIGNUM**)&g2);
  41028. ExpectIntEQ(BN_cmp(p1, p2), 0);
  41029. ExpectIntEQ(BN_cmp(q1, q2), 0);
  41030. ExpectIntEQ(BN_cmp(g1, g2), 0);
  41031. DH_free(dh);
  41032. EVP_PKEY_free(copy);
  41033. EVP_PKEY_free(params);
  41034. #endif
  41035. return EXPECT_RESULT();
  41036. }
  41037. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  41038. {
  41039. EXPECT_DECLS;
  41040. WOLFSSL_EVP_PKEY* pkey = NULL;
  41041. EVP_PKEY_CTX* ctx = NULL;
  41042. int bits = 2048;
  41043. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41044. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41045. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  41046. WOLFSSL_SUCCESS);
  41047. EVP_PKEY_CTX_free(ctx);
  41048. EVP_PKEY_free(pkey);
  41049. return EXPECT_RESULT();
  41050. }
  41051. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  41052. {
  41053. EXPECT_DECLS;
  41054. /* This is large enough to be used for all key sizes */
  41055. byte key[AES_256_KEY_SIZE] = {0};
  41056. byte iv[AES_BLOCK_SIZE] = {0};
  41057. int i;
  41058. int nids[] = {
  41059. #ifdef HAVE_AES_CBC
  41060. NID_aes_128_cbc,
  41061. #endif
  41062. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41063. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41064. #ifdef HAVE_AESGCM
  41065. NID_aes_128_gcm,
  41066. #endif
  41067. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41068. #ifdef WOLFSSL_AES_COUNTER
  41069. NID_aes_128_ctr,
  41070. #endif
  41071. #ifndef NO_DES3
  41072. NID_des_cbc,
  41073. NID_des_ede3_cbc,
  41074. #endif
  41075. };
  41076. int iv_lengths[] = {
  41077. #ifdef HAVE_AES_CBC
  41078. AES_BLOCK_SIZE,
  41079. #endif
  41080. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41081. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41082. #ifdef HAVE_AESGCM
  41083. GCM_NONCE_MID_SZ,
  41084. #endif
  41085. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41086. #ifdef WOLFSSL_AES_COUNTER
  41087. AES_BLOCK_SIZE,
  41088. #endif
  41089. #ifndef NO_DES3
  41090. DES_BLOCK_SIZE,
  41091. DES_BLOCK_SIZE,
  41092. #endif
  41093. };
  41094. int nidsLen = (sizeof(nids)/sizeof(int));
  41095. for (i = 0; i < nidsLen; i++) {
  41096. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  41097. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  41098. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41099. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41100. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  41101. EVP_CIPHER_CTX_free(ctx);
  41102. }
  41103. return EXPECT_RESULT();
  41104. }
  41105. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  41106. {
  41107. EXPECT_DECLS;
  41108. byte key[AES_256_KEY_SIZE] = {0};
  41109. byte iv[AES_BLOCK_SIZE] = {0};
  41110. int i;
  41111. int nids[] = {
  41112. #ifdef HAVE_AES_CBC
  41113. NID_aes_128_cbc,
  41114. NID_aes_256_cbc,
  41115. #endif
  41116. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41117. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41118. #ifdef HAVE_AESGCM
  41119. NID_aes_128_gcm,
  41120. NID_aes_256_gcm,
  41121. #endif
  41122. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41123. #ifdef WOLFSSL_AES_COUNTER
  41124. NID_aes_128_ctr,
  41125. NID_aes_256_ctr,
  41126. #endif
  41127. #ifndef NO_DES3
  41128. NID_des_cbc,
  41129. NID_des_ede3_cbc,
  41130. #endif
  41131. };
  41132. int key_lengths[] = {
  41133. #ifdef HAVE_AES_CBC
  41134. AES_128_KEY_SIZE,
  41135. AES_256_KEY_SIZE,
  41136. #endif
  41137. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41138. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41139. #ifdef HAVE_AESGCM
  41140. AES_128_KEY_SIZE,
  41141. AES_256_KEY_SIZE,
  41142. #endif
  41143. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41144. #ifdef WOLFSSL_AES_COUNTER
  41145. AES_128_KEY_SIZE,
  41146. AES_256_KEY_SIZE,
  41147. #endif
  41148. #ifndef NO_DES3
  41149. DES_KEY_SIZE,
  41150. DES3_KEY_SIZE,
  41151. #endif
  41152. };
  41153. int nidsLen = (sizeof(nids)/sizeof(int));
  41154. for (i = 0; i < nidsLen; i++) {
  41155. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  41156. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  41157. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41158. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41159. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  41160. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  41161. WOLFSSL_SUCCESS);
  41162. EVP_CIPHER_CTX_free(ctx);
  41163. }
  41164. return EXPECT_RESULT();
  41165. }
  41166. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  41167. {
  41168. EXPECT_DECLS;
  41169. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  41170. int ivLen, keyLen;
  41171. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  41172. #ifdef HAVE_AESGCM
  41173. byte key[AES_128_KEY_SIZE] = {0};
  41174. byte iv[AES_BLOCK_SIZE] = {0};
  41175. const EVP_CIPHER *init = EVP_aes_128_gcm();
  41176. #else
  41177. byte key[DES3_KEY_SIZE] = {0};
  41178. byte iv[DES_BLOCK_SIZE] = {0};
  41179. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  41180. #endif
  41181. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41182. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41183. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  41184. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  41185. /* Bad cases */
  41186. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  41187. WOLFSSL_FAILURE);
  41188. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  41189. WOLFSSL_FAILURE);
  41190. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  41191. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  41192. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  41193. WOLFSSL_FAILURE);
  41194. /* Good case */
  41195. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  41196. EVP_CIPHER_CTX_free(ctx);
  41197. #endif
  41198. return EXPECT_RESULT();
  41199. }
  41200. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  41201. {
  41202. EXPECT_DECLS;
  41203. WOLFSSL_ENGINE* e = NULL;
  41204. int id = 0;
  41205. EVP_PKEY_CTX *ctx = NULL;
  41206. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  41207. EVP_PKEY_CTX_free(ctx);
  41208. return EXPECT_RESULT();
  41209. }
  41210. static int test_wolfSSL_EVP_rc4(void)
  41211. {
  41212. EXPECT_DECLS;
  41213. #if !defined(NO_RC4)
  41214. ExpectNotNull(wolfSSL_EVP_rc4());
  41215. #endif
  41216. return EXPECT_RESULT();
  41217. }
  41218. static int test_wolfSSL_EVP_enc_null(void)
  41219. {
  41220. EXPECT_DECLS;
  41221. ExpectNotNull(wolfSSL_EVP_enc_null());
  41222. return EXPECT_RESULT();
  41223. }
  41224. static int test_wolfSSL_EVP_rc2_cbc(void)
  41225. {
  41226. EXPECT_DECLS;
  41227. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  41228. ExpectNull(wolfSSL_EVP_rc2_cbc());
  41229. #endif
  41230. return EXPECT_RESULT();
  41231. }
  41232. static int test_wolfSSL_EVP_mdc2(void)
  41233. {
  41234. EXPECT_DECLS;
  41235. #if !defined(NO_WOLFSSL_STUB)
  41236. ExpectNull(wolfSSL_EVP_mdc2());
  41237. #endif
  41238. return EXPECT_RESULT();
  41239. }
  41240. static int test_wolfSSL_EVP_md4(void)
  41241. {
  41242. EXPECT_DECLS;
  41243. #if !defined(NO_MD4)
  41244. ExpectNotNull(wolfSSL_EVP_md4());
  41245. #endif
  41246. return EXPECT_RESULT();
  41247. }
  41248. static int test_wolfSSL_EVP_aes_256_gcm(void)
  41249. {
  41250. EXPECT_DECLS;
  41251. #ifdef HAVE_AESGCM
  41252. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  41253. #endif
  41254. return EXPECT_RESULT();
  41255. }
  41256. static int test_wolfSSL_EVP_aes_192_gcm(void)
  41257. {
  41258. EXPECT_DECLS;
  41259. #ifdef HAVE_AESGCM
  41260. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  41261. #endif
  41262. return EXPECT_RESULT();
  41263. }
  41264. static int test_wolfSSL_EVP_aes_256_ccm(void)
  41265. {
  41266. EXPECT_DECLS;
  41267. #ifdef HAVE_AESCCM
  41268. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  41269. #endif
  41270. return EXPECT_RESULT();
  41271. }
  41272. static int test_wolfSSL_EVP_aes_192_ccm(void)
  41273. {
  41274. EXPECT_DECLS;
  41275. #ifdef HAVE_AESCCM
  41276. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  41277. #endif
  41278. return EXPECT_RESULT();
  41279. }
  41280. static int test_wolfSSL_EVP_aes_128_ccm(void)
  41281. {
  41282. EXPECT_DECLS;
  41283. #ifdef HAVE_AESCCM
  41284. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  41285. #endif
  41286. return EXPECT_RESULT();
  41287. }
  41288. static int test_wolfSSL_EVP_ripemd160(void)
  41289. {
  41290. EXPECT_DECLS;
  41291. #if !defined(NO_WOLFSSL_STUB)
  41292. ExpectNull(wolfSSL_EVP_ripemd160());
  41293. #endif
  41294. return EXPECT_RESULT();
  41295. }
  41296. static int test_wolfSSL_EVP_get_digestbynid(void)
  41297. {
  41298. EXPECT_DECLS;
  41299. #ifndef NO_MD5
  41300. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  41301. #endif
  41302. #ifndef NO_SHA
  41303. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  41304. #endif
  41305. #ifndef NO_SHA256
  41306. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  41307. #endif
  41308. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  41309. return EXPECT_RESULT();
  41310. }
  41311. static int test_wolfSSL_EVP_MD_nid(void)
  41312. {
  41313. EXPECT_DECLS;
  41314. #ifndef NO_MD5
  41315. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  41316. #endif
  41317. #ifndef NO_SHA
  41318. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  41319. #endif
  41320. #ifndef NO_SHA256
  41321. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  41322. #endif
  41323. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  41324. return EXPECT_RESULT();
  41325. }
  41326. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  41327. {
  41328. EXPECT_DECLS;
  41329. #if defined(HAVE_ECC)
  41330. WOLFSSL_EVP_PKEY* pkey = NULL;
  41331. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  41332. ExpectNotNull(pkey = EVP_PKEY_new());
  41333. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  41334. EVP_PKEY_free(pkey);
  41335. #endif
  41336. return EXPECT_RESULT();
  41337. }
  41338. static int test_wolfSSL_EVP_X_STATE(void)
  41339. {
  41340. EXPECT_DECLS;
  41341. #if !defined(NO_DES3) && !defined(NO_RC4)
  41342. byte key[DES3_KEY_SIZE] = {0};
  41343. byte iv[DES_IV_SIZE] = {0};
  41344. EVP_CIPHER_CTX *ctx = NULL;
  41345. const EVP_CIPHER *init = NULL;
  41346. /* Bad test cases */
  41347. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  41348. ExpectNotNull(init = EVP_des_ede3_cbc());
  41349. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41350. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41351. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  41352. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  41353. EVP_CIPHER_CTX_free(ctx);
  41354. ctx = NULL;
  41355. /* Good test case */
  41356. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  41357. ExpectNotNull(init = wolfSSL_EVP_rc4());
  41358. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41359. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41360. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  41361. EVP_CIPHER_CTX_free(ctx);
  41362. #endif
  41363. return EXPECT_RESULT();
  41364. }
  41365. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  41366. {
  41367. EXPECT_DECLS;
  41368. #if !defined(NO_DES3) && !defined(NO_RC4)
  41369. byte key[DES3_KEY_SIZE] = {0};
  41370. byte iv[DES_IV_SIZE] = {0};
  41371. EVP_CIPHER_CTX *ctx = NULL;
  41372. const EVP_CIPHER *init = NULL;
  41373. /* Bad test cases */
  41374. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  41375. ExpectNotNull(init = EVP_des_ede3_cbc());
  41376. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41377. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41378. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  41379. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  41380. EVP_CIPHER_CTX_free(ctx);
  41381. ctx = NULL;
  41382. /* Good test case */
  41383. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  41384. ExpectNotNull(init = wolfSSL_EVP_rc4());
  41385. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  41386. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  41387. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  41388. EVP_CIPHER_CTX_free(ctx);
  41389. #endif
  41390. return EXPECT_RESULT();
  41391. }
  41392. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  41393. {
  41394. EXPECT_DECLS;
  41395. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  41396. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  41397. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  41398. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  41399. #ifdef HAVE_AES_CBC
  41400. #ifdef WOLFSSL_AES_128
  41401. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  41402. #endif
  41403. #ifdef WOLFSSL_AES_192
  41404. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  41405. #endif
  41406. #ifdef WOLFSSL_AES_256
  41407. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  41408. #endif
  41409. #endif
  41410. #ifdef HAVE_AESGCM
  41411. #ifdef WOLFSSL_AES_128
  41412. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  41413. #endif
  41414. #ifdef WOLFSSL_AES_192
  41415. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  41416. #endif
  41417. #ifdef WOLFSSL_AES_256
  41418. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  41419. #endif
  41420. #endif
  41421. #ifdef HAVE_AESCCM
  41422. #ifdef WOLFSSL_AES_128
  41423. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  41424. #endif
  41425. #ifdef WOLFSSL_AES_192
  41426. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  41427. #endif
  41428. #ifdef WOLFSSL_AES_256
  41429. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  41430. #endif
  41431. #endif
  41432. #ifdef WOLFSSL_AES_COUNTER
  41433. #ifdef WOLFSSL_AES_128
  41434. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  41435. #endif
  41436. #ifdef WOLFSSL_AES_192
  41437. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  41438. #endif
  41439. #ifdef WOLFSSL_AES_256
  41440. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  41441. #endif
  41442. #endif
  41443. #ifdef HAVE_AES_ECB
  41444. #ifdef WOLFSSL_AES_128
  41445. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  41446. #endif
  41447. #ifdef WOLFSSL_AES_192
  41448. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  41449. #endif
  41450. #ifdef WOLFSSL_AES_256
  41451. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  41452. #endif
  41453. #endif
  41454. #ifdef WOLFSSL_AES_OFB
  41455. #ifdef WOLFSSL_AES_128
  41456. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  41457. #endif
  41458. #ifdef WOLFSSL_AES_192
  41459. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  41460. #endif
  41461. #ifdef WOLFSSL_AES_256
  41462. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  41463. #endif
  41464. #endif
  41465. #ifndef NO_RC4
  41466. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  41467. #endif
  41468. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  41469. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  41470. #endif
  41471. #endif
  41472. #ifdef WOLFSSL_SM4_ECB
  41473. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  41474. #endif
  41475. #ifdef WOLFSSL_SM4_CBC
  41476. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  41477. #endif
  41478. #ifdef WOLFSSL_SM4_CTR
  41479. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  41480. #endif
  41481. #ifdef WOLFSSL_SM4_GCM
  41482. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  41483. #endif
  41484. #ifdef WOLFSSL_SM4_CCM
  41485. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  41486. #endif
  41487. return EXPECT_RESULT();
  41488. }
  41489. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  41490. {
  41491. EXPECT_DECLS;
  41492. int nids[] = {
  41493. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  41494. #ifdef WOLFSSL_AES_128
  41495. NID_aes_128_cbc,
  41496. #endif
  41497. #ifdef WOLFSSL_AES_192
  41498. NID_aes_192_cbc,
  41499. #endif
  41500. #ifdef WOLFSSL_AES_256
  41501. NID_aes_256_cbc,
  41502. #endif
  41503. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  41504. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41505. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41506. #ifdef HAVE_AESGCM
  41507. #ifdef WOLFSSL_AES_128
  41508. NID_aes_128_gcm,
  41509. #endif
  41510. #ifdef WOLFSSL_AES_192
  41511. NID_aes_192_gcm,
  41512. #endif
  41513. #ifdef WOLFSSL_AES_256
  41514. NID_aes_256_gcm,
  41515. #endif
  41516. #endif /* HAVE_AESGCM */
  41517. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41518. #ifdef WOLFSSL_AES_COUNTER
  41519. #ifdef WOLFSSL_AES_128
  41520. NID_aes_128_ctr,
  41521. #endif
  41522. #ifdef WOLFSSL_AES_192
  41523. NID_aes_192_ctr,
  41524. #endif
  41525. #ifdef WOLFSSL_AES_256
  41526. NID_aes_256_ctr,
  41527. #endif
  41528. #endif
  41529. #ifndef NO_DES3
  41530. NID_des_cbc,
  41531. NID_des_ede3_cbc,
  41532. #endif
  41533. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  41534. NID_chacha20_poly1305,
  41535. #endif
  41536. };
  41537. int iv_lengths[] = {
  41538. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  41539. #ifdef WOLFSSL_AES_128
  41540. AES_BLOCK_SIZE,
  41541. #endif
  41542. #ifdef WOLFSSL_AES_192
  41543. AES_BLOCK_SIZE,
  41544. #endif
  41545. #ifdef WOLFSSL_AES_256
  41546. AES_BLOCK_SIZE,
  41547. #endif
  41548. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  41549. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  41550. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  41551. #ifdef HAVE_AESGCM
  41552. #ifdef WOLFSSL_AES_128
  41553. GCM_NONCE_MID_SZ,
  41554. #endif
  41555. #ifdef WOLFSSL_AES_192
  41556. GCM_NONCE_MID_SZ,
  41557. #endif
  41558. #ifdef WOLFSSL_AES_256
  41559. GCM_NONCE_MID_SZ,
  41560. #endif
  41561. #endif /* HAVE_AESGCM */
  41562. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  41563. #ifdef WOLFSSL_AES_COUNTER
  41564. #ifdef WOLFSSL_AES_128
  41565. AES_BLOCK_SIZE,
  41566. #endif
  41567. #ifdef WOLFSSL_AES_192
  41568. AES_BLOCK_SIZE,
  41569. #endif
  41570. #ifdef WOLFSSL_AES_256
  41571. AES_BLOCK_SIZE,
  41572. #endif
  41573. #endif
  41574. #ifndef NO_DES3
  41575. DES_BLOCK_SIZE,
  41576. DES_BLOCK_SIZE,
  41577. #endif
  41578. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  41579. CHACHA20_POLY1305_AEAD_IV_SIZE,
  41580. #endif
  41581. };
  41582. int i;
  41583. int nidsLen = (sizeof(nids)/sizeof(int));
  41584. for (i = 0; i < nidsLen; i++) {
  41585. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  41586. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  41587. }
  41588. return EXPECT_RESULT();
  41589. }
  41590. static int test_wolfSSL_EVP_SignInit_ex(void)
  41591. {
  41592. EXPECT_DECLS;
  41593. WOLFSSL_EVP_MD_CTX mdCtx;
  41594. WOLFSSL_ENGINE* e = 0;
  41595. const EVP_MD* md = EVP_sha256();
  41596. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  41597. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  41598. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  41599. return EXPECT_RESULT();
  41600. }
  41601. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  41602. {
  41603. EXPECT_DECLS;
  41604. #if !defined(NO_SHA256)
  41605. WOLFSSL_EVP_MD_CTX mdCtx;
  41606. unsigned int s = 0;
  41607. unsigned char md[WC_SHA256_DIGEST_SIZE];
  41608. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  41609. /* Bad Case */
  41610. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41611. (HAVE_FIPS_VERSION > 2))
  41612. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  41613. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  41614. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  41615. #else
  41616. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  41617. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  41618. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  41619. #endif
  41620. /* Good Case */
  41621. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  41622. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  41623. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  41624. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  41625. #endif
  41626. return EXPECT_RESULT();
  41627. }
  41628. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  41629. {
  41630. EXPECT_DECLS;
  41631. #if defined(OPENSSL_EXTRA)
  41632. EVP_PKEY* pkey = NULL;
  41633. EVP_PKEY_CTX* ctx = NULL;
  41634. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41635. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41636. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  41637. /* void */
  41638. EVP_PKEY_CTX_free(ctx);
  41639. #else
  41640. /* int */
  41641. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  41642. #endif
  41643. EVP_PKEY_free(pkey);
  41644. #endif
  41645. return EXPECT_RESULT();
  41646. }
  41647. static int test_wolfSSL_EVP_PKEY_param_check(void)
  41648. {
  41649. EXPECT_DECLS;
  41650. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  41651. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  41652. DH *dh = NULL;
  41653. DH *setDh = NULL;
  41654. EVP_PKEY *pkey = NULL;
  41655. EVP_PKEY_CTX* ctx = NULL;
  41656. FILE* f = NULL;
  41657. unsigned char buf[512];
  41658. const unsigned char* pt = buf;
  41659. const char* dh2048 = "./certs/dh2048.der";
  41660. long len = 0;
  41661. int code = -1;
  41662. XMEMSET(buf, 0, sizeof(buf));
  41663. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  41664. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  41665. if (f != XBADFILE)
  41666. XFCLOSE(f);
  41667. /* Load dh2048.der into DH with internal format */
  41668. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  41669. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  41670. ExpectIntEQ(code, 0);
  41671. code = -1;
  41672. pkey = wolfSSL_EVP_PKEY_new();
  41673. /* Set DH into PKEY */
  41674. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  41675. /* create ctx from pkey */
  41676. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41677. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  41678. /* TODO: more invalid cases */
  41679. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  41680. EVP_PKEY_CTX_free(ctx);
  41681. EVP_PKEY_free(pkey);
  41682. DH_free(setDh);
  41683. DH_free(dh);
  41684. #endif
  41685. #endif
  41686. return EXPECT_RESULT();
  41687. }
  41688. static int test_wolfSSL_EVP_BytesToKey(void)
  41689. {
  41690. EXPECT_DECLS;
  41691. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  41692. byte key[AES_BLOCK_SIZE] = {0};
  41693. byte iv[AES_BLOCK_SIZE] = {0};
  41694. int count = 0;
  41695. const EVP_MD* md = EVP_sha256();
  41696. const EVP_CIPHER *type;
  41697. const unsigned char *salt = (unsigned char *)"salt1234";
  41698. int sz = 5;
  41699. const byte data[] = {
  41700. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41701. 0x72,0x6c,0x64
  41702. };
  41703. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  41704. /* Bad cases */
  41705. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  41706. 0);
  41707. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  41708. 16);
  41709. md = "2";
  41710. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  41711. WOLFSSL_FAILURE);
  41712. /* Good case */
  41713. md = EVP_sha256();
  41714. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  41715. 16);
  41716. #endif
  41717. return EXPECT_RESULT();
  41718. }
  41719. static int test_evp_cipher_aes_gcm(void)
  41720. {
  41721. EXPECT_DECLS;
  41722. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  41723. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  41724. (HAVE_FIPS_VERSION >= 2)))
  41725. /*
  41726. * This test checks data at various points in the encrypt/decrypt process
  41727. * against known values produced using the same test with OpenSSL. This
  41728. * interop testing is critical for verifying the correctness of our
  41729. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  41730. * a flow supported by OpenSSL that uses the control command
  41731. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  41732. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  41733. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  41734. * wolfSSL OpenSSH clients because there was a bug in this flow that
  41735. * happened to "cancel out" if both sides of the connection had the bug.
  41736. */
  41737. enum {
  41738. NUM_ENCRYPTIONS = 3,
  41739. AAD_SIZE = 4
  41740. };
  41741. byte plainText1[] = {
  41742. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  41743. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  41744. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  41745. };
  41746. byte plainText2[] = {
  41747. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  41748. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  41749. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  41750. };
  41751. byte plainText3[] = {
  41752. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  41753. 0x5b, 0x57, 0x10
  41754. };
  41755. byte* plainTexts[NUM_ENCRYPTIONS] = {
  41756. plainText1,
  41757. plainText2,
  41758. plainText3
  41759. };
  41760. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  41761. sizeof(plainText1),
  41762. sizeof(plainText2),
  41763. sizeof(plainText3)
  41764. };
  41765. byte aad1[AAD_SIZE] = {
  41766. 0x00, 0x00, 0x00, 0x01
  41767. };
  41768. byte aad2[AAD_SIZE] = {
  41769. 0x00, 0x00, 0x00, 0x10
  41770. };
  41771. byte aad3[AAD_SIZE] = {
  41772. 0x00, 0x00, 0x01, 0x00
  41773. };
  41774. byte* aads[NUM_ENCRYPTIONS] = {
  41775. aad1,
  41776. aad2,
  41777. aad3
  41778. };
  41779. const byte iv[GCM_NONCE_MID_SZ] = {
  41780. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  41781. };
  41782. byte currentIv[GCM_NONCE_MID_SZ];
  41783. const byte key[] = {
  41784. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  41785. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  41786. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  41787. };
  41788. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  41789. {
  41790. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  41791. 0xEF
  41792. },
  41793. {
  41794. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  41795. 0xF0
  41796. },
  41797. {
  41798. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  41799. 0xF1
  41800. }
  41801. };
  41802. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  41803. {
  41804. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  41805. 0x3D, 0x32, 0x18, 0x34, 0xA9
  41806. },
  41807. {
  41808. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  41809. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  41810. },
  41811. {
  41812. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  41813. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  41814. }
  41815. };
  41816. const byte expCipherText1[] = {
  41817. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  41818. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  41819. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  41820. };
  41821. const byte expCipherText2[] = {
  41822. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  41823. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  41824. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  41825. };
  41826. const byte expCipherText3[] = {
  41827. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  41828. 0x80, 0x5E, 0x6B,
  41829. };
  41830. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  41831. expCipherText1,
  41832. expCipherText2,
  41833. expCipherText3
  41834. };
  41835. byte* cipherText = NULL;
  41836. byte* calcPlainText = NULL;
  41837. byte tag[AES_BLOCK_SIZE];
  41838. EVP_CIPHER_CTX* encCtx = NULL;
  41839. EVP_CIPHER_CTX* decCtx = NULL;
  41840. int i, j, outl;
  41841. /****************************************************/
  41842. for (i = 0; i < 3; ++i) {
  41843. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  41844. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  41845. /* First iteration, set key before IV. */
  41846. if (i == 0) {
  41847. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  41848. SSL_SUCCESS);
  41849. /*
  41850. * The call to EVP_CipherInit below (with NULL key) should clear the
  41851. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  41852. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  41853. * behavior.
  41854. */
  41855. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  41856. (void*)iv), SSL_SUCCESS);
  41857. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  41858. SSL_SUCCESS);
  41859. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  41860. currentIv), SSL_FAILURE);
  41861. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  41862. SSL_SUCCESS);
  41863. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  41864. SSL_SUCCESS);
  41865. }
  41866. /* Second iteration, IV before key. */
  41867. else {
  41868. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  41869. SSL_SUCCESS);
  41870. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  41871. SSL_SUCCESS);
  41872. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  41873. SSL_SUCCESS);
  41874. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  41875. SSL_SUCCESS);
  41876. }
  41877. /*
  41878. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  41879. * been issued first.
  41880. */
  41881. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  41882. currentIv), SSL_FAILURE);
  41883. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  41884. (void*)iv), SSL_SUCCESS);
  41885. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  41886. (void*)iv), SSL_SUCCESS);
  41887. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  41888. /*************** Encrypt ***************/
  41889. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  41890. currentIv), SSL_SUCCESS);
  41891. /* Check current IV against expected. */
  41892. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  41893. /* Add AAD. */
  41894. if (i == 2) {
  41895. /* Test streaming API. */
  41896. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  41897. AAD_SIZE), SSL_SUCCESS);
  41898. }
  41899. else {
  41900. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  41901. AAD_SIZE);
  41902. }
  41903. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  41904. DYNAMIC_TYPE_TMP_BUFFER));
  41905. /* Encrypt plaintext. */
  41906. if (i == 2) {
  41907. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  41908. plainTexts[j], plainTextSzs[j]),
  41909. SSL_SUCCESS);
  41910. }
  41911. else {
  41912. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  41913. plainTextSzs[j]), plainTextSzs[j]);
  41914. }
  41915. if (i == 2) {
  41916. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  41917. SSL_SUCCESS);
  41918. }
  41919. else {
  41920. /*
  41921. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  41922. * akin to calling EVP_CipherFinal.
  41923. */
  41924. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  41925. }
  41926. /* Check ciphertext against expected. */
  41927. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  41928. 0);
  41929. /* Get and check tag against expected. */
  41930. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  41931. sizeof(tag), tag), SSL_SUCCESS);
  41932. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  41933. /*************** Decrypt ***************/
  41934. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  41935. currentIv), SSL_SUCCESS);
  41936. /* Check current IV against expected. */
  41937. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  41938. /* Add AAD. */
  41939. if (i == 2) {
  41940. /* Test streaming API. */
  41941. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  41942. AAD_SIZE), SSL_SUCCESS);
  41943. }
  41944. else {
  41945. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  41946. AAD_SIZE);
  41947. }
  41948. /* Set expected tag. */
  41949. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  41950. sizeof(tag), tag), SSL_SUCCESS);
  41951. /* Decrypt ciphertext. */
  41952. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  41953. DYNAMIC_TYPE_TMP_BUFFER));
  41954. if (i == 2) {
  41955. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  41956. cipherText, plainTextSzs[j]),
  41957. SSL_SUCCESS);
  41958. }
  41959. else {
  41960. /* This first EVP_Cipher call will check the tag, too. */
  41961. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  41962. plainTextSzs[j]), plainTextSzs[j]);
  41963. }
  41964. if (i == 2) {
  41965. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  41966. SSL_SUCCESS);
  41967. }
  41968. else {
  41969. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  41970. }
  41971. /* Check plaintext against expected. */
  41972. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  41973. 0);
  41974. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41975. cipherText = NULL;
  41976. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41977. calcPlainText = NULL;
  41978. }
  41979. EVP_CIPHER_CTX_free(encCtx);
  41980. encCtx = NULL;
  41981. EVP_CIPHER_CTX_free(decCtx);
  41982. decCtx = NULL;
  41983. }
  41984. #endif
  41985. return EXPECT_RESULT();
  41986. }
  41987. static int test_wolfSSL_OBJ_ln(void)
  41988. {
  41989. EXPECT_DECLS;
  41990. const int nid_set[] = {
  41991. NID_commonName,
  41992. NID_serialNumber,
  41993. NID_countryName,
  41994. NID_localityName,
  41995. NID_stateOrProvinceName,
  41996. NID_organizationName,
  41997. NID_organizationalUnitName,
  41998. NID_domainComponent,
  41999. NID_businessCategory,
  42000. NID_jurisdictionCountryName,
  42001. NID_jurisdictionStateOrProvinceName,
  42002. NID_emailAddress
  42003. };
  42004. const char* ln_set[] = {
  42005. "commonName",
  42006. "serialNumber",
  42007. "countryName",
  42008. "localityName",
  42009. "stateOrProvinceName",
  42010. "organizationName",
  42011. "organizationalUnitName",
  42012. "domainComponent",
  42013. "businessCategory",
  42014. "jurisdictionCountryName",
  42015. "jurisdictionStateOrProvinceName",
  42016. "emailAddress",
  42017. };
  42018. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  42019. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  42020. #ifdef HAVE_ECC
  42021. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42022. {
  42023. EC_builtin_curve r[27];
  42024. size_t nCurves = sizeof(r) / sizeof(r[0]);
  42025. nCurves = EC_get_builtin_curves(r, nCurves);
  42026. for (i = 0; i < nCurves; i++) {
  42027. /* skip ECC_CURVE_INVALID */
  42028. if (r[i].nid != ECC_CURVE_INVALID) {
  42029. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  42030. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  42031. }
  42032. }
  42033. }
  42034. #endif
  42035. #endif
  42036. for (i = 0; i < maxIdx; i++) {
  42037. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  42038. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  42039. }
  42040. return EXPECT_RESULT();
  42041. }
  42042. static int test_wolfSSL_OBJ_sn(void)
  42043. {
  42044. EXPECT_DECLS;
  42045. int i = 0, maxIdx = 7;
  42046. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  42047. NID_stateOrProvinceName,NID_organizationName,
  42048. NID_organizationalUnitName,NID_emailAddress};
  42049. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  42050. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  42051. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  42052. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  42053. WOLFSSL_EMAIL_ADDR};
  42054. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  42055. for (i = 0; i < maxIdx; i++) {
  42056. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  42057. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  42058. }
  42059. return EXPECT_RESULT();
  42060. }
  42061. #if !defined(NO_BIO)
  42062. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  42063. {
  42064. return lh_strhash(s[3]);
  42065. }
  42066. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  42067. {
  42068. return XSTRCMP(a[3], b[3]);
  42069. }
  42070. #endif
  42071. static int test_wolfSSL_TXT_DB(void)
  42072. {
  42073. EXPECT_DECLS;
  42074. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42075. BIO *bio = NULL;
  42076. TXT_DB *db = NULL;
  42077. const int columns = 6;
  42078. const char *fields[6] = {
  42079. "V",
  42080. "320926161116Z",
  42081. "",
  42082. "12BD",
  42083. "unknown",
  42084. "/CN=rsa doe",
  42085. };
  42086. char** fields_copy = NULL;
  42087. /* Test read */
  42088. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  42089. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  42090. ExpectNotNull(db = TXT_DB_read(bio, columns));
  42091. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  42092. DYNAMIC_TYPE_OPENSSL));
  42093. if (fields_copy != NULL) {
  42094. XMEMCPY(fields_copy, fields, sizeof(fields));
  42095. }
  42096. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  42097. if (EXPECT_FAIL()) {
  42098. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  42099. }
  42100. BIO_free(bio);
  42101. bio = NULL;
  42102. /* Test write */
  42103. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42104. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  42105. BIO_free(bio);
  42106. /* Test index */
  42107. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  42108. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  42109. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42110. fields[3] = "12DA";
  42111. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42112. fields[3] = "FFFF";
  42113. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42114. fields[3] = "";
  42115. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  42116. TXT_DB_free(db);
  42117. #endif
  42118. return EXPECT_RESULT();
  42119. }
  42120. static int test_wolfSSL_NCONF(void)
  42121. {
  42122. EXPECT_DECLS;
  42123. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  42124. const char* confFile = "./tests/NCONF_test.cnf";
  42125. CONF* conf = NULL;
  42126. long eline = 0;
  42127. long num = 0;
  42128. ExpectNotNull(conf = NCONF_new(NULL));
  42129. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  42130. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  42131. ExpectIntEQ(num, 1234);
  42132. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  42133. ExpectIntEQ(num, 4321);
  42134. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  42135. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  42136. "./test-dir/file1");
  42137. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  42138. "./test-dir/file1:./test-dir/file2:./section1:file2");
  42139. NCONF_free(conf);
  42140. #endif
  42141. return EXPECT_RESULT();
  42142. }
  42143. #endif /* OPENSSL_ALL */
  42144. static int test_wolfSSL_X509V3_EXT_get(void) {
  42145. EXPECT_DECLS;
  42146. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42147. XFILE f = XBADFILE;
  42148. int numOfExt =0;
  42149. int extNid = 0;
  42150. int i = 0;
  42151. WOLFSSL_X509* x509 = NULL;
  42152. WOLFSSL_X509_EXTENSION* ext = NULL;
  42153. const WOLFSSL_v3_ext_method* method = NULL;
  42154. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42155. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42156. if (f != XBADFILE)
  42157. XFCLOSE(f);
  42158. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  42159. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  42160. for (i = 0; i < numOfExt; i++) {
  42161. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42162. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  42163. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  42164. ExpectIntEQ(method->ext_nid, extNid);
  42165. }
  42166. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  42167. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  42168. wolfSSL_X509_free(x509);
  42169. #endif
  42170. return EXPECT_RESULT();
  42171. }
  42172. static int test_wolfSSL_X509V3_EXT_nconf(void)
  42173. {
  42174. EXPECT_DECLS;
  42175. #ifdef OPENSSL_ALL
  42176. const char *ext_names[] = {
  42177. "subjectKeyIdentifier",
  42178. "authorityKeyIdentifier",
  42179. "subjectAltName",
  42180. "keyUsage",
  42181. "extendedKeyUsage",
  42182. };
  42183. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  42184. int ext_nids[] = {
  42185. NID_subject_key_identifier,
  42186. NID_authority_key_identifier,
  42187. NID_subject_alt_name,
  42188. NID_key_usage,
  42189. NID_ext_key_usage,
  42190. };
  42191. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  42192. const char *ext_values[] = {
  42193. "hash",
  42194. "hash",
  42195. "DNS:example.com, IP:127.0.0.1",
  42196. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  42197. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  42198. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  42199. "OCSPSigning",
  42200. };
  42201. size_t i;
  42202. X509_EXTENSION* ext = NULL;
  42203. X509* x509 = NULL;
  42204. unsigned int keyUsageFlags;
  42205. unsigned int extKeyUsageFlags;
  42206. ExpectNotNull(x509 = X509_new());
  42207. /* keyUsage / extKeyUsage should match string above */
  42208. keyUsageFlags = KU_DIGITAL_SIGNATURE
  42209. | KU_NON_REPUDIATION
  42210. | KU_KEY_ENCIPHERMENT
  42211. | KU_DATA_ENCIPHERMENT
  42212. | KU_KEY_AGREEMENT
  42213. | KU_KEY_CERT_SIGN
  42214. | KU_CRL_SIGN
  42215. | KU_ENCIPHER_ONLY
  42216. | KU_DECIPHER_ONLY;
  42217. extKeyUsageFlags = XKU_SSL_CLIENT
  42218. | XKU_SSL_SERVER
  42219. | XKU_CODE_SIGN
  42220. | XKU_SMIME
  42221. | XKU_TIMESTAMP
  42222. | XKU_OCSP_SIGN;
  42223. for (i = 0; i < ext_names_count; i++) {
  42224. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  42225. ext_values[i]));
  42226. X509_EXTENSION_free(ext);
  42227. ext = NULL;
  42228. }
  42229. for (i = 0; i < ext_nids_count; i++) {
  42230. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  42231. ext_values[i]));
  42232. X509_EXTENSION_free(ext);
  42233. ext = NULL;
  42234. }
  42235. /* Test adding extension to X509 */
  42236. for (i = 0; i < ext_nids_count; i++) {
  42237. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  42238. ext_values[i]));
  42239. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  42240. if (ext_nids[i] == NID_key_usage) {
  42241. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  42242. }
  42243. else if (ext_nids[i] == NID_ext_key_usage) {
  42244. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  42245. }
  42246. X509_EXTENSION_free(ext);
  42247. ext = NULL;
  42248. }
  42249. X509_free(x509);
  42250. #endif
  42251. return EXPECT_RESULT();
  42252. }
  42253. static int test_wolfSSL_X509V3_EXT(void) {
  42254. EXPECT_DECLS;
  42255. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42256. XFILE f = XBADFILE;
  42257. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  42258. char* str = NULL;
  42259. unsigned char* data = NULL;
  42260. const WOLFSSL_v3_ext_method* method = NULL;
  42261. WOLFSSL_X509* x509 = NULL;
  42262. WOLFSSL_X509_EXTENSION* ext = NULL;
  42263. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  42264. WOLFSSL_ASN1_OBJECT *obj = NULL;
  42265. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  42266. WOLFSSL_ASN1_STRING* asn1str = NULL;
  42267. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  42268. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  42269. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  42270. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  42271. WOLFSSL_GENERAL_NAME* gn = NULL;
  42272. /* Check NULL argument */
  42273. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  42274. /* Using OCSP cert with X509V3 extensions */
  42275. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42276. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42277. if (f != XBADFILE)
  42278. XFCLOSE(f);
  42279. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  42280. /* Basic Constraints */
  42281. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42282. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42283. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  42284. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  42285. ExpectIntEQ(bc->ca, 1);
  42286. ExpectNull(bc->pathlen);
  42287. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  42288. bc = NULL;
  42289. i++;
  42290. /* Subject Key Identifier */
  42291. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42292. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42293. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  42294. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  42295. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  42296. asn1str));
  42297. X509_EXTENSION_free(ext2);
  42298. ext2 = NULL;
  42299. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  42300. ExpectNotNull(method->i2s);
  42301. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  42302. wolfSSL_ASN1_STRING_free(asn1str);
  42303. asn1str = NULL;
  42304. if (str != NULL) {
  42305. actual = strcmp(str,
  42306. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  42307. }
  42308. ExpectIntEQ(actual, 0);
  42309. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42310. str = NULL;
  42311. i++;
  42312. /* Authority Key Identifier */
  42313. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42314. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42315. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  42316. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  42317. ext));
  42318. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  42319. ExpectNotNull(asn1str = aKeyId->keyid);
  42320. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  42321. asn1str));
  42322. asn1str = NULL;
  42323. if (str != NULL) {
  42324. actual = strcmp(str,
  42325. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  42326. }
  42327. ExpectIntEQ(actual, 0);
  42328. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42329. str = NULL;
  42330. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  42331. aKeyId = NULL;
  42332. i++;
  42333. /* Key Usage */
  42334. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42335. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42336. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  42337. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  42338. #if defined(WOLFSSL_QT)
  42339. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  42340. #else
  42341. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  42342. #endif
  42343. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  42344. if (data != NULL) {
  42345. #ifdef BIG_ENDIAN_ORDER
  42346. actual = data[1];
  42347. #else
  42348. actual = data[0];
  42349. #endif
  42350. }
  42351. ExpectIntEQ(actual, expected);
  42352. wolfSSL_ASN1_STRING_free(asn1str);
  42353. asn1str = NULL;
  42354. #if 1
  42355. i++;
  42356. /* Authority Info Access */
  42357. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  42358. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  42359. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  42360. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42361. ext));
  42362. #if defined(WOLFSSL_QT)
  42363. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  42364. #else
  42365. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  42366. #endif
  42367. /* URI entry is an ACCESS_DESCRIPTION type */
  42368. #if defined(WOLFSSL_QT)
  42369. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  42370. #else
  42371. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  42372. #endif
  42373. ExpectNotNull(adObj = ad->method);
  42374. /* Make sure nid is OCSP */
  42375. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  42376. /* GENERAL_NAME stores URI as an ASN1_STRING */
  42377. ExpectNotNull(gn = ad->location);
  42378. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  42379. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  42380. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  42381. #if defined(WOLFSSL_QT)
  42382. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  42383. #else
  42384. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  42385. #endif
  42386. if (str != NULL) {
  42387. actual = strcmp(str, "http://127.0.0.1:22220");
  42388. }
  42389. ExpectIntEQ(actual, 0);
  42390. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42391. aia = NULL;
  42392. #else
  42393. (void) aia; (void) ad; (void) adObj; (void) gn;
  42394. #endif
  42395. wolfSSL_X509_free(x509);
  42396. #endif
  42397. return EXPECT_RESULT();
  42398. }
  42399. static int test_wolfSSL_X509_get_extension_flags(void)
  42400. {
  42401. EXPECT_DECLS;
  42402. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42403. XFILE f = XBADFILE;
  42404. X509* x509 = NULL;
  42405. unsigned int extFlags;
  42406. unsigned int keyUsageFlags;
  42407. unsigned int extKeyUsageFlags;
  42408. /* client-int-cert.pem has the following extension flags. */
  42409. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  42410. /* and the following key usage flags. */
  42411. keyUsageFlags = KU_DIGITAL_SIGNATURE
  42412. | KU_NON_REPUDIATION
  42413. | KU_KEY_ENCIPHERMENT;
  42414. /* and the following extended key usage flags. */
  42415. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  42416. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  42417. XBADFILE);
  42418. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  42419. if (f != XBADFILE) {
  42420. XFCLOSE(f);
  42421. f = XBADFILE;
  42422. }
  42423. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  42424. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  42425. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  42426. X509_free(x509);
  42427. x509 = NULL;
  42428. /* client-cert-ext.pem has the following extension flags. */
  42429. extFlags = EXFLAG_KUSAGE;
  42430. /* and the following key usage flags. */
  42431. keyUsageFlags = KU_DIGITAL_SIGNATURE
  42432. | KU_KEY_CERT_SIGN
  42433. | KU_CRL_SIGN;
  42434. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  42435. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  42436. if (f != XBADFILE)
  42437. XFCLOSE(f);
  42438. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  42439. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  42440. X509_free(x509);
  42441. #endif /* OPENSSL_ALL */
  42442. return EXPECT_RESULT();
  42443. }
  42444. static int test_wolfSSL_X509_get_ext(void)
  42445. {
  42446. EXPECT_DECLS;
  42447. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42448. int ret = 0;
  42449. XFILE f = XBADFILE;
  42450. WOLFSSL_X509* x509 = NULL;
  42451. WOLFSSL_X509_EXTENSION* foundExtension;
  42452. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42453. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42454. if (f != XBADFILE)
  42455. XFCLOSE(f);
  42456. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  42457. /* wolfSSL_X509_get_ext() valid input */
  42458. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  42459. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  42460. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  42461. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  42462. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  42463. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  42464. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  42465. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  42466. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  42467. wolfSSL_X509_free(x509);
  42468. #endif
  42469. return EXPECT_RESULT();
  42470. }
  42471. static int test_wolfSSL_X509_get_ext_by_NID(void)
  42472. {
  42473. EXPECT_DECLS;
  42474. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42475. int rc;
  42476. XFILE f = XBADFILE;
  42477. WOLFSSL_X509* x509 = NULL;
  42478. ASN1_OBJECT* obj = NULL;
  42479. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42480. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42481. if (f != XBADFILE)
  42482. XFCLOSE(f);
  42483. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  42484. -1), 0);
  42485. /* Start search from last location (should fail) */
  42486. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  42487. rc), -1);
  42488. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  42489. -2), -1);
  42490. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  42491. -1), -1);
  42492. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  42493. /* NID_ext_key_usage, check also its nid and oid */
  42494. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  42495. -1);
  42496. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  42497. x509, rc)));
  42498. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  42499. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  42500. wolfSSL_X509_free(x509);
  42501. #endif
  42502. return EXPECT_RESULT();
  42503. }
  42504. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  42505. {
  42506. EXPECT_DECLS;
  42507. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42508. int rc;
  42509. XFILE f = XBADFILE;
  42510. WOLFSSL_X509* x509 = NULL;
  42511. WOLFSSL_X509_EXTENSION* ext;
  42512. WOLFSSL_ASN1_STRING* sanString;
  42513. byte* sanDer;
  42514. const byte expectedDer[] = {
  42515. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  42516. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  42517. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42518. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  42519. if (f != XBADFILE)
  42520. XFCLOSE(f);
  42521. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  42522. ExpectNotNull(ext = X509_get_ext(x509, rc));
  42523. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  42524. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  42525. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  42526. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  42527. X509_free(x509);
  42528. #endif
  42529. return EXPECT_RESULT();
  42530. }
  42531. static int test_wolfSSL_X509_EXTENSION_new(void)
  42532. {
  42533. EXPECT_DECLS;
  42534. #if defined (OPENSSL_ALL)
  42535. WOLFSSL_X509_EXTENSION* ext = NULL;
  42536. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  42537. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  42538. wolfSSL_X509_EXTENSION_free(ext);
  42539. #endif
  42540. return EXPECT_RESULT();
  42541. }
  42542. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  42543. {
  42544. EXPECT_DECLS;
  42545. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42546. WOLFSSL_X509* x509 = NULL;
  42547. WOLFSSL_X509_EXTENSION* ext = NULL;
  42548. WOLFSSL_ASN1_OBJECT* o = NULL;
  42549. XFILE file = XBADFILE;
  42550. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42551. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  42552. if (file != XBADFILE)
  42553. XFCLOSE(file);
  42554. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  42555. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  42556. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  42557. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  42558. ExpectIntEQ(o->nid, 128);
  42559. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  42560. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  42561. wolfSSL_X509_free(x509);
  42562. #endif
  42563. return EXPECT_RESULT();
  42564. }
  42565. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  42566. {
  42567. EXPECT_DECLS;
  42568. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42569. WOLFSSL_X509* x509 = NULL;
  42570. WOLFSSL_X509_EXTENSION* ext;
  42571. WOLFSSL_ASN1_STRING* str;
  42572. XFILE file = XBADFILE;
  42573. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42574. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  42575. if (file != XBADFILE)
  42576. XFCLOSE(file);
  42577. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  42578. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  42579. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  42580. wolfSSL_X509_free(x509);
  42581. #endif
  42582. return EXPECT_RESULT();
  42583. }
  42584. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  42585. {
  42586. EXPECT_DECLS;
  42587. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  42588. WOLFSSL_X509* x509 = NULL;
  42589. WOLFSSL_X509_EXTENSION* ext;
  42590. XFILE file = XBADFILE;
  42591. int crit;
  42592. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42593. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  42594. if (file != XBADFILE)
  42595. XFCLOSE(file);
  42596. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  42597. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  42598. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  42599. wolfSSL_X509_free(x509);
  42600. #endif
  42601. return EXPECT_RESULT();
  42602. }
  42603. static int test_wolfSSL_X509V3_EXT_print(void)
  42604. {
  42605. EXPECT_DECLS;
  42606. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  42607. !defined(NO_RSA)
  42608. {
  42609. XFILE f = XBADFILE;
  42610. WOLFSSL_X509* x509 = NULL;
  42611. X509_EXTENSION * ext = NULL;
  42612. int loc;
  42613. BIO *bio = NULL;
  42614. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  42615. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42616. if (f != XBADFILE)
  42617. fclose(f);
  42618. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  42619. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  42620. NID_basic_constraints, -1), -1);
  42621. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  42622. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  42623. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  42624. NID_subject_key_identifier, -1), -1);
  42625. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  42626. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  42627. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  42628. NID_authority_key_identifier, -1), -1);
  42629. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  42630. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  42631. wolfSSL_BIO_free(bio);
  42632. wolfSSL_X509_free(x509);
  42633. }
  42634. {
  42635. X509 *x509 = NULL;
  42636. BIO *bio = NULL;
  42637. X509_EXTENSION *ext;
  42638. unsigned int i;
  42639. unsigned int idx;
  42640. /* Some NIDs to test with */
  42641. int nids[] = {
  42642. /* NID_key_usage, currently X509_get_ext returns this as a bit
  42643. * string, which messes up X509V3_EXT_print */
  42644. /* NID_ext_key_usage, */
  42645. NID_subject_alt_name,
  42646. };
  42647. int* n;
  42648. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  42649. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  42650. WOLFSSL_FILETYPE_PEM));
  42651. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  42652. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  42653. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  42654. * update the index */
  42655. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  42656. ExpectNotNull(ext = X509_get_ext(x509, idx));
  42657. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  42658. ExpectIntGT(fprintf(stderr, "\n"), 0);
  42659. }
  42660. BIO_free(bio);
  42661. X509_free(x509);
  42662. }
  42663. #endif
  42664. return EXPECT_RESULT();
  42665. }
  42666. static int test_wolfSSL_X509_cmp(void)
  42667. {
  42668. EXPECT_DECLS;
  42669. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42670. XFILE file1 = XBADFILE;
  42671. XFILE file2 = XBADFILE;
  42672. WOLFSSL_X509* cert1 = NULL;
  42673. WOLFSSL_X509* cert2 = NULL;
  42674. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  42675. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  42676. XBADFILE);
  42677. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  42678. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  42679. if (file1 != XBADFILE)
  42680. fclose(file1);
  42681. if (file2 != XBADFILE)
  42682. fclose(file2);
  42683. /* wolfSSL_X509_cmp() testing matching certs */
  42684. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  42685. /* wolfSSL_X509_cmp() testing mismatched certs */
  42686. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  42687. /* wolfSSL_X509_cmp() testing NULL, valid args */
  42688. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  42689. /* wolfSSL_X509_cmp() testing valid, NULL args */
  42690. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  42691. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  42692. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  42693. wolfSSL_X509_free(cert1);
  42694. wolfSSL_X509_free(cert2);
  42695. #endif
  42696. return EXPECT_RESULT();
  42697. }
  42698. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  42699. {
  42700. EXPECT_DECLS;
  42701. #if defined(OPENSSL_ALL)
  42702. EVP_PKEY* pkey;
  42703. pkey = EVP_PKEY_new();
  42704. ExpectNotNull(pkey);
  42705. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  42706. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  42707. EVP_PKEY_free(pkey);
  42708. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  42709. EVP_PKEY_free(pkey);
  42710. EVP_PKEY_free(pkey);
  42711. #endif
  42712. return EXPECT_RESULT();
  42713. }
  42714. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  42715. {
  42716. EXPECT_DECLS;
  42717. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  42718. EVP_PKEY* pkey = NULL;
  42719. const unsigned char* p;
  42720. unsigned char *der = NULL;
  42721. unsigned char *tmp = NULL;
  42722. int derLen;
  42723. p = client_keypub_der_2048;
  42724. /* Check that key can be successfully decoded. */
  42725. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  42726. sizeof_client_keypub_der_2048));
  42727. /* Check that key can be successfully encoded. */
  42728. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  42729. /* Ensure that the encoded version matches the original. */
  42730. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  42731. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  42732. /* Do same test except with pre-allocated buffer to ensure the der pointer
  42733. * is advanced. */
  42734. tmp = der;
  42735. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  42736. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  42737. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  42738. ExpectTrue(der + derLen == tmp);
  42739. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  42740. EVP_PKEY_free(pkey);
  42741. #endif
  42742. return EXPECT_RESULT();
  42743. }
  42744. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  42745. {
  42746. EXPECT_DECLS;
  42747. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  42748. !defined(NO_ASN) && !defined(NO_PWDBASED)
  42749. EVP_PKEY* pkey = NULL;
  42750. const unsigned char* p;
  42751. unsigned char *der = NULL;
  42752. unsigned char *tmp = NULL;
  42753. int derLen;
  42754. unsigned char pub_buf[65];
  42755. const int pub_len = 65;
  42756. BN_CTX* ctx;
  42757. EC_GROUP* curve = NULL;
  42758. EC_KEY* ephemeral_key = NULL;
  42759. const EC_POINT* h;
  42760. /* Generate an x963 key pair and get public part into pub_buf */
  42761. ExpectNotNull(ctx = BN_CTX_new());
  42762. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  42763. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  42764. NID_X9_62_prime256v1));
  42765. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  42766. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  42767. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  42768. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  42769. /* Prepare the EVP_PKEY */
  42770. ExpectNotNull(pkey = EVP_PKEY_new());
  42771. p = pub_buf;
  42772. /* Check that key can be successfully decoded. */
  42773. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  42774. pub_len));
  42775. /* Check that key can be successfully encoded. */
  42776. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  42777. /* Ensure that the encoded version matches the original. */
  42778. ExpectIntEQ(derLen, pub_len);
  42779. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  42780. /* Do same test except with pre-allocated buffer to ensure the der pointer
  42781. * is advanced. */
  42782. tmp = der;
  42783. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  42784. ExpectIntEQ(derLen, pub_len);
  42785. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  42786. ExpectTrue(der + derLen == tmp);
  42787. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  42788. EVP_PKEY_free(pkey);
  42789. EC_KEY_free(ephemeral_key);
  42790. EC_GROUP_free(curve);
  42791. #endif
  42792. return EXPECT_RESULT();
  42793. }
  42794. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  42795. {
  42796. EXPECT_DECLS;
  42797. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  42798. DSA* dsa = NULL;
  42799. byte derIn[] = {
  42800. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  42801. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  42802. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  42803. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  42804. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  42805. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  42806. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  42807. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  42808. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  42809. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  42810. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  42811. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  42812. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  42813. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  42814. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  42815. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  42816. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  42817. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  42818. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  42819. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  42820. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  42821. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  42822. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  42823. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  42824. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  42825. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  42826. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  42827. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  42828. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  42829. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  42830. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  42831. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  42832. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  42833. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  42834. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  42835. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  42836. 0x1e, 0xde, 0xc4
  42837. };
  42838. int derInLen = sizeof(derIn);
  42839. byte* derOut = NULL;
  42840. int derOutLen;
  42841. byte* p = derIn;
  42842. /* Check that params can be successfully decoded. */
  42843. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  42844. /* Check that params can be successfully encoded. */
  42845. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  42846. /* Ensure that the encoded version matches the original. */
  42847. ExpectIntEQ(derInLen, derOutLen);
  42848. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  42849. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  42850. DSA_free(dsa);
  42851. #endif
  42852. return EXPECT_RESULT();
  42853. }
  42854. static int test_wolfSSL_i2d_PrivateKey(void)
  42855. {
  42856. EXPECT_DECLS;
  42857. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  42858. !defined(NO_ASN) && !defined(NO_PWDBASED)
  42859. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  42860. {
  42861. EVP_PKEY* pkey = NULL;
  42862. const unsigned char* server_key =
  42863. (const unsigned char*)server_key_der_2048;
  42864. unsigned char buf[FOURK_BUF];
  42865. unsigned char* pt = NULL;
  42866. int bufSz;
  42867. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  42868. (long)sizeof_server_key_der_2048));
  42869. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  42870. pt = buf;
  42871. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  42872. ExpectIntNE((pt - buf), 0);
  42873. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  42874. EVP_PKEY_free(pkey);
  42875. }
  42876. #endif
  42877. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42878. {
  42879. EVP_PKEY* pkey = NULL;
  42880. const unsigned char* client_key =
  42881. (const unsigned char*)ecc_clikey_der_256;
  42882. unsigned char buf[FOURK_BUF];
  42883. unsigned char* pt = NULL;
  42884. int bufSz;
  42885. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  42886. (long)sizeof_ecc_clikey_der_256)));
  42887. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  42888. pt = buf;
  42889. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  42890. ExpectIntNE((pt - buf), 0);
  42891. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  42892. EVP_PKEY_free(pkey);
  42893. }
  42894. #endif
  42895. #endif
  42896. return EXPECT_RESULT();
  42897. }
  42898. static int test_wolfSSL_OCSP_id_get0_info(void)
  42899. {
  42900. EXPECT_DECLS;
  42901. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  42902. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42903. X509* cert = NULL;
  42904. X509* issuer = NULL;
  42905. OCSP_CERTID* id = NULL;
  42906. OCSP_CERTID* id2 = NULL;
  42907. ASN1_STRING* name = NULL;
  42908. ASN1_OBJECT* pmd = NULL;
  42909. ASN1_STRING* keyHash = NULL;
  42910. ASN1_INTEGER* serial = NULL;
  42911. ASN1_INTEGER* x509Int;
  42912. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  42913. SSL_FILETYPE_PEM));
  42914. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  42915. SSL_FILETYPE_PEM));
  42916. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  42917. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  42918. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  42919. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  42920. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  42921. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  42922. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  42923. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  42924. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  42925. ExpectNotNull(serial);
  42926. /* compare serial number to one in cert, should be equal */
  42927. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  42928. ExpectIntEQ(x509Int->length, serial->length);
  42929. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  42930. /* test OCSP_id_cmp */
  42931. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  42932. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  42933. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  42934. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  42935. if (id != NULL) {
  42936. id->issuerHash[0] = ~id->issuerHash[0];
  42937. }
  42938. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  42939. OCSP_CERTID_free(id);
  42940. OCSP_CERTID_free(id2);
  42941. X509_free(cert); /* free's x509Int */
  42942. X509_free(issuer);
  42943. #endif
  42944. return EXPECT_RESULT();
  42945. }
  42946. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  42947. {
  42948. EXPECT_DECLS;
  42949. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  42950. WOLFSSL_OCSP_CERTID certId;
  42951. byte* targetBuffer = NULL;
  42952. byte* p;
  42953. /* OCSP CertID bytes taken from PCAP */
  42954. byte rawCertId[] = {
  42955. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  42956. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  42957. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  42958. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  42959. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  42960. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  42961. 0xcf, 0xbc, 0x91
  42962. };
  42963. int ret = 0;
  42964. int i;
  42965. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  42966. certId.rawCertId = rawCertId;
  42967. certId.rawCertIdSize = sizeof(rawCertId);
  42968. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  42969. DYNAMIC_TYPE_TMP_BUFFER));
  42970. p = targetBuffer;
  42971. /* Function returns the size of the encoded data. */
  42972. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  42973. /* If target buffer is not null, function increments targetBuffer to point
  42974. * just past the end of the encoded data. */
  42975. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  42976. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  42977. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  42978. }
  42979. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42980. targetBuffer = NULL;
  42981. /* If target buffer is null, function allocates memory for a buffer and
  42982. * copies the encoded data into it. targetBuffer then points to the start of
  42983. * this newly allocate buffer. */
  42984. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  42985. sizeof(rawCertId));
  42986. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  42987. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  42988. }
  42989. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  42990. #endif
  42991. return EXPECT_RESULT();
  42992. }
  42993. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  42994. {
  42995. EXPECT_DECLS;
  42996. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  42997. WOLFSSL_OCSP_CERTID* certId;
  42998. WOLFSSL_OCSP_CERTID* certIdGood;
  42999. WOLFSSL_OCSP_CERTID* certIdBad;
  43000. const unsigned char* rawCertIdPtr;
  43001. const unsigned char rawCertId[] = {
  43002. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  43003. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  43004. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  43005. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  43006. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  43007. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  43008. 0xcf, 0xbc, 0x91
  43009. };
  43010. rawCertIdPtr = &rawCertId[0];
  43011. /* If the cert ID is NULL the function should allocate it and copy the
  43012. * data to it. */
  43013. certId = NULL;
  43014. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  43015. sizeof(rawCertId)));
  43016. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  43017. if (certId != NULL) {
  43018. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  43019. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  43020. }
  43021. /* If the cert ID is not NULL the function will just copy the data to it. */
  43022. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  43023. DYNAMIC_TYPE_TMP_BUFFER));
  43024. ExpectNotNull(certId);
  43025. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  43026. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  43027. rawCertIdPtr = &rawCertId[0];
  43028. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  43029. sizeof(rawCertId)));
  43030. ExpectPtrEq(certIdGood, certId);
  43031. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  43032. if (certId != NULL) {
  43033. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  43034. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43035. certId = NULL;
  43036. }
  43037. /* The below tests should fail when passed bad parameters. NULL should
  43038. * always be returned. */
  43039. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  43040. sizeof(rawCertId)));
  43041. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  43042. sizeof(rawCertId)));
  43043. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  43044. #endif
  43045. return EXPECT_RESULT();
  43046. }
  43047. static int test_wolfSSL_OCSP_id_cmp(void)
  43048. {
  43049. EXPECT_DECLS;
  43050. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43051. OCSP_CERTID id1;
  43052. OCSP_CERTID id2;
  43053. XMEMSET(&id1, 0, sizeof(id1));
  43054. XMEMSET(&id2, 0, sizeof(id2));
  43055. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  43056. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  43057. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  43058. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  43059. #endif
  43060. return EXPECT_RESULT();
  43061. }
  43062. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  43063. {
  43064. EXPECT_DECLS;
  43065. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43066. WOLFSSL_OCSP_SINGLERESP single;
  43067. const WOLFSSL_OCSP_CERTID* certId;
  43068. XMEMSET(&single, 0, sizeof(single));
  43069. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  43070. ExpectPtrEq(&single, certId);
  43071. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  43072. #endif
  43073. return EXPECT_RESULT();
  43074. }
  43075. static int test_wolfSSL_OCSP_single_get0_status(void)
  43076. {
  43077. EXPECT_DECLS;
  43078. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43079. WOLFSSL_OCSP_SINGLERESP single;
  43080. CertStatus certStatus;
  43081. WOLFSSL_ASN1_TIME* thisDate;
  43082. WOLFSSL_ASN1_TIME* nextDate;
  43083. int ret, i;
  43084. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43085. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  43086. /* Fill the date fields with some dummy data. */
  43087. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  43088. certStatus.thisDateParsed.data[i] = i;
  43089. certStatus.nextDateParsed.data[i] = i;
  43090. }
  43091. certStatus.status = CERT_GOOD;
  43092. single.status = &certStatus;
  43093. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  43094. &nextDate);
  43095. ExpectIntEQ(ret, CERT_GOOD);
  43096. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  43097. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  43098. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  43099. CERT_GOOD);
  43100. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  43101. NULL), CERT_GOOD);
  43102. #endif
  43103. return EXPECT_RESULT();
  43104. }
  43105. static int test_wolfSSL_OCSP_resp_count(void)
  43106. {
  43107. EXPECT_DECLS;
  43108. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43109. WOLFSSL_OCSP_BASICRESP basicResp;
  43110. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  43111. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  43112. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  43113. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43114. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43115. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  43116. basicResp.single = &singleRespOne;
  43117. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  43118. singleRespOne.next = &singleRespTwo;
  43119. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  43120. #endif
  43121. return EXPECT_RESULT();
  43122. }
  43123. static int test_wolfSSL_OCSP_resp_get0(void)
  43124. {
  43125. EXPECT_DECLS;
  43126. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  43127. WOLFSSL_OCSP_BASICRESP basicResp;
  43128. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  43129. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  43130. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  43131. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43132. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  43133. basicResp.single = &singleRespOne;
  43134. singleRespOne.next = &singleRespTwo;
  43135. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  43136. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  43137. #endif
  43138. return EXPECT_RESULT();
  43139. }
  43140. static int test_wolfSSL_EVP_PKEY_derive(void)
  43141. {
  43142. EXPECT_DECLS;
  43143. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43144. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  43145. EVP_PKEY_CTX *ctx = NULL;
  43146. unsigned char *skey = NULL;
  43147. size_t skeylen;
  43148. EVP_PKEY *pkey = NULL;
  43149. EVP_PKEY *peerkey = NULL;
  43150. const unsigned char* key;
  43151. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  43152. /* DH */
  43153. key = dh_key_der_2048;
  43154. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  43155. sizeof_dh_key_der_2048)));
  43156. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  43157. key = dh_key_der_2048;
  43158. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  43159. sizeof_dh_key_der_2048)));
  43160. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  43161. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43162. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  43163. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  43164. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  43165. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  43166. DYNAMIC_TYPE_OPENSSL));
  43167. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  43168. EVP_PKEY_CTX_free(ctx);
  43169. ctx = NULL;
  43170. EVP_PKEY_free(peerkey);
  43171. peerkey = NULL;
  43172. EVP_PKEY_free(pkey);
  43173. pkey = NULL;
  43174. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  43175. skey = NULL;
  43176. #endif
  43177. #ifdef HAVE_ECC
  43178. /* ECDH */
  43179. key = ecc_clikey_der_256;
  43180. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  43181. sizeof_ecc_clikey_der_256)));
  43182. key = ecc_clikeypub_der_256;
  43183. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  43184. sizeof_ecc_clikeypub_der_256)));
  43185. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43186. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  43187. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  43188. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  43189. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  43190. DYNAMIC_TYPE_OPENSSL));
  43191. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  43192. EVP_PKEY_CTX_free(ctx);
  43193. EVP_PKEY_free(peerkey);
  43194. EVP_PKEY_free(pkey);
  43195. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  43196. #endif /* HAVE_ECC */
  43197. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  43198. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43199. return EXPECT_RESULT();
  43200. }
  43201. static int test_wolfSSL_EVP_PBE_scrypt(void)
  43202. {
  43203. EXPECT_DECLS;
  43204. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  43205. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  43206. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  43207. int ret;
  43208. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  43209. int pwdlen = sizeof(pwd);
  43210. const byte salt[] = {'N','a','C','l'};
  43211. int saltlen = sizeof(salt);
  43212. byte key[80];
  43213. word64 numOvr32 = (word64)INT32_MAX + 1;
  43214. /* expected derived key for N:16, r:1, p:1 */
  43215. const byte expectedKey[] = {
  43216. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  43217. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  43218. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  43219. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  43220. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  43221. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  43222. 0xE7, 0xE9, 0xC0, 0x9A};
  43223. /* N r p mx key keylen */
  43224. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  43225. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  43226. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  43227. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  43228. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  43229. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  43230. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  43231. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  43232. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  43233. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  43234. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  43235. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  43236. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  43237. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  43238. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  43239. ExpectIntEQ(ret, 1); /* should succeed */
  43240. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  43241. key, 64);
  43242. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  43243. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  43244. key, 64);
  43245. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  43246. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  43247. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  43248. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  43249. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  43250. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  43251. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  43252. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  43253. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  43254. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  43255. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  43256. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  43257. ExpectIntEQ(ret, 1);
  43258. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  43259. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  43260. #endif /* !NO_PWDBASED && !NO_SHA256 */
  43261. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  43262. return EXPECT_RESULT();
  43263. }
  43264. static int test_no_op_functions(void)
  43265. {
  43266. EXPECT_DECLS;
  43267. #if defined(OPENSSL_EXTRA)
  43268. /* this makes sure wolfSSL can compile and run these no-op functions */
  43269. SSL_load_error_strings();
  43270. ENGINE_load_builtin_engines();
  43271. OpenSSL_add_all_ciphers();
  43272. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  43273. #endif
  43274. return EXPECT_RESULT();
  43275. }
  43276. static int test_wolfSSL_CRYPTO_memcmp(void)
  43277. {
  43278. EXPECT_DECLS;
  43279. #ifdef OPENSSL_EXTRA
  43280. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  43281. "implementation of TLS/SSL for embedded devices to the cloud.";
  43282. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  43283. "implementation of TLS/SSL for embedded devices to the cloud.";
  43284. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  43285. "implementation of TLS/SSL for embedded devices to the cloud!";
  43286. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  43287. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  43288. #endif
  43289. return EXPECT_RESULT();
  43290. }
  43291. /*----------------------------------------------------------------------------*
  43292. | wolfCrypt ASN
  43293. *----------------------------------------------------------------------------*/
  43294. static int test_wc_CreateEncryptedPKCS8Key(void)
  43295. {
  43296. EXPECT_DECLS;
  43297. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  43298. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  43299. WC_RNG rng;
  43300. byte* encKey = NULL;
  43301. word32 encKeySz = 0;
  43302. word32 decKeySz = 0;
  43303. const char password[] = "Lorem ipsum dolor sit amet";
  43304. word32 passwordSz = (word32)XSTRLEN(password);
  43305. word32 tradIdx = 0;
  43306. XMEMSET(&rng, 0, sizeof(WC_RNG));
  43307. ExpectIntEQ(wc_InitRng(&rng), 0);
  43308. /* Call with NULL for out buffer to get necessary length. */
  43309. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  43310. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  43311. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  43312. LENGTH_ONLY_E);
  43313. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  43314. DYNAMIC_TYPE_TMP_BUFFER));
  43315. /* Call with the allocated out buffer. */
  43316. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  43317. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  43318. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  43319. 0);
  43320. /* Decrypt the encrypted PKCS8 key we just made. */
  43321. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  43322. passwordSz)), 0);
  43323. /* encKey now holds the decrypted key (decrypted in place). */
  43324. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  43325. /* Check that the decrypted key matches the key prior to encryption. */
  43326. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  43327. sizeof_server_key_der_2048), 0);
  43328. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43329. wc_FreeRng(&rng);
  43330. #endif
  43331. return EXPECT_RESULT();
  43332. }
  43333. static int test_wc_GetPkcs8TraditionalOffset(void)
  43334. {
  43335. EXPECT_DECLS;
  43336. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  43337. int length;
  43338. int derSz = 0;
  43339. word32 inOutIdx;
  43340. const char* path = "./certs/server-keyPkcs8.der";
  43341. XFILE file = XBADFILE;
  43342. byte der[2048];
  43343. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  43344. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  43345. if (file != XBADFILE)
  43346. XFCLOSE(file);
  43347. /* valid case */
  43348. inOutIdx = 0;
  43349. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  43350. 0);
  43351. /* inOutIdx > sz */
  43352. inOutIdx = 4000;
  43353. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  43354. BAD_FUNC_ARG);
  43355. /* null input */
  43356. inOutIdx = 0;
  43357. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  43358. BAD_FUNC_ARG);
  43359. /* invalid input, fill buffer with 1's */
  43360. XMEMSET(der, 1, sizeof(der));
  43361. inOutIdx = 0;
  43362. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  43363. ASN_PARSE_E);
  43364. #endif /* NO_ASN */
  43365. return EXPECT_RESULT();
  43366. }
  43367. static int test_wc_SetSubjectRaw(void)
  43368. {
  43369. EXPECT_DECLS;
  43370. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  43371. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  43372. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  43373. WOLFSSL_X509* x509 = NULL;
  43374. int peerCertSz;
  43375. const byte* peerCertBuf = NULL;
  43376. Cert forgedCert;
  43377. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  43378. WOLFSSL_FILETYPE_ASN1));
  43379. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  43380. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  43381. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  43382. wolfSSL_FreeX509(x509);
  43383. #endif
  43384. return EXPECT_RESULT();
  43385. }
  43386. static int test_wc_GetSubjectRaw(void)
  43387. {
  43388. EXPECT_DECLS;
  43389. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  43390. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  43391. Cert cert;
  43392. byte *subjectRaw;
  43393. ExpectIntEQ(0, wc_InitCert(&cert));
  43394. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  43395. #endif
  43396. return EXPECT_RESULT();
  43397. }
  43398. static int test_wc_SetIssuerRaw(void)
  43399. {
  43400. EXPECT_DECLS;
  43401. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  43402. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  43403. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  43404. WOLFSSL_X509* x509 = NULL;
  43405. int peerCertSz;
  43406. const byte* peerCertBuf;
  43407. Cert forgedCert;
  43408. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  43409. WOLFSSL_FILETYPE_ASN1));
  43410. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  43411. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  43412. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  43413. wolfSSL_FreeX509(x509);
  43414. #endif
  43415. return EXPECT_RESULT();
  43416. }
  43417. static int test_wc_SetIssueBuffer(void)
  43418. {
  43419. EXPECT_DECLS;
  43420. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  43421. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  43422. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  43423. WOLFSSL_X509* x509 = NULL;
  43424. int peerCertSz;
  43425. const byte* peerCertBuf;
  43426. Cert forgedCert;
  43427. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  43428. WOLFSSL_FILETYPE_ASN1));
  43429. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  43430. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  43431. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  43432. wolfSSL_FreeX509(x509);
  43433. #endif
  43434. return EXPECT_RESULT();
  43435. }
  43436. /*
  43437. * Testing wc_SetSubjectKeyId
  43438. */
  43439. static int test_wc_SetSubjectKeyId(void)
  43440. {
  43441. EXPECT_DECLS;
  43442. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  43443. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  43444. Cert cert;
  43445. const char* file = "certs/ecc-client-keyPub.pem";
  43446. ExpectIntEQ(0, wc_InitCert(&cert));
  43447. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  43448. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  43449. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  43450. #endif
  43451. return EXPECT_RESULT();
  43452. } /* END test_wc_SetSubjectKeyId */
  43453. /*
  43454. * Testing wc_SetSubject
  43455. */
  43456. static int test_wc_SetSubject(void)
  43457. {
  43458. EXPECT_DECLS;
  43459. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  43460. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  43461. Cert cert;
  43462. const char* file = "./certs/ca-ecc-cert.pem";
  43463. ExpectIntEQ(0, wc_InitCert(&cert));
  43464. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  43465. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  43466. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  43467. #endif
  43468. return EXPECT_RESULT();
  43469. } /* END test_wc_SetSubject */
  43470. static int test_CheckCertSignature(void)
  43471. {
  43472. EXPECT_DECLS;
  43473. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  43474. WOLFSSL_CERT_MANAGER* cm = NULL;
  43475. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  43476. XFILE fp = XBADFILE;
  43477. byte cert[4096];
  43478. int certSz;
  43479. #endif
  43480. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  43481. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  43482. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  43483. #ifndef NO_RSA
  43484. #ifdef USE_CERT_BUFFERS_1024
  43485. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  43486. sizeof_server_cert_der_1024, NULL, cm));
  43487. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  43488. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  43489. WOLFSSL_FILETYPE_ASN1));
  43490. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  43491. sizeof_server_cert_der_1024, NULL, cm));
  43492. #elif defined(USE_CERT_BUFFERS_2048)
  43493. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  43494. sizeof_server_cert_der_2048, NULL, cm));
  43495. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  43496. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  43497. WOLFSSL_FILETYPE_ASN1));
  43498. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  43499. sizeof_server_cert_der_2048, NULL, cm));
  43500. #endif
  43501. #endif
  43502. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  43503. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  43504. sizeof_serv_ecc_der_256, NULL, cm));
  43505. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  43506. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  43507. WOLFSSL_FILETYPE_ASN1));
  43508. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  43509. NULL, cm));
  43510. #endif
  43511. #if !defined(NO_FILESYSTEM)
  43512. wolfSSL_CertManagerFree(cm);
  43513. cm = NULL;
  43514. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  43515. #ifndef NO_RSA
  43516. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  43517. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  43518. if (fp != XBADFILE) {
  43519. XFCLOSE(fp);
  43520. fp = XBADFILE;
  43521. }
  43522. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  43523. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  43524. "./certs/ca-cert.pem", NULL));
  43525. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  43526. #endif
  43527. #ifdef HAVE_ECC
  43528. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  43529. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  43530. if (fp != XBADFILE) {
  43531. XFCLOSE(fp);
  43532. fp = XBADFILE;
  43533. }
  43534. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  43535. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  43536. "./certs/ca-ecc-cert.pem", NULL));
  43537. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  43538. #endif
  43539. #endif
  43540. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  43541. (void)fp;
  43542. (void)cert;
  43543. (void)certSz;
  43544. #endif
  43545. wolfSSL_CertManagerFree(cm);
  43546. #endif
  43547. return EXPECT_RESULT();
  43548. }
  43549. static int test_wc_ParseCert(void)
  43550. {
  43551. EXPECT_DECLS;
  43552. #if !defined(NO_CERTS) && !defined(NO_RSA)
  43553. DecodedCert decodedCert;
  43554. const byte* rawCert = client_cert_der_2048;
  43555. const int rawCertSize = sizeof_client_cert_der_2048;
  43556. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  43557. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  43558. #ifndef IGNORE_NAME_CONSTRAINTS
  43559. /* check that the subjects emailAddress was not put in the alt name list */
  43560. ExpectNotNull(decodedCert.subjectEmail);
  43561. ExpectNull(decodedCert.altEmailNames);
  43562. #endif
  43563. wc_FreeDecodedCert(&decodedCert);
  43564. #endif
  43565. return EXPECT_RESULT();
  43566. }
  43567. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  43568. * the API safely errors out on badly-formed ASN input.
  43569. * NOTE: Test not compatible with released FIPS implementations!
  43570. */
  43571. static int test_wc_ParseCert_Error(void)
  43572. {
  43573. EXPECT_DECLS;
  43574. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  43575. (!defined(HAVE_FIPS) || \
  43576. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  43577. DecodedCert decodedCert;
  43578. int i;
  43579. /* Certificate data */
  43580. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  43581. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  43582. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  43583. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  43584. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  43585. /* Test data */
  43586. const struct testStruct {
  43587. const byte* c;
  43588. const int cSz;
  43589. const int expRet;
  43590. } t[] = {
  43591. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  43592. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  43593. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  43594. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  43595. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  43596. };
  43597. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  43598. for (i = 0; i < tSz; i++) {
  43599. WOLFSSL_MSG_EX("i == %d", i);
  43600. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  43601. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  43602. wc_FreeDecodedCert(&decodedCert);
  43603. }
  43604. #endif
  43605. return EXPECT_RESULT();
  43606. }
  43607. static int test_MakeCertWithPathLen(void)
  43608. {
  43609. EXPECT_DECLS;
  43610. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  43611. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  43612. const byte expectedPathLen = 7;
  43613. Cert cert;
  43614. DecodedCert decodedCert;
  43615. byte der[FOURK_BUF];
  43616. int derSize = 0;
  43617. WC_RNG rng;
  43618. ecc_key key;
  43619. int ret;
  43620. XMEMSET(&rng, 0, sizeof(WC_RNG));
  43621. XMEMSET(&key, 0, sizeof(ecc_key));
  43622. XMEMSET(&cert, 0, sizeof(Cert));
  43623. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  43624. ExpectIntEQ(wc_InitRng(&rng), 0);
  43625. ExpectIntEQ(wc_ecc_init(&key), 0);
  43626. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  43627. ExpectIntEQ(wc_InitCert(&cert), 0);
  43628. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  43629. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  43630. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  43631. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  43632. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  43633. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  43634. CTC_NAME_SIZE);
  43635. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  43636. CTC_NAME_SIZE);
  43637. cert.selfSigned = 1;
  43638. cert.isCA = 1;
  43639. cert.pathLen = expectedPathLen;
  43640. cert.pathLenSet = 1;
  43641. cert.sigType = CTC_SHA256wECDSA;
  43642. #ifdef WOLFSSL_CERT_EXT
  43643. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  43644. #endif
  43645. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  43646. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  43647. FOURK_BUF, NULL, &key, &rng), 0);
  43648. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  43649. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  43650. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  43651. wc_FreeDecodedCert(&decodedCert);
  43652. ret = wc_ecc_free(&key);
  43653. ExpectIntEQ(ret, 0);
  43654. ret = wc_FreeRng(&rng);
  43655. ExpectIntEQ(ret, 0);
  43656. #endif
  43657. return EXPECT_RESULT();
  43658. }
  43659. /*----------------------------------------------------------------------------*
  43660. | wolfCrypt ECC
  43661. *----------------------------------------------------------------------------*/
  43662. static int test_wc_ecc_get_curve_size_from_name(void)
  43663. {
  43664. EXPECT_DECLS;
  43665. #ifdef HAVE_ECC
  43666. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  43667. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  43668. #endif
  43669. /* invalid case */
  43670. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  43671. /* NULL input */
  43672. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  43673. #endif /* HAVE_ECC */
  43674. return EXPECT_RESULT();
  43675. }
  43676. static int test_wc_ecc_get_curve_id_from_name(void)
  43677. {
  43678. EXPECT_DECLS;
  43679. #ifdef HAVE_ECC
  43680. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  43681. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  43682. ECC_SECP256R1);
  43683. #endif
  43684. /* invalid case */
  43685. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  43686. /* NULL input */
  43687. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  43688. #endif /* HAVE_ECC */
  43689. return EXPECT_RESULT();
  43690. }
  43691. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  43692. !defined(HAVE_SELFTEST) && \
  43693. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  43694. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  43695. {
  43696. EXPECT_DECLS;
  43697. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  43698. ecc_key* key;
  43699. const ecc_set_type* params = NULL;
  43700. int ret;
  43701. #endif
  43702. WOLFSSL_EC_KEY *ecKey = NULL;
  43703. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  43704. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  43705. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  43706. if (EXPECT_SUCCESS()) {
  43707. ret = EC_KEY_generate_key(ecKey);
  43708. } else
  43709. ret = 0;
  43710. if (ret == 1) {
  43711. /* normal test */
  43712. key = (ecc_key*)ecKey->internal;
  43713. if (key != NULL) {
  43714. params = key->dp;
  43715. }
  43716. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  43717. ECC_SECP256R1);
  43718. }
  43719. #endif
  43720. /* invalid case, NULL input*/
  43721. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  43722. wolfSSL_EC_KEY_free(ecKey);
  43723. return EXPECT_RESULT();
  43724. }
  43725. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43726. static int test_wc_ecc_get_curve_id_from_params(void)
  43727. {
  43728. EXPECT_DECLS;
  43729. #ifdef HAVE_ECC
  43730. const byte prime[] =
  43731. {
  43732. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  43733. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43734. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  43735. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  43736. };
  43737. const byte primeInvalid[] =
  43738. {
  43739. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  43740. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43741. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  43742. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  43743. };
  43744. const byte Af[] =
  43745. {
  43746. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  43747. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43748. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  43749. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  43750. };
  43751. const byte Bf[] =
  43752. {
  43753. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  43754. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  43755. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  43756. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  43757. };
  43758. const byte order[] =
  43759. {
  43760. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  43761. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  43762. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  43763. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  43764. };
  43765. const byte Gx[] =
  43766. {
  43767. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  43768. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  43769. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  43770. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  43771. };
  43772. const byte Gy[] =
  43773. {
  43774. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  43775. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  43776. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  43777. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  43778. };
  43779. int cofactor = 1;
  43780. int fieldSize = 256;
  43781. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  43782. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  43783. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  43784. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  43785. ECC_SECP256R1);
  43786. #endif
  43787. /* invalid case, fieldSize = 0 */
  43788. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  43789. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  43790. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  43791. /* invalid case, NULL prime */
  43792. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  43793. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  43794. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  43795. /* invalid case, invalid prime */
  43796. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  43797. primeInvalid, sizeof(primeInvalid),
  43798. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  43799. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  43800. #endif
  43801. return EXPECT_RESULT();
  43802. }
  43803. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  43804. {
  43805. EXPECT_DECLS;
  43806. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  43807. !defined(HAVE_FAST_RSA)
  43808. WOLFSSL_RSA* rsa = NULL;
  43809. WOLFSSL_EVP_PKEY* pkey = NULL;
  43810. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  43811. const char* in = "What is easy to do is easy not to do.";
  43812. size_t inlen = XSTRLEN(in);
  43813. size_t outEncLen = 0;
  43814. byte* outEnc = NULL;
  43815. byte* outDec = NULL;
  43816. size_t outDecLen = 0;
  43817. size_t rsaKeySz = 2048/8; /* Bytes */
  43818. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  43819. byte* inTmp = NULL;
  43820. byte* outEncTmp = NULL;
  43821. byte* outDecTmp = NULL;
  43822. #endif
  43823. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  43824. DYNAMIC_TYPE_TMP_BUFFER));
  43825. if (outEnc != NULL) {
  43826. XMEMSET(outEnc, 0, rsaKeySz);
  43827. }
  43828. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  43829. DYNAMIC_TYPE_TMP_BUFFER));
  43830. if (outDec != NULL) {
  43831. XMEMSET(outDec, 0, rsaKeySz);
  43832. }
  43833. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  43834. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43835. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  43836. if (EXPECT_FAIL()) {
  43837. RSA_free(rsa);
  43838. }
  43839. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43840. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  43841. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  43842. WOLFSSL_SUCCESS);
  43843. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  43844. since ctx uses it.*/
  43845. ExpectIntEQ(pkey->ref.count, 2);
  43846. EVP_PKEY_free(pkey);
  43847. ExpectIntEQ(pkey->ref.count, 1);
  43848. /* Encrypt data */
  43849. /* Check that we can get the required output buffer length by passing in a
  43850. * NULL output buffer. */
  43851. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  43852. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  43853. ExpectIntEQ(rsaKeySz, outEncLen);
  43854. /* Now do the actual encryption. */
  43855. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  43856. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  43857. /* Decrypt data */
  43858. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  43859. /* Check that we can get the required output buffer length by passing in a
  43860. * NULL output buffer. */
  43861. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  43862. WOLFSSL_SUCCESS);
  43863. ExpectIntEQ(rsaKeySz, outDecLen);
  43864. /* Now do the actual decryption. */
  43865. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  43866. WOLFSSL_SUCCESS);
  43867. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  43868. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  43869. /* The input length must be the same size as the RSA key.*/
  43870. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  43871. DYNAMIC_TYPE_TMP_BUFFER));
  43872. if (inTmp != NULL) {
  43873. XMEMSET(inTmp, 9, rsaKeySz);
  43874. }
  43875. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  43876. DYNAMIC_TYPE_TMP_BUFFER));
  43877. if (outEncTmp != NULL) {
  43878. XMEMSET(outEncTmp, 0, rsaKeySz);
  43879. }
  43880. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  43881. DYNAMIC_TYPE_TMP_BUFFER));
  43882. if (outDecTmp != NULL) {
  43883. XMEMSET(outDecTmp, 0, rsaKeySz);
  43884. }
  43885. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  43886. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  43887. WOLFSSL_SUCCESS);
  43888. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  43889. WOLFSSL_SUCCESS);
  43890. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  43891. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  43892. outEncLen), WOLFSSL_SUCCESS);
  43893. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  43894. #endif
  43895. EVP_PKEY_CTX_free(ctx);
  43896. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43897. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43898. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  43899. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43900. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43901. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  43902. #endif
  43903. #endif
  43904. return EXPECT_RESULT();
  43905. }
  43906. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  43907. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  43908. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43909. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43910. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43911. #endif
  43912. #endif
  43913. #endif
  43914. #if defined(OPENSSL_EXTRA)
  43915. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43916. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43917. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43918. #endif
  43919. #endif
  43920. #endif
  43921. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43922. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43923. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43924. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43925. #endif
  43926. #endif
  43927. #endif
  43928. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  43929. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  43930. {
  43931. EXPECT_DECLS;
  43932. #if defined(OPENSSL_EXTRA)
  43933. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  43934. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  43935. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43936. WOLFSSL_RSA* rsa = NULL;
  43937. #endif
  43938. #endif
  43939. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43940. WOLFSSL_DSA* dsa = NULL;
  43941. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43942. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43943. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43944. WOLFSSL_EC_KEY* ecKey = NULL;
  43945. #endif
  43946. #endif
  43947. WOLFSSL_EVP_PKEY* pkey = NULL;
  43948. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  43949. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  43950. const char* in = "What is easy to do is easy not to do.";
  43951. size_t inlen = XSTRLEN(in);
  43952. byte hash[SHA256_DIGEST_LENGTH] = {0};
  43953. byte zero[SHA256_DIGEST_LENGTH] = {0};
  43954. SHA256_CTX c;
  43955. byte* sig = NULL;
  43956. byte* sigVerify = NULL;
  43957. size_t siglen;
  43958. size_t siglenOnlyLen;
  43959. size_t keySz = 2048/8; /* Bytes */
  43960. ExpectNotNull(sig =
  43961. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  43962. ExpectNotNull(sigVerify =
  43963. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  43964. siglen = keySz;
  43965. ExpectNotNull(XMEMSET(sig, 0, keySz));
  43966. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  43967. /* Generate hash */
  43968. SHA256_Init(&c);
  43969. SHA256_Update(&c, in, inlen);
  43970. SHA256_Final(hash, &c);
  43971. #ifdef WOLFSSL_SMALL_STACK_CACHE
  43972. /* workaround for small stack cache case */
  43973. wc_Sha256Free((wc_Sha256*)&c);
  43974. #endif
  43975. /* Generate key */
  43976. ExpectNotNull(pkey = EVP_PKEY_new());
  43977. switch (keyType) {
  43978. case EVP_PKEY_RSA:
  43979. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  43980. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  43981. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43982. {
  43983. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  43984. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  43985. }
  43986. #endif
  43987. #endif
  43988. break;
  43989. case EVP_PKEY_DSA:
  43990. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43991. ExpectNotNull(dsa = DSA_new());
  43992. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  43993. NULL, 0, NULL, NULL, NULL), 1);
  43994. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43995. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  43996. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43997. break;
  43998. case EVP_PKEY_EC:
  43999. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44000. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44001. {
  44002. ExpectNotNull(ecKey = EC_KEY_new());
  44003. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  44004. ExpectIntEQ(
  44005. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44006. if (EXPECT_FAIL()) {
  44007. EC_KEY_free(ecKey);
  44008. }
  44009. }
  44010. #endif
  44011. #endif
  44012. break;
  44013. }
  44014. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44015. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44016. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44017. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44018. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44019. if (keyType == EVP_PKEY_RSA)
  44020. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44021. WOLFSSL_SUCCESS);
  44022. #endif
  44023. #endif
  44024. /* Check returning only length */
  44025. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  44026. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  44027. ExpectIntGT(siglenOnlyLen, 0);
  44028. /* Sign data */
  44029. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  44030. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  44031. ExpectIntGE(siglenOnlyLen, siglen);
  44032. /* Verify signature */
  44033. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  44034. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44035. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44036. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44037. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44038. if (keyType == EVP_PKEY_RSA)
  44039. ExpectIntEQ(
  44040. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  44041. WOLFSSL_SUCCESS);
  44042. #endif
  44043. #endif
  44044. ExpectIntEQ(EVP_PKEY_verify(
  44045. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  44046. WOLFSSL_SUCCESS);
  44047. ExpectIntEQ(EVP_PKEY_verify(
  44048. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  44049. WOLFSSL_FAILURE);
  44050. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44051. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44052. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44053. if (keyType == EVP_PKEY_RSA) {
  44054. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  44055. /* Try RSA sign/verify with no padding. */
  44056. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44057. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  44058. WOLFSSL_SUCCESS);
  44059. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  44060. siglen), WOLFSSL_SUCCESS);
  44061. ExpectIntGE(siglenOnlyLen, siglen);
  44062. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44063. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44064. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  44065. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  44066. siglen), WOLFSSL_SUCCESS);
  44067. #endif
  44068. /* Wrong padding schemes. */
  44069. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44070. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  44071. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  44072. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  44073. siglen), WOLFSSL_SUCCESS);
  44074. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  44075. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44076. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  44077. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  44078. siglen), WOLFSSL_SUCCESS);
  44079. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  44080. WOLFSSL_SUCCESS);
  44081. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  44082. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  44083. }
  44084. #endif
  44085. #endif
  44086. /* error cases */
  44087. siglen = keySz; /* Reset because sig size may vary slightly */
  44088. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  44089. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  44090. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  44091. WOLFSSL_SUCCESS);
  44092. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  44093. WOLFSSL_SUCCESS);
  44094. EVP_PKEY_free(pkey);
  44095. pkey = NULL;
  44096. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44097. DSA_free(dsa);
  44098. dsa = NULL;
  44099. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  44100. EVP_PKEY_CTX_free(ctx_verify);
  44101. ctx_verify = NULL;
  44102. EVP_PKEY_CTX_free(ctx);
  44103. ctx = NULL;
  44104. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44105. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  44106. #endif /* OPENSSL_EXTRA */
  44107. return EXPECT_RESULT();
  44108. }
  44109. #endif
  44110. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  44111. {
  44112. EXPECT_DECLS;
  44113. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  44114. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  44115. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44116. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  44117. #endif
  44118. #endif
  44119. return EXPECT_RESULT();
  44120. }
  44121. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  44122. {
  44123. EXPECT_DECLS;
  44124. #if defined(OPENSSL_EXTRA)
  44125. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  44126. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  44127. #endif
  44128. #endif
  44129. return EXPECT_RESULT();
  44130. }
  44131. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  44132. {
  44133. EXPECT_DECLS;
  44134. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44135. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44136. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  44137. #endif
  44138. #endif
  44139. return EXPECT_RESULT();
  44140. }
  44141. static int test_EVP_PKEY_rsa(void)
  44142. {
  44143. EXPECT_DECLS;
  44144. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  44145. WOLFSSL_RSA* rsa = NULL;
  44146. WOLFSSL_EVP_PKEY* pkey = NULL;
  44147. ExpectNotNull(rsa = wolfSSL_RSA_new());
  44148. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44149. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  44150. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  44151. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  44152. if (EXPECT_FAIL()) {
  44153. wolfSSL_RSA_free(rsa);
  44154. }
  44155. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  44156. wolfSSL_EVP_PKEY_free(pkey);
  44157. #endif
  44158. return EXPECT_RESULT();
  44159. }
  44160. static int test_EVP_PKEY_ec(void)
  44161. {
  44162. EXPECT_DECLS;
  44163. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  44164. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44165. WOLFSSL_EC_KEY* ecKey = NULL;
  44166. WOLFSSL_EVP_PKEY* pkey = NULL;
  44167. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  44168. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44169. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  44170. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  44171. /* Should fail since ecKey is empty */
  44172. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  44173. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  44174. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  44175. if (EXPECT_FAIL()) {
  44176. wolfSSL_EC_KEY_free(ecKey);
  44177. }
  44178. wolfSSL_EVP_PKEY_free(pkey);
  44179. #endif
  44180. #endif
  44181. return EXPECT_RESULT();
  44182. }
  44183. static int test_EVP_PKEY_cmp(void)
  44184. {
  44185. EXPECT_DECLS;
  44186. #if defined(OPENSSL_EXTRA)
  44187. EVP_PKEY *a = NULL;
  44188. EVP_PKEY *b = NULL;
  44189. const unsigned char *in;
  44190. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  44191. in = client_key_der_2048;
  44192. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44193. &in, (long)sizeof_client_key_der_2048));
  44194. in = client_key_der_2048;
  44195. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44196. &in, (long)sizeof_client_key_der_2048));
  44197. /* Test success case RSA */
  44198. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44199. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  44200. #else
  44201. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  44202. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44203. EVP_PKEY_free(b);
  44204. b = NULL;
  44205. EVP_PKEY_free(a);
  44206. a = NULL;
  44207. #endif
  44208. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44209. in = ecc_clikey_der_256;
  44210. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44211. &in, (long)sizeof_ecc_clikey_der_256));
  44212. in = ecc_clikey_der_256;
  44213. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44214. &in, (long)sizeof_ecc_clikey_der_256));
  44215. /* Test success case ECC */
  44216. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44217. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  44218. #else
  44219. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  44220. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44221. EVP_PKEY_free(b);
  44222. b = NULL;
  44223. EVP_PKEY_free(a);
  44224. a = NULL;
  44225. #endif
  44226. /* Test failure cases */
  44227. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  44228. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44229. in = client_key_der_2048;
  44230. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  44231. &in, (long)sizeof_client_key_der_2048));
  44232. in = ecc_clikey_der_256;
  44233. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  44234. &in, (long)sizeof_ecc_clikey_der_256));
  44235. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44236. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  44237. #else
  44238. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  44239. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  44240. EVP_PKEY_free(b);
  44241. b = NULL;
  44242. EVP_PKEY_free(a);
  44243. a = NULL;
  44244. #endif
  44245. /* invalid or empty failure cases */
  44246. a = EVP_PKEY_new();
  44247. b = EVP_PKEY_new();
  44248. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  44249. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  44250. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  44251. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  44252. #ifdef NO_RSA
  44253. /* Type check will fail since RSA is the default EVP key type */
  44254. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  44255. #else
  44256. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  44257. #endif
  44258. #else
  44259. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  44260. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  44261. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  44262. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  44263. #endif
  44264. EVP_PKEY_free(b);
  44265. EVP_PKEY_free(a);
  44266. (void)in;
  44267. #endif
  44268. return EXPECT_RESULT();
  44269. }
  44270. static int test_ERR_load_crypto_strings(void)
  44271. {
  44272. #if defined(OPENSSL_ALL)
  44273. ERR_load_crypto_strings();
  44274. return TEST_SUCCESS;
  44275. #else
  44276. return TEST_SKIPPED;
  44277. #endif
  44278. }
  44279. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  44280. static void free_x509(X509* x)
  44281. {
  44282. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  44283. }
  44284. #endif
  44285. static int test_sk_X509(void)
  44286. {
  44287. EXPECT_DECLS;
  44288. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  44289. {
  44290. STACK_OF(X509)* s = NULL;
  44291. ExpectNotNull(s = sk_X509_new_null());
  44292. ExpectIntEQ(sk_X509_num(s), 0);
  44293. sk_X509_pop_free(s, NULL);
  44294. ExpectNotNull(s = sk_X509_new_null());
  44295. ExpectIntEQ(sk_X509_num(s), 0);
  44296. sk_X509_pop_free(s, NULL);
  44297. ExpectNotNull(s = sk_X509_new_null());
  44298. sk_X509_push(s, (X509*)1);
  44299. ExpectIntEQ(sk_X509_num(s), 1);
  44300. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  44301. sk_X509_push(s, (X509*)2);
  44302. ExpectIntEQ(sk_X509_num(s), 2);
  44303. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  44304. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  44305. sk_X509_push(s, (X509*)2);
  44306. sk_X509_pop_free(s, free_x509);
  44307. }
  44308. {
  44309. /* Push a list of 10 X509s onto stack, then verify that
  44310. * value(), push(), shift(), and pop() behave as expected. */
  44311. STACK_OF(X509)* s = NULL;
  44312. X509* xList[10];
  44313. int i = 0;
  44314. const int len = (sizeof(xList) / sizeof(xList[0]));
  44315. for (i = 0; i < len; ++i) {
  44316. xList[i] = NULL;
  44317. ExpectNotNull(xList[i] = X509_new());
  44318. }
  44319. /* test push, pop, and free */
  44320. ExpectNotNull(s = sk_X509_new_null());
  44321. for (i = 0; i < len; ++i) {
  44322. sk_X509_push(s, xList[i]);
  44323. ExpectIntEQ(sk_X509_num(s), i + 1);
  44324. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  44325. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  44326. }
  44327. /* pop returns and removes last pushed on stack, which is index 0
  44328. * in sk_x509_value */
  44329. for (i = 0; i < len; ++i) {
  44330. X509 * x = sk_X509_value(s, 0);
  44331. X509 * y = sk_X509_pop(s);
  44332. X509 * z = xList[len - 1 - i];
  44333. ExpectIntEQ((x == y), 1);
  44334. ExpectIntEQ((x == z), 1);
  44335. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  44336. }
  44337. sk_free(s);
  44338. s = NULL;
  44339. /* test push, shift, and free */
  44340. ExpectNotNull(s = sk_X509_new_null());
  44341. for (i = 0; i < len; ++i) {
  44342. sk_X509_push(s, xList[i]);
  44343. ExpectIntEQ(sk_X509_num(s), i + 1);
  44344. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  44345. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  44346. }
  44347. /* shift returns and removes first pushed on stack, which is index i
  44348. * in sk_x509_value() */
  44349. for (i = 0; i < len; ++i) {
  44350. X509 * x = sk_X509_value(s, len - 1 - i);
  44351. X509 * y = sk_X509_shift(s);
  44352. X509 * z = xList[i];
  44353. ExpectIntEQ((x == y), 1);
  44354. ExpectIntEQ((x == z), 1);
  44355. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  44356. }
  44357. sk_free(s);
  44358. for (i = 0; i < len; ++i)
  44359. X509_free(xList[i]);
  44360. }
  44361. #endif
  44362. return EXPECT_RESULT();
  44363. }
  44364. static int test_sk_X509_CRL(void)
  44365. {
  44366. EXPECT_DECLS;
  44367. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  44368. X509_CRL* crl = NULL;
  44369. XFILE fp = XBADFILE;
  44370. STACK_OF(X509_CRL)* s = NULL;
  44371. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  44372. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  44373. NULL, NULL));
  44374. if (fp != XBADFILE)
  44375. XFCLOSE(fp);
  44376. ExpectNotNull(s = sk_X509_CRL_new());
  44377. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  44378. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  44379. if (EXPECT_FAIL()) {
  44380. X509_CRL_free(crl);
  44381. }
  44382. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  44383. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  44384. sk_X509_CRL_free(s);
  44385. #endif
  44386. return EXPECT_RESULT();
  44387. }
  44388. static int test_X509_get_signature_nid(void)
  44389. {
  44390. EXPECT_DECLS;
  44391. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44392. X509* x509 = NULL;
  44393. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  44394. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  44395. SSL_FILETYPE_PEM));
  44396. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  44397. X509_free(x509);
  44398. #endif
  44399. return EXPECT_RESULT();
  44400. }
  44401. static int test_X509_REQ(void)
  44402. {
  44403. EXPECT_DECLS;
  44404. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  44405. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  44406. X509_NAME* name = NULL;
  44407. #ifndef NO_RSA
  44408. X509_NAME* subject = NULL;
  44409. #endif
  44410. #if !defined(NO_RSA) || defined(HAVE_ECC)
  44411. X509_REQ* req = NULL;
  44412. EVP_PKEY* priv = NULL;
  44413. EVP_PKEY* pub = NULL;
  44414. unsigned char* der = NULL;
  44415. int len;
  44416. #endif
  44417. #ifndef NO_RSA
  44418. EVP_MD_CTX *mctx = NULL;
  44419. EVP_PKEY_CTX *pkctx = NULL;
  44420. #ifdef USE_CERT_BUFFERS_1024
  44421. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  44422. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  44423. #elif defined(USE_CERT_BUFFERS_2048)
  44424. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  44425. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  44426. #endif
  44427. #endif
  44428. #ifdef HAVE_ECC
  44429. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  44430. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  44431. #endif
  44432. ExpectNotNull(name = X509_NAME_new());
  44433. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  44434. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  44435. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  44436. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  44437. #ifndef NO_RSA
  44438. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  44439. (long)sizeof_client_key_der_2048));
  44440. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  44441. (long)sizeof_client_keypub_der_2048));
  44442. ExpectNotNull(req = X509_REQ_new());
  44443. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  44444. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  44445. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  44446. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  44447. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  44448. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  44449. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  44450. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  44451. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  44452. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  44453. len = i2d_X509_REQ(req, &der);
  44454. DEBUG_WRITE_DER(der, len, "req.der");
  44455. #ifdef USE_CERT_BUFFERS_1024
  44456. ExpectIntEQ(len, 381);
  44457. #else
  44458. ExpectIntEQ(len, 643);
  44459. #endif
  44460. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  44461. der = NULL;
  44462. mctx = EVP_MD_CTX_new();
  44463. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  44464. WOLFSSL_SUCCESS);
  44465. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  44466. EVP_MD_CTX_free(mctx);
  44467. mctx = NULL;
  44468. X509_REQ_free(NULL);
  44469. X509_REQ_free(req);
  44470. req = NULL;
  44471. /* Test getting the subject from a newly created X509_REQ */
  44472. ExpectNotNull(req = X509_REQ_new());
  44473. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  44474. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  44475. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  44476. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  44477. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  44478. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  44479. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  44480. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  44481. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  44482. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  44483. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  44484. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  44485. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  44486. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  44487. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  44488. len = i2d_X509_REQ(req, &der);
  44489. DEBUG_WRITE_DER(der, len, "req2.der");
  44490. #ifdef USE_CERT_BUFFERS_1024
  44491. ExpectIntEQ(len, 435);
  44492. #else
  44493. ExpectIntEQ(len, 696);
  44494. #endif
  44495. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  44496. der = NULL;
  44497. EVP_PKEY_free(pub);
  44498. pub = NULL;
  44499. EVP_PKEY_free(priv);
  44500. priv = NULL;
  44501. X509_REQ_free(req);
  44502. req = NULL;
  44503. #endif
  44504. #ifdef HAVE_ECC
  44505. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  44506. sizeof_ecc_clikey_der_256));
  44507. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  44508. sizeof_ecc_clikeypub_der_256));
  44509. ExpectNotNull(req = X509_REQ_new());
  44510. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  44511. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  44512. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  44513. /* Signature is random and may be shorter or longer. */
  44514. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  44515. ExpectIntLE(len, 253);
  44516. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  44517. X509_REQ_free(req);
  44518. EVP_PKEY_free(pub);
  44519. EVP_PKEY_free(priv);
  44520. #ifdef FP_ECC
  44521. wc_ecc_fp_free();
  44522. #endif
  44523. #endif /* HAVE_ECC */
  44524. X509_NAME_free(name);
  44525. #endif
  44526. return EXPECT_RESULT();
  44527. }
  44528. static int test_wolfssl_PKCS7(void)
  44529. {
  44530. EXPECT_DECLS;
  44531. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  44532. !defined(NO_RSA)
  44533. PKCS7* pkcs7 = NULL;
  44534. byte data[FOURK_BUF];
  44535. word32 len = sizeof(data);
  44536. const byte* p = data;
  44537. byte content[] = "Test data to encode.";
  44538. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  44539. BIO* bio = NULL;
  44540. byte key[sizeof(client_key_der_2048)];
  44541. word32 keySz = (word32)sizeof(key);
  44542. byte* out = NULL;
  44543. #endif
  44544. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  44545. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  44546. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  44547. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  44548. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  44549. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  44550. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  44551. PKCS7_free(pkcs7);
  44552. pkcs7 = NULL;
  44553. /* fail case, without PKCS7_NOVERIFY */
  44554. p = data;
  44555. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  44556. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  44557. 0), WOLFSSL_FAILURE);
  44558. PKCS7_free(pkcs7);
  44559. pkcs7 = NULL;
  44560. /* success case, with PKCS7_NOVERIFY */
  44561. p = data;
  44562. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  44563. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  44564. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  44565. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  44566. /* test i2d */
  44567. XMEMCPY(key, client_key_der_2048, keySz);
  44568. if (pkcs7 != NULL) {
  44569. pkcs7->privateKey = key;
  44570. pkcs7->privateKeySz = (word32)sizeof(key);
  44571. pkcs7->encryptOID = RSAk;
  44572. #ifdef NO_SHA
  44573. pkcs7->hashOID = SHA256h;
  44574. #else
  44575. pkcs7->hashOID = SHAh;
  44576. #endif
  44577. }
  44578. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44579. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  44580. #ifndef NO_ASN_TIME
  44581. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  44582. #else
  44583. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  44584. #endif
  44585. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44586. BIO_free(bio);
  44587. #endif
  44588. PKCS7_free(NULL);
  44589. PKCS7_free(pkcs7);
  44590. #endif
  44591. return EXPECT_RESULT();
  44592. }
  44593. static int test_wolfSSL_PKCS7_sign(void)
  44594. {
  44595. EXPECT_DECLS;
  44596. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  44597. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44598. PKCS7* p7 = NULL;
  44599. PKCS7* p7Ver = NULL;
  44600. byte* out = NULL;
  44601. byte* tmpPtr = NULL;
  44602. int outLen = 0;
  44603. int flags = 0;
  44604. byte data[] = "Test data to encode.";
  44605. const char* cert = "./certs/server-cert.pem";
  44606. const char* key = "./certs/server-key.pem";
  44607. const char* ca = "./certs/ca-cert.pem";
  44608. WOLFSSL_BIO* certBio = NULL;
  44609. WOLFSSL_BIO* keyBio = NULL;
  44610. WOLFSSL_BIO* caBio = NULL;
  44611. WOLFSSL_BIO* inBio = NULL;
  44612. X509* signCert = NULL;
  44613. EVP_PKEY* signKey = NULL;
  44614. X509* caCert = NULL;
  44615. X509_STORE* store = NULL;
  44616. /* read signer cert/key into BIO */
  44617. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  44618. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  44619. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  44620. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  44621. /* read CA cert into store (for verify) */
  44622. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  44623. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  44624. ExpectNotNull(store = X509_STORE_new());
  44625. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  44626. /* data to be signed into BIO */
  44627. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  44628. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  44629. /* PKCS7_sign, bad args: signer NULL */
  44630. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  44631. /* PKCS7_sign, bad args: signer key NULL */
  44632. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  44633. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  44634. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  44635. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  44636. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  44637. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  44638. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  44639. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  44640. {
  44641. flags = PKCS7_BINARY;
  44642. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  44643. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  44644. /* verify with d2i_PKCS7 */
  44645. tmpPtr = out;
  44646. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  44647. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  44648. PKCS7_free(p7Ver);
  44649. p7Ver = NULL;
  44650. /* verify with wc_PKCS7_VerifySignedData */
  44651. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  44652. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  44653. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  44654. /* compare the signer found to expected signer */
  44655. ExpectIntNE(p7Ver->verifyCertSz, 0);
  44656. tmpPtr = NULL;
  44657. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  44658. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  44659. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  44660. tmpPtr = NULL;
  44661. wc_PKCS7_Free(p7Ver);
  44662. p7Ver = NULL;
  44663. ExpectNotNull(out);
  44664. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44665. out = NULL;
  44666. PKCS7_free(p7);
  44667. p7 = NULL;
  44668. }
  44669. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  44670. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  44671. {
  44672. /* re-populate input BIO, may have been consumed */
  44673. BIO_free(inBio);
  44674. inBio = NULL;
  44675. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  44676. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  44677. flags = PKCS7_BINARY | PKCS7_STREAM;
  44678. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  44679. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  44680. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  44681. /* PKCS7_final, bad args: PKCS7 null */
  44682. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  44683. /* PKCS7_final, bad args: PKCS7 null */
  44684. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  44685. tmpPtr = out;
  44686. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  44687. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  44688. PKCS7_free(p7Ver);
  44689. p7Ver = NULL;
  44690. ExpectNotNull(out);
  44691. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44692. out = NULL;
  44693. PKCS7_free(p7);
  44694. p7 = NULL;
  44695. }
  44696. /* TEST SUCCESS: Detached, not streaming, not MIME */
  44697. {
  44698. /* re-populate input BIO, may have been consumed */
  44699. BIO_free(inBio);
  44700. inBio = NULL;
  44701. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  44702. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  44703. flags = PKCS7_BINARY | PKCS7_DETACHED;
  44704. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  44705. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  44706. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  44707. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  44708. if (p7Ver != NULL) {
  44709. p7Ver->content = data;
  44710. p7Ver->contentSz = sizeof(data);
  44711. }
  44712. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  44713. wc_PKCS7_Free(p7Ver);
  44714. p7Ver = NULL;
  44715. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  44716. * yet support detached content */
  44717. tmpPtr = out;
  44718. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  44719. PKCS7_free(p7Ver);
  44720. p7Ver = NULL;
  44721. ExpectNotNull(out);
  44722. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44723. out = NULL;
  44724. PKCS7_free(p7);
  44725. p7 = NULL;
  44726. }
  44727. /* TEST SUCCESS: Detached, streaming, not MIME */
  44728. {
  44729. /* re-populate input BIO, may have been consumed */
  44730. BIO_free(inBio);
  44731. inBio = NULL;
  44732. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  44733. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  44734. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  44735. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  44736. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  44737. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  44738. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  44739. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  44740. if (p7Ver != NULL) {
  44741. p7Ver->content = data;
  44742. p7Ver->contentSz = sizeof(data);
  44743. }
  44744. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  44745. wc_PKCS7_Free(p7Ver);
  44746. p7Ver = NULL;
  44747. ExpectNotNull(out);
  44748. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44749. PKCS7_free(p7);
  44750. p7 = NULL;
  44751. }
  44752. X509_STORE_free(store);
  44753. X509_free(caCert);
  44754. X509_free(signCert);
  44755. EVP_PKEY_free(signKey);
  44756. BIO_free(inBio);
  44757. BIO_free(keyBio);
  44758. BIO_free(certBio);
  44759. BIO_free(caBio);
  44760. #endif
  44761. return EXPECT_RESULT();
  44762. }
  44763. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  44764. {
  44765. EXPECT_DECLS;
  44766. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  44767. PKCS7_SIGNED* pkcs7 = NULL;
  44768. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  44769. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  44770. PKCS7_SIGNED_free(pkcs7);
  44771. #endif
  44772. return EXPECT_RESULT();
  44773. }
  44774. #ifndef NO_BIO
  44775. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  44776. {
  44777. EXPECT_DECLS;
  44778. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  44779. PKCS7* pkcs7 = NULL;
  44780. BIO* bio = NULL;
  44781. const byte* cert_buf = NULL;
  44782. int ret = 0;
  44783. WC_RNG rng;
  44784. const byte data[] = { /* Hello World */
  44785. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44786. 0x72,0x6c,0x64
  44787. };
  44788. #ifndef NO_RSA
  44789. #if defined(USE_CERT_BUFFERS_2048)
  44790. byte key[sizeof(client_key_der_2048)];
  44791. byte cert[sizeof(client_cert_der_2048)];
  44792. word32 keySz = (word32)sizeof(key);
  44793. word32 certSz = (word32)sizeof(cert);
  44794. XMEMSET(key, 0, keySz);
  44795. XMEMSET(cert, 0, certSz);
  44796. XMEMCPY(key, client_key_der_2048, keySz);
  44797. XMEMCPY(cert, client_cert_der_2048, certSz);
  44798. #elif defined(USE_CERT_BUFFERS_1024)
  44799. byte key[sizeof_client_key_der_1024];
  44800. byte cert[sizeof(sizeof_client_cert_der_1024)];
  44801. word32 keySz = (word32)sizeof(key);
  44802. word32 certSz = (word32)sizeof(cert);
  44803. XMEMSET(key, 0, keySz);
  44804. XMEMSET(cert, 0, certSz);
  44805. XMEMCPY(key, client_key_der_1024, keySz);
  44806. XMEMCPY(cert, client_cert_der_1024, certSz);
  44807. #else
  44808. unsigned char cert[ONEK_BUF];
  44809. unsigned char key[ONEK_BUF];
  44810. XFILE fp = XBADFILE;
  44811. int certSz;
  44812. int keySz;
  44813. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  44814. XBADFILE);
  44815. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  44816. fp), 0);
  44817. if (fp != XBADFILE) {
  44818. XFCLOSE(fp);
  44819. fp = XBADFILE;
  44820. }
  44821. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  44822. XBADFILE);
  44823. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  44824. 0);
  44825. if (fp != XBADFILE) {
  44826. XFCLOSE(fp);
  44827. fp = XBADFILE;
  44828. }
  44829. #endif
  44830. #elif defined(HAVE_ECC)
  44831. #if defined(USE_CERT_BUFFERS_256)
  44832. unsigned char cert[sizeof(cliecc_cert_der_256)];
  44833. unsigned char key[sizeof(ecc_clikey_der_256)];
  44834. int certSz = (int)sizeof(cert);
  44835. int keySz = (int)sizeof(key);
  44836. XMEMSET(cert, 0, certSz);
  44837. XMEMSET(key, 0, keySz);
  44838. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  44839. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  44840. #else
  44841. unsigned char cert[ONEK_BUF];
  44842. unsigned char key[ONEK_BUF];
  44843. XFILE fp = XBADFILE;
  44844. int certSz, keySz;
  44845. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  44846. XBADFILE);
  44847. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  44848. fp), 0);
  44849. if (fp != XBADFILE) {
  44850. XFCLOSE(fp);
  44851. fp = XBADFILE;
  44852. }
  44853. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  44854. XBADFILE);
  44855. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  44856. 0);
  44857. if (fp != XBADFILE) {
  44858. XFCLOSE(fp);
  44859. fp = XBADFILE;
  44860. }
  44861. #endif
  44862. #else
  44863. #error PKCS7 requires ECC or RSA
  44864. #endif
  44865. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  44866. /* initialize with DER encoded cert */
  44867. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  44868. /* init rng */
  44869. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44870. ExpectIntEQ(wc_InitRng(&rng), 0);
  44871. if (pkcs7 != NULL) {
  44872. pkcs7->rng = &rng;
  44873. pkcs7->content = (byte*)data; /* not used for ex */
  44874. pkcs7->contentSz = (word32)sizeof(data);
  44875. pkcs7->contentOID = SIGNED_DATA;
  44876. pkcs7->privateKey = key;
  44877. pkcs7->privateKeySz = (word32)sizeof(key);
  44878. pkcs7->encryptOID = RSAk;
  44879. #ifdef NO_SHA
  44880. pkcs7->hashOID = SHA256h;
  44881. #else
  44882. pkcs7->hashOID = SHAh;
  44883. #endif
  44884. pkcs7->signedAttribs = NULL;
  44885. pkcs7->signedAttribsSz = 0;
  44886. }
  44887. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44888. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  44889. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  44890. /* Read PKCS#7 PEM from BIO */
  44891. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  44892. ExpectIntGE(ret, 0);
  44893. BIO_free(bio);
  44894. wc_PKCS7_Free(pkcs7);
  44895. wc_FreeRng(&rng);
  44896. #endif
  44897. return EXPECT_RESULT();
  44898. }
  44899. #ifdef HAVE_SMIME
  44900. static int test_wolfSSL_SMIME_read_PKCS7(void)
  44901. {
  44902. EXPECT_DECLS;
  44903. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  44904. !defined(NO_RSA)
  44905. PKCS7* pkcs7 = NULL;
  44906. BIO* bio = NULL;
  44907. BIO* bcont = NULL;
  44908. BIO* out = NULL;
  44909. const byte* outBuf = NULL;
  44910. int outBufLen = 0;
  44911. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  44912. XFILE smimeTestFile = XBADFILE;
  44913. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  44914. XBADFILE);
  44915. /* smime-test.p7s */
  44916. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  44917. ExpectNotNull(bio);
  44918. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  44919. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  44920. ExpectNotNull(pkcs7);
  44921. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  44922. PKCS7_NOVERIFY), SSL_SUCCESS);
  44923. XFCLOSE(smimeTestFile);
  44924. if (bcont) BIO_free(bcont);
  44925. bcont = NULL;
  44926. wolfSSL_PKCS7_free(pkcs7);
  44927. pkcs7 = NULL;
  44928. /* smime-test-multipart.p7s */
  44929. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  44930. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  44931. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  44932. ExpectNotNull(pkcs7);
  44933. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  44934. PKCS7_NOVERIFY), SSL_SUCCESS);
  44935. XFCLOSE(smimeTestFile);
  44936. if (bcont) BIO_free(bcont);
  44937. bcont = NULL;
  44938. wolfSSL_PKCS7_free(pkcs7);
  44939. pkcs7 = NULL;
  44940. /* smime-test-multipart-badsig.p7s */
  44941. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  44942. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  44943. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  44944. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  44945. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  44946. PKCS7_NOVERIFY), SSL_FAILURE);
  44947. XFCLOSE(smimeTestFile);
  44948. if (bcont) BIO_free(bcont);
  44949. bcont = NULL;
  44950. wolfSSL_PKCS7_free(pkcs7);
  44951. pkcs7 = NULL;
  44952. /* smime-test-canon.p7s */
  44953. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  44954. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  44955. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  44956. ExpectNotNull(pkcs7);
  44957. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  44958. PKCS7_NOVERIFY), SSL_SUCCESS);
  44959. XFCLOSE(smimeTestFile);
  44960. if (bcont) BIO_free(bcont);
  44961. bcont = NULL;
  44962. wolfSSL_PKCS7_free(pkcs7);
  44963. pkcs7 = NULL;
  44964. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  44965. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  44966. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  44967. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  44968. ExpectNotNull(pkcs7);
  44969. out = wolfSSL_BIO_new(BIO_s_mem());
  44970. ExpectNotNull(out);
  44971. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  44972. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  44973. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  44974. /* Content-Type should not show up at beginning of output buffer */
  44975. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  44976. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  44977. BIO_free(out);
  44978. BIO_free(bio);
  44979. if (bcont) BIO_free(bcont);
  44980. wolfSSL_PKCS7_free(pkcs7);
  44981. #endif
  44982. return EXPECT_RESULT();
  44983. }
  44984. static int test_wolfSSL_SMIME_write_PKCS7(void)
  44985. {
  44986. EXPECT_DECLS;
  44987. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  44988. PKCS7* p7 = NULL;
  44989. PKCS7* p7Ver = NULL;
  44990. int flags = 0;
  44991. byte data[] = "Test data to encode.";
  44992. const char* cert = "./certs/server-cert.pem";
  44993. const char* key = "./certs/server-key.pem";
  44994. const char* ca = "./certs/ca-cert.pem";
  44995. WOLFSSL_BIO* certBio = NULL;
  44996. WOLFSSL_BIO* keyBio = NULL;
  44997. WOLFSSL_BIO* caBio = NULL;
  44998. WOLFSSL_BIO* inBio = NULL;
  44999. WOLFSSL_BIO* outBio = NULL;
  45000. WOLFSSL_BIO* content = NULL;
  45001. X509* signCert = NULL;
  45002. EVP_PKEY* signKey = NULL;
  45003. X509* caCert = NULL;
  45004. X509_STORE* store = NULL;
  45005. /* read signer cert/key into BIO */
  45006. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  45007. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  45008. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  45009. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  45010. /* read CA cert into store (for verify) */
  45011. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  45012. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  45013. ExpectNotNull(store = X509_STORE_new());
  45014. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  45015. /* generate and verify SMIME: not detached */
  45016. {
  45017. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45018. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45019. flags = PKCS7_STREAM;
  45020. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45021. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45022. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45023. /* bad arg: out NULL */
  45024. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  45025. /* bad arg: pkcs7 NULL */
  45026. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  45027. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45028. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45029. BIO_free(content);
  45030. content = NULL;
  45031. BIO_free(inBio);
  45032. inBio = NULL;
  45033. BIO_free(outBio);
  45034. outBio = NULL;
  45035. PKCS7_free(p7Ver);
  45036. p7Ver = NULL;
  45037. PKCS7_free(p7);
  45038. p7 = NULL;
  45039. }
  45040. /* generate and verify SMIME: not detached, add Content-Type */
  45041. {
  45042. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45043. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45044. flags = PKCS7_STREAM | PKCS7_TEXT;
  45045. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45046. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45047. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45048. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45049. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  45050. BIO_free(content);
  45051. content = NULL;
  45052. BIO_free(inBio);
  45053. inBio = NULL;
  45054. BIO_free(outBio);
  45055. outBio = NULL;
  45056. PKCS7_free(p7Ver);
  45057. p7Ver = NULL;
  45058. PKCS7_free(p7);
  45059. p7 = NULL;
  45060. }
  45061. /* generate and verify SMIME: detached */
  45062. {
  45063. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45064. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45065. flags = PKCS7_DETACHED | PKCS7_STREAM;
  45066. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45067. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45068. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45069. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45070. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  45071. BIO_free(content);
  45072. content = NULL;
  45073. BIO_free(inBio);
  45074. inBio = NULL;
  45075. BIO_free(outBio);
  45076. outBio = NULL;
  45077. PKCS7_free(p7Ver);
  45078. p7Ver = NULL;
  45079. PKCS7_free(p7);
  45080. p7 = NULL;
  45081. }
  45082. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  45083. {
  45084. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  45085. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  45086. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  45087. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  45088. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  45089. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  45090. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  45091. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  45092. BIO_free(content);
  45093. content = NULL;
  45094. BIO_free(inBio);
  45095. inBio = NULL;
  45096. BIO_free(outBio);
  45097. outBio = NULL;
  45098. PKCS7_free(p7Ver);
  45099. p7Ver = NULL;
  45100. PKCS7_free(p7);
  45101. p7 = NULL;
  45102. }
  45103. X509_STORE_free(store);
  45104. X509_free(caCert);
  45105. X509_free(signCert);
  45106. EVP_PKEY_free(signKey);
  45107. BIO_free(keyBio);
  45108. BIO_free(certBio);
  45109. BIO_free(caBio);
  45110. #endif
  45111. return EXPECT_RESULT();
  45112. }
  45113. #endif /* HAVE_SMIME */
  45114. #endif /* !NO_BIO */
  45115. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  45116. * returns 0) */
  45117. static int test_X509_STORE_No_SSL_CTX(void)
  45118. {
  45119. EXPECT_DECLS;
  45120. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  45121. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  45122. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  45123. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  45124. defined(HAVE_CRL) && !defined(NO_RSA)
  45125. X509_STORE * store = NULL;
  45126. X509_STORE_CTX * storeCtx = NULL;
  45127. X509_CRL * crl = NULL;
  45128. X509 * ca = NULL;
  45129. X509 * cert = NULL;
  45130. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  45131. const char srvCert[] = "./certs/server-cert.pem";
  45132. const char caCert[] = "./certs/ca-cert.pem";
  45133. const char caDir[] = "./certs/crl/hash_pem";
  45134. XFILE fp = XBADFILE;
  45135. X509_LOOKUP * lookup = NULL;
  45136. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45137. /* Set up store with CA */
  45138. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45139. SSL_FILETYPE_PEM)));
  45140. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45141. /* Add CRL lookup directory to store
  45142. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  45143. * of crl.pem */
  45144. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45145. X509_LOOKUP_hash_dir())));
  45146. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  45147. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  45148. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45149. SSL_SUCCESS);
  45150. /* Add CRL to store NOT containing the verified certificate, which
  45151. * forces use of the CRL lookup directory */
  45152. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45153. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45154. NULL, NULL));
  45155. if (fp != XBADFILE)
  45156. XFCLOSE(fp);
  45157. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45158. /* Create verification context outside of an SSL session */
  45159. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45160. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45161. SSL_FILETYPE_PEM)));
  45162. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45163. /* Perform verification, which should NOT indicate CRL missing due to the
  45164. * store CM's X509 store pointer being NULL */
  45165. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  45166. X509_CRL_free(crl);
  45167. X509_STORE_free(store);
  45168. X509_STORE_CTX_free(storeCtx);
  45169. X509_free(cert);
  45170. X509_free(ca);
  45171. #endif
  45172. return EXPECT_RESULT();
  45173. }
  45174. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  45175. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  45176. static int test_X509_LOOKUP_add_dir(void)
  45177. {
  45178. EXPECT_DECLS;
  45179. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  45180. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  45181. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  45182. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  45183. defined(HAVE_CRL) && !defined(NO_RSA)
  45184. X509_STORE * store = NULL;
  45185. X509_STORE_CTX * storeCtx = NULL;
  45186. X509_CRL * crl = NULL;
  45187. X509 * ca = NULL;
  45188. X509 * cert = NULL;
  45189. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  45190. const char srvCert[] = "./certs/server-cert.pem";
  45191. const char caCert[] = "./certs/ca-cert.pem";
  45192. const char caDir[] = "./certs/crl/hash_der";
  45193. XFILE fp = XBADFILE;
  45194. X509_LOOKUP * lookup = NULL;
  45195. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45196. /* Set up store with CA */
  45197. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45198. SSL_FILETYPE_PEM)));
  45199. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45200. /* Add CRL lookup directory to store.
  45201. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  45202. * of crl.der */
  45203. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45204. X509_LOOKUP_hash_dir())));
  45205. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  45206. SSL_SUCCESS);
  45207. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45208. SSL_SUCCESS);
  45209. /* Add CRL to store NOT containing the verified certificate, which
  45210. * forces use of the CRL lookup directory */
  45211. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45212. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45213. NULL, NULL));
  45214. if (fp != XBADFILE) {
  45215. XFCLOSE(fp);
  45216. fp = XBADFILE;
  45217. }
  45218. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45219. /* Create verification context outside of an SSL session */
  45220. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45221. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45222. SSL_FILETYPE_PEM)));
  45223. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45224. /* Perform verification, which should NOT return CRL missing */
  45225. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  45226. X509_CRL_free(crl);
  45227. crl = NULL;
  45228. X509_STORE_free(store);
  45229. store = NULL;
  45230. X509_STORE_CTX_free(storeCtx);
  45231. storeCtx = NULL;
  45232. X509_free(cert);
  45233. cert = NULL;
  45234. X509_free(ca);
  45235. ca = NULL;
  45236. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  45237. * We should get CRL_MISSING at the end, because the lookup
  45238. * dir has only ASN1 CRLs. */
  45239. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  45240. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  45241. SSL_FILETYPE_PEM)));
  45242. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  45243. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  45244. X509_LOOKUP_hash_dir())));
  45245. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  45246. SSL_SUCCESS);
  45247. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  45248. SSL_SUCCESS);
  45249. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  45250. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45251. NULL, NULL));
  45252. if (fp != XBADFILE) {
  45253. XFCLOSE(fp);
  45254. fp = XBADFILE;
  45255. }
  45256. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  45257. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  45258. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  45259. SSL_FILETYPE_PEM)));
  45260. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  45261. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  45262. * in dir containing only ASN1/DER. */
  45263. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  45264. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  45265. X509_CRL_free(crl);
  45266. X509_STORE_free(store);
  45267. X509_STORE_CTX_free(storeCtx);
  45268. X509_free(cert);
  45269. X509_free(ca);
  45270. #endif
  45271. return EXPECT_RESULT();
  45272. }
  45273. /*----------------------------------------------------------------------------*
  45274. | Certificate Failure Checks
  45275. *----------------------------------------------------------------------------*/
  45276. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  45277. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  45278. #if !defined(NO_RSA) || defined(HAVE_ECC)
  45279. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  45280. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  45281. int type)
  45282. {
  45283. int ret;
  45284. WOLFSSL_CERT_MANAGER* cm = NULL;
  45285. switch (type) {
  45286. case TESTING_RSA:
  45287. #ifdef NO_RSA
  45288. fprintf(stderr, "RSA disabled, skipping test\n");
  45289. return ASN_SIG_CONFIRM_E;
  45290. #else
  45291. break;
  45292. #endif
  45293. case TESTING_ECC:
  45294. #ifndef HAVE_ECC
  45295. fprintf(stderr, "ECC disabled, skipping test\n");
  45296. return ASN_SIG_CONFIRM_E;
  45297. #else
  45298. break;
  45299. #endif
  45300. default:
  45301. fprintf(stderr, "Bad function argument\n");
  45302. return BAD_FUNC_ARG;
  45303. }
  45304. cm = wolfSSL_CertManagerNew();
  45305. if (cm == NULL) {
  45306. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  45307. return -1;
  45308. }
  45309. #ifndef NO_FILESYSTEM
  45310. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  45311. if (ret != WOLFSSL_SUCCESS) {
  45312. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  45313. wolfSSL_CertManagerFree(cm);
  45314. return ret;
  45315. }
  45316. #else
  45317. (void)ca;
  45318. #endif
  45319. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  45320. WOLFSSL_FILETYPE_ASN1);
  45321. /* Let ExpectIntEQ handle return code */
  45322. wolfSSL_CertManagerFree(cm);
  45323. return ret;
  45324. }
  45325. #endif
  45326. #if !defined(NO_FILESYSTEM)
  45327. static int test_RsaSigFailure_cm(void)
  45328. {
  45329. EXPECT_DECLS;
  45330. #ifndef NO_RSA
  45331. const char* ca_cert = "./certs/ca-cert.pem";
  45332. const char* server_cert = "./certs/server-cert.der";
  45333. byte* cert_buf = NULL;
  45334. size_t cert_sz = 0;
  45335. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  45336. if (cert_buf != NULL) {
  45337. /* corrupt DER - invert last byte, which is signature */
  45338. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  45339. /* test bad cert */
  45340. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  45341. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  45342. WOLFSSL_FATAL_ERROR);
  45343. #else
  45344. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  45345. ASN_SIG_CONFIRM_E);
  45346. #endif
  45347. }
  45348. /* load_file() uses malloc. */
  45349. if (cert_buf != NULL) {
  45350. free(cert_buf);
  45351. }
  45352. #endif /* !NO_RSA */
  45353. return EXPECT_RESULT();
  45354. }
  45355. static int test_EccSigFailure_cm(void)
  45356. {
  45357. EXPECT_DECLS;
  45358. #ifdef HAVE_ECC
  45359. /* self-signed ECC cert, so use server cert as CA */
  45360. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  45361. const char* server_cert = "./certs/server-ecc.der";
  45362. byte* cert_buf = NULL;
  45363. size_t cert_sz = 0;
  45364. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  45365. if (cert_buf != NULL) {
  45366. /* corrupt DER - invert last byte, which is signature */
  45367. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  45368. /* test bad cert */
  45369. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  45370. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  45371. WOLFSSL_FATAL_ERROR);
  45372. #else
  45373. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  45374. ASN_SIG_CONFIRM_E);
  45375. #endif
  45376. }
  45377. /* load_file() uses malloc. */
  45378. if (cert_buf != NULL) {
  45379. free(cert_buf);
  45380. }
  45381. #ifdef FP_ECC
  45382. wc_ecc_fp_free();
  45383. #endif
  45384. #endif /* HAVE_ECC */
  45385. return EXPECT_RESULT();
  45386. }
  45387. #endif /* !NO_FILESYSTEM */
  45388. #endif /* NO_CERTS */
  45389. #ifdef WOLFSSL_TLS13
  45390. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  45391. #ifdef WC_SHA384_DIGEST_SIZE
  45392. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  45393. #else
  45394. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  45395. #endif
  45396. #endif
  45397. #ifdef WOLFSSL_EARLY_DATA
  45398. static const char earlyData[] = "Early Data";
  45399. static char earlyDataBuffer[1];
  45400. #endif
  45401. static int test_tls13_apis(void)
  45402. {
  45403. EXPECT_DECLS;
  45404. int ret;
  45405. #ifndef WOLFSSL_NO_TLS12
  45406. #ifndef NO_WOLFSSL_CLIENT
  45407. WOLFSSL_CTX* clientTls12Ctx = NULL;
  45408. WOLFSSL* clientTls12Ssl = NULL;
  45409. #endif
  45410. #ifndef NO_WOLFSSL_SERVER
  45411. WOLFSSL_CTX* serverTls12Ctx = NULL;
  45412. WOLFSSL* serverTls12Ssl = NULL;
  45413. #endif
  45414. #endif
  45415. #ifndef NO_WOLFSSL_CLIENT
  45416. WOLFSSL_CTX* clientCtx = NULL;
  45417. WOLFSSL* clientSsl = NULL;
  45418. #endif
  45419. #ifndef NO_WOLFSSL_SERVER
  45420. WOLFSSL_CTX* serverCtx = NULL;
  45421. WOLFSSL* serverSsl = NULL;
  45422. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  45423. const char* ourCert = svrCertFile;
  45424. const char* ourKey = svrKeyFile;
  45425. #endif
  45426. #endif
  45427. int required;
  45428. #ifdef WOLFSSL_EARLY_DATA
  45429. int outSz;
  45430. #endif
  45431. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  45432. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  45433. #ifdef HAVE_PQC
  45434. WOLFSSL_KYBER_LEVEL1
  45435. #else
  45436. WOLFSSL_ECC_SECP256R1
  45437. #endif
  45438. };
  45439. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  45440. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  45441. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  45442. int numGroups = 2;
  45443. #endif
  45444. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  45445. char groupList[] =
  45446. #ifndef NO_ECC_SECP
  45447. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  45448. "P-521:"
  45449. #endif
  45450. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  45451. "P-384:"
  45452. #endif
  45453. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  45454. "P-256"
  45455. #ifdef HAVE_PQC
  45456. ":P256_KYBER_LEVEL1"
  45457. #endif
  45458. #endif
  45459. #endif /* !defined(NO_ECC_SECP) */
  45460. #ifdef HAVE_PQC
  45461. ":KYBER_LEVEL1"
  45462. #endif
  45463. "";
  45464. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  45465. (void)ret;
  45466. #ifndef WOLFSSL_NO_TLS12
  45467. #ifndef NO_WOLFSSL_CLIENT
  45468. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  45469. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  45470. #endif
  45471. #ifndef NO_WOLFSSL_SERVER
  45472. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  45473. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  45474. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  45475. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  45476. WOLFSSL_FILETYPE_PEM);
  45477. #endif
  45478. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  45479. #endif
  45480. #endif
  45481. #ifndef NO_WOLFSSL_CLIENT
  45482. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  45483. clientSsl = wolfSSL_new(clientCtx);
  45484. #endif
  45485. #ifndef NO_WOLFSSL_SERVER
  45486. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  45487. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  45488. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  45489. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  45490. #endif
  45491. serverSsl = wolfSSL_new(serverCtx);
  45492. ExpectNotNull(serverSsl);
  45493. #endif
  45494. #ifdef WOLFSSL_SEND_HRR_COOKIE
  45495. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  45496. #ifndef NO_WOLFSSL_CLIENT
  45497. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  45498. #endif
  45499. #ifndef NO_WOLFSSL_SERVER
  45500. #ifndef WOLFSSL_NO_TLS12
  45501. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  45502. BAD_FUNC_ARG);
  45503. #endif
  45504. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  45505. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  45506. WOLFSSL_SUCCESS);
  45507. #endif
  45508. #endif
  45509. #ifdef HAVE_SUPPORTED_CURVES
  45510. #ifdef HAVE_ECC
  45511. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  45512. BAD_FUNC_ARG);
  45513. #ifndef NO_WOLFSSL_SERVER
  45514. do {
  45515. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  45516. #ifdef WOLFSSL_ASYNC_CRYPT
  45517. if (ret == WC_PENDING_E)
  45518. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  45519. #endif
  45520. }
  45521. while (ret == WC_PENDING_E);
  45522. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  45523. #endif
  45524. #ifndef NO_WOLFSSL_CLIENT
  45525. #ifndef WOLFSSL_NO_TLS12
  45526. do {
  45527. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  45528. #ifdef WOLFSSL_ASYNC_CRYPT
  45529. if (ret == WC_PENDING_E)
  45530. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  45531. #endif
  45532. }
  45533. while (ret == WC_PENDING_E);
  45534. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  45535. #endif
  45536. do {
  45537. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  45538. #ifdef WOLFSSL_ASYNC_CRYPT
  45539. if (ret == WC_PENDING_E)
  45540. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  45541. #endif
  45542. }
  45543. while (ret == WC_PENDING_E);
  45544. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  45545. #endif
  45546. #elif defined(HAVE_CURVE25519)
  45547. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  45548. #ifndef NO_WOLFSSL_SERVER
  45549. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  45550. WOLFSSL_SUCCESS);
  45551. #endif
  45552. #ifndef NO_WOLFSSL_CLIENT
  45553. #ifndef WOLFSSL_NO_TLS12
  45554. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  45555. WOLFSSL_SUCCESS);
  45556. #endif
  45557. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  45558. WOLFSSL_SUCCESS);
  45559. #endif
  45560. #elif defined(HAVE_CURVE448)
  45561. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  45562. #ifndef NO_WOLFSSL_SERVER
  45563. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  45564. WOLFSSL_SUCCESS);
  45565. #endif
  45566. #ifndef NO_WOLFSSL_CLIENT
  45567. #ifndef WOLFSSL_NO_TLS12
  45568. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  45569. WOLFSSL_SUCCESS);
  45570. #endif
  45571. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  45572. WOLFSSL_SUCCESS);
  45573. #endif
  45574. #else
  45575. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  45576. BAD_FUNC_ARG);
  45577. #ifndef NO_WOLFSSL_CLIENT
  45578. #ifndef WOLFSSL_NO_TLS12
  45579. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  45580. NOT_COMPILED_IN);
  45581. #endif
  45582. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  45583. NOT_COMPILED_IN);
  45584. #endif
  45585. #endif
  45586. #if defined(HAVE_PQC)
  45587. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  45588. #ifndef NO_WOLFSSL_SERVER
  45589. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  45590. WOLFSSL_SUCCESS);
  45591. #endif
  45592. #ifndef NO_WOLFSSL_CLIENT
  45593. #ifndef WOLFSSL_NO_TLS12
  45594. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  45595. BAD_FUNC_ARG);
  45596. #endif
  45597. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  45598. WOLFSSL_SUCCESS);
  45599. #endif
  45600. #endif
  45601. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  45602. #ifndef NO_WOLFSSL_SERVER
  45603. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  45604. #endif
  45605. #ifndef NO_WOLFSSL_CLIENT
  45606. #ifndef WOLFSSL_NO_TLS12
  45607. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  45608. #endif
  45609. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  45610. #endif
  45611. #endif /* HAVE_SUPPORTED_CURVES */
  45612. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  45613. #ifndef NO_WOLFSSL_CLIENT
  45614. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  45615. #endif
  45616. #ifndef NO_WOLFSSL_SERVER
  45617. #ifndef WOLFSSL_NO_TLS12
  45618. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  45619. #endif
  45620. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  45621. #endif
  45622. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  45623. #ifndef NO_WOLFSSL_CLIENT
  45624. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  45625. #endif
  45626. #ifndef NO_WOLFSSL_SERVER
  45627. #ifndef WOLFSSL_NO_TLS12
  45628. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  45629. #endif
  45630. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  45631. #endif
  45632. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  45633. #ifndef NO_WOLFSSL_CLIENT
  45634. #ifndef WOLFSSL_NO_TLS12
  45635. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  45636. #endif
  45637. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  45638. #endif
  45639. #ifndef NO_WOLFSSL_SERVER
  45640. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  45641. #endif
  45642. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  45643. #ifndef NO_WOLFSSL_CLIENT
  45644. #ifndef WOLFSSL_NO_TLS12
  45645. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  45646. #endif
  45647. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  45648. #endif
  45649. #ifndef NO_WOLFSSL_SERVER
  45650. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  45651. #endif
  45652. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  45653. #ifndef NO_WOLFSSL_CLIENT
  45654. #ifndef WOLFSSL_NO_TLS12
  45655. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  45656. #endif
  45657. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  45658. #endif
  45659. #ifndef NO_WOLFSSL_SERVER
  45660. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  45661. #endif
  45662. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  45663. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  45664. #ifndef NO_WOLFSSL_CLIENT
  45665. #ifndef WOLFSSL_NO_TLS12
  45666. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  45667. BAD_FUNC_ARG);
  45668. #endif
  45669. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  45670. #endif
  45671. #ifndef NO_WOLFSSL_SERVER
  45672. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  45673. #endif
  45674. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  45675. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  45676. #ifndef NO_WOLFSSL_SERVER
  45677. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  45678. #endif
  45679. #ifndef NO_WOLFSSL_CLIENT
  45680. #ifndef WOLFSSL_NO_TLS12
  45681. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  45682. BAD_FUNC_ARG);
  45683. #endif
  45684. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  45685. #endif
  45686. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  45687. #ifndef NO_WOLFSSL_SERVER
  45688. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  45689. #endif
  45690. #ifndef NO_WOLFSSL_CLIENT
  45691. #ifndef WOLFSSL_NO_TLS12
  45692. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  45693. BAD_FUNC_ARG);
  45694. #endif
  45695. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  45696. #endif
  45697. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  45698. #ifndef NO_WOLFSSL_CLIENT
  45699. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  45700. #endif
  45701. #ifndef NO_WOLFSSL_SERVER
  45702. #ifndef WOLFSSL_NO_TLS12
  45703. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  45704. BAD_FUNC_ARG);
  45705. #endif
  45706. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  45707. #endif
  45708. #endif
  45709. #ifdef HAVE_ECC
  45710. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  45711. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  45712. #ifndef NO_WOLFSSL_SERVER
  45713. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  45714. #endif
  45715. #ifndef NO_WOLFSSL_CLIENT
  45716. #ifndef WOLFSSL_NO_TLS12
  45717. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  45718. #endif
  45719. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  45720. #endif
  45721. #endif
  45722. #ifdef HAVE_SUPPORTED_CURVES
  45723. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  45724. #ifndef NO_WOLFSSL_CLIENT
  45725. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  45726. #endif
  45727. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  45728. #ifndef NO_WOLFSSL_CLIENT
  45729. #ifndef WOLFSSL_NO_TLS12
  45730. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  45731. BAD_FUNC_ARG);
  45732. #endif
  45733. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  45734. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  45735. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  45736. WOLFSSL_SUCCESS);
  45737. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  45738. BAD_FUNC_ARG);
  45739. #endif
  45740. #ifndef NO_WOLFSSL_SERVER
  45741. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  45742. WOLFSSL_SUCCESS);
  45743. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  45744. BAD_FUNC_ARG);
  45745. #endif
  45746. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  45747. #ifndef NO_WOLFSSL_CLIENT
  45748. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  45749. #endif
  45750. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  45751. #ifndef NO_WOLFSSL_CLIENT
  45752. #ifndef WOLFSSL_NO_TLS12
  45753. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  45754. BAD_FUNC_ARG);
  45755. #endif
  45756. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  45757. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  45758. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  45759. WOLFSSL_SUCCESS);
  45760. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  45761. BAD_FUNC_ARG);
  45762. #endif
  45763. #ifndef NO_WOLFSSL_SERVER
  45764. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  45765. WOLFSSL_SUCCESS);
  45766. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  45767. BAD_FUNC_ARG);
  45768. #endif
  45769. #ifdef OPENSSL_EXTRA
  45770. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  45771. #ifndef NO_WOLFSSL_CLIENT
  45772. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  45773. WOLFSSL_FAILURE);
  45774. #endif
  45775. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  45776. WOLFSSL_FAILURE);
  45777. #ifndef NO_WOLFSSL_CLIENT
  45778. #ifndef WOLFSSL_NO_TLS12
  45779. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  45780. WOLFSSL_FAILURE);
  45781. #endif
  45782. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  45783. WOLFSSL_SUCCESS);
  45784. #endif
  45785. #ifndef NO_WOLFSSL_SERVER
  45786. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  45787. WOLFSSL_SUCCESS);
  45788. #endif
  45789. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  45790. #ifndef NO_WOLFSSL_CLIENT
  45791. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  45792. #endif
  45793. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  45794. #ifndef NO_WOLFSSL_CLIENT
  45795. #ifndef WOLFSSL_NO_TLS12
  45796. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  45797. WOLFSSL_FAILURE);
  45798. #endif
  45799. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  45800. WOLFSSL_SUCCESS);
  45801. #endif
  45802. #ifndef NO_WOLFSSL_SERVER
  45803. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  45804. WOLFSSL_SUCCESS);
  45805. #endif
  45806. #endif /* OPENSSL_EXTRA */
  45807. #endif /* HAVE_SUPPORTED_CURVES */
  45808. #endif /* HAVE_ECC */
  45809. #ifdef WOLFSSL_EARLY_DATA
  45810. #ifndef OPENSSL_EXTRA
  45811. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  45812. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  45813. #else
  45814. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  45815. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  45816. #endif
  45817. #ifndef NO_WOLFSSL_CLIENT
  45818. #ifndef OPENSSL_EXTRA
  45819. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  45820. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  45821. #else
  45822. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  45823. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  45824. #endif
  45825. #endif
  45826. #ifndef NO_WOLFSSL_SERVER
  45827. #ifndef WOLFSSL_NO_TLS12
  45828. #ifndef OPENSSL_EXTRA
  45829. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  45830. BAD_FUNC_ARG);
  45831. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  45832. #else
  45833. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  45834. BAD_FUNC_ARG);
  45835. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  45836. #endif
  45837. #endif
  45838. #ifndef OPENSSL_EXTRA
  45839. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  45840. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  45841. WOLFSSL_SUCCESS);
  45842. #else
  45843. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  45844. #endif
  45845. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  45846. #else
  45847. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  45848. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  45849. #endif
  45850. #endif
  45851. #ifndef OPENSSL_EXTRA
  45852. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  45853. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  45854. #else
  45855. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  45856. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  45857. #endif
  45858. #ifndef NO_WOLFSSL_CLIENT
  45859. #ifndef OPENSSL_EXTRA
  45860. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  45861. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  45862. #else
  45863. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  45864. #endif
  45865. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  45866. #else
  45867. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  45868. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  45869. #endif
  45870. #endif
  45871. #ifndef NO_WOLFSSL_SERVER
  45872. #ifndef WOLFSSL_NO_TLS12
  45873. #ifndef OPENSSL_EXTRA
  45874. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  45875. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  45876. #else
  45877. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  45878. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  45879. #endif
  45880. #endif
  45881. #ifndef OPENSSL_EXTRA
  45882. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  45883. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  45884. #else
  45885. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  45886. #endif
  45887. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  45888. #else
  45889. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  45890. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  45891. #endif
  45892. #endif
  45893. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  45894. &outSz), BAD_FUNC_ARG);
  45895. #ifndef NO_WOLFSSL_CLIENT
  45896. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  45897. &outSz), BAD_FUNC_ARG);
  45898. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  45899. BAD_FUNC_ARG);
  45900. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  45901. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  45902. #endif
  45903. #ifndef NO_WOLFSSL_SERVER
  45904. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  45905. sizeof(earlyData), &outSz), SIDE_ERROR);
  45906. #endif
  45907. #ifndef NO_WOLFSSL_CLIENT
  45908. #ifndef WOLFSSL_NO_TLS12
  45909. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  45910. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  45911. #endif
  45912. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  45913. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  45914. #endif
  45915. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  45916. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  45917. #ifndef NO_WOLFSSL_SERVER
  45918. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  45919. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  45920. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  45921. &outSz), BAD_FUNC_ARG);
  45922. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  45923. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  45924. #endif
  45925. #ifndef NO_WOLFSSL_CLIENT
  45926. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  45927. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  45928. #endif
  45929. #ifndef NO_WOLFSSL_SERVER
  45930. #ifndef WOLFSSL_NO_TLS12
  45931. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  45932. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  45933. #endif
  45934. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  45935. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  45936. #endif
  45937. #endif
  45938. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  45939. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  45940. #endif
  45941. #ifndef NO_WOLFSSL_SERVER
  45942. wolfSSL_free(serverSsl);
  45943. wolfSSL_CTX_free(serverCtx);
  45944. #endif
  45945. #ifndef NO_WOLFSSL_CLIENT
  45946. wolfSSL_free(clientSsl);
  45947. wolfSSL_CTX_free(clientCtx);
  45948. #endif
  45949. #ifndef WOLFSSL_NO_TLS12
  45950. #ifndef NO_WOLFSSL_SERVER
  45951. wolfSSL_free(serverTls12Ssl);
  45952. wolfSSL_CTX_free(serverTls12Ctx);
  45953. #endif
  45954. #ifndef NO_WOLFSSL_CLIENT
  45955. wolfSSL_free(clientTls12Ssl);
  45956. wolfSSL_CTX_free(clientTls12Ctx);
  45957. #endif
  45958. #endif
  45959. return EXPECT_RESULT();
  45960. }
  45961. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  45962. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  45963. defined(BUILD_TLS_AES_256_GCM_SHA384)
  45964. /* Called when writing. */
  45965. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  45966. {
  45967. (void)ssl;
  45968. (void)buf;
  45969. (void)sz;
  45970. (void)ctx;
  45971. /* Force error return from wolfSSL_accept_TLSv13(). */
  45972. return WANT_WRITE;
  45973. }
  45974. /* Called when reading. */
  45975. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  45976. {
  45977. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  45978. int len = (int)msg->length;
  45979. (void)ssl;
  45980. (void)sz;
  45981. /* Pass back as much of message as will fit in buffer. */
  45982. if (len > sz)
  45983. len = sz;
  45984. XMEMCPY(buf, msg->buffer, len);
  45985. /* Move over returned data. */
  45986. msg->buffer += len;
  45987. msg->length -= len;
  45988. /* Amount actually copied. */
  45989. return len;
  45990. }
  45991. #endif
  45992. static int test_tls13_cipher_suites(void)
  45993. {
  45994. EXPECT_DECLS;
  45995. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  45996. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  45997. defined(BUILD_TLS_AES_256_GCM_SHA384)
  45998. WOLFSSL_CTX* ctx = NULL;
  45999. WOLFSSL *ssl = NULL;
  46000. int i;
  46001. byte clientHello[] = {
  46002. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  46003. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  46004. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  46005. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  46006. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  46007. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  46008. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  46009. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  46010. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46011. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  46012. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  46013. 0x13, 0x01,
  46014. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  46015. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  46016. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  46017. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  46018. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  46019. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  46020. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  46021. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  46022. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  46023. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  46024. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  46025. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  46026. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  46027. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  46028. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  46029. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  46030. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  46031. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  46032. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  46033. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  46034. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  46035. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  46036. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  46037. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  46038. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  46039. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  46040. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  46041. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  46042. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  46043. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  46044. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  46045. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  46046. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  46047. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  46048. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  46049. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46050. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46051. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  46052. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  46053. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  46054. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  46055. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  46056. };
  46057. WOLFSSL_BUFFER_INFO msg;
  46058. /* Offset into ClientHello message data of first cipher suite. */
  46059. const int csOff = 78;
  46060. /* Server cipher list. */
  46061. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  46062. /* Suite list with duplicates. */
  46063. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  46064. "TLS13-AES128-GCM-SHA256:"
  46065. "TLS13-AES256-GCM-SHA384:"
  46066. "TLS13-AES256-GCM-SHA384:"
  46067. "TLS13-AES128-GCM-SHA256";
  46068. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  46069. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  46070. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  46071. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  46072. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  46073. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  46074. #endif
  46075. /* Set up wolfSSL context. */
  46076. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  46077. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  46078. WOLFSSL_FILETYPE_PEM));
  46079. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  46080. WOLFSSL_FILETYPE_PEM));
  46081. /* Read from 'msg'. */
  46082. wolfSSL_SetIORecv(ctx, CsRecv);
  46083. /* No where to send to - dummy sender. */
  46084. wolfSSL_SetIOSend(ctx, CsSend);
  46085. /* Test cipher suite list with many copies of a cipher suite. */
  46086. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46087. msg.buffer = clientHello;
  46088. msg.length = (unsigned int)sizeof(clientHello);
  46089. wolfSSL_SetIOReadCtx(ssl, &msg);
  46090. /* Force server to have as many occurrences of same cipher suite as
  46091. * possible. */
  46092. if (ssl != NULL) {
  46093. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  46094. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  46095. for (i = 0; i < suites->suiteSz; i += 2) {
  46096. suites->suites[i + 0] = TLS13_BYTE;
  46097. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  46098. }
  46099. }
  46100. /* Test multiple occurrences of same cipher suite. */
  46101. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46102. wolfSSL_free(ssl);
  46103. ssl = NULL;
  46104. /* Set client order opposite to server order:
  46105. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  46106. clientHello[csOff + 0] = TLS13_BYTE;
  46107. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  46108. clientHello[csOff + 2] = TLS13_BYTE;
  46109. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  46110. /* Test server order negotiation. */
  46111. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46112. msg.buffer = clientHello;
  46113. msg.length = (unsigned int)sizeof(clientHello);
  46114. wolfSSL_SetIOReadCtx(ssl, &msg);
  46115. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  46116. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  46117. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  46118. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46119. /* Check refined order - server order. */
  46120. ExpectIntEQ(ssl->suites->suiteSz, 4);
  46121. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  46122. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  46123. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  46124. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  46125. wolfSSL_free(ssl);
  46126. ssl = NULL;
  46127. /* Test client order negotiation. */
  46128. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46129. msg.buffer = clientHello;
  46130. msg.length = (unsigned int)sizeof(clientHello);
  46131. wolfSSL_SetIOReadCtx(ssl, &msg);
  46132. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  46133. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  46134. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  46135. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  46136. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  46137. /* Check refined order - client order. */
  46138. ExpectIntEQ(ssl->suites->suiteSz, 4);
  46139. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  46140. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  46141. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  46142. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  46143. wolfSSL_free(ssl);
  46144. ssl = NULL;
  46145. /* Check duplicate detection is working. */
  46146. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  46147. ExpectIntEQ(ctx->suites->suiteSz, 4);
  46148. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  46149. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  46150. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  46151. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  46152. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  46153. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  46154. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  46155. ExpectIntEQ(ctx->suites->suiteSz, 4);
  46156. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  46157. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  46158. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  46159. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  46160. #endif
  46161. wolfSSL_CTX_free(ctx);
  46162. #endif
  46163. return EXPECT_RESULT();
  46164. }
  46165. #endif
  46166. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  46167. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  46168. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  46169. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46170. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  46171. const unsigned char* priv, unsigned int privSz,
  46172. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  46173. unsigned char* out, unsigned int* outlen,
  46174. void* ctx)
  46175. {
  46176. int result;
  46177. /* Test fail when context associated with WOLFSSL is NULL */
  46178. if (ctx == NULL) {
  46179. return -1;
  46180. }
  46181. (void)ssl;
  46182. /* return 0 on success */
  46183. PRIVATE_KEY_UNLOCK();
  46184. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  46185. PRIVATE_KEY_LOCK();
  46186. return result;
  46187. }
  46188. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  46189. EXPECT_DECLS;
  46190. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  46191. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  46192. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  46193. WOLFSSL_SUCCESS);
  46194. #endif
  46195. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  46196. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  46197. WOLFSSL_SUCCESS);
  46198. #endif
  46199. return EXPECT_RESULT();
  46200. }
  46201. static int test_dh_ssl_setup(WOLFSSL* ssl)
  46202. {
  46203. EXPECT_DECLS;
  46204. static int dh_test_ctx = 1;
  46205. int ret;
  46206. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  46207. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  46208. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  46209. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  46210. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46211. }
  46212. return EXPECT_RESULT();
  46213. }
  46214. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  46215. {
  46216. EXPECT_DECLS;
  46217. int ret;
  46218. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  46219. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  46220. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  46221. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  46222. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  46223. }
  46224. return EXPECT_RESULT();
  46225. }
  46226. #endif
  46227. static int test_DhCallbacks(void)
  46228. {
  46229. EXPECT_DECLS;
  46230. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  46231. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  46232. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  46233. WOLFSSL_CTX *ctx = NULL;
  46234. WOLFSSL *ssl = NULL;
  46235. int test;
  46236. test_ssl_cbf func_cb_client;
  46237. test_ssl_cbf func_cb_server;
  46238. /* Test that DH callback APIs work. */
  46239. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46240. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  46241. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  46242. /* load client ca cert */
  46243. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  46244. WOLFSSL_SUCCESS);
  46245. /* test with NULL arguments */
  46246. wolfSSL_SetDhAgreeCtx(NULL, &test);
  46247. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  46248. /* test success case */
  46249. test = 1;
  46250. ExpectNotNull(ssl = wolfSSL_new(ctx));
  46251. wolfSSL_SetDhAgreeCtx(ssl, &test);
  46252. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  46253. wolfSSL_free(ssl);
  46254. wolfSSL_CTX_free(ctx);
  46255. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  46256. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  46257. /* set callbacks to use DH functions */
  46258. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  46259. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  46260. func_cb_client.method = wolfTLSv1_2_client_method;
  46261. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  46262. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  46263. func_cb_server.method = wolfTLSv1_2_server_method;
  46264. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  46265. &func_cb_server, NULL), TEST_SUCCESS);
  46266. /* Test fail */
  46267. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  46268. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  46269. /* set callbacks to use DH functions */
  46270. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  46271. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  46272. func_cb_client.method = wolfTLSv1_2_client_method;
  46273. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  46274. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  46275. func_cb_server.method = wolfTLSv1_2_server_method;
  46276. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  46277. &func_cb_server, NULL), TEST_FAIL);
  46278. #endif
  46279. return EXPECT_RESULT();
  46280. }
  46281. #endif /* HAVE_PK_CALLBACKS */
  46282. #ifdef HAVE_HASHDRBG
  46283. #ifdef TEST_RESEED_INTERVAL
  46284. static int test_wc_RNG_GenerateBlock_Reseed(void)
  46285. {
  46286. EXPECT_DECLS;
  46287. int i;
  46288. WC_RNG rng;
  46289. byte key[32];
  46290. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46291. ExpectIntEQ(wc_InitRng(&rng), 0);
  46292. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  46293. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  46294. }
  46295. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  46296. return EXPECT_RESULT();
  46297. }
  46298. #endif /* TEST_RESEED_INTERVAL */
  46299. static int test_wc_RNG_GenerateBlock(void)
  46300. {
  46301. EXPECT_DECLS;
  46302. int i;
  46303. WC_RNG rng;
  46304. byte key[32];
  46305. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46306. ExpectIntEQ(wc_InitRng(&rng), 0);
  46307. for (i = 0; i < 10; i++) {
  46308. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  46309. }
  46310. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  46311. return EXPECT_RESULT();
  46312. }
  46313. #endif /* HAVE_HASHDRBG */
  46314. /*
  46315. * Testing get_rand_digit
  46316. */
  46317. static int test_get_rand_digit(void)
  46318. {
  46319. EXPECT_DECLS;
  46320. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  46321. WC_RNG rng;
  46322. mp_digit d;
  46323. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46324. ExpectIntEQ(wc_InitRng(&rng), 0);
  46325. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  46326. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  46327. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  46328. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  46329. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  46330. #endif
  46331. return EXPECT_RESULT();
  46332. } /* End test_get_rand_digit*/
  46333. /*
  46334. * Testing get_digit_count
  46335. */
  46336. static int test_get_digit_count(void)
  46337. {
  46338. EXPECT_DECLS;
  46339. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  46340. mp_int a;
  46341. XMEMSET(&a, 0, sizeof(mp_int));
  46342. ExpectIntEQ(mp_init(&a), 0);
  46343. ExpectIntEQ(get_digit_count(NULL), 0);
  46344. ExpectIntEQ(get_digit_count(&a), 0);
  46345. mp_clear(&a);
  46346. #endif
  46347. return EXPECT_RESULT();
  46348. } /* End test_get_digit_count*/
  46349. /*
  46350. * Testing mp_cond_copy
  46351. */
  46352. static int test_mp_cond_copy(void)
  46353. {
  46354. EXPECT_DECLS;
  46355. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  46356. defined(WOLFSSL_PUBLIC_MP)
  46357. mp_int a;
  46358. mp_int b;
  46359. int copy = 0;
  46360. XMEMSET(&a, 0, sizeof(mp_int));
  46361. XMEMSET(&b, 0, sizeof(mp_int));
  46362. ExpectIntEQ(mp_init(&a), MP_OKAY);
  46363. ExpectIntEQ(mp_init(&b), MP_OKAY);
  46364. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  46365. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  46366. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  46367. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  46368. mp_clear(&a);
  46369. mp_clear(&b);
  46370. #endif
  46371. return EXPECT_RESULT();
  46372. } /* End test_mp_cond_copy*/
  46373. /*
  46374. * Testing mp_rand
  46375. */
  46376. static int test_mp_rand(void)
  46377. {
  46378. EXPECT_DECLS;
  46379. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  46380. mp_int a;
  46381. WC_RNG rng;
  46382. int digits = 1;
  46383. XMEMSET(&a, 0, sizeof(mp_int));
  46384. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46385. ExpectIntEQ(mp_init(&a), MP_OKAY);
  46386. ExpectIntEQ(wc_InitRng(&rng), 0);
  46387. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  46388. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  46389. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  46390. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  46391. mp_clear(&a);
  46392. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  46393. #endif
  46394. return EXPECT_RESULT();
  46395. } /* End test_mp_rand*/
  46396. /*
  46397. * Testing get_digit
  46398. */
  46399. static int test_get_digit(void)
  46400. {
  46401. EXPECT_DECLS;
  46402. #if defined(WOLFSSL_PUBLIC_MP)
  46403. mp_int a;
  46404. int n = 0;
  46405. XMEMSET(&a, 0, sizeof(mp_int));
  46406. ExpectIntEQ(mp_init(&a), MP_OKAY);
  46407. ExpectIntEQ(get_digit(NULL, n), 0);
  46408. ExpectIntEQ(get_digit(&a, n), 0);
  46409. mp_clear(&a);
  46410. #endif
  46411. return EXPECT_RESULT();
  46412. } /* End test_get_digit*/
  46413. /*
  46414. * Testing wc_export_int
  46415. */
  46416. static int test_wc_export_int(void)
  46417. {
  46418. EXPECT_DECLS;
  46419. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  46420. defined(WOLFSSL_PUBLIC_MP)
  46421. mp_int mp;
  46422. byte buf[32];
  46423. word32 keySz = (word32)sizeof(buf);
  46424. word32 len = (word32)sizeof(buf);
  46425. XMEMSET(&mp, 0, sizeof(mp_int));
  46426. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  46427. ExpectIntEQ(mp_set(&mp, 1234), 0);
  46428. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  46429. BAD_FUNC_ARG);
  46430. len = sizeof(buf)-1;
  46431. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  46432. BUFFER_E);
  46433. len = sizeof(buf);
  46434. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  46435. len = 4; /* test input too small */
  46436. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  46437. len = sizeof(buf);
  46438. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  46439. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  46440. ExpectIntEQ(len, 5);
  46441. mp_clear(&mp);
  46442. #endif
  46443. return EXPECT_RESULT();
  46444. } /* End test_wc_export_int*/
  46445. static int test_wc_InitRngNonce(void)
  46446. {
  46447. EXPECT_DECLS;
  46448. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  46449. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  46450. HAVE_FIPS_VERSION >= 2))
  46451. WC_RNG rng;
  46452. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  46453. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  46454. word32 nonceSz = sizeof(nonce);
  46455. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  46456. ExpectIntEQ(wc_FreeRng(&rng), 0);
  46457. #endif
  46458. return EXPECT_RESULT();
  46459. } /* End test_wc_InitRngNonce*/
  46460. /*
  46461. * Testing wc_InitRngNonce_ex
  46462. */
  46463. static int test_wc_InitRngNonce_ex(void)
  46464. {
  46465. EXPECT_DECLS;
  46466. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  46467. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  46468. HAVE_FIPS_VERSION >= 2))
  46469. WC_RNG rng;
  46470. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  46471. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  46472. word32 nonceSz = sizeof(nonce);
  46473. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  46474. 0);
  46475. ExpectIntEQ(wc_FreeRng(&rng), 0);
  46476. #endif
  46477. return EXPECT_RESULT();
  46478. } /* End test_wc_InitRngNonce_ex */
  46479. static int test_wolfSSL_X509_CRL(void)
  46480. {
  46481. EXPECT_DECLS;
  46482. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  46483. X509_CRL *crl = NULL;
  46484. char pem[][100] = {
  46485. "./certs/crl/crl.pem",
  46486. "./certs/crl/crl2.pem",
  46487. "./certs/crl/caEccCrl.pem",
  46488. "./certs/crl/eccCliCRL.pem",
  46489. "./certs/crl/eccSrvCRL.pem",
  46490. ""
  46491. };
  46492. #ifndef NO_BIO
  46493. BIO *bio = NULL;
  46494. #endif
  46495. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  46496. char der[][100] = {
  46497. "./certs/crl/crl.der",
  46498. "./certs/crl/crl2.der",
  46499. ""};
  46500. #endif
  46501. XFILE fp = XBADFILE;
  46502. int i;
  46503. for (i = 0; pem[i][0] != '\0'; i++)
  46504. {
  46505. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  46506. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46507. NULL, NULL));
  46508. ExpectNotNull(crl);
  46509. X509_CRL_free(crl);
  46510. if (fp != XBADFILE) {
  46511. XFCLOSE(fp);
  46512. fp = XBADFILE;
  46513. }
  46514. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  46515. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  46516. NULL));
  46517. if (EXPECT_FAIL()) {
  46518. crl = NULL;
  46519. }
  46520. ExpectNotNull(crl);
  46521. X509_CRL_free(crl);
  46522. crl = NULL;
  46523. if (fp != XBADFILE) {
  46524. XFCLOSE(fp);
  46525. fp = XBADFILE;
  46526. }
  46527. }
  46528. #ifndef NO_BIO
  46529. for (i = 0; pem[i][0] != '\0'; i++)
  46530. {
  46531. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  46532. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  46533. X509_CRL_free(crl);
  46534. crl = NULL;
  46535. BIO_free(bio);
  46536. bio = NULL;
  46537. }
  46538. #endif
  46539. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  46540. for (i = 0; der[i][0] != '\0'; i++) {
  46541. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  46542. ExpectTrue((fp != XBADFILE));
  46543. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  46544. ExpectNotNull(crl);
  46545. X509_CRL_free(crl);
  46546. if (fp != XBADFILE) {
  46547. XFCLOSE(fp);
  46548. fp = XBADFILE;
  46549. }
  46550. fp = XFOPEN(der[i], "rb");
  46551. ExpectTrue((fp != XBADFILE));
  46552. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  46553. if (EXPECT_FAIL()) {
  46554. crl = NULL;
  46555. }
  46556. ExpectNotNull(crl);
  46557. X509_CRL_free(crl);
  46558. crl = NULL;
  46559. if (fp != XBADFILE) {
  46560. XFCLOSE(fp);
  46561. fp = XBADFILE;
  46562. }
  46563. }
  46564. #endif
  46565. #endif
  46566. return EXPECT_RESULT();
  46567. }
  46568. static int test_wolfSSL_X509_load_crl_file(void)
  46569. {
  46570. EXPECT_DECLS;
  46571. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  46572. !defined(NO_RSA) && !defined(NO_BIO)
  46573. int i;
  46574. char pem[][100] = {
  46575. "./certs/crl/crl.pem",
  46576. "./certs/crl/crl2.pem",
  46577. "./certs/crl/caEccCrl.pem",
  46578. "./certs/crl/eccCliCRL.pem",
  46579. "./certs/crl/eccSrvCRL.pem",
  46580. ""
  46581. };
  46582. char der[][100] = {
  46583. "./certs/crl/crl.der",
  46584. "./certs/crl/crl2.der",
  46585. ""
  46586. };
  46587. WOLFSSL_X509_STORE* store = NULL;
  46588. WOLFSSL_X509_LOOKUP* lookup = NULL;
  46589. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  46590. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  46591. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  46592. X509_FILETYPE_PEM), 1);
  46593. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  46594. X509_FILETYPE_PEM), 1);
  46595. if (store) {
  46596. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  46597. WOLFSSL_FILETYPE_PEM), 1);
  46598. /* since store hasn't yet known the revoked cert*/
  46599. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  46600. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  46601. }
  46602. for (i = 0; pem[i][0] != '\0'; i++) {
  46603. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  46604. 1);
  46605. }
  46606. if (store) {
  46607. /* since store knows crl list */
  46608. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  46609. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  46610. CRL_CERT_REVOKED);
  46611. }
  46612. /* once feeing store */
  46613. X509_STORE_free(store);
  46614. store = NULL;
  46615. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  46616. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  46617. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  46618. X509_FILETYPE_PEM), 1);
  46619. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  46620. X509_FILETYPE_PEM), 1);
  46621. if (store) {
  46622. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  46623. WOLFSSL_FILETYPE_PEM), 1);
  46624. /* since store hasn't yet known the revoked cert*/
  46625. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  46626. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  46627. }
  46628. for (i = 0; der[i][0] != '\0'; i++) {
  46629. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  46630. 1);
  46631. }
  46632. if (store) {
  46633. /* since store knows crl list */
  46634. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  46635. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  46636. CRL_CERT_REVOKED);
  46637. }
  46638. /* test for incorrect parameter */
  46639. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  46640. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  46641. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  46642. X509_STORE_free(store);
  46643. store = NULL;
  46644. #endif
  46645. return EXPECT_RESULT();
  46646. }
  46647. static int test_wolfSSL_i2d_X509(void)
  46648. {
  46649. EXPECT_DECLS;
  46650. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  46651. const unsigned char* cert_buf = server_cert_der_2048;
  46652. unsigned char* out = NULL;
  46653. unsigned char* tmp = NULL;
  46654. X509* cert = NULL;
  46655. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  46656. /* Pointer should be advanced */
  46657. ExpectPtrGT(cert_buf, server_cert_der_2048);
  46658. ExpectIntGT(i2d_X509(cert, &out), 0);
  46659. ExpectNotNull(out);
  46660. tmp = out;
  46661. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  46662. ExpectPtrGT(tmp, out);
  46663. if (out != NULL)
  46664. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  46665. X509_free(cert);
  46666. #endif
  46667. return EXPECT_RESULT();
  46668. }
  46669. static int test_wolfSSL_d2i_X509_REQ(void)
  46670. {
  46671. EXPECT_DECLS;
  46672. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  46673. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  46674. !defined(WOLFSSL_SP_MATH)
  46675. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  46676. * were generated by libest
  46677. * ./certs/csr.attr.der contains sample attributes
  46678. * ./certs/csr.ext.der contains sample extensions */
  46679. const char* csrFile = "./certs/csr.signed.der";
  46680. const char* csrPopFile = "./certs/csr.attr.der";
  46681. const char* csrExtFile = "./certs/csr.ext.der";
  46682. /* ./certs/csr.dsa.pem is generated using
  46683. * openssl req -newkey dsa:certs/dsaparams.pem \
  46684. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  46685. * -outform PEM
  46686. * with the passphrase "wolfSSL"
  46687. */
  46688. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  46689. const char* csrDsaFile = "./certs/csr.dsa.pem";
  46690. XFILE f = XBADFILE;
  46691. #endif
  46692. BIO* bio = NULL;
  46693. X509* req = NULL;
  46694. EVP_PKEY *pub_key = NULL;
  46695. {
  46696. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  46697. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  46698. /*
  46699. * Extract the public key from the CSR
  46700. */
  46701. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  46702. /*
  46703. * Verify the signature in the CSR
  46704. */
  46705. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  46706. X509_free(req);
  46707. req = NULL;
  46708. BIO_free(bio);
  46709. bio = NULL;
  46710. EVP_PKEY_free(pub_key);
  46711. pub_key = NULL;
  46712. }
  46713. {
  46714. #ifdef OPENSSL_ALL
  46715. X509_ATTRIBUTE* attr;
  46716. ASN1_TYPE *at;
  46717. #endif
  46718. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  46719. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  46720. /*
  46721. * Extract the public key from the CSR
  46722. */
  46723. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  46724. /*
  46725. * Verify the signature in the CSR
  46726. */
  46727. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  46728. #ifdef OPENSSL_ALL
  46729. /*
  46730. * Obtain the challenge password from the CSR
  46731. */
  46732. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  46733. -1), 1);
  46734. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  46735. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  46736. ExpectNotNull(at->value.asn1_string);
  46737. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  46738. "2xIE+qqp/rhyTXP+");
  46739. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  46740. #endif
  46741. X509_free(req);
  46742. req = NULL;
  46743. BIO_free(bio);
  46744. bio = NULL;
  46745. EVP_PKEY_free(pub_key);
  46746. pub_key = NULL;
  46747. }
  46748. {
  46749. #ifdef OPENSSL_ALL
  46750. X509_ATTRIBUTE* attr;
  46751. ASN1_TYPE *at;
  46752. STACK_OF(X509_EXTENSION) *exts = NULL;
  46753. #endif
  46754. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  46755. /* This CSR contains an Extension Request attribute so
  46756. * we test extension parsing in a CSR attribute here. */
  46757. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  46758. /*
  46759. * Extract the public key from the CSR
  46760. */
  46761. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  46762. /*
  46763. * Verify the signature in the CSR
  46764. */
  46765. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  46766. #ifdef OPENSSL_ALL
  46767. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  46768. req));
  46769. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  46770. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  46771. /*
  46772. * Obtain the challenge password from the CSR
  46773. */
  46774. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  46775. -1), 0);
  46776. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  46777. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  46778. ExpectNotNull(at->value.asn1_string);
  46779. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  46780. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  46781. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  46782. #endif
  46783. X509_free(req);
  46784. req = NULL;
  46785. BIO_free(bio);
  46786. bio = NULL;
  46787. EVP_PKEY_free(pub_key);
  46788. pub_key = NULL;
  46789. }
  46790. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  46791. {
  46792. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  46793. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  46794. /*
  46795. * Extract the public key from the CSR
  46796. */
  46797. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  46798. /*
  46799. * Verify the signature in the CSR
  46800. */
  46801. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  46802. X509_free(req);
  46803. req = NULL;
  46804. BIO_free(bio);
  46805. /* Run the same test, but with a file pointer instead of a BIO.
  46806. * (PEM_read_X509_REQ)*/
  46807. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  46808. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  46809. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  46810. X509_free(req);
  46811. EVP_PKEY_free(pub_key);
  46812. }
  46813. #endif /* !NO_DSA && !HAVE_SELFTEST */
  46814. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  46815. return EXPECT_RESULT();
  46816. }
  46817. static int test_wolfSSL_PEM_read_X509(void)
  46818. {
  46819. EXPECT_DECLS;
  46820. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  46821. !defined(NO_RSA)
  46822. X509 *x509 = NULL;
  46823. XFILE fp = XBADFILE;
  46824. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  46825. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  46826. X509_free(x509);
  46827. if (fp != XBADFILE)
  46828. XFCLOSE(fp);
  46829. #endif
  46830. return EXPECT_RESULT();
  46831. }
  46832. static int test_wolfSSL_PEM_read(void)
  46833. {
  46834. EXPECT_DECLS;
  46835. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  46836. const char* filename = "./certs/server-keyEnc.pem";
  46837. XFILE fp = XBADFILE;
  46838. char* name = NULL;
  46839. char* header = NULL;
  46840. byte* data = NULL;
  46841. long len;
  46842. EVP_CIPHER_INFO cipher;
  46843. WOLFSSL_BIO* bio = NULL;
  46844. byte* fileData = NULL;
  46845. size_t fileDataSz = 0;
  46846. byte* out;
  46847. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  46848. /* Fail cases. */
  46849. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  46850. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  46851. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  46852. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  46853. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  46854. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  46855. ExpectIntGT(XSTRLEN(header), 0);
  46856. ExpectIntGT(len, 0);
  46857. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  46858. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  46859. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  46860. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  46861. DYNAMIC_TYPE_TMP_BUFFER));
  46862. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  46863. if (fp != XBADFILE) {
  46864. XFCLOSE(fp);
  46865. fp = XBADFILE;
  46866. }
  46867. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  46868. /* Fail cases. */
  46869. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  46870. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  46871. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  46872. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  46873. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  46874. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  46875. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  46876. /* Fail cases. */
  46877. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  46878. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  46879. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  46880. #ifndef NO_DES3
  46881. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  46882. #endif
  46883. /* Fail cases. */
  46884. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  46885. (void*)"yassl123"), WOLFSSL_FAILURE);
  46886. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  46887. (void*)"yassl123"), WOLFSSL_FAILURE);
  46888. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  46889. (void*)"yassl123"), WOLFSSL_FAILURE);
  46890. #if !defined(NO_DES3) && !defined(NO_MD5)
  46891. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  46892. (void*)"yassl123"), WOLFSSL_SUCCESS);
  46893. #endif
  46894. BIO_free(bio);
  46895. bio = NULL;
  46896. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46897. fileData = NULL;
  46898. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46899. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46900. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46901. name = NULL;
  46902. header = NULL;
  46903. data = NULL;
  46904. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  46905. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  46906. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  46907. ExpectIntEQ(XSTRLEN(header), 0);
  46908. ExpectIntGT(len, 0);
  46909. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  46910. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  46911. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  46912. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  46913. DYNAMIC_TYPE_TMP_BUFFER));
  46914. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  46915. if (fp != XBADFILE)
  46916. XFCLOSE(fp);
  46917. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  46918. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  46919. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  46920. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  46921. BIO_free(bio);
  46922. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46923. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46924. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46925. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46926. #endif
  46927. return EXPECT_RESULT();
  46928. }
  46929. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  46930. {
  46931. EXPECT_DECLS;
  46932. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  46933. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  46934. const byte iv[12] = { 0 };
  46935. const byte key[16] = { 0 };
  46936. const byte cleartext[16] = { 0 };
  46937. const byte aad[] = {
  46938. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  46939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  46940. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  46941. 0x4f
  46942. };
  46943. byte out1Part[16];
  46944. byte outTag1Part[16];
  46945. byte out2Part[16];
  46946. byte outTag2Part[16];
  46947. byte decryptBuf[16];
  46948. int len = 0;
  46949. int tlen;
  46950. EVP_CIPHER_CTX* ctx = NULL;
  46951. /* ENCRYPT */
  46952. /* Send AAD and data in 1 part */
  46953. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46954. tlen = 0;
  46955. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  46956. 1);
  46957. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  46958. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  46959. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  46960. sizeof(cleartext)), 1);
  46961. tlen += len;
  46962. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  46963. tlen += len;
  46964. ExpectIntEQ(tlen, sizeof(cleartext));
  46965. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  46966. outTag1Part), 1);
  46967. EVP_CIPHER_CTX_free(ctx);
  46968. ctx = NULL;
  46969. /* DECRYPT */
  46970. /* Send AAD and data in 1 part */
  46971. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46972. tlen = 0;
  46973. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  46974. 1);
  46975. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  46976. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  46977. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  46978. sizeof(cleartext)), 1);
  46979. tlen += len;
  46980. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  46981. outTag1Part), 1);
  46982. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  46983. tlen += len;
  46984. ExpectIntEQ(tlen, sizeof(cleartext));
  46985. EVP_CIPHER_CTX_free(ctx);
  46986. ctx = NULL;
  46987. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  46988. /* ENCRYPT */
  46989. /* Send AAD and data in 2 parts */
  46990. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46991. tlen = 0;
  46992. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  46993. 1);
  46994. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  46995. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  46996. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  46997. 1);
  46998. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  46999. tlen += len;
  47000. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  47001. sizeof(cleartext) - 1), 1);
  47002. tlen += len;
  47003. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  47004. tlen += len;
  47005. ExpectIntEQ(tlen, sizeof(cleartext));
  47006. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  47007. outTag2Part), 1);
  47008. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  47009. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  47010. EVP_CIPHER_CTX_free(ctx);
  47011. ctx = NULL;
  47012. /* DECRYPT */
  47013. /* Send AAD and data in 2 parts */
  47014. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  47015. tlen = 0;
  47016. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  47017. 1);
  47018. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  47019. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  47020. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  47021. 1);
  47022. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  47023. tlen += len;
  47024. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  47025. sizeof(cleartext) - 1), 1);
  47026. tlen += len;
  47027. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  47028. outTag1Part), 1);
  47029. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  47030. tlen += len;
  47031. ExpectIntEQ(tlen, sizeof(cleartext));
  47032. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  47033. /* Test AAD reuse */
  47034. EVP_CIPHER_CTX_free(ctx);
  47035. #endif
  47036. return EXPECT_RESULT();
  47037. }
  47038. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  47039. {
  47040. EXPECT_DECLS;
  47041. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47042. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47043. /* Zero length plain text */
  47044. byte key[] = {
  47045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47049. }; /* align */
  47050. byte iv[] = {
  47051. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47052. }; /* align */
  47053. byte plaintxt[1];
  47054. int ivSz = 12;
  47055. int plaintxtSz = 0;
  47056. unsigned char tag[16];
  47057. unsigned char tag_kat[] = {
  47058. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  47059. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  47060. };
  47061. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47062. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47063. int ciphertxtSz = 0;
  47064. int decryptedtxtSz = 0;
  47065. int len = 0;
  47066. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  47067. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  47068. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  47069. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47070. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  47071. plaintxtSz));
  47072. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  47073. ciphertxtSz += len;
  47074. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  47075. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  47076. ExpectIntEQ(0, ciphertxtSz);
  47077. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  47078. EVP_CIPHER_CTX_init(de);
  47079. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  47080. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47081. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  47082. decryptedtxtSz = len;
  47083. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  47084. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  47085. decryptedtxtSz += len;
  47086. ExpectIntEQ(0, decryptedtxtSz);
  47087. EVP_CIPHER_CTX_free(en);
  47088. EVP_CIPHER_CTX_free(de);
  47089. #endif
  47090. return EXPECT_RESULT();
  47091. }
  47092. static int test_wolfssl_EVP_aes_gcm(void)
  47093. {
  47094. EXPECT_DECLS;
  47095. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  47096. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47097. /* A 256 bit key, AES_128 will use the first 128 bit*/
  47098. byte *key = (byte*)"01234567890123456789012345678901";
  47099. /* A 128 bit IV */
  47100. byte *iv = (byte*)"0123456789012345";
  47101. int ivSz = AES_BLOCK_SIZE;
  47102. /* Message to be encrypted */
  47103. byte *plaintxt = (byte*)"for things to change you have to change";
  47104. /* Additional non-confidential data */
  47105. byte *aad = (byte*)"Don't spend major time on minor things.";
  47106. unsigned char tag[AES_BLOCK_SIZE] = {0};
  47107. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  47108. int aadSz = (int)XSTRLEN((char*)aad);
  47109. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47110. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47111. int ciphertxtSz = 0;
  47112. int decryptedtxtSz = 0;
  47113. int len = 0;
  47114. int i = 0;
  47115. EVP_CIPHER_CTX en[2];
  47116. EVP_CIPHER_CTX de[2];
  47117. for (i = 0; i < 2; i++) {
  47118. EVP_CIPHER_CTX_init(&en[i]);
  47119. if (i == 0) {
  47120. /* Default uses 96-bits IV length */
  47121. #ifdef WOLFSSL_AES_128
  47122. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  47123. key, iv));
  47124. #elif defined(WOLFSSL_AES_192)
  47125. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  47126. key, iv));
  47127. #elif defined(WOLFSSL_AES_256)
  47128. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  47129. key, iv));
  47130. #endif
  47131. }
  47132. else {
  47133. #ifdef WOLFSSL_AES_128
  47134. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  47135. NULL, NULL));
  47136. #elif defined(WOLFSSL_AES_192)
  47137. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  47138. NULL, NULL));
  47139. #elif defined(WOLFSSL_AES_256)
  47140. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  47141. NULL, NULL));
  47142. #endif
  47143. /* non-default must to set the IV length first */
  47144. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  47145. ivSz, NULL));
  47146. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47147. }
  47148. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  47149. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  47150. plaintxtSz));
  47151. ciphertxtSz = len;
  47152. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  47153. ciphertxtSz += len;
  47154. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  47155. AES_BLOCK_SIZE, tag));
  47156. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  47157. EVP_CIPHER_CTX_init(&de[i]);
  47158. if (i == 0) {
  47159. /* Default uses 96-bits IV length */
  47160. #ifdef WOLFSSL_AES_128
  47161. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47162. key, iv));
  47163. #elif defined(WOLFSSL_AES_192)
  47164. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47165. key, iv));
  47166. #elif defined(WOLFSSL_AES_256)
  47167. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47168. key, iv));
  47169. #endif
  47170. }
  47171. else {
  47172. #ifdef WOLFSSL_AES_128
  47173. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  47174. NULL, NULL));
  47175. #elif defined(WOLFSSL_AES_192)
  47176. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  47177. NULL, NULL));
  47178. #elif defined(WOLFSSL_AES_256)
  47179. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  47180. NULL, NULL));
  47181. #endif
  47182. /* non-default must to set the IV length first */
  47183. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  47184. ivSz, NULL));
  47185. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47186. }
  47187. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47188. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47189. ciphertxtSz));
  47190. decryptedtxtSz = len;
  47191. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47192. AES_BLOCK_SIZE, tag));
  47193. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47194. decryptedtxtSz += len;
  47195. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  47196. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  47197. /* modify tag*/
  47198. tag[AES_BLOCK_SIZE-1]+=0xBB;
  47199. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47200. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47201. AES_BLOCK_SIZE, tag));
  47202. /* fail due to wrong tag */
  47203. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47204. ciphertxtSz));
  47205. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47206. ExpectIntEQ(0, len);
  47207. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  47208. }
  47209. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  47210. return EXPECT_RESULT();
  47211. }
  47212. static int test_wolfssl_EVP_aria_gcm(void)
  47213. {
  47214. int res = TEST_SKIPPED;
  47215. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  47216. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47217. /* A 256 bit key, AES_128 will use the first 128 bit*/
  47218. byte *key = (byte*)"01234567890123456789012345678901";
  47219. /* A 128 bit IV */
  47220. byte *iv = (byte*)"0123456789012345";
  47221. int ivSz = ARIA_BLOCK_SIZE;
  47222. /* Message to be encrypted */
  47223. const int plaintxtSz = 40;
  47224. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  47225. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  47226. /* Additional non-confidential data */
  47227. byte *aad = (byte*)"Don't spend major time on minor things.";
  47228. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  47229. int aadSz = (int)XSTRLEN((char*)aad);
  47230. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  47231. byte decryptedtxt[plaintxtSz];
  47232. int ciphertxtSz = 0;
  47233. int decryptedtxtSz = 0;
  47234. int len = 0;
  47235. int i = 0;
  47236. #define TEST_ARIA_GCM_COUNT 6
  47237. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  47238. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  47239. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  47240. EVP_CIPHER_CTX_init(&en[i]);
  47241. switch (i) {
  47242. case 0:
  47243. /* Default uses 96-bits IV length */
  47244. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  47245. break;
  47246. case 1:
  47247. /* Default uses 96-bits IV length */
  47248. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  47249. break;
  47250. case 2:
  47251. /* Default uses 96-bits IV length */
  47252. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  47253. break;
  47254. case 3:
  47255. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  47256. /* non-default must to set the IV length first */
  47257. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47258. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47259. break;
  47260. case 4:
  47261. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  47262. /* non-default must to set the IV length first */
  47263. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47264. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47265. break;
  47266. case 5:
  47267. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  47268. /* non-default must to set the IV length first */
  47269. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47270. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47271. break;
  47272. }
  47273. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  47274. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  47275. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  47276. ciphertxtSz = len;
  47277. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  47278. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  47279. ciphertxtSz += len;
  47280. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  47281. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  47282. EVP_CIPHER_CTX_init(&de[i]);
  47283. switch (i) {
  47284. case 0:
  47285. /* Default uses 96-bits IV length */
  47286. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  47287. break;
  47288. case 1:
  47289. /* Default uses 96-bits IV length */
  47290. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  47291. break;
  47292. case 2:
  47293. /* Default uses 96-bits IV length */
  47294. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  47295. break;
  47296. case 3:
  47297. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  47298. /* non-default must to set the IV length first */
  47299. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47300. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47301. break;
  47302. case 4:
  47303. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  47304. /* non-default must to set the IV length first */
  47305. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47306. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47307. break;
  47308. case 5:
  47309. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  47310. /* non-default must to set the IV length first */
  47311. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47312. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47313. break;
  47314. }
  47315. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  47316. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47317. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  47318. decryptedtxtSz = len;
  47319. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  47320. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47321. decryptedtxtSz += len;
  47322. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  47323. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  47324. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  47325. /* modify tag*/
  47326. tag[AES_BLOCK_SIZE-1]+=0xBB;
  47327. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47328. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  47329. /* fail due to wrong tag */
  47330. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  47331. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47332. AssertIntEQ(0, len);
  47333. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  47334. }
  47335. res = TEST_RES_CHECK(1);
  47336. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  47337. return res;
  47338. }
  47339. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  47340. {
  47341. EXPECT_DECLS;
  47342. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  47343. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47344. /* Zero length plain text */
  47345. byte key[] = {
  47346. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47347. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47348. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47349. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47350. }; /* align */
  47351. byte iv[] = {
  47352. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47353. }; /* align */
  47354. byte plaintxt[1];
  47355. int ivSz = 12;
  47356. int plaintxtSz = 0;
  47357. unsigned char tag[16];
  47358. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47359. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47360. int ciphertxtSz = 0;
  47361. int decryptedtxtSz = 0;
  47362. int len = 0;
  47363. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  47364. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  47365. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  47366. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  47367. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  47368. plaintxtSz));
  47369. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  47370. ciphertxtSz += len;
  47371. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  47372. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  47373. ExpectIntEQ(0, ciphertxtSz);
  47374. EVP_CIPHER_CTX_init(de);
  47375. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  47376. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  47377. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  47378. decryptedtxtSz = len;
  47379. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  47380. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  47381. decryptedtxtSz += len;
  47382. ExpectIntEQ(0, decryptedtxtSz);
  47383. EVP_CIPHER_CTX_free(en);
  47384. EVP_CIPHER_CTX_free(de);
  47385. #endif
  47386. return EXPECT_RESULT();
  47387. }
  47388. static int test_wolfssl_EVP_aes_ccm(void)
  47389. {
  47390. EXPECT_DECLS;
  47391. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  47392. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  47393. /* A 256 bit key, AES_128 will use the first 128 bit*/
  47394. byte *key = (byte*)"01234567890123456789012345678901";
  47395. /* A 128 bit IV */
  47396. byte *iv = (byte*)"0123456789012";
  47397. int ivSz = (int)XSTRLEN((char*)iv);
  47398. /* Message to be encrypted */
  47399. byte *plaintxt = (byte*)"for things to change you have to change";
  47400. /* Additional non-confidential data */
  47401. byte *aad = (byte*)"Don't spend major time on minor things.";
  47402. unsigned char tag[AES_BLOCK_SIZE] = {0};
  47403. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  47404. int aadSz = (int)XSTRLEN((char*)aad);
  47405. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  47406. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  47407. int ciphertxtSz = 0;
  47408. int decryptedtxtSz = 0;
  47409. int len = 0;
  47410. int i = 0;
  47411. int ret;
  47412. EVP_CIPHER_CTX en[2];
  47413. EVP_CIPHER_CTX de[2];
  47414. for (i = 0; i < 2; i++) {
  47415. EVP_CIPHER_CTX_init(&en[i]);
  47416. if (i == 0) {
  47417. /* Default uses 96-bits IV length */
  47418. #ifdef WOLFSSL_AES_128
  47419. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  47420. key, iv));
  47421. #elif defined(WOLFSSL_AES_192)
  47422. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  47423. key, iv));
  47424. #elif defined(WOLFSSL_AES_256)
  47425. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  47426. key, iv));
  47427. #endif
  47428. }
  47429. else {
  47430. #ifdef WOLFSSL_AES_128
  47431. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  47432. NULL, NULL));
  47433. #elif defined(WOLFSSL_AES_192)
  47434. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  47435. NULL, NULL));
  47436. #elif defined(WOLFSSL_AES_256)
  47437. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  47438. NULL, NULL));
  47439. #endif
  47440. /* non-default must to set the IV length first */
  47441. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  47442. ivSz, NULL));
  47443. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47444. }
  47445. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  47446. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  47447. plaintxtSz));
  47448. ciphertxtSz = len;
  47449. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  47450. ciphertxtSz += len;
  47451. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  47452. AES_BLOCK_SIZE, tag));
  47453. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  47454. ExpectIntEQ(ret, 1);
  47455. EVP_CIPHER_CTX_init(&de[i]);
  47456. if (i == 0) {
  47457. /* Default uses 96-bits IV length */
  47458. #ifdef WOLFSSL_AES_128
  47459. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  47460. key, iv));
  47461. #elif defined(WOLFSSL_AES_192)
  47462. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  47463. key, iv));
  47464. #elif defined(WOLFSSL_AES_256)
  47465. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  47466. key, iv));
  47467. #endif
  47468. }
  47469. else {
  47470. #ifdef WOLFSSL_AES_128
  47471. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  47472. NULL, NULL));
  47473. #elif defined(WOLFSSL_AES_192)
  47474. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  47475. NULL, NULL));
  47476. #elif defined(WOLFSSL_AES_256)
  47477. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  47478. NULL, NULL));
  47479. #endif
  47480. /* non-default must to set the IV length first */
  47481. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  47482. ivSz, NULL));
  47483. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47484. }
  47485. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47486. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47487. ciphertxtSz));
  47488. decryptedtxtSz = len;
  47489. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  47490. AES_BLOCK_SIZE, tag));
  47491. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47492. decryptedtxtSz += len;
  47493. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  47494. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  47495. /* modify tag*/
  47496. tag[AES_BLOCK_SIZE-1]+=0xBB;
  47497. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47498. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  47499. AES_BLOCK_SIZE, tag));
  47500. /* fail due to wrong tag */
  47501. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47502. ciphertxtSz));
  47503. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47504. ExpectIntEQ(0, len);
  47505. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  47506. ExpectIntEQ(ret, 1);
  47507. }
  47508. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  47509. return EXPECT_RESULT();
  47510. }
  47511. static int test_wolfssl_EVP_chacha20_poly1305(void)
  47512. {
  47513. EXPECT_DECLS;
  47514. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  47515. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  47516. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  47517. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  47518. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  47519. byte cipherText[sizeof(plainText)];
  47520. byte decryptedText[sizeof(plainText)];
  47521. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  47522. EVP_CIPHER_CTX* ctx = NULL;
  47523. int outSz;
  47524. /* Encrypt. */
  47525. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47526. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  47527. NULL), WOLFSSL_SUCCESS);
  47528. /* Invalid IV length. */
  47529. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  47530. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  47531. /* Valid IV length. */
  47532. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  47533. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  47534. /* Invalid tag length. */
  47535. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  47536. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  47537. /* Valid tag length. */
  47538. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  47539. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  47540. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47541. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  47542. WOLFSSL_SUCCESS);
  47543. ExpectIntEQ(outSz, sizeof(aad));
  47544. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  47545. sizeof(plainText)), WOLFSSL_SUCCESS);
  47546. ExpectIntEQ(outSz, sizeof(plainText));
  47547. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  47548. ExpectIntEQ(outSz, 0);
  47549. /* Invalid tag length. */
  47550. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  47551. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  47552. /* Valid tag length. */
  47553. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  47554. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  47555. EVP_CIPHER_CTX_free(ctx);
  47556. ctx = NULL;
  47557. /* Decrypt. */
  47558. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47559. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  47560. NULL), WOLFSSL_SUCCESS);
  47561. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  47562. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  47563. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  47564. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  47565. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47566. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  47567. WOLFSSL_SUCCESS);
  47568. ExpectIntEQ(outSz, sizeof(aad));
  47569. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47570. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47571. ExpectIntEQ(outSz, sizeof(cipherText));
  47572. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  47573. WOLFSSL_SUCCESS);
  47574. ExpectIntEQ(outSz, 0);
  47575. EVP_CIPHER_CTX_free(ctx);
  47576. ctx = NULL;
  47577. /* Test partial Inits. CipherInit() allow setting of key and iv
  47578. * in separate calls. */
  47579. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47580. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  47581. key, NULL, 1), WOLFSSL_SUCCESS);
  47582. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  47583. WOLFSSL_SUCCESS);
  47584. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  47585. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  47586. ExpectIntEQ(outSz, sizeof(aad));
  47587. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47588. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47589. ExpectIntEQ(outSz, sizeof(cipherText));
  47590. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  47591. WOLFSSL_SUCCESS);
  47592. ExpectIntEQ(outSz, 0);
  47593. EVP_CIPHER_CTX_free(ctx);
  47594. #endif
  47595. return EXPECT_RESULT();
  47596. }
  47597. static int test_wolfssl_EVP_chacha20(void)
  47598. {
  47599. EXPECT_DECLS;
  47600. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  47601. byte key[CHACHA_MAX_KEY_SZ];
  47602. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  47603. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  47604. byte cipherText[sizeof(plainText)];
  47605. byte decryptedText[sizeof(plainText)];
  47606. EVP_CIPHER_CTX* ctx = NULL;
  47607. int outSz;
  47608. XMEMSET(key, 0, sizeof(key));
  47609. XMEMSET(iv, 0, sizeof(iv));
  47610. /* Encrypt. */
  47611. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47612. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  47613. NULL), WOLFSSL_SUCCESS);
  47614. /* Any tag length must fail - not an AEAD cipher. */
  47615. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  47616. 16, NULL), WOLFSSL_FAILURE);
  47617. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47618. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  47619. sizeof(plainText)), WOLFSSL_SUCCESS);
  47620. ExpectIntEQ(outSz, sizeof(plainText));
  47621. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  47622. ExpectIntEQ(outSz, 0);
  47623. EVP_CIPHER_CTX_free(ctx);
  47624. ctx = NULL;
  47625. /* Decrypt. */
  47626. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47627. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  47628. NULL), WOLFSSL_SUCCESS);
  47629. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47630. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47631. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47632. ExpectIntEQ(outSz, sizeof(cipherText));
  47633. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  47634. WOLFSSL_SUCCESS);
  47635. ExpectIntEQ(outSz, 0);
  47636. EVP_CIPHER_CTX_free(ctx);
  47637. ctx = NULL;
  47638. /* Test partial Inits. CipherInit() allow setting of key and iv
  47639. * in separate calls. */
  47640. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47641. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  47642. key, NULL, 1), WOLFSSL_SUCCESS);
  47643. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  47644. WOLFSSL_SUCCESS);
  47645. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47646. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47647. ExpectIntEQ(outSz, sizeof(cipherText));
  47648. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  47649. WOLFSSL_SUCCESS);
  47650. ExpectIntEQ(outSz, 0);
  47651. EVP_CIPHER_CTX_free(ctx);
  47652. #endif
  47653. return EXPECT_RESULT();
  47654. }
  47655. static int test_wolfssl_EVP_sm4_ecb(void)
  47656. {
  47657. int res = TEST_SKIPPED;
  47658. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  47659. EXPECT_DECLS;
  47660. byte key[SM4_KEY_SIZE];
  47661. byte plainText[SM4_BLOCK_SIZE] = {
  47662. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  47663. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  47664. };
  47665. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  47666. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  47667. EVP_CIPHER_CTX* ctx;
  47668. int outSz;
  47669. XMEMSET(key, 0, sizeof(key));
  47670. /* Encrypt. */
  47671. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47672. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  47673. WOLFSSL_SUCCESS);
  47674. /* Any tag length must fail - not an AEAD cipher. */
  47675. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  47676. WOLFSSL_FAILURE);
  47677. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  47678. WOLFSSL_SUCCESS);
  47679. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  47680. sizeof(plainText)), WOLFSSL_SUCCESS);
  47681. ExpectIntEQ(outSz, sizeof(plainText));
  47682. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  47683. WOLFSSL_SUCCESS);
  47684. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  47685. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  47686. EVP_CIPHER_CTX_free(ctx);
  47687. /* Decrypt. */
  47688. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47689. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  47690. WOLFSSL_SUCCESS);
  47691. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  47692. WOLFSSL_SUCCESS);
  47693. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47694. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47695. ExpectIntEQ(outSz, sizeof(plainText));
  47696. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  47697. WOLFSSL_SUCCESS);
  47698. ExpectIntEQ(outSz, 0);
  47699. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  47700. EVP_CIPHER_CTX_free(ctx);
  47701. res = EXPECT_RESULT();
  47702. #endif
  47703. return res;
  47704. }
  47705. static int test_wolfssl_EVP_sm4_cbc(void)
  47706. {
  47707. int res = TEST_SKIPPED;
  47708. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  47709. EXPECT_DECLS;
  47710. byte key[SM4_KEY_SIZE];
  47711. byte iv[SM4_BLOCK_SIZE];
  47712. byte plainText[SM4_BLOCK_SIZE] = {
  47713. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  47714. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  47715. };
  47716. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  47717. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  47718. EVP_CIPHER_CTX* ctx;
  47719. int outSz;
  47720. XMEMSET(key, 0, sizeof(key));
  47721. XMEMSET(iv, 0, sizeof(iv));
  47722. /* Encrypt. */
  47723. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47724. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  47725. WOLFSSL_SUCCESS);
  47726. /* Any tag length must fail - not an AEAD cipher. */
  47727. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  47728. WOLFSSL_FAILURE);
  47729. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47730. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  47731. sizeof(plainText)), WOLFSSL_SUCCESS);
  47732. ExpectIntEQ(outSz, sizeof(plainText));
  47733. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  47734. WOLFSSL_SUCCESS);
  47735. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  47736. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  47737. EVP_CIPHER_CTX_free(ctx);
  47738. /* Decrypt. */
  47739. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47740. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  47741. WOLFSSL_SUCCESS);
  47742. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47743. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47744. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47745. ExpectIntEQ(outSz, sizeof(plainText));
  47746. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  47747. WOLFSSL_SUCCESS);
  47748. ExpectIntEQ(outSz, 0);
  47749. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  47750. EVP_CIPHER_CTX_free(ctx);
  47751. /* Test partial Inits. CipherInit() allow setting of key and iv
  47752. * in separate calls. */
  47753. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47754. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  47755. WOLFSSL_SUCCESS);
  47756. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  47757. WOLFSSL_SUCCESS);
  47758. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47759. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47760. ExpectIntEQ(outSz, sizeof(plainText));
  47761. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  47762. WOLFSSL_SUCCESS);
  47763. ExpectIntEQ(outSz, 0);
  47764. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  47765. EVP_CIPHER_CTX_free(ctx);
  47766. res = EXPECT_RESULT();
  47767. #endif
  47768. return res;
  47769. }
  47770. static int test_wolfssl_EVP_sm4_ctr(void)
  47771. {
  47772. int res = TEST_SKIPPED;
  47773. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  47774. EXPECT_DECLS;
  47775. byte key[SM4_KEY_SIZE];
  47776. byte iv[SM4_BLOCK_SIZE];
  47777. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  47778. byte cipherText[sizeof(plainText)];
  47779. byte decryptedText[sizeof(plainText)];
  47780. EVP_CIPHER_CTX* ctx;
  47781. int outSz;
  47782. XMEMSET(key, 0, sizeof(key));
  47783. XMEMSET(iv, 0, sizeof(iv));
  47784. /* Encrypt. */
  47785. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47786. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  47787. WOLFSSL_SUCCESS);
  47788. /* Any tag length must fail - not an AEAD cipher. */
  47789. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  47790. WOLFSSL_FAILURE);
  47791. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47792. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  47793. sizeof(plainText)), WOLFSSL_SUCCESS);
  47794. ExpectIntEQ(outSz, sizeof(plainText));
  47795. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  47796. ExpectIntEQ(outSz, 0);
  47797. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  47798. EVP_CIPHER_CTX_free(ctx);
  47799. /* Decrypt. */
  47800. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47801. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  47802. WOLFSSL_SUCCESS);
  47803. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  47804. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47805. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47806. ExpectIntEQ(outSz, sizeof(cipherText));
  47807. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  47808. WOLFSSL_SUCCESS);
  47809. ExpectIntEQ(outSz, 0);
  47810. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  47811. EVP_CIPHER_CTX_free(ctx);
  47812. /* Test partial Inits. CipherInit() allow setting of key and iv
  47813. * in separate calls. */
  47814. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  47815. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  47816. WOLFSSL_SUCCESS);
  47817. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  47818. WOLFSSL_SUCCESS);
  47819. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  47820. sizeof(cipherText)), WOLFSSL_SUCCESS);
  47821. ExpectIntEQ(outSz, sizeof(cipherText));
  47822. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  47823. WOLFSSL_SUCCESS);
  47824. ExpectIntEQ(outSz, 0);
  47825. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  47826. EVP_CIPHER_CTX_free(ctx);
  47827. res = EXPECT_RESULT();
  47828. #endif
  47829. return res;
  47830. }
  47831. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  47832. {
  47833. int res = TEST_SKIPPED;
  47834. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  47835. /* Zero length plain text */
  47836. EXPECT_DECLS;
  47837. byte key[] = {
  47838. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47839. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47840. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47841. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47842. }; /* align */
  47843. byte iv[] = {
  47844. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47845. }; /* align */
  47846. byte plaintxt[1];
  47847. int ivSz = 12;
  47848. int plaintxtSz = 0;
  47849. unsigned char tag[16];
  47850. unsigned char tag_kat[16] = {
  47851. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  47852. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  47853. };
  47854. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  47855. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  47856. int ciphertxtSz = 0;
  47857. int decryptedtxtSz = 0;
  47858. int len = 0;
  47859. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  47860. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  47861. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  47862. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47863. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  47864. plaintxtSz));
  47865. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  47866. ciphertxtSz += len;
  47867. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  47868. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  47869. ExpectIntEQ(0, ciphertxtSz);
  47870. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  47871. EVP_CIPHER_CTX_init(de);
  47872. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  47873. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  47874. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  47875. decryptedtxtSz = len;
  47876. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  47877. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  47878. decryptedtxtSz += len;
  47879. ExpectIntEQ(0, decryptedtxtSz);
  47880. EVP_CIPHER_CTX_free(en);
  47881. EVP_CIPHER_CTX_free(de);
  47882. res = EXPECT_RESULT();
  47883. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  47884. return res;
  47885. }
  47886. static int test_wolfssl_EVP_sm4_gcm(void)
  47887. {
  47888. int res = TEST_SKIPPED;
  47889. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  47890. EXPECT_DECLS;
  47891. byte *key = (byte*)"0123456789012345";
  47892. /* A 128 bit IV */
  47893. byte *iv = (byte*)"0123456789012345";
  47894. int ivSz = SM4_BLOCK_SIZE;
  47895. /* Message to be encrypted */
  47896. byte *plaintxt = (byte*)"for things to change you have to change";
  47897. /* Additional non-confidential data */
  47898. byte *aad = (byte*)"Don't spend major time on minor things.";
  47899. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  47900. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  47901. int aadSz = (int)XSTRLEN((char*)aad);
  47902. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  47903. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  47904. int ciphertxtSz = 0;
  47905. int decryptedtxtSz = 0;
  47906. int len = 0;
  47907. int i = 0;
  47908. EVP_CIPHER_CTX en[2];
  47909. EVP_CIPHER_CTX de[2];
  47910. for (i = 0; i < 2; i++) {
  47911. EVP_CIPHER_CTX_init(&en[i]);
  47912. if (i == 0) {
  47913. /* Default uses 96-bits IV length */
  47914. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  47915. iv));
  47916. }
  47917. else {
  47918. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  47919. NULL));
  47920. /* non-default must to set the IV length first */
  47921. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  47922. ivSz, NULL));
  47923. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  47924. }
  47925. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  47926. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  47927. plaintxtSz));
  47928. ciphertxtSz = len;
  47929. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  47930. ciphertxtSz += len;
  47931. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  47932. SM4_BLOCK_SIZE, tag));
  47933. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  47934. EVP_CIPHER_CTX_init(&de[i]);
  47935. if (i == 0) {
  47936. /* Default uses 96-bits IV length */
  47937. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  47938. iv));
  47939. }
  47940. else {
  47941. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  47942. NULL));
  47943. /* non-default must to set the IV length first */
  47944. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  47945. ivSz, NULL));
  47946. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  47947. }
  47948. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47949. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47950. ciphertxtSz));
  47951. decryptedtxtSz = len;
  47952. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47953. SM4_BLOCK_SIZE, tag));
  47954. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47955. decryptedtxtSz += len;
  47956. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  47957. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  47958. /* modify tag*/
  47959. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  47960. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  47961. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  47962. SM4_BLOCK_SIZE, tag));
  47963. /* fail due to wrong tag */
  47964. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  47965. ciphertxtSz));
  47966. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  47967. ExpectIntEQ(0, len);
  47968. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  47969. }
  47970. res = EXPECT_RESULT();
  47971. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  47972. return res;
  47973. }
  47974. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  47975. {
  47976. int res = TEST_SKIPPED;
  47977. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  47978. /* Zero length plain text */
  47979. EXPECT_DECLS;
  47980. byte key[] = {
  47981. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47982. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47983. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  47984. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47985. }; /* align */
  47986. byte iv[] = {
  47987. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  47988. }; /* align */
  47989. byte plaintxt[1];
  47990. int ivSz = 12;
  47991. int plaintxtSz = 0;
  47992. unsigned char tag[16];
  47993. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  47994. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  47995. int ciphertxtSz = 0;
  47996. int decryptedtxtSz = 0;
  47997. int len = 0;
  47998. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  47999. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  48000. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  48001. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48002. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  48003. plaintxtSz));
  48004. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  48005. ciphertxtSz += len;
  48006. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  48007. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  48008. ExpectIntEQ(0, ciphertxtSz);
  48009. EVP_CIPHER_CTX_init(de);
  48010. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  48011. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  48012. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  48013. decryptedtxtSz = len;
  48014. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  48015. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  48016. decryptedtxtSz += len;
  48017. ExpectIntEQ(0, decryptedtxtSz);
  48018. EVP_CIPHER_CTX_free(en);
  48019. EVP_CIPHER_CTX_free(de);
  48020. res = EXPECT_RESULT();
  48021. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  48022. return res;
  48023. }
  48024. static int test_wolfssl_EVP_sm4_ccm(void)
  48025. {
  48026. int res = TEST_SKIPPED;
  48027. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  48028. EXPECT_DECLS;
  48029. byte *key = (byte*)"0123456789012345";
  48030. byte *iv = (byte*)"0123456789012";
  48031. int ivSz = (int)XSTRLEN((char*)iv);
  48032. /* Message to be encrypted */
  48033. byte *plaintxt = (byte*)"for things to change you have to change";
  48034. /* Additional non-confidential data */
  48035. byte *aad = (byte*)"Don't spend major time on minor things.";
  48036. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  48037. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  48038. int aadSz = (int)XSTRLEN((char*)aad);
  48039. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  48040. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  48041. int ciphertxtSz = 0;
  48042. int decryptedtxtSz = 0;
  48043. int len = 0;
  48044. int i = 0;
  48045. EVP_CIPHER_CTX en[2];
  48046. EVP_CIPHER_CTX de[2];
  48047. for (i = 0; i < 2; i++) {
  48048. EVP_CIPHER_CTX_init(&en[i]);
  48049. if (i == 0) {
  48050. /* Default uses 96-bits IV length */
  48051. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  48052. iv));
  48053. }
  48054. else {
  48055. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  48056. NULL));
  48057. /* non-default must to set the IV length first */
  48058. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  48059. ivSz, NULL));
  48060. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  48061. }
  48062. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  48063. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  48064. plaintxtSz));
  48065. ciphertxtSz = len;
  48066. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  48067. ciphertxtSz += len;
  48068. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  48069. SM4_BLOCK_SIZE, tag));
  48070. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  48071. EVP_CIPHER_CTX_init(&de[i]);
  48072. if (i == 0) {
  48073. /* Default uses 96-bits IV length */
  48074. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  48075. iv));
  48076. }
  48077. else {
  48078. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  48079. NULL));
  48080. /* non-default must to set the IV length first */
  48081. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  48082. ivSz, NULL));
  48083. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  48084. }
  48085. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48086. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48087. ciphertxtSz));
  48088. decryptedtxtSz = len;
  48089. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48090. SM4_BLOCK_SIZE, tag));
  48091. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48092. decryptedtxtSz += len;
  48093. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  48094. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  48095. /* modify tag*/
  48096. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  48097. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  48098. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  48099. SM4_BLOCK_SIZE, tag));
  48100. /* fail due to wrong tag */
  48101. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  48102. ciphertxtSz));
  48103. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  48104. ExpectIntEQ(0, len);
  48105. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  48106. }
  48107. res = EXPECT_RESULT();
  48108. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  48109. return res;
  48110. }
  48111. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  48112. {
  48113. EXPECT_DECLS;
  48114. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  48115. EVP_PKEY_CTX* ctx = NULL;
  48116. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  48117. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  48118. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  48119. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  48120. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  48121. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  48122. byte outKey[34];
  48123. size_t outKeySz = sizeof(outKey);
  48124. /* These expected outputs were gathered by running the same test below using
  48125. * OpenSSL. */
  48126. const byte extractAndExpand[] = {
  48127. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  48128. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  48129. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  48130. };
  48131. const byte extractOnly[] = {
  48132. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  48133. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  48134. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  48135. };
  48136. const byte expandOnly[] = {
  48137. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  48138. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  48139. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  48140. };
  48141. const byte extractAndExpandAddInfo[] = {
  48142. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  48143. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  48144. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  48145. };
  48146. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  48147. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  48148. /* NULL ctx. */
  48149. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  48150. /* NULL md. */
  48151. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  48152. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  48153. /* NULL ctx. */
  48154. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  48155. WOLFSSL_FAILURE);
  48156. /* NULL salt is ok. */
  48157. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  48158. WOLFSSL_SUCCESS);
  48159. /* Salt length <= 0. */
  48160. /* Length 0 salt is ok. */
  48161. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  48162. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  48163. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  48164. WOLFSSL_SUCCESS);
  48165. /* NULL ctx. */
  48166. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  48167. WOLFSSL_FAILURE);
  48168. /* NULL key. */
  48169. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  48170. WOLFSSL_FAILURE);
  48171. /* Key length <= 0 */
  48172. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  48173. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  48174. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  48175. WOLFSSL_SUCCESS);
  48176. /* NULL ctx. */
  48177. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  48178. WOLFSSL_FAILURE);
  48179. /* NULL info is ok. */
  48180. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  48181. WOLFSSL_SUCCESS);
  48182. /* Info length <= 0 */
  48183. /* Length 0 info is ok. */
  48184. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  48185. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  48186. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  48187. WOLFSSL_SUCCESS);
  48188. /* NULL ctx. */
  48189. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48190. WOLFSSL_FAILURE);
  48191. /* Extract and expand (default). */
  48192. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48193. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  48194. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  48195. /* Extract only. */
  48196. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  48197. WOLFSSL_SUCCESS);
  48198. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48199. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  48200. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  48201. outKeySz = sizeof(outKey);
  48202. /* Expand only. */
  48203. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  48204. WOLFSSL_SUCCESS);
  48205. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48206. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  48207. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  48208. outKeySz = sizeof(outKey);
  48209. /* Extract and expand with appended additional info. */
  48210. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  48211. WOLFSSL_SUCCESS);
  48212. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  48213. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  48214. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  48215. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  48216. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  48217. EVP_PKEY_CTX_free(ctx);
  48218. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  48219. return EXPECT_RESULT();
  48220. }
  48221. #ifndef NO_BIO
  48222. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  48223. {
  48224. EXPECT_DECLS;
  48225. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48226. BIO* bio = NULL;
  48227. X509_INFO* info = NULL;
  48228. STACK_OF(X509_INFO)* sk = NULL;
  48229. char* subject = NULL;
  48230. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  48231. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48232. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  48233. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  48234. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  48235. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  48236. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  48237. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  48238. /* using dereference to maintain testing for Apache port*/
  48239. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  48240. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  48241. 0, 0));
  48242. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  48243. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48244. X509_INFO_free(info);
  48245. info = NULL;
  48246. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  48247. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  48248. 0, 0));
  48249. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  48250. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  48251. X509_INFO_free(info);
  48252. ExpectNull(info = sk_X509_INFO_pop(sk));
  48253. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  48254. BIO_free(bio);
  48255. #endif
  48256. return EXPECT_RESULT();
  48257. }
  48258. #endif /* !NO_BIO */
  48259. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  48260. {
  48261. EXPECT_DECLS;
  48262. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48263. X509 *x509 = NULL;
  48264. X509_NAME* name = NULL;
  48265. int idx = 0;
  48266. X509_NAME_ENTRY *ne = NULL;
  48267. ASN1_OBJECT *object = NULL;
  48268. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  48269. WOLFSSL_FILETYPE_PEM));
  48270. ExpectNotNull(name = X509_get_subject_name(x509));
  48271. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  48272. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  48273. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  48274. X509_free(x509);
  48275. #endif
  48276. return EXPECT_RESULT();
  48277. }
  48278. static int test_wolfSSL_X509_STORE_get1_certs(void)
  48279. {
  48280. EXPECT_DECLS;
  48281. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  48282. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  48283. X509_STORE_CTX *storeCtx = NULL;
  48284. X509_STORE *store = NULL;
  48285. X509 *caX509 = NULL;
  48286. X509 *svrX509 = NULL;
  48287. X509_NAME *subject = NULL;
  48288. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  48289. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  48290. SSL_FILETYPE_PEM));
  48291. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  48292. SSL_FILETYPE_PEM)));
  48293. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  48294. ExpectNotNull(store = X509_STORE_new());
  48295. ExpectNotNull(subject = X509_get_subject_name(caX509));
  48296. /* Errors */
  48297. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  48298. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  48299. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  48300. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  48301. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  48302. SSL_SUCCESS);
  48303. /* Should find the cert */
  48304. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  48305. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  48306. sk_X509_pop_free(certs, NULL);
  48307. certs = NULL;
  48308. /* Should not find the cert */
  48309. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  48310. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  48311. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  48312. sk_X509_pop_free(certs, NULL);
  48313. certs = NULL;
  48314. X509_STORE_free(store);
  48315. X509_STORE_CTX_free(storeCtx);
  48316. X509_free(svrX509);
  48317. X509_free(caX509);
  48318. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  48319. return EXPECT_RESULT();
  48320. }
  48321. static int test_wolfSSL_dup_CA_list(void)
  48322. {
  48323. int res = TEST_SKIPPED;
  48324. #if defined(OPENSSL_ALL)
  48325. EXPECT_DECLS;
  48326. STACK_OF(X509_NAME) *originalStack = NULL;
  48327. STACK_OF(X509_NAME) *copyStack = NULL;
  48328. int originalCount = 0;
  48329. int copyCount = 0;
  48330. X509_NAME *name = NULL;
  48331. int i;
  48332. originalStack = sk_X509_NAME_new_null();
  48333. ExpectNotNull(originalStack);
  48334. for (i = 0; i < 3; i++) {
  48335. name = X509_NAME_new();
  48336. ExpectNotNull(name);
  48337. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  48338. }
  48339. copyStack = SSL_dup_CA_list(originalStack);
  48340. ExpectNotNull(copyStack);
  48341. originalCount = sk_X509_NAME_num(originalStack);
  48342. copyCount = sk_X509_NAME_num(copyStack);
  48343. AssertIntEQ(originalCount, copyCount);
  48344. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  48345. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  48346. originalStack = NULL;
  48347. copyStack = NULL;
  48348. res = EXPECT_RESULT();
  48349. #endif /* OPENSSL_ALL */
  48350. return res;
  48351. }
  48352. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  48353. * have default (hidden) visibility, and in the absence of visibility, it's
  48354. * benign to mask out the library implementation.
  48355. */
  48356. #define WOLFSSL_MISC_INCLUDED
  48357. #include <wolfcrypt/src/misc.c>
  48358. static int test_ForceZero(void)
  48359. {
  48360. EXPECT_DECLS;
  48361. unsigned char data[32];
  48362. unsigned int i, j, len;
  48363. /* Test case with 0 length */
  48364. ForceZero(data, 0);
  48365. /* Test ForceZero */
  48366. for (i = 0; i < sizeof(data); i++) {
  48367. for (len = 1; len < sizeof(data) - i; len++) {
  48368. for (j = 0; j < sizeof(data); j++)
  48369. data[j] = j + 1;
  48370. ForceZero(data + i, len);
  48371. for (j = 0; j < sizeof(data); j++) {
  48372. if (j < i || j >= i + len) {
  48373. ExpectIntNE(data[j], 0x00);
  48374. }
  48375. else {
  48376. ExpectIntEQ(data[j], 0x00);
  48377. }
  48378. }
  48379. }
  48380. }
  48381. return EXPECT_RESULT();
  48382. }
  48383. #ifndef NO_BIO
  48384. static int test_wolfSSL_X509_print(void)
  48385. {
  48386. EXPECT_DECLS;
  48387. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  48388. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  48389. X509 *x509 = NULL;
  48390. BIO *bio = NULL;
  48391. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  48392. const X509_ALGOR *cert_sig_alg;
  48393. #endif
  48394. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  48395. WOLFSSL_FILETYPE_PEM));
  48396. /* print to memory */
  48397. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48398. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  48399. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  48400. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  48401. /* Will print IP address subject alt name. */
  48402. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  48403. #elif defined(NO_ASN_TIME)
  48404. /* Will print IP address subject alt name but not Validity. */
  48405. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  48406. #else
  48407. /* Will print IP address subject alt name. */
  48408. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  48409. #endif
  48410. #elif defined(NO_ASN_TIME)
  48411. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  48412. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  48413. #else
  48414. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  48415. #endif
  48416. BIO_free(bio);
  48417. bio = NULL;
  48418. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  48419. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  48420. /* Print signature */
  48421. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  48422. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  48423. #endif
  48424. /* print to stderr */
  48425. #if !defined(NO_WOLFSSL_DIR)
  48426. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  48427. #endif
  48428. /* print again */
  48429. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  48430. X509_free(x509);
  48431. BIO_free(bio);
  48432. #endif
  48433. return EXPECT_RESULT();
  48434. }
  48435. static int test_wolfSSL_X509_CRL_print(void)
  48436. {
  48437. EXPECT_DECLS;
  48438. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  48439. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  48440. X509_CRL* crl = NULL;
  48441. BIO *bio = NULL;
  48442. XFILE fp = XBADFILE;
  48443. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  48444. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48445. NULL, NULL));
  48446. if (fp != XBADFILE)
  48447. XFCLOSE(fp);
  48448. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48449. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  48450. X509_CRL_free(crl);
  48451. BIO_free(bio);
  48452. #endif
  48453. return EXPECT_RESULT();
  48454. }
  48455. static int test_wolfSSL_BIO_get_len(void)
  48456. {
  48457. EXPECT_DECLS;
  48458. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  48459. BIO *bio = NULL;
  48460. const char txt[] = "Some example text to push to the BIO.";
  48461. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  48462. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  48463. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  48464. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  48465. BIO_free(bio);
  48466. bio = NULL;
  48467. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  48468. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  48469. BIO_free(bio);
  48470. #endif
  48471. return EXPECT_RESULT();
  48472. }
  48473. #endif /* !NO_BIO */
  48474. static int test_wolfSSL_RSA(void)
  48475. {
  48476. EXPECT_DECLS;
  48477. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  48478. defined(WOLFSSL_KEY_GEN)
  48479. RSA* rsa = NULL;
  48480. const BIGNUM *n;
  48481. const BIGNUM *e;
  48482. const BIGNUM *d;
  48483. const BIGNUM *p;
  48484. const BIGNUM *q;
  48485. const BIGNUM *dmp1;
  48486. const BIGNUM *dmq1;
  48487. const BIGNUM *iqmp;
  48488. ExpectNotNull(rsa = RSA_new());
  48489. ExpectIntEQ(RSA_size(NULL), 0);
  48490. ExpectIntEQ(RSA_size(rsa), 0);
  48491. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  48492. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  48493. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  48494. #ifdef WOLFSSL_RSA_KEY_CHECK
  48495. ExpectIntEQ(RSA_check_key(rsa), 0);
  48496. #endif
  48497. RSA_free(rsa);
  48498. rsa = NULL;
  48499. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48500. ExpectIntEQ(RSA_size(rsa), 256);
  48501. #ifdef WOLFSSL_RSA_KEY_CHECK
  48502. ExpectIntEQ(RSA_check_key(NULL), 0);
  48503. ExpectIntEQ(RSA_check_key(rsa), 1);
  48504. #endif
  48505. /* sanity check */
  48506. ExpectIntEQ(RSA_bits(NULL), 0);
  48507. /* key */
  48508. ExpectIntEQ(RSA_bits(rsa), 2048);
  48509. RSA_get0_key(rsa, &n, &e, &d);
  48510. ExpectPtrEq(rsa->n, n);
  48511. ExpectPtrEq(rsa->e, e);
  48512. ExpectPtrEq(rsa->d, d);
  48513. n = NULL;
  48514. e = NULL;
  48515. d = NULL;
  48516. ExpectNotNull(n = BN_new());
  48517. ExpectNotNull(e = BN_new());
  48518. ExpectNotNull(d = BN_new());
  48519. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  48520. if (EXPECT_FAIL()) {
  48521. BN_free((BIGNUM*)n);
  48522. BN_free((BIGNUM*)e);
  48523. BN_free((BIGNUM*)d);
  48524. }
  48525. ExpectPtrEq(rsa->n, n);
  48526. ExpectPtrEq(rsa->e, e);
  48527. ExpectPtrEq(rsa->d, d);
  48528. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  48529. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  48530. /* crt_params */
  48531. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  48532. ExpectPtrEq(rsa->dmp1, dmp1);
  48533. ExpectPtrEq(rsa->dmq1, dmq1);
  48534. ExpectPtrEq(rsa->iqmp, iqmp);
  48535. dmp1 = NULL;
  48536. dmq1 = NULL;
  48537. iqmp = NULL;
  48538. ExpectNotNull(dmp1 = BN_new());
  48539. ExpectNotNull(dmq1 = BN_new());
  48540. ExpectNotNull(iqmp = BN_new());
  48541. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  48542. (BIGNUM*)iqmp), 1);
  48543. if (EXPECT_FAIL()) {
  48544. BN_free((BIGNUM*)dmp1);
  48545. BN_free((BIGNUM*)dmq1);
  48546. BN_free((BIGNUM*)iqmp);
  48547. }
  48548. ExpectPtrEq(rsa->dmp1, dmp1);
  48549. ExpectPtrEq(rsa->dmq1, dmq1);
  48550. ExpectPtrEq(rsa->iqmp, iqmp);
  48551. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  48552. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  48553. (BIGNUM*)iqmp), 0);
  48554. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  48555. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  48556. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  48557. ExpectNull(dmp1);
  48558. ExpectNull(dmq1);
  48559. ExpectNull(iqmp);
  48560. /* factors */
  48561. RSA_get0_factors(rsa, NULL, NULL);
  48562. RSA_get0_factors(rsa, &p, &q);
  48563. ExpectPtrEq(rsa->p, p);
  48564. ExpectPtrEq(rsa->q, q);
  48565. p = NULL;
  48566. q = NULL;
  48567. ExpectNotNull(p = BN_new());
  48568. ExpectNotNull(q = BN_new());
  48569. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  48570. if (EXPECT_FAIL()) {
  48571. BN_free((BIGNUM*)p);
  48572. BN_free((BIGNUM*)q);
  48573. }
  48574. ExpectPtrEq(rsa->p, p);
  48575. ExpectPtrEq(rsa->q, q);
  48576. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  48577. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  48578. RSA_get0_factors(NULL, NULL, NULL);
  48579. RSA_get0_factors(NULL, &p, &q);
  48580. ExpectNull(p);
  48581. ExpectNull(q);
  48582. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  48583. ExpectIntEQ(RSA_bits(rsa), 21);
  48584. RSA_free(rsa);
  48585. rsa = NULL;
  48586. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  48587. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  48588. ExpectIntEQ(RSA_size(rsa), 384);
  48589. ExpectIntEQ(RSA_bits(rsa), 3072);
  48590. RSA_free(rsa);
  48591. rsa = NULL;
  48592. #endif
  48593. /* remove for now with odd key size until adjusting rsa key size check with
  48594. wc_MakeRsaKey()
  48595. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  48596. RSA_free(rsa);
  48597. rsa = NULL;
  48598. */
  48599. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  48600. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  48601. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  48602. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  48603. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  48604. {
  48605. byte buff[FOURK_BUF];
  48606. byte der[FOURK_BUF];
  48607. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  48608. XFILE f = XBADFILE;
  48609. int bytes;
  48610. /* test loading encrypted RSA private pem w/o password */
  48611. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  48612. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  48613. if (f != XBADFILE)
  48614. XFCLOSE(f);
  48615. XMEMSET(der, 0, sizeof(der));
  48616. /* test that error value is returned with no password */
  48617. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  48618. 0);
  48619. }
  48620. #endif
  48621. #endif
  48622. return EXPECT_RESULT();
  48623. }
  48624. static int test_wolfSSL_RSA_DER(void)
  48625. {
  48626. EXPECT_DECLS;
  48627. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48628. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  48629. RSA *rsa = NULL;
  48630. int i;
  48631. const unsigned char *buff = NULL;
  48632. unsigned char *newBuff = NULL;
  48633. struct tbl_s
  48634. {
  48635. const unsigned char *der;
  48636. int sz;
  48637. } tbl[] = {
  48638. #ifdef USE_CERT_BUFFERS_1024
  48639. {client_key_der_1024, sizeof_client_key_der_1024},
  48640. {server_key_der_1024, sizeof_server_key_der_1024},
  48641. #endif
  48642. #ifdef USE_CERT_BUFFERS_2048
  48643. {client_key_der_2048, sizeof_client_key_der_2048},
  48644. {server_key_der_2048, sizeof_server_key_der_2048},
  48645. #endif
  48646. {NULL, 0}
  48647. };
  48648. /* Public Key DER */
  48649. struct tbl_s pub[] = {
  48650. #ifdef USE_CERT_BUFFERS_1024
  48651. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  48652. #endif
  48653. #ifdef USE_CERT_BUFFERS_2048
  48654. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  48655. #endif
  48656. {NULL, 0}
  48657. };
  48658. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  48659. buff = pub[0].der;
  48660. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  48661. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  48662. buff = tbl[0].der;
  48663. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  48664. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  48665. rsa = RSA_new();
  48666. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  48667. RSA_free(rsa);
  48668. rsa = NULL;
  48669. for (i = 0; tbl[i].der != NULL; i++)
  48670. {
  48671. /* Passing in pointer results in pointer moving. */
  48672. buff = tbl[i].der;
  48673. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  48674. ExpectNotNull(rsa);
  48675. RSA_free(rsa);
  48676. rsa = NULL;
  48677. }
  48678. for (i = 0; tbl[i].der != NULL; i++)
  48679. {
  48680. /* Passing in pointer results in pointer moving. */
  48681. buff = tbl[i].der;
  48682. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  48683. ExpectNotNull(rsa);
  48684. RSA_free(rsa);
  48685. rsa = NULL;
  48686. }
  48687. for (i = 0; pub[i].der != NULL; i++)
  48688. {
  48689. buff = pub[i].der;
  48690. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  48691. ExpectNotNull(rsa);
  48692. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  48693. newBuff = NULL;
  48694. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  48695. ExpectNotNull(newBuff);
  48696. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  48697. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48698. RSA_free(rsa);
  48699. rsa = NULL;
  48700. }
  48701. #endif
  48702. return EXPECT_RESULT();
  48703. }
  48704. static int test_wolfSSL_RSA_print(void)
  48705. {
  48706. EXPECT_DECLS;
  48707. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  48708. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48709. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  48710. BIO *bio = NULL;
  48711. WOLFSSL_RSA* rsa = NULL;
  48712. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  48713. ExpectNotNull(rsa = RSA_new());
  48714. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  48715. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  48716. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  48717. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  48718. /* Some very large number of indent spaces. */
  48719. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  48720. /* RSA is empty. */
  48721. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  48722. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  48723. RSA_free(rsa);
  48724. rsa = NULL;
  48725. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48726. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  48727. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  48728. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  48729. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  48730. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  48731. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  48732. BIO_free(bio);
  48733. RSA_free(rsa);
  48734. #endif
  48735. return EXPECT_RESULT();
  48736. }
  48737. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  48738. {
  48739. EXPECT_DECLS;
  48740. #ifndef NO_RSA
  48741. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  48742. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48743. RSA *rsa = NULL;
  48744. const unsigned char *derBuf = client_key_der_2048;
  48745. unsigned char em[256] = {0}; /* len = 2048/8 */
  48746. /* Random data simulating a hash */
  48747. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  48748. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  48749. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  48750. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  48751. };
  48752. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  48753. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  48754. RSA_PSS_SALTLEN_DIGEST), 0);
  48755. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  48756. RSA_PSS_SALTLEN_DIGEST), 0);
  48757. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  48758. RSA_PSS_SALTLEN_DIGEST), 0);
  48759. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  48760. RSA_PSS_SALTLEN_DIGEST), 0);
  48761. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  48762. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  48763. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  48764. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  48765. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  48766. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  48767. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  48768. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  48769. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  48770. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  48771. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  48772. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  48773. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  48774. RSA_PSS_SALTLEN_DIGEST), 1);
  48775. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  48776. RSA_PSS_SALTLEN_DIGEST), 1);
  48777. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  48778. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  48779. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  48780. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  48781. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  48782. RSA_PSS_SALTLEN_MAX), 1);
  48783. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  48784. RSA_PSS_SALTLEN_MAX), 1);
  48785. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  48786. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  48787. RSA_free(rsa);
  48788. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48789. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  48790. #endif
  48791. return EXPECT_RESULT();
  48792. }
  48793. static int test_wolfSSL_RSA_sign_sha3(void)
  48794. {
  48795. EXPECT_DECLS;
  48796. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  48797. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  48798. RSA* rsa = NULL;
  48799. const unsigned char *derBuf = client_key_der_2048;
  48800. unsigned char sigRet[256] = {0};
  48801. unsigned int sigLen = sizeof(sigRet);
  48802. /* Random data simulating a hash */
  48803. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  48804. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  48805. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  48806. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  48807. };
  48808. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  48809. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  48810. rsa), 1);
  48811. RSA_free(rsa);
  48812. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  48813. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  48814. return EXPECT_RESULT();
  48815. }
  48816. static int test_wolfSSL_RSA_get0_key(void)
  48817. {
  48818. EXPECT_DECLS;
  48819. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  48820. RSA *rsa = NULL;
  48821. const BIGNUM* n = NULL;
  48822. const BIGNUM* e = NULL;
  48823. const BIGNUM* d = NULL;
  48824. const unsigned char* der;
  48825. int derSz;
  48826. #ifdef USE_CERT_BUFFERS_1024
  48827. der = client_key_der_1024;
  48828. derSz = sizeof_client_key_der_1024;
  48829. #elif defined(USE_CERT_BUFFERS_2048)
  48830. der = client_key_der_2048;
  48831. derSz = sizeof_client_key_der_2048;
  48832. #else
  48833. der = NULL;
  48834. derSz = 0;
  48835. #endif
  48836. if (der != NULL) {
  48837. RSA_get0_key(NULL, NULL, NULL, NULL);
  48838. RSA_get0_key(rsa, NULL, NULL, NULL);
  48839. RSA_get0_key(NULL, &n, &e, &d);
  48840. ExpectNull(n);
  48841. ExpectNull(e);
  48842. ExpectNull(d);
  48843. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  48844. ExpectNotNull(rsa);
  48845. RSA_get0_key(rsa, NULL, NULL, NULL);
  48846. RSA_get0_key(rsa, &n, NULL, NULL);
  48847. ExpectNotNull(n);
  48848. RSA_get0_key(rsa, NULL, &e, NULL);
  48849. ExpectNotNull(e);
  48850. RSA_get0_key(rsa, NULL, NULL, &d);
  48851. ExpectNotNull(d);
  48852. RSA_get0_key(rsa, &n, &e, &d);
  48853. ExpectNotNull(n);
  48854. ExpectNotNull(e);
  48855. ExpectNotNull(d);
  48856. RSA_free(rsa);
  48857. }
  48858. #endif
  48859. return EXPECT_RESULT();
  48860. }
  48861. static int test_wolfSSL_RSA_meth(void)
  48862. {
  48863. EXPECT_DECLS;
  48864. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  48865. RSA *rsa = NULL;
  48866. RSA_METHOD *rsa_meth = NULL;
  48867. #ifdef WOLFSSL_KEY_GEN
  48868. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48869. RSA_free(rsa);
  48870. rsa = NULL;
  48871. #else
  48872. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48873. #endif
  48874. ExpectNotNull(RSA_get_default_method());
  48875. wolfSSL_RSA_meth_free(NULL);
  48876. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  48877. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  48878. RSA_METHOD_FLAG_NO_CHECK));
  48879. #ifndef NO_WOLFSSL_STUB
  48880. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  48881. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  48882. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  48883. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  48884. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  48885. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  48886. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  48887. #endif
  48888. ExpectIntEQ(RSA_flags(NULL), 0);
  48889. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  48890. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  48891. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  48892. ExpectNotNull(rsa = RSA_new());
  48893. /* No method set. */
  48894. ExpectIntEQ(RSA_flags(rsa), 0);
  48895. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  48896. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  48897. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  48898. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  48899. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  48900. if (EXPECT_FAIL()) {
  48901. wolfSSL_RSA_meth_free(rsa_meth);
  48902. }
  48903. ExpectNull(RSA_get_method(NULL));
  48904. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  48905. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  48906. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  48907. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  48908. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  48909. RSA_METHOD_FLAG_NO_CHECK);
  48910. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  48911. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  48912. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  48913. /* rsa_meth is freed here */
  48914. RSA_free(rsa);
  48915. #endif
  48916. return EXPECT_RESULT();
  48917. }
  48918. static int test_wolfSSL_RSA_verify(void)
  48919. {
  48920. EXPECT_DECLS;
  48921. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  48922. !defined(NO_FILESYSTEM)
  48923. #ifndef NO_BIO
  48924. XFILE fp = XBADFILE;
  48925. RSA *pKey = NULL;
  48926. RSA *pubKey = NULL;
  48927. X509 *cert = NULL;
  48928. const char *text = "Hello wolfSSL !";
  48929. unsigned char hash[SHA256_DIGEST_LENGTH];
  48930. unsigned char signature[2048/8];
  48931. unsigned int signatureLength;
  48932. byte *buf = NULL;
  48933. BIO *bio = NULL;
  48934. SHA256_CTX c;
  48935. EVP_PKEY *evpPkey = NULL;
  48936. EVP_PKEY *evpPubkey = NULL;
  48937. size_t sz;
  48938. /* generate hash */
  48939. SHA256_Init(&c);
  48940. SHA256_Update(&c, text, strlen(text));
  48941. SHA256_Final(hash, &c);
  48942. #ifdef WOLFSSL_SMALL_STACK_CACHE
  48943. /* workaround for small stack cache case */
  48944. wc_Sha256Free((wc_Sha256*)&c);
  48945. #endif
  48946. /* read privete key file */
  48947. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  48948. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  48949. ExpectTrue((sz = XFTELL(fp)) > 0);
  48950. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  48951. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  48952. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  48953. if (fp != XBADFILE) {
  48954. XFCLOSE(fp);
  48955. fp = XBADFILE;
  48956. }
  48957. /* read private key and sign hash data */
  48958. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  48959. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  48960. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  48961. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  48962. signature, &signatureLength, pKey), SSL_SUCCESS);
  48963. /* read public key and verify signed data */
  48964. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  48965. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  48966. if (fp != XBADFILE)
  48967. XFCLOSE(fp);
  48968. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  48969. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  48970. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  48971. signatureLength, pubKey), SSL_SUCCESS);
  48972. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  48973. signatureLength, NULL), SSL_FAILURE);
  48974. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  48975. signatureLength, pubKey), SSL_FAILURE);
  48976. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  48977. signatureLength, pubKey), SSL_FAILURE);
  48978. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  48979. signatureLength, NULL), SSL_FAILURE);
  48980. RSA_free(pKey);
  48981. EVP_PKEY_free(evpPkey);
  48982. RSA_free(pubKey);
  48983. EVP_PKEY_free(evpPubkey);
  48984. X509_free(cert);
  48985. BIO_free(bio);
  48986. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  48987. #endif
  48988. #endif
  48989. return EXPECT_RESULT();
  48990. }
  48991. static int test_wolfSSL_RSA_sign(void)
  48992. {
  48993. EXPECT_DECLS;
  48994. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  48995. RSA *rsa;
  48996. unsigned char hash[SHA256_DIGEST_LENGTH];
  48997. #ifdef USE_CERT_BUFFERS_1024
  48998. const unsigned char* privDer = client_key_der_1024;
  48999. size_t privDerSz = sizeof_client_key_der_1024;
  49000. const unsigned char* pubDer = client_keypub_der_1024;
  49001. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49002. unsigned char signature[1024/8];
  49003. #else
  49004. const unsigned char* privDer = client_key_der_2048;
  49005. size_t privDerSz = sizeof_client_key_der_2048;
  49006. const unsigned char* pubDer = client_keypub_der_2048;
  49007. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49008. unsigned char signature[2048/8];
  49009. #endif
  49010. unsigned int signatureLen;
  49011. const unsigned char* der;
  49012. XMEMSET(hash, 0, sizeof(hash));
  49013. der = privDer;
  49014. rsa = NULL;
  49015. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49016. /* Invalid parameters. */
  49017. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  49018. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  49019. &signatureLen, rsa), 0);
  49020. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  49021. &signatureLen, rsa), 0);
  49022. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  49023. &signatureLen, rsa), 0);
  49024. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49025. NULL, rsa), 0);
  49026. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49027. &signatureLen, NULL), 0);
  49028. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  49029. &signatureLen, rsa), 1);
  49030. RSA_free(rsa);
  49031. der = pubDer;
  49032. rsa = NULL;
  49033. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49034. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49035. signatureLen, rsa), 1);
  49036. RSA_free(rsa);
  49037. #endif
  49038. return EXPECT_RESULT();
  49039. }
  49040. static int test_wolfSSL_RSA_sign_ex(void)
  49041. {
  49042. EXPECT_DECLS;
  49043. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49044. RSA *rsa = NULL;
  49045. unsigned char hash[SHA256_DIGEST_LENGTH];
  49046. #ifdef USE_CERT_BUFFERS_1024
  49047. const unsigned char* privDer = client_key_der_1024;
  49048. size_t privDerSz = sizeof_client_key_der_1024;
  49049. const unsigned char* pubDer = client_keypub_der_1024;
  49050. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49051. unsigned char signature[1024/8];
  49052. #else
  49053. const unsigned char* privDer = client_key_der_2048;
  49054. size_t privDerSz = sizeof_client_key_der_2048;
  49055. const unsigned char* pubDer = client_keypub_der_2048;
  49056. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49057. unsigned char signature[2048/8];
  49058. #endif
  49059. unsigned int signatureLen;
  49060. const unsigned char* der;
  49061. unsigned char encodedHash[51];
  49062. unsigned int encodedHashLen;
  49063. const unsigned char expEncHash[] = {
  49064. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  49065. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  49066. 0x00, 0x04, 0x20,
  49067. /* Hash data */
  49068. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49069. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49070. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49071. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49072. };
  49073. XMEMSET(hash, 0, sizeof(hash));
  49074. ExpectNotNull(rsa = wolfSSL_RSA_new());
  49075. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49076. &signatureLen, rsa, 1), 0);
  49077. wolfSSL_RSA_free(rsa);
  49078. der = privDer;
  49079. rsa = NULL;
  49080. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49081. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  49082. -1), 0);
  49083. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  49084. signature, &signatureLen, rsa, 1), 0);
  49085. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49086. &signatureLen, rsa, 1), 0);
  49087. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49088. &signatureLen, rsa, 1), 0);
  49089. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49090. NULL, rsa, 1), 0);
  49091. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49092. &signatureLen, NULL, 1), 0);
  49093. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49094. &signatureLen, rsa, -1), 0);
  49095. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  49096. &signatureLen, rsa, 0), 0);
  49097. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  49098. &signatureLen, rsa, 0), 0);
  49099. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49100. NULL, rsa, 0), 0);
  49101. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  49102. &signatureLen, rsa, 1), 1);
  49103. /* Test returning encoded hash. */
  49104. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  49105. &encodedHashLen, rsa, 0), 1);
  49106. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  49107. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  49108. RSA_free(rsa);
  49109. der = pubDer;
  49110. rsa = NULL;
  49111. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49112. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  49113. signatureLen, rsa), 1);
  49114. RSA_free(rsa);
  49115. #endif
  49116. return EXPECT_RESULT();
  49117. }
  49118. static int test_wolfSSL_RSA_public_decrypt(void)
  49119. {
  49120. EXPECT_DECLS;
  49121. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49122. RSA *rsa;
  49123. unsigned char msg[SHA256_DIGEST_LENGTH];
  49124. #ifdef USE_CERT_BUFFERS_1024
  49125. const unsigned char* pubDer = client_keypub_der_1024;
  49126. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49127. unsigned char decMsg[1024/8];
  49128. const unsigned char encMsg[] = {
  49129. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49130. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49131. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49132. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49133. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49134. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49135. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49136. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49137. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49138. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49139. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49140. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49141. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49142. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49143. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49144. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49145. };
  49146. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49147. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49148. defined(WC_RSA_NO_PADDING)
  49149. const unsigned char encMsgNoPad[] = {
  49150. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49151. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49152. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49153. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49154. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49155. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49156. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49157. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49158. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49159. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49160. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49161. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49162. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49163. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49164. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49165. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49166. };
  49167. #endif
  49168. #else
  49169. const unsigned char* pubDer = client_keypub_der_2048;
  49170. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49171. unsigned char decMsg[2048/8];
  49172. const unsigned char encMsg[] = {
  49173. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49174. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49175. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49176. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49177. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49178. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49179. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49180. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49181. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49182. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49183. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49184. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49185. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49186. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49187. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49188. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49189. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49190. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49191. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49192. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49193. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49194. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49195. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49196. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49197. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49198. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49199. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49200. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49201. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49202. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49203. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49204. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49205. };
  49206. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49207. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49208. defined(WC_RSA_NO_PADDING)
  49209. const unsigned char encMsgNoPad[] = {
  49210. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49211. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49212. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49213. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49214. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49215. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49216. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49217. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49218. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49219. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49220. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49221. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49222. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  49223. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  49224. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  49225. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  49226. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  49227. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  49228. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  49229. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  49230. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  49231. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  49232. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  49233. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  49234. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  49235. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  49236. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  49237. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  49238. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  49239. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  49240. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  49241. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  49242. };
  49243. #endif
  49244. #endif
  49245. const unsigned char* der;
  49246. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49247. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49248. defined(WC_RSA_NO_PADDING)
  49249. int i;
  49250. #endif
  49251. XMEMSET(msg, 0, sizeof(msg));
  49252. der = pubDer;
  49253. rsa = NULL;
  49254. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49255. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  49256. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  49257. RSA_PKCS1_PADDING), -1);
  49258. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  49259. RSA_PKCS1_PADDING), -1);
  49260. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  49261. RSA_PKCS1_PADDING), -1);
  49262. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  49263. RSA_PKCS1_PADDING), -1);
  49264. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  49265. RSA_PKCS1_PSS_PADDING), -1);
  49266. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  49267. RSA_PKCS1_PADDING), 32);
  49268. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  49269. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  49270. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  49271. defined(WC_RSA_NO_PADDING)
  49272. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  49273. rsa, RSA_NO_PADDING), sizeof(decMsg));
  49274. /* Zeros before actual data. */
  49275. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  49276. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  49277. }
  49278. /* Check actual data. */
  49279. XMEMSET(msg, 0x01, sizeof(msg));
  49280. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  49281. #endif
  49282. RSA_free(rsa);
  49283. #endif
  49284. return EXPECT_RESULT();
  49285. }
  49286. static int test_wolfSSL_RSA_private_encrypt(void)
  49287. {
  49288. EXPECT_DECLS;
  49289. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49290. RSA *rsa;
  49291. unsigned char msg[SHA256_DIGEST_LENGTH];
  49292. #ifdef USE_CERT_BUFFERS_1024
  49293. const unsigned char* privDer = client_key_der_1024;
  49294. size_t privDerSz = sizeof_client_key_der_1024;
  49295. unsigned char encMsg[1024/8];
  49296. const unsigned char expEncMsg[] = {
  49297. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  49298. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  49299. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  49300. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  49301. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  49302. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  49303. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  49304. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  49305. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  49306. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  49307. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  49308. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  49309. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  49310. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  49311. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  49312. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  49313. };
  49314. #ifdef WC_RSA_NO_PADDING
  49315. const unsigned char expEncMsgNoPad[] = {
  49316. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  49317. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  49318. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  49319. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  49320. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  49321. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  49322. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  49323. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  49324. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  49325. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  49326. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  49327. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  49328. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  49329. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  49330. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  49331. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  49332. };
  49333. #endif
  49334. #else
  49335. const unsigned char* privDer = client_key_der_2048;
  49336. size_t privDerSz = sizeof_client_key_der_2048;
  49337. unsigned char encMsg[2048/8];
  49338. const unsigned char expEncMsg[] = {
  49339. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  49340. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  49341. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  49342. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  49343. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  49344. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  49345. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  49346. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  49347. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  49348. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  49349. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  49350. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  49351. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  49352. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  49353. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  49354. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  49355. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  49356. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  49357. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  49358. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  49359. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  49360. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  49361. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  49362. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  49363. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  49364. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  49365. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  49366. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  49367. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  49368. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  49369. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  49370. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  49371. };
  49372. #ifdef WC_RSA_NO_PADDING
  49373. const unsigned char expEncMsgNoPad[] = {
  49374. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  49375. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  49376. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  49377. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  49378. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  49379. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  49380. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  49381. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  49382. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  49383. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  49384. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  49385. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  49386. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  49387. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  49388. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  49389. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  49390. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  49391. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  49392. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  49393. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  49394. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  49395. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  49396. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  49397. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  49398. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  49399. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  49400. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  49401. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  49402. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  49403. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  49404. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  49405. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  49406. };
  49407. #endif
  49408. #endif
  49409. const unsigned char* der;
  49410. XMEMSET(msg, 0x00, sizeof(msg));
  49411. der = privDer;
  49412. rsa = NULL;
  49413. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49414. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  49415. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  49416. -1);
  49417. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  49418. RSA_PKCS1_PADDING), -1);
  49419. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  49420. RSA_PKCS1_PADDING), -1);
  49421. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  49422. RSA_PKCS1_PADDING), -1);
  49423. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  49424. RSA_PKCS1_PSS_PADDING), -1);
  49425. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  49426. RSA_PKCS1_PADDING), sizeof(encMsg));
  49427. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  49428. #ifdef WC_RSA_NO_PADDING
  49429. /* Non-zero message. */
  49430. XMEMSET(msg, 0x01, sizeof(msg));
  49431. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  49432. RSA_NO_PADDING), sizeof(encMsg));
  49433. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  49434. #endif
  49435. RSA_free(rsa);
  49436. #endif
  49437. return EXPECT_RESULT();
  49438. }
  49439. static int test_wolfSSL_RSA_public_encrypt(void)
  49440. {
  49441. EXPECT_DECLS;
  49442. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49443. RSA* rsa = NULL;
  49444. const unsigned char msg[2048/8] = { 0 };
  49445. unsigned char encMsg[2048/8];
  49446. ExpectNotNull(rsa = RSA_new());
  49447. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  49448. RSA_PKCS1_PADDING), -1);
  49449. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  49450. RSA_PKCS1_PADDING), -1);
  49451. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  49452. RSA_PKCS1_PADDING), -1);
  49453. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  49454. RSA_PKCS1_PADDING), -1);
  49455. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  49456. RSA_PKCS1_PSS_PADDING), -1);
  49457. /* Empty RSA key. */
  49458. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  49459. RSA_PKCS1_PADDING), -1);
  49460. RSA_free(rsa);
  49461. #endif
  49462. return EXPECT_RESULT();
  49463. }
  49464. static int test_wolfSSL_RSA_private_decrypt(void)
  49465. {
  49466. EXPECT_DECLS;
  49467. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  49468. RSA* rsa = NULL;
  49469. unsigned char msg[2048/8];
  49470. const unsigned char encMsg[2048/8] = { 0 };
  49471. ExpectNotNull(rsa = RSA_new());
  49472. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  49473. RSA_PKCS1_PADDING), -1);
  49474. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  49475. RSA_PKCS1_PADDING), -1);
  49476. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  49477. RSA_PKCS1_PADDING), -1);
  49478. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  49479. RSA_PKCS1_PADDING), -1);
  49480. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  49481. RSA_PKCS1_PSS_PADDING), -1);
  49482. /* Empty RSA key. */
  49483. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  49484. RSA_PKCS1_PADDING), -1);
  49485. RSA_free(rsa);
  49486. #endif
  49487. return EXPECT_RESULT();
  49488. }
  49489. static int test_wolfSSL_RSA_GenAdd(void)
  49490. {
  49491. EXPECT_DECLS;
  49492. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  49493. RSA *rsa;
  49494. #ifdef USE_CERT_BUFFERS_1024
  49495. const unsigned char* privDer = client_key_der_1024;
  49496. size_t privDerSz = sizeof_client_key_der_1024;
  49497. const unsigned char* pubDer = client_keypub_der_1024;
  49498. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49499. #else
  49500. const unsigned char* privDer = client_key_der_2048;
  49501. size_t privDerSz = sizeof_client_key_der_2048;
  49502. const unsigned char* pubDer = client_keypub_der_2048;
  49503. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49504. #endif
  49505. const unsigned char* der;
  49506. der = privDer;
  49507. rsa = NULL;
  49508. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49509. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  49510. #ifndef RSA_LOW_MEM
  49511. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  49512. #else
  49513. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  49514. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  49515. #endif
  49516. RSA_free(rsa);
  49517. der = pubDer;
  49518. rsa = NULL;
  49519. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49520. /* Need private values. */
  49521. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  49522. RSA_free(rsa);
  49523. #endif
  49524. return EXPECT_RESULT();
  49525. }
  49526. static int test_wolfSSL_RSA_blinding_on(void)
  49527. {
  49528. EXPECT_DECLS;
  49529. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  49530. RSA *rsa;
  49531. WOLFSSL_BN_CTX *bnCtx = NULL;
  49532. #ifdef USE_CERT_BUFFERS_1024
  49533. const unsigned char* privDer = client_key_der_1024;
  49534. size_t privDerSz = sizeof_client_key_der_1024;
  49535. #else
  49536. const unsigned char* privDer = client_key_der_2048;
  49537. size_t privDerSz = sizeof_client_key_der_2048;
  49538. #endif
  49539. const unsigned char* der;
  49540. der = privDer;
  49541. rsa = NULL;
  49542. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49543. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  49544. /* Does nothing so all parameters are valid. */
  49545. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  49546. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  49547. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  49548. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  49549. wolfSSL_BN_CTX_free(bnCtx);
  49550. RSA_free(rsa);
  49551. #endif
  49552. return EXPECT_RESULT();
  49553. }
  49554. static int test_wolfSSL_RSA_ex_data(void)
  49555. {
  49556. EXPECT_DECLS;
  49557. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  49558. RSA* rsa = NULL;
  49559. unsigned char data[1];
  49560. ExpectNotNull(rsa = RSA_new());
  49561. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  49562. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  49563. #ifdef MAX_EX_DATA
  49564. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  49565. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  49566. #endif
  49567. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  49568. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  49569. #ifdef HAVE_EX_DATA
  49570. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  49571. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  49572. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  49573. #else
  49574. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  49575. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  49576. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  49577. #endif
  49578. RSA_free(rsa);
  49579. #endif /* !NO_RSA && OPENSSL_EXTRA */
  49580. return EXPECT_RESULT();
  49581. }
  49582. static int test_wolfSSL_RSA_LoadDer(void)
  49583. {
  49584. EXPECT_DECLS;
  49585. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  49586. defined(OPENSSL_EXTRA_X509_SMALL))
  49587. RSA *rsa = NULL;
  49588. #ifdef USE_CERT_BUFFERS_1024
  49589. const unsigned char* privDer = client_key_der_1024;
  49590. size_t privDerSz = sizeof_client_key_der_1024;
  49591. #else
  49592. const unsigned char* privDer = client_key_der_2048;
  49593. size_t privDerSz = sizeof_client_key_der_2048;
  49594. #endif
  49595. ExpectNotNull(rsa = RSA_new());
  49596. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  49597. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  49598. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  49599. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  49600. RSA_free(rsa);
  49601. #endif /* !NO_RSA && OPENSSL_EXTRA */
  49602. return EXPECT_RESULT();
  49603. }
  49604. /* Local API. */
  49605. static int test_wolfSSL_RSA_To_Der(void)
  49606. {
  49607. EXPECT_DECLS;
  49608. #ifdef WOLFSSL_TEST_STATIC_BUILD
  49609. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  49610. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  49611. RSA* rsa;
  49612. #ifdef USE_CERT_BUFFERS_1024
  49613. const unsigned char* privDer = client_key_der_1024;
  49614. size_t privDerSz = sizeof_client_key_der_1024;
  49615. const unsigned char* pubDer = client_keypub_der_1024;
  49616. size_t pubDerSz = sizeof_client_keypub_der_1024;
  49617. unsigned char out[sizeof(client_key_der_1024)];
  49618. #else
  49619. const unsigned char* privDer = client_key_der_2048;
  49620. size_t privDerSz = sizeof_client_key_der_2048;
  49621. const unsigned char* pubDer = client_keypub_der_2048;
  49622. size_t pubDerSz = sizeof_client_keypub_der_2048;
  49623. unsigned char out[sizeof(client_key_der_2048)];
  49624. #endif
  49625. const unsigned char* der;
  49626. unsigned char* outDer = NULL;
  49627. der = privDer;
  49628. rsa = NULL;
  49629. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49630. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  49631. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  49632. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  49633. outDer = out;
  49634. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  49635. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  49636. outDer = NULL;
  49637. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  49638. ExpectNotNull(outDer);
  49639. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  49640. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49641. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  49642. outDer = out;
  49643. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  49644. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  49645. RSA_free(rsa);
  49646. ExpectNotNull(rsa = RSA_new());
  49647. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  49648. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  49649. RSA_free(rsa);
  49650. der = pubDer;
  49651. rsa = NULL;
  49652. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  49653. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  49654. RSA_free(rsa);
  49655. #endif
  49656. #endif
  49657. return EXPECT_RESULT();
  49658. }
  49659. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  49660. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  49661. {
  49662. EXPECT_DECLS;
  49663. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  49664. XFILE file = XBADFILE;
  49665. const char* fname = "./certs/server-keyPub.pem";
  49666. RSA *rsa = NULL;
  49667. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  49668. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  49669. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  49670. ExpectIntEQ(RSA_size(rsa), 256);
  49671. RSA_free(rsa);
  49672. if (file != XBADFILE)
  49673. XFCLOSE(file);
  49674. #endif
  49675. return EXPECT_RESULT();
  49676. }
  49677. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  49678. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  49679. {
  49680. EXPECT_DECLS;
  49681. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  49682. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  49683. RSA* rsa = NULL;
  49684. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  49685. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  49686. /* Valid but stub so returns 0. */
  49687. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  49688. #endif
  49689. return EXPECT_RESULT();
  49690. }
  49691. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  49692. {
  49693. EXPECT_DECLS;
  49694. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  49695. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  49696. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  49697. RSA* rsa = NULL;
  49698. #ifdef USE_CERT_BUFFERS_1024
  49699. const unsigned char* privDer = client_key_der_1024;
  49700. size_t privDerSz = sizeof_client_key_der_1024;
  49701. #else
  49702. const unsigned char* privDer = client_key_der_2048;
  49703. size_t privDerSz = sizeof_client_key_der_2048;
  49704. #endif
  49705. const unsigned char* der;
  49706. #ifndef NO_AES
  49707. unsigned char passwd[] = "password";
  49708. #endif
  49709. ExpectNotNull(rsa = RSA_new());
  49710. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  49711. NULL, NULL), 0);
  49712. RSA_free(rsa);
  49713. der = privDer;
  49714. rsa = NULL;
  49715. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49716. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  49717. NULL, NULL), 0);
  49718. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  49719. NULL, NULL), 0);
  49720. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  49721. NULL, NULL), 1);
  49722. #ifndef NO_AES
  49723. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  49724. NULL, 0, NULL, NULL), 1);
  49725. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  49726. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  49727. #endif
  49728. RSA_free(rsa);
  49729. #endif
  49730. return EXPECT_RESULT();
  49731. }
  49732. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  49733. {
  49734. EXPECT_DECLS;
  49735. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  49736. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  49737. defined(WOLFSSL_DER_TO_PEM))
  49738. RSA* rsa = NULL;
  49739. #ifdef USE_CERT_BUFFERS_1024
  49740. const unsigned char* privDer = client_key_der_1024;
  49741. size_t privDerSz = sizeof_client_key_der_1024;
  49742. #else
  49743. const unsigned char* privDer = client_key_der_2048;
  49744. size_t privDerSz = sizeof_client_key_der_2048;
  49745. #endif
  49746. const unsigned char* der;
  49747. #ifndef NO_AES
  49748. unsigned char passwd[] = "password";
  49749. #endif
  49750. unsigned char* pem = NULL;
  49751. int plen;
  49752. ExpectNotNull(rsa = RSA_new());
  49753. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  49754. &plen), 0);
  49755. RSA_free(rsa);
  49756. der = privDer;
  49757. rsa = NULL;
  49758. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  49759. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  49760. &plen), 0);
  49761. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  49762. &plen), 0);
  49763. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  49764. NULL), 0);
  49765. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  49766. &plen), 1);
  49767. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  49768. pem = NULL;
  49769. #ifndef NO_AES
  49770. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  49771. NULL, 0, &pem, &plen), 1);
  49772. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  49773. pem = NULL;
  49774. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  49775. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  49776. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  49777. #endif
  49778. RSA_free(rsa);
  49779. #endif
  49780. return EXPECT_RESULT();
  49781. }
  49782. static int test_wolfSSL_DH(void)
  49783. {
  49784. EXPECT_DECLS;
  49785. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  49786. DH *dh = NULL;
  49787. BIGNUM* p;
  49788. BIGNUM* q;
  49789. BIGNUM* g;
  49790. BIGNUM* pub = NULL;
  49791. BIGNUM* priv = NULL;
  49792. #if defined(OPENSSL_ALL)
  49793. #if !defined(HAVE_FIPS) || \
  49794. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  49795. FILE* f = NULL;
  49796. unsigned char buf[268];
  49797. const unsigned char* pt = buf;
  49798. long len = 0;
  49799. dh = NULL;
  49800. XMEMSET(buf, 0, sizeof(buf));
  49801. /* Test 2048 bit parameters */
  49802. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  49803. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  49804. if (f != XBADFILE)
  49805. XFCLOSE(f);
  49806. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  49807. ExpectNotNull(dh->p);
  49808. ExpectNotNull(dh->g);
  49809. ExpectTrue(pt == buf);
  49810. ExpectIntEQ(DH_generate_key(dh), 1);
  49811. ExpectIntEQ(DH_generate_key(dh), 1);
  49812. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  49813. ExpectNotNull(pub = BN_new());
  49814. ExpectIntEQ(BN_set_word(pub, 1), 1);
  49815. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  49816. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  49817. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  49818. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  49819. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  49820. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  49821. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  49822. BN_free(pub);
  49823. pub = NULL;
  49824. DH_get0_pqg(dh, (const BIGNUM**)&p,
  49825. (const BIGNUM**)&q,
  49826. (const BIGNUM**)&g);
  49827. ExpectPtrEq(p, dh->p);
  49828. ExpectPtrEq(q, dh->q);
  49829. ExpectPtrEq(g, dh->g);
  49830. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  49831. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  49832. ExpectPtrEq(pub, dh->pub_key);
  49833. ExpectPtrEq(priv, dh->priv_key);
  49834. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  49835. ExpectPtrEq(pub, dh->pub_key);
  49836. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  49837. ExpectPtrEq(priv, dh->priv_key);
  49838. pub = NULL;
  49839. priv = NULL;
  49840. ExpectNotNull(pub = BN_new());
  49841. ExpectNotNull(priv = BN_new());
  49842. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  49843. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  49844. if (EXPECT_FAIL()) {
  49845. BN_free(pub);
  49846. BN_free(priv);
  49847. }
  49848. pub = NULL;
  49849. priv = NULL;
  49850. ExpectNotNull(pub = BN_new());
  49851. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  49852. if (EXPECT_FAIL()) {
  49853. BN_free(pub);
  49854. }
  49855. ExpectNotNull(priv = BN_new());
  49856. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  49857. if (EXPECT_FAIL()) {
  49858. BN_free(priv);
  49859. }
  49860. ExpectPtrEq(pub, dh->pub_key);
  49861. ExpectPtrEq(priv, dh->priv_key);
  49862. pub = NULL;
  49863. priv = NULL;
  49864. DH_free(dh);
  49865. dh = NULL;
  49866. ExpectNotNull(dh = DH_new());
  49867. p = NULL;
  49868. ExpectNotNull(p = BN_new());
  49869. ExpectIntEQ(BN_set_word(p, 1), 1);
  49870. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  49871. ExpectNotNull(pub = BN_new());
  49872. ExpectNotNull(priv = BN_new());
  49873. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  49874. if (EXPECT_FAIL()) {
  49875. BN_free(pub);
  49876. BN_free(priv);
  49877. }
  49878. pub = NULL;
  49879. priv = NULL;
  49880. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  49881. BN_free(p);
  49882. p = NULL;
  49883. DH_free(dh);
  49884. dh = NULL;
  49885. #ifdef WOLFSSL_KEY_GEN
  49886. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  49887. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  49888. DH_free(dh);
  49889. #endif
  49890. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  49891. #endif /* OPENSSL_ALL */
  49892. (void)dh;
  49893. (void)p;
  49894. (void)q;
  49895. (void)g;
  49896. (void)pub;
  49897. (void)priv;
  49898. ExpectNotNull(dh = wolfSSL_DH_new());
  49899. /* invalid parameters test */
  49900. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  49901. (const BIGNUM**)&q,
  49902. (const BIGNUM**)&g);
  49903. DH_get0_pqg(dh, NULL,
  49904. (const BIGNUM**)&q,
  49905. (const BIGNUM**)&g);
  49906. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  49907. DH_get0_pqg(dh, NULL, NULL, NULL);
  49908. DH_get0_pqg(dh, (const BIGNUM**)&p,
  49909. (const BIGNUM**)&q,
  49910. (const BIGNUM**)&g);
  49911. ExpectPtrEq(p, NULL);
  49912. ExpectPtrEq(q, NULL);
  49913. ExpectPtrEq(g, NULL);
  49914. DH_free(dh);
  49915. dh = NULL;
  49916. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  49917. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  49918. #if defined(OPENSSL_ALL) || \
  49919. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  49920. dh = wolfSSL_DH_new();
  49921. ExpectNotNull(dh);
  49922. p = wolfSSL_BN_new();
  49923. ExpectNotNull(p);
  49924. ExpectIntEQ(BN_set_word(p, 11), 1);
  49925. g = wolfSSL_BN_new();
  49926. ExpectNotNull(g);
  49927. ExpectIntEQ(BN_set_word(g, 2), 1);
  49928. q = wolfSSL_BN_new();
  49929. ExpectNotNull(q);
  49930. ExpectIntEQ(BN_set_word(q, 5), 1);
  49931. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  49932. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  49933. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  49934. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  49935. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  49936. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  49937. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  49938. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  49939. /* Don't need q. */
  49940. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  49941. if (EXPECT_FAIL()) {
  49942. BN_free(p);
  49943. BN_free(g);
  49944. }
  49945. p = NULL;
  49946. g = NULL;
  49947. /* Setting again will free the p and g. */
  49948. wolfSSL_BN_free(q);
  49949. q = NULL;
  49950. DH_free(dh);
  49951. dh = NULL;
  49952. dh = wolfSSL_DH_new();
  49953. ExpectNotNull(dh);
  49954. p = wolfSSL_BN_new();
  49955. ExpectNotNull(p);
  49956. ExpectIntEQ(BN_set_word(p, 11), 1);
  49957. g = wolfSSL_BN_new();
  49958. ExpectNotNull(g);
  49959. ExpectIntEQ(BN_set_word(g, 2), 1);
  49960. q = wolfSSL_BN_new();
  49961. ExpectNotNull(q);
  49962. ExpectIntEQ(BN_set_word(q, 5), 1);
  49963. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  49964. /* p, q and g are now owned by dh - don't free. */
  49965. if (EXPECT_FAIL()) {
  49966. BN_free(p);
  49967. BN_free(q);
  49968. BN_free(g);
  49969. }
  49970. p = NULL;
  49971. q = NULL;
  49972. g = NULL;
  49973. p = wolfSSL_BN_new();
  49974. ExpectNotNull(p);
  49975. ExpectIntEQ(BN_set_word(p, 11), 1);
  49976. g = wolfSSL_BN_new();
  49977. ExpectNotNull(g);
  49978. ExpectIntEQ(BN_set_word(g, 2), 1);
  49979. q = wolfSSL_BN_new();
  49980. ExpectNotNull(q);
  49981. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  49982. if (EXPECT_FAIL()) {
  49983. BN_free(p);
  49984. }
  49985. p = NULL;
  49986. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  49987. if (EXPECT_FAIL()) {
  49988. BN_free(q);
  49989. }
  49990. q = NULL;
  49991. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  49992. if (EXPECT_FAIL()) {
  49993. BN_free(g);
  49994. }
  49995. g = NULL;
  49996. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  49997. /* p, q and g are now owned by dh - don't free. */
  49998. DH_free(dh);
  49999. dh = NULL;
  50000. ExpectIntEQ(DH_generate_key(NULL), 0);
  50001. ExpectNotNull(dh = DH_new());
  50002. ExpectIntEQ(DH_generate_key(dh), 0);
  50003. p = wolfSSL_BN_new();
  50004. ExpectNotNull(p);
  50005. ExpectIntEQ(BN_set_word(p, 0), 1);
  50006. g = wolfSSL_BN_new();
  50007. ExpectNotNull(g);
  50008. ExpectIntEQ(BN_set_word(g, 2), 1);
  50009. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50010. if (EXPECT_FAIL()) {
  50011. BN_free(p);
  50012. BN_free(g);
  50013. }
  50014. p = NULL;
  50015. g = NULL;
  50016. ExpectIntEQ(DH_generate_key(dh), 0);
  50017. DH_free(dh);
  50018. dh = NULL;
  50019. #endif
  50020. #endif
  50021. /* Test DH_up_ref() */
  50022. dh = wolfSSL_DH_new();
  50023. ExpectNotNull(dh);
  50024. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  50025. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  50026. DH_free(dh); /* decrease ref count */
  50027. DH_free(dh); /* free WOLFSSL_DH */
  50028. q = NULL;
  50029. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  50030. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  50031. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  50032. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  50033. #ifdef HAVE_FFDHE_2048
  50034. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50035. DH_free(dh);
  50036. q = NULL;
  50037. #endif
  50038. #ifdef HAVE_FFDHE_3072
  50039. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  50040. DH_free(dh);
  50041. q = NULL;
  50042. #endif
  50043. #ifdef HAVE_FFDHE_4096
  50044. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  50045. DH_free(dh);
  50046. q = NULL;
  50047. #endif
  50048. #else
  50049. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  50050. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  50051. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  50052. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  50053. #endif /* OPENSSL_EXTRA && !NO_DH */
  50054. return EXPECT_RESULT();
  50055. }
  50056. static int test_wolfSSL_DH_dup(void)
  50057. {
  50058. EXPECT_DECLS;
  50059. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  50060. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  50061. defined(OPENSSL_EXTRA)
  50062. DH *dh = NULL;
  50063. DH *dhDup = NULL;
  50064. ExpectNotNull(dh = wolfSSL_DH_new());
  50065. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  50066. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  50067. #if defined(OPENSSL_ALL) || \
  50068. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  50069. {
  50070. WOLFSSL_BIGNUM* p = NULL;
  50071. WOLFSSL_BIGNUM* g = NULL;
  50072. ExpectNotNull(p = wolfSSL_BN_new());
  50073. ExpectNotNull(g = wolfSSL_BN_new());
  50074. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  50075. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  50076. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  50077. if (EXPECT_FAIL()) {
  50078. wolfSSL_BN_free(p);
  50079. wolfSSL_BN_free(g);
  50080. }
  50081. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  50082. wolfSSL_DH_free(dhDup);
  50083. }
  50084. #endif
  50085. wolfSSL_DH_free(dh);
  50086. #endif
  50087. #endif
  50088. return EXPECT_RESULT();
  50089. }
  50090. static int test_wolfSSL_DH_check(void)
  50091. {
  50092. EXPECT_DECLS;
  50093. #ifdef OPENSSL_ALL
  50094. #ifndef NO_DH
  50095. #ifndef NO_BIO
  50096. #ifndef NO_DSA
  50097. byte buf[6000];
  50098. char file[] = "./certs/dsaparams.pem";
  50099. XFILE f = XBADFILE;
  50100. int bytes;
  50101. BIO* bio = NULL;
  50102. DSA* dsa = NULL;
  50103. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50104. static const byte dh2048[] = {
  50105. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  50106. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  50107. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  50108. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  50109. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  50110. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  50111. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  50112. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  50113. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  50114. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  50115. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  50116. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  50117. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  50118. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  50119. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  50120. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  50121. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  50122. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  50123. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  50124. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  50125. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  50126. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  50127. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  50128. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  50129. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  50130. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  50131. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  50132. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  50133. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  50134. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  50135. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  50136. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  50137. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  50138. 0x93, 0x02, 0x01, 0x02
  50139. };
  50140. const byte* params;
  50141. #endif
  50142. DH* dh = NULL;
  50143. WOLFSSL_BIGNUM* p = NULL;
  50144. WOLFSSL_BIGNUM* g = NULL;
  50145. WOLFSSL_BIGNUM* pTmp = NULL;
  50146. WOLFSSL_BIGNUM* gTmp = NULL;
  50147. int codes = -1;
  50148. #ifndef NO_DSA
  50149. /* Initialize DH */
  50150. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  50151. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  50152. if (f != XBADFILE)
  50153. XFCLOSE(f);
  50154. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  50155. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  50156. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  50157. ExpectNotNull(dh);
  50158. BIO_free(bio);
  50159. DSA_free(dsa);
  50160. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  50161. params = dh2048;
  50162. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  50163. (long)sizeof(dh2048)));
  50164. #else
  50165. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  50166. #endif
  50167. /* Test assumed to be valid dh.
  50168. * Should return WOLFSSL_SUCCESS
  50169. * codes should be 0
  50170. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  50171. */
  50172. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50173. ExpectIntEQ(codes, 0);
  50174. /* Test NULL dh: expected BAD_FUNC_ARG */
  50175. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  50176. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  50177. if (dh != NULL) {
  50178. pTmp = dh->p;
  50179. dh->p = NULL;
  50180. }
  50181. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50182. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50183. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50184. /* set dh->p back to normal so it won't fail on next tests */
  50185. if (dh != NULL) {
  50186. dh->p = pTmp;
  50187. pTmp = NULL;
  50188. }
  50189. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  50190. if (dh != NULL) {
  50191. gTmp = dh->g;
  50192. dh->g = NULL;
  50193. }
  50194. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50195. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50196. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  50197. if (dh != NULL) {
  50198. dh->g = gTmp;
  50199. gTmp = NULL;
  50200. }
  50201. /* Cleanup */
  50202. DH_free(dh);
  50203. dh = NULL;
  50204. dh = DH_new();
  50205. ExpectNotNull(dh);
  50206. /* Check empty DH. */
  50207. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50208. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50209. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  50210. /* Check non-prime valued p. */
  50211. ExpectNotNull(p = BN_new());
  50212. ExpectIntEQ(BN_set_word(p, 4), 1);
  50213. ExpectNotNull(g = BN_new());
  50214. ExpectIntEQ(BN_set_word(g, 2), 1);
  50215. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  50216. if (EXPECT_FAIL()) {
  50217. wolfSSL_BN_free(p);
  50218. wolfSSL_BN_free(g);
  50219. }
  50220. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  50221. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  50222. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  50223. DH_free(dh);
  50224. #endif
  50225. #endif /* !NO_DH && !NO_DSA */
  50226. #endif
  50227. return EXPECT_RESULT();
  50228. }
  50229. static int test_wolfSSL_DH_prime(void)
  50230. {
  50231. EXPECT_DECLS;
  50232. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50233. WOLFSSL_BIGNUM* bn = NULL;
  50234. #if WOLFSSL_MAX_BN_BITS >= 768
  50235. WOLFSSL_BIGNUM* bn2 = NULL;
  50236. #endif
  50237. bn = wolfSSL_DH_768_prime(NULL);
  50238. #if WOLFSSL_MAX_BN_BITS >= 768
  50239. ExpectNotNull(bn);
  50240. bn2 = wolfSSL_DH_768_prime(bn);
  50241. ExpectNotNull(bn2);
  50242. ExpectTrue(bn == bn2);
  50243. wolfSSL_BN_free(bn);
  50244. bn = NULL;
  50245. #else
  50246. ExpectNull(bn);
  50247. #endif
  50248. bn = wolfSSL_DH_1024_prime(NULL);
  50249. #if WOLFSSL_MAX_BN_BITS >= 1024
  50250. ExpectNotNull(bn);
  50251. wolfSSL_BN_free(bn);
  50252. bn = NULL;
  50253. #else
  50254. ExpectNull(bn);
  50255. #endif
  50256. bn = wolfSSL_DH_2048_prime(NULL);
  50257. #if WOLFSSL_MAX_BN_BITS >= 2048
  50258. ExpectNotNull(bn);
  50259. wolfSSL_BN_free(bn);
  50260. bn = NULL;
  50261. #else
  50262. ExpectNull(bn);
  50263. #endif
  50264. bn = wolfSSL_DH_3072_prime(NULL);
  50265. #if WOLFSSL_MAX_BN_BITS >= 3072
  50266. ExpectNotNull(bn);
  50267. wolfSSL_BN_free(bn);
  50268. bn = NULL;
  50269. #else
  50270. ExpectNull(bn);
  50271. #endif
  50272. bn = wolfSSL_DH_4096_prime(NULL);
  50273. #if WOLFSSL_MAX_BN_BITS >= 4096
  50274. ExpectNotNull(bn);
  50275. wolfSSL_BN_free(bn);
  50276. bn = NULL;
  50277. #else
  50278. ExpectNull(bn);
  50279. #endif
  50280. bn = wolfSSL_DH_6144_prime(NULL);
  50281. #if WOLFSSL_MAX_BN_BITS >= 6144
  50282. ExpectNotNull(bn);
  50283. wolfSSL_BN_free(bn);
  50284. bn = NULL;
  50285. #else
  50286. ExpectNull(bn);
  50287. #endif
  50288. bn = wolfSSL_DH_8192_prime(NULL);
  50289. #if WOLFSSL_MAX_BN_BITS >= 8192
  50290. ExpectNotNull(bn);
  50291. wolfSSL_BN_free(bn);
  50292. bn = NULL;
  50293. #else
  50294. ExpectNull(bn);
  50295. #endif
  50296. #endif
  50297. return EXPECT_RESULT();
  50298. }
  50299. static int test_wolfSSL_DH_1536_prime(void)
  50300. {
  50301. EXPECT_DECLS;
  50302. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50303. BIGNUM* bn = NULL;
  50304. unsigned char bits[200];
  50305. int sz = 192; /* known binary size */
  50306. const byte expected[] = {
  50307. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50308. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  50309. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  50310. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  50311. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  50312. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  50313. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  50314. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  50315. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  50316. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  50317. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  50318. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  50319. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  50320. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  50321. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  50322. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  50323. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  50324. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  50325. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  50326. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  50327. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  50328. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  50329. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  50330. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50331. };
  50332. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  50333. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  50334. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  50335. BN_free(bn);
  50336. #endif
  50337. return EXPECT_RESULT();
  50338. }
  50339. static int test_wolfSSL_DH_get_2048_256(void)
  50340. {
  50341. EXPECT_DECLS;
  50342. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  50343. WOLFSSL_DH* dh = NULL;
  50344. const WOLFSSL_BIGNUM* pBn;
  50345. const WOLFSSL_BIGNUM* gBn;
  50346. const WOLFSSL_BIGNUM* qBn;
  50347. const byte pExpected[] = {
  50348. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  50349. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  50350. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  50351. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  50352. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  50353. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  50354. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  50355. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  50356. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  50357. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  50358. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  50359. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  50360. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  50361. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  50362. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  50363. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  50364. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  50365. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  50366. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  50367. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  50368. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  50369. 0x1E, 0x1A, 0x15, 0x97
  50370. };
  50371. const byte gExpected[] = {
  50372. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  50373. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  50374. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  50375. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  50376. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  50377. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  50378. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  50379. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  50380. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  50381. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  50382. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  50383. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  50384. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  50385. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  50386. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  50387. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  50388. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  50389. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  50390. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  50391. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  50392. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  50393. 0x6C, 0xC4, 0x16, 0x59
  50394. };
  50395. const byte qExpected[] = {
  50396. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  50397. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  50398. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  50399. };
  50400. int pSz;
  50401. int qSz;
  50402. int gSz;
  50403. byte* pReturned = NULL;
  50404. byte* qReturned = NULL;
  50405. byte* gReturned = NULL;
  50406. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  50407. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  50408. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  50409. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  50410. DYNAMIC_TYPE_TMP_BUFFER));
  50411. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  50412. ExpectIntEQ(pSz, sizeof(pExpected));
  50413. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  50414. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  50415. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  50416. DYNAMIC_TYPE_TMP_BUFFER));
  50417. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  50418. ExpectIntEQ(qSz, sizeof(qExpected));
  50419. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  50420. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  50421. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  50422. DYNAMIC_TYPE_TMP_BUFFER));
  50423. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  50424. ExpectIntEQ(gSz, sizeof(gExpected));
  50425. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  50426. wolfSSL_DH_free(dh);
  50427. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50428. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50429. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50430. #endif
  50431. return EXPECT_RESULT();
  50432. }
  50433. static int test_wolfSSL_PEM_write_DHparams(void)
  50434. {
  50435. EXPECT_DECLS;
  50436. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  50437. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  50438. DH* dh = NULL;
  50439. BIO* bio = NULL;
  50440. XFILE fp = XBADFILE;
  50441. byte pem[2048];
  50442. int pemSz = 0;
  50443. const char expected[] =
  50444. "-----BEGIN DH PARAMETERS-----\n"
  50445. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  50446. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  50447. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  50448. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  50449. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  50450. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  50451. "-----END DH PARAMETERS-----\n";
  50452. const char badPem[] =
  50453. "-----BEGIN DH PARAMETERS-----\n"
  50454. "-----END DH PARAMETERS-----\n";
  50455. const char emptySeqPem[] =
  50456. "-----BEGIN DH PARAMETERS-----\n"
  50457. "MAA=\n"
  50458. "-----END DH PARAMETERS-----\n";
  50459. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  50460. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  50461. if (fp != XBADFILE) {
  50462. XFCLOSE(fp);
  50463. fp = XBADFILE;
  50464. }
  50465. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  50466. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50467. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  50468. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  50469. (int)sizeof(badPem));
  50470. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  50471. BIO_free(bio);
  50472. bio = NULL;
  50473. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50474. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  50475. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  50476. (int)sizeof(emptySeqPem));
  50477. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  50478. BIO_free(bio);
  50479. bio = NULL;
  50480. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  50481. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  50482. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  50483. BIO_free(bio);
  50484. bio = NULL;
  50485. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  50486. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  50487. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  50488. DH_free(dh);
  50489. dh = NULL;
  50490. dh = wolfSSL_DH_new();
  50491. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  50492. if (fp != XBADFILE) {
  50493. XFCLOSE(fp);
  50494. fp = XBADFILE;
  50495. }
  50496. wolfSSL_DH_free(dh);
  50497. dh = NULL;
  50498. /* check results */
  50499. XMEMSET(pem, 0, sizeof(pem));
  50500. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  50501. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  50502. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  50503. if (fp != XBADFILE)
  50504. XFCLOSE(fp);
  50505. #endif
  50506. return EXPECT_RESULT();
  50507. }
  50508. static int test_wolfSSL_d2i_DHparams(void)
  50509. {
  50510. EXPECT_DECLS;
  50511. #ifdef OPENSSL_ALL
  50512. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  50513. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50514. XFILE f = XBADFILE;
  50515. unsigned char buf[4096];
  50516. const unsigned char* pt = buf;
  50517. #ifdef HAVE_FFDHE_2048
  50518. const char* params1 = "./certs/dh2048.der";
  50519. #endif
  50520. #ifdef HAVE_FFDHE_3072
  50521. const char* params2 = "./certs/dh3072.der";
  50522. #endif
  50523. long len = 0;
  50524. WOLFSSL_DH* dh = NULL;
  50525. XMEMSET(buf, 0, sizeof(buf));
  50526. /* Test 2048 bit parameters */
  50527. #ifdef HAVE_FFDHE_2048
  50528. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  50529. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  50530. if (f != XBADFILE) {
  50531. XFCLOSE(f);
  50532. f = XBADFILE;
  50533. }
  50534. /* Valid case */
  50535. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  50536. ExpectNotNull(dh->p);
  50537. ExpectNotNull(dh->g);
  50538. ExpectTrue(pt == buf);
  50539. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  50540. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  50541. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  50542. /* Invalid cases */
  50543. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  50544. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  50545. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  50546. DH_free(dh);
  50547. dh = NULL;
  50548. *buf = 0;
  50549. pt = buf;
  50550. #endif /* HAVE_FFDHE_2048 */
  50551. /* Test 3072 bit parameters */
  50552. #ifdef HAVE_FFDHE_3072
  50553. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  50554. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  50555. if (f != XBADFILE) {
  50556. XFCLOSE(f);
  50557. f = XBADFILE;
  50558. }
  50559. /* Valid case */
  50560. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  50561. ExpectNotNull(dh->p);
  50562. ExpectNotNull(dh->g);
  50563. ExpectTrue(pt != buf);
  50564. ExpectIntEQ(DH_generate_key(dh), 1);
  50565. /* Invalid cases */
  50566. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  50567. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  50568. DH_free(dh);
  50569. dh = NULL;
  50570. #endif /* HAVE_FFDHE_3072 */
  50571. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50572. #endif /* !NO_DH */
  50573. #endif
  50574. return EXPECT_RESULT();
  50575. }
  50576. static int test_wolfSSL_DH_LoadDer(void)
  50577. {
  50578. EXPECT_DECLS;
  50579. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  50580. defined(OPENSSL_EXTRA)
  50581. static const byte dh2048[] = {
  50582. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  50583. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  50584. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  50585. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  50586. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  50587. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  50588. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  50589. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  50590. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  50591. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  50592. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  50593. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  50594. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  50595. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  50596. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  50597. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  50598. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  50599. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  50600. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  50601. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  50602. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  50603. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  50604. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  50605. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  50606. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  50607. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  50608. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  50609. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  50610. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  50611. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  50612. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  50613. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  50614. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  50615. 0x93, 0x02, 0x01, 0x02
  50616. };
  50617. WOLFSSL_DH* dh = NULL;
  50618. ExpectNotNull(dh = wolfSSL_DH_new());
  50619. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  50620. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  50621. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  50622. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  50623. wolfSSL_DH_free(dh);
  50624. #endif
  50625. return EXPECT_RESULT();
  50626. }
  50627. static int test_wolfSSL_i2d_DHparams(void)
  50628. {
  50629. EXPECT_DECLS;
  50630. #ifdef OPENSSL_ALL
  50631. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  50632. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50633. XFILE f = XBADFILE;
  50634. unsigned char buf[4096];
  50635. const unsigned char* pt;
  50636. unsigned char* pt2;
  50637. #ifdef HAVE_FFDHE_2048
  50638. const char* params1 = "./certs/dh2048.der";
  50639. #endif
  50640. #ifdef HAVE_FFDHE_3072
  50641. const char* params2 = "./certs/dh3072.der";
  50642. #endif
  50643. long len;
  50644. WOLFSSL_DH* dh = NULL;
  50645. /* Test 2048 bit parameters */
  50646. #ifdef HAVE_FFDHE_2048
  50647. pt = buf;
  50648. pt2 = buf;
  50649. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  50650. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  50651. if (f != XBADFILE) {
  50652. XFCLOSE(f);
  50653. f = XBADFILE;
  50654. }
  50655. /* Valid case */
  50656. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  50657. ExpectTrue(pt == buf);
  50658. ExpectIntEQ(DH_generate_key(dh), 1);
  50659. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  50660. /* Invalid case */
  50661. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  50662. /* Return length only */
  50663. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  50664. DH_free(dh);
  50665. *buf = 0;
  50666. #endif
  50667. /* Test 3072 bit parameters */
  50668. #ifdef HAVE_FFDHE_3072
  50669. pt = buf;
  50670. pt2 = buf;
  50671. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  50672. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  50673. if (f != XBADFILE) {
  50674. XFCLOSE(f);
  50675. f = XBADFILE;
  50676. }
  50677. /* Valid case */
  50678. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  50679. ExpectTrue(pt == buf);
  50680. ExpectIntEQ(DH_generate_key(dh), 1);
  50681. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  50682. /* Invalid case */
  50683. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  50684. /* Return length only */
  50685. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  50686. DH_free(dh);
  50687. #endif
  50688. dh = DH_new();
  50689. ExpectNotNull(dh);
  50690. pt2 = buf;
  50691. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  50692. DH_free(dh);
  50693. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50694. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  50695. #endif
  50696. return EXPECT_RESULT();
  50697. }
  50698. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  50699. /*----------------------------------------------------------------------------*
  50700. | EC
  50701. *----------------------------------------------------------------------------*/
  50702. static int test_wolfSSL_EC_GROUP(void)
  50703. {
  50704. EXPECT_DECLS;
  50705. #ifdef OPENSSL_EXTRA
  50706. EC_GROUP *group = NULL;
  50707. EC_GROUP *group2 = NULL;
  50708. EC_GROUP *group3 = NULL;
  50709. #ifndef HAVE_ECC_BRAINPOOL
  50710. EC_GROUP *group4 = NULL;
  50711. #endif
  50712. WOLFSSL_BIGNUM* order = NULL;
  50713. int group_bits;
  50714. int i;
  50715. static const int knownEccNids[] = {
  50716. NID_X9_62_prime192v1,
  50717. NID_X9_62_prime192v2,
  50718. NID_X9_62_prime192v3,
  50719. NID_X9_62_prime239v1,
  50720. NID_X9_62_prime239v2,
  50721. NID_X9_62_prime239v3,
  50722. NID_X9_62_prime256v1,
  50723. NID_secp112r1,
  50724. NID_secp112r2,
  50725. NID_secp128r1,
  50726. NID_secp128r2,
  50727. NID_secp160r1,
  50728. NID_secp160r2,
  50729. NID_secp224r1,
  50730. NID_secp384r1,
  50731. NID_secp521r1,
  50732. NID_secp160k1,
  50733. NID_secp192k1,
  50734. NID_secp224k1,
  50735. NID_secp256k1,
  50736. NID_brainpoolP160r1,
  50737. NID_brainpoolP192r1,
  50738. NID_brainpoolP224r1,
  50739. NID_brainpoolP256r1,
  50740. NID_brainpoolP320r1,
  50741. NID_brainpoolP384r1,
  50742. NID_brainpoolP512r1,
  50743. };
  50744. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  50745. static const int knownEccEnums[] = {
  50746. ECC_SECP192R1,
  50747. ECC_PRIME192V2,
  50748. ECC_PRIME192V3,
  50749. ECC_PRIME239V1,
  50750. ECC_PRIME239V2,
  50751. ECC_PRIME239V3,
  50752. ECC_SECP256R1,
  50753. ECC_SECP112R1,
  50754. ECC_SECP112R2,
  50755. ECC_SECP128R1,
  50756. ECC_SECP128R2,
  50757. ECC_SECP160R1,
  50758. ECC_SECP160R2,
  50759. ECC_SECP224R1,
  50760. ECC_SECP384R1,
  50761. ECC_SECP521R1,
  50762. ECC_SECP160K1,
  50763. ECC_SECP192K1,
  50764. ECC_SECP224K1,
  50765. ECC_SECP256K1,
  50766. ECC_BRAINPOOLP160R1,
  50767. ECC_BRAINPOOLP192R1,
  50768. ECC_BRAINPOOLP224R1,
  50769. ECC_BRAINPOOLP256R1,
  50770. ECC_BRAINPOOLP320R1,
  50771. ECC_BRAINPOOLP384R1,
  50772. ECC_BRAINPOOLP512R1,
  50773. };
  50774. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  50775. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  50776. ExpectNotNull(group2 = EC_GROUP_dup(group));
  50777. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  50778. #ifndef HAVE_ECC_BRAINPOOL
  50779. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  50780. NID_brainpoolP256r1));
  50781. #endif
  50782. ExpectNull(EC_GROUP_dup(NULL));
  50783. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  50784. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  50785. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  50786. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  50787. #ifndef HAVE_ECC_BRAINPOOL
  50788. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  50789. #endif
  50790. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  50791. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  50792. ExpectNotNull(order = BN_new());
  50793. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  50794. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  50795. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  50796. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  50797. wolfSSL_BN_free(order);
  50798. ExpectNotNull(EC_GROUP_method_of(group));
  50799. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  50800. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  50801. NID_X9_62_prime_field);
  50802. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  50803. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  50804. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  50805. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  50806. #ifndef NO_WOLFSSL_STUB
  50807. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  50808. #endif
  50809. #ifndef HAVE_ECC_BRAINPOOL
  50810. EC_GROUP_free(group4);
  50811. #endif
  50812. EC_GROUP_free(group3);
  50813. EC_GROUP_free(group2);
  50814. EC_GROUP_free(group);
  50815. for (i = 0; i < knowEccNidsLen; i++) {
  50816. group = NULL;
  50817. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  50818. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  50819. EC_GROUP_free(group);
  50820. }
  50821. for (i = 0; i < knowEccEnumsLen; i++) {
  50822. group = NULL;
  50823. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  50824. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  50825. EC_GROUP_free(group);
  50826. }
  50827. #endif
  50828. return EXPECT_RESULT();
  50829. }
  50830. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  50831. {
  50832. EXPECT_DECLS;
  50833. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50834. EC_GROUP *group = NULL;
  50835. BIO* bio = NULL;
  50836. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  50837. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  50838. EC_GROUP *ret = NULL;
  50839. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  50840. "BgUrgQQAIg==\n"
  50841. "-----END EC PARAMETERS-----";
  50842. #endif
  50843. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  50844. "MAA=\n"
  50845. "-----END EC PARAMETERS-----";
  50846. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  50847. "BgA=\n"
  50848. "-----END EC PARAMETERS-----";
  50849. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  50850. "BgE=\n"
  50851. "-----END EC PARAMETERS-----";
  50852. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  50853. "BgE*\n"
  50854. "-----END EC PARAMETERS-----";
  50855. /* Test that first parameter, bio, being NULL fails. */
  50856. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  50857. /* Test that reading named parameters works. */
  50858. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  50859. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  50860. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  50861. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  50862. BIO_free(bio);
  50863. bio = NULL;
  50864. EC_GROUP_free(group);
  50865. group = NULL;
  50866. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  50867. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  50868. /* Test that reusing group works. */
  50869. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  50870. sizeof(ec_nc_p384)));
  50871. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  50872. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  50873. BIO_free(bio);
  50874. bio = NULL;
  50875. EC_GROUP_free(group);
  50876. group = NULL;
  50877. /* Test that returning through group works. */
  50878. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  50879. sizeof(ec_nc_p384)));
  50880. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  50881. ExpectIntEQ(group == ret, 1);
  50882. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  50883. BIO_free(bio);
  50884. bio = NULL;
  50885. EC_GROUP_free(group);
  50886. group = NULL;
  50887. #endif
  50888. /* Test 0x30, 0x00 (not and object id) fails. */
  50889. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  50890. sizeof(ec_nc_bad_1)));
  50891. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  50892. BIO_free(bio);
  50893. bio = NULL;
  50894. /* Test 0x06, 0x00 (empty object id) fails. */
  50895. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  50896. sizeof(ec_nc_bad_2)));
  50897. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  50898. BIO_free(bio);
  50899. bio = NULL;
  50900. /* Test 0x06, 0x01 (badly formed object id) fails. */
  50901. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  50902. sizeof(ec_nc_bad_3)));
  50903. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  50904. BIO_free(bio);
  50905. bio = NULL;
  50906. /* Test invalid PEM encoding - invalid character. */
  50907. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  50908. sizeof(ec_nc_bad_4)));
  50909. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  50910. BIO_free(bio);
  50911. #endif
  50912. return EXPECT_RESULT();
  50913. }
  50914. static int test_wolfSSL_EC_POINT(void)
  50915. {
  50916. EXPECT_DECLS;
  50917. #if !defined(WOLFSSL_SP_MATH) && \
  50918. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  50919. #ifdef OPENSSL_EXTRA
  50920. BN_CTX* ctx = NULL;
  50921. EC_GROUP* group = NULL;
  50922. #ifndef HAVE_ECC_BRAINPOOL
  50923. EC_GROUP* group2 = NULL;
  50924. #endif
  50925. EC_POINT* Gxy = NULL;
  50926. EC_POINT* new_point = NULL;
  50927. EC_POINT* set_point = NULL;
  50928. EC_POINT* infinity = NULL;
  50929. BIGNUM* k = NULL;
  50930. BIGNUM* Gx = NULL;
  50931. BIGNUM* Gy = NULL;
  50932. BIGNUM* Gz = NULL;
  50933. BIGNUM* X = NULL;
  50934. BIGNUM* Y = NULL;
  50935. BIGNUM* set_point_bn = NULL;
  50936. char* hexStr = NULL;
  50937. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  50938. "17AF381913FF7A96314EA47055EA0FD0";
  50939. /* NISTP256R1 Gx/Gy */
  50940. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  50941. "77037D812DEB33A0F4A13945D898C296";
  50942. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  50943. "2BCE33576B315ECECBB6406837BF51F5";
  50944. #ifndef HAVE_SELFTEST
  50945. EC_POINT *tmp = NULL;
  50946. size_t bin_len;
  50947. unsigned int blen = 0;
  50948. unsigned char* buf = NULL;
  50949. unsigned char bufInf[1] = { 0x00 };
  50950. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  50951. "77037D812DEB33A0F4A13945D898C296"
  50952. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  50953. "2BCE33576B315ECECBB6406837BF51F5";
  50954. const unsigned char binUncompG[] = {
  50955. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  50956. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  50957. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  50958. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  50959. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  50960. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  50961. };
  50962. const unsigned char binUncompGBad[] = {
  50963. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  50964. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  50965. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  50966. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  50967. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  50968. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  50969. };
  50970. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  50971. "77037D812DEB33A0F4A13945D898C296";
  50972. #ifdef HAVE_COMP_KEY
  50973. const unsigned char binCompG[] = {
  50974. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  50975. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  50976. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  50977. };
  50978. #endif
  50979. #endif
  50980. ExpectNotNull(ctx = BN_CTX_new());
  50981. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  50982. #ifndef HAVE_ECC_BRAINPOOL
  50983. /* Used to make groups curve_idx == -1. */
  50984. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  50985. #endif
  50986. ExpectNull(EC_POINT_new(NULL));
  50987. ExpectNotNull(Gxy = EC_POINT_new(group));
  50988. ExpectNotNull(new_point = EC_POINT_new(group));
  50989. ExpectNotNull(set_point = EC_POINT_new(group));
  50990. ExpectNotNull(X = BN_new());
  50991. ExpectNotNull(Y = BN_new());
  50992. ExpectNotNull(set_point_bn = BN_new());
  50993. ExpectNotNull(infinity = EC_POINT_new(group));
  50994. /* load test values */
  50995. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  50996. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  50997. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  50998. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  50999. /* populate coordinates for input point */
  51000. if (Gxy != NULL) {
  51001. Gxy->X = Gx;
  51002. Gxy->Y = Gy;
  51003. Gxy->Z = Gz;
  51004. }
  51005. /* Test handling of NULL point. */
  51006. EC_POINT_clear_free(NULL);
  51007. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51008. NULL, NULL, ctx), 0);
  51009. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51010. NULL, NULL, ctx), 0);
  51011. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51012. NULL, NULL, ctx), 0);
  51013. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51014. X, NULL, ctx), 0);
  51015. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  51016. NULL, Y, ctx), 0);
  51017. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  51018. X, Y, ctx), 0);
  51019. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  51020. X, Y, ctx), 0);
  51021. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51022. NULL, Y, ctx), 0);
  51023. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  51024. X, NULL, ctx), 0);
  51025. /* Getting point at infinity returns an error. */
  51026. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  51027. X, Y, ctx), 0);
  51028. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51029. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51030. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51031. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  51032. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  51033. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  51034. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  51035. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  51036. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  51037. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  51038. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  51039. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  51040. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  51041. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  51042. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  51043. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  51044. /* perform point multiplication */
  51045. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  51046. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51047. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51048. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51049. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  51050. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51051. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51052. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51053. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  51054. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51055. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51056. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51057. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  51058. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  51059. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  51060. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  51061. /* Set point to something. */
  51062. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  51063. #else
  51064. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  51065. ctx), 1);
  51066. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51067. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  51068. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  51069. #endif
  51070. /* check if point X coordinate is zero */
  51071. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  51072. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  51073. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  51074. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  51075. #endif
  51076. /* extract the coordinates from point */
  51077. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51078. ctx), WOLFSSL_SUCCESS);
  51079. /* check if point X coordinate is zero */
  51080. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  51081. /* set the same X and Y points in another object */
  51082. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  51083. ctx), WOLFSSL_SUCCESS);
  51084. /* compare points as they should be the same */
  51085. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  51086. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  51087. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  51088. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  51089. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  51090. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  51091. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  51092. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  51093. /* Test copying */
  51094. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  51095. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  51096. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  51097. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  51098. /* Test inverting */
  51099. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  51100. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  51101. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  51102. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  51103. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  51104. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  51105. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51106. {
  51107. EC_POINT* orig_point = NULL;
  51108. ExpectNotNull(orig_point = EC_POINT_new(group));
  51109. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  51110. 1);
  51111. /* new_point should be set_point inverted so adding it will revert
  51112. * the point back to set_point */
  51113. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  51114. NULL), 1);
  51115. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  51116. EC_POINT_free(orig_point);
  51117. }
  51118. #endif
  51119. /* Test getting affine converts from projective. */
  51120. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  51121. /* Force non-affine coordinates */
  51122. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51123. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  51124. if (new_point != NULL) {
  51125. new_point->inSet = 0;
  51126. }
  51127. /* extract the coordinates from point */
  51128. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  51129. ctx), WOLFSSL_SUCCESS);
  51130. /* check if point ordinates have changed. */
  51131. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  51132. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  51133. /* Test check for infinity */
  51134. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  51135. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  51136. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  51137. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  51138. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  51139. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  51140. #else
  51141. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  51142. #endif
  51143. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  51144. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  51145. /* check bn2hex */
  51146. hexStr = BN_bn2hex(k);
  51147. ExpectStrEQ(hexStr, kTest);
  51148. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51149. BN_print_fp(stderr, k);
  51150. fprintf(stderr, "\n");
  51151. #endif
  51152. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51153. hexStr = BN_bn2hex(Gx);
  51154. ExpectStrEQ(hexStr, kGx);
  51155. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51156. BN_print_fp(stderr, Gx);
  51157. fprintf(stderr, "\n");
  51158. #endif
  51159. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51160. hexStr = BN_bn2hex(Gy);
  51161. ExpectStrEQ(hexStr, kGy);
  51162. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  51163. BN_print_fp(stderr, Gy);
  51164. fprintf(stderr, "\n");
  51165. #endif
  51166. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51167. #ifndef HAVE_SELFTEST
  51168. /* Test point to hex */
  51169. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51170. ctx));
  51171. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51172. ctx));
  51173. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51174. ctx));
  51175. #ifndef HAVE_ECC_BRAINPOOL
  51176. /* Group not supported in wolfCrypt. */
  51177. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51178. ctx));
  51179. #endif
  51180. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  51181. ExpectNotNull(hexStr);
  51182. ExpectStrEQ(hexStr, uncompG);
  51183. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51184. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  51185. ExpectNotNull(hexStr);
  51186. ExpectStrEQ(hexStr, compG);
  51187. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  51188. /* Test point to oct */
  51189. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51190. NULL, 0, ctx), 0);
  51191. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51192. NULL, 0, ctx), 0);
  51193. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  51194. NULL, 0, ctx), 0);
  51195. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51196. NULL, 0, ctx);
  51197. ExpectIntEQ(bin_len, sizeof(binUncompG));
  51198. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51199. DYNAMIC_TYPE_ECC));
  51200. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  51201. buf, bin_len, ctx), bin_len);
  51202. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51203. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51204. /* Infinity (x=0, y=0) encodes as '0x00'. */
  51205. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  51206. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  51207. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  51208. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  51209. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  51210. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  51211. ExpectIntEQ(bufInf[0], 0);
  51212. wolfSSL_EC_POINT_dump(NULL, NULL);
  51213. /* Test point i2d */
  51214. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  51215. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  51216. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  51217. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  51218. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  51219. ExpectIntEQ(blen, sizeof(binUncompG));
  51220. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  51221. blen -= 1;
  51222. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  51223. blen += 1;
  51224. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  51225. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  51226. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51227. #ifdef HAVE_COMP_KEY
  51228. /* Test point to oct compressed */
  51229. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  51230. 0, ctx);
  51231. ExpectIntEQ(bin_len, sizeof(binCompG));
  51232. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  51233. DYNAMIC_TYPE_ECC));
  51234. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  51235. bin_len, ctx), bin_len);
  51236. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  51237. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  51238. #endif
  51239. /* Test point BN */
  51240. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  51241. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51242. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  51243. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51244. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  51245. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  51246. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  51247. /* Test oct to point */
  51248. ExpectNotNull(tmp = EC_POINT_new(group));
  51249. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  51250. ctx), 0);
  51251. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  51252. ctx), 0);
  51253. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  51254. ctx), 0);
  51255. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  51256. sizeof(binUncompGBad), ctx), 0);
  51257. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  51258. ctx), 1);
  51259. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51260. EC_POINT_free(tmp);
  51261. tmp = NULL;
  51262. /* Test setting BN ordinates. */
  51263. ExpectNotNull(tmp = EC_POINT_new(group));
  51264. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  51265. NULL, ctx), 0);
  51266. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  51267. NULL, ctx), 0);
  51268. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  51269. NULL, ctx), 0);
  51270. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  51271. NULL, ctx), 0);
  51272. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  51273. Gy, ctx), 0);
  51274. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  51275. ctx), 0);
  51276. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  51277. ctx), 0);
  51278. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  51279. Gy, ctx), 0);
  51280. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  51281. NULL, ctx), 0);
  51282. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  51283. ctx), 1);
  51284. EC_POINT_free(tmp);
  51285. tmp = NULL;
  51286. /* Test point d2i */
  51287. ExpectNotNull(tmp = EC_POINT_new(group));
  51288. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  51289. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  51290. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  51291. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  51292. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  51293. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  51294. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  51295. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  51296. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  51297. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51298. EC_POINT_free(tmp);
  51299. tmp = NULL;
  51300. #ifdef HAVE_COMP_KEY
  51301. /* Test oct compressed to point */
  51302. ExpectNotNull(tmp = EC_POINT_new(group));
  51303. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  51304. 1);
  51305. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51306. EC_POINT_free(tmp);
  51307. tmp = NULL;
  51308. /* Test point d2i - compressed */
  51309. ExpectNotNull(tmp = EC_POINT_new(group));
  51310. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  51311. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  51312. EC_POINT_free(tmp);
  51313. tmp = NULL;
  51314. #endif
  51315. #endif
  51316. /* test BN_mod_add */
  51317. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  51318. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  51319. 1);
  51320. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  51321. /* cleanup */
  51322. BN_free(X);
  51323. BN_free(Y);
  51324. BN_free(k);
  51325. BN_free(set_point_bn);
  51326. EC_POINT_free(infinity);
  51327. EC_POINT_free(new_point);
  51328. EC_POINT_free(set_point);
  51329. EC_POINT_clear_free(Gxy);
  51330. #ifndef HAVE_ECC_BRAINPOOL
  51331. EC_GROUP_free(group2);
  51332. #endif
  51333. EC_GROUP_free(group);
  51334. BN_CTX_free(ctx);
  51335. #endif
  51336. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  51337. return EXPECT_RESULT();
  51338. }
  51339. static int test_wolfSSL_SPAKE(void)
  51340. {
  51341. EXPECT_DECLS;
  51342. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  51343. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  51344. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51345. BIGNUM* x = NULL; /* kdc priv */
  51346. BIGNUM* y = NULL; /* client priv */
  51347. BIGNUM* w = NULL; /* shared value */
  51348. byte M_bytes[] = {
  51349. /* uncompressed */
  51350. 0x04,
  51351. /* x */
  51352. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  51353. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  51354. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  51355. /* y */
  51356. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  51357. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  51358. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  51359. };
  51360. EC_POINT* M = NULL; /* shared value */
  51361. byte N_bytes[] = {
  51362. /* uncompressed */
  51363. 0x04,
  51364. /* x */
  51365. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  51366. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  51367. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  51368. /* y */
  51369. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  51370. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  51371. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  51372. };
  51373. EC_POINT* N = NULL; /* shared value */
  51374. EC_POINT* T = NULL; /* kdc pub */
  51375. EC_POINT* tmp1 = NULL; /* kdc pub */
  51376. EC_POINT* tmp2 = NULL; /* kdc pub */
  51377. EC_POINT* S = NULL; /* client pub */
  51378. EC_POINT* client_secret = NULL;
  51379. EC_POINT* kdc_secret = NULL;
  51380. EC_GROUP* group = NULL;
  51381. BN_CTX* bn_ctx = NULL;
  51382. /* Values taken from a test run of Kerberos 5 */
  51383. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51384. ExpectNotNull(bn_ctx = BN_CTX_new());
  51385. ExpectNotNull(M = EC_POINT_new(group));
  51386. ExpectNotNull(N = EC_POINT_new(group));
  51387. ExpectNotNull(T = EC_POINT_new(group));
  51388. ExpectNotNull(tmp1 = EC_POINT_new(group));
  51389. ExpectNotNull(tmp2 = EC_POINT_new(group));
  51390. ExpectNotNull(S = EC_POINT_new(group));
  51391. ExpectNotNull(client_secret = EC_POINT_new(group));
  51392. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  51393. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  51394. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  51395. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  51396. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  51397. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  51398. "506AB395478F0AAB647752CF117B36250"), 1);
  51399. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  51400. 1);
  51401. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  51402. 1);
  51403. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  51404. /* kdc */
  51405. /* T=x*P+w*M */
  51406. /* All in one function call */
  51407. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  51408. /* Spread into separate calls */
  51409. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  51410. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  51411. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  51412. 1);
  51413. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  51414. /* client */
  51415. /* S=y*P+w*N */
  51416. /* All in one function call */
  51417. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  51418. /* Spread into separate calls */
  51419. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  51420. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  51421. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  51422. 1);
  51423. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  51424. /* K=y*(T-w*M) */
  51425. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  51426. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  51427. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  51428. 1);
  51429. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  51430. bn_ctx), 1);
  51431. /* kdc */
  51432. /* K=x*(S-w*N) */
  51433. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  51434. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  51435. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  51436. 1);
  51437. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  51438. 1);
  51439. /* kdc_secret == client_secret */
  51440. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  51441. BN_free(x);
  51442. BN_free(y);
  51443. BN_free(w);
  51444. EC_POINT_free(M);
  51445. EC_POINT_free(N);
  51446. EC_POINT_free(T);
  51447. EC_POINT_free(tmp1);
  51448. EC_POINT_free(tmp2);
  51449. EC_POINT_free(S);
  51450. EC_POINT_free(client_secret);
  51451. EC_POINT_free(kdc_secret);
  51452. EC_GROUP_free(group);
  51453. BN_CTX_free(bn_ctx);
  51454. #endif
  51455. return EXPECT_RESULT();
  51456. }
  51457. static int test_wolfSSL_EC_KEY_generate(void)
  51458. {
  51459. EXPECT_DECLS;
  51460. #ifdef OPENSSL_EXTRA
  51461. WOLFSSL_EC_KEY* key = NULL;
  51462. #ifndef HAVE_ECC_BRAINPOOL
  51463. WOLFSSL_EC_GROUP* group = NULL;
  51464. #endif
  51465. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51466. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  51467. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  51468. wolfSSL_EC_KEY_free(key);
  51469. key = NULL;
  51470. #ifndef HAVE_ECC_BRAINPOOL
  51471. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  51472. NID_brainpoolP256r1));
  51473. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  51474. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  51475. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  51476. wolfSSL_EC_KEY_free(key);
  51477. wolfSSL_EC_GROUP_free(group);
  51478. #endif
  51479. #endif
  51480. return EXPECT_RESULT();
  51481. }
  51482. static int test_EC_i2d(void)
  51483. {
  51484. EXPECT_DECLS;
  51485. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  51486. EC_KEY *key = NULL;
  51487. EC_KEY *copy = NULL;
  51488. int len = 0;
  51489. unsigned char *buf = NULL;
  51490. unsigned char *p = NULL;
  51491. const unsigned char *tmp = NULL;
  51492. const unsigned char octBad[] = {
  51493. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  51494. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  51495. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  51496. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  51497. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  51498. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  51499. };
  51500. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51501. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  51502. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  51503. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51504. DYNAMIC_TYPE_TMP_BUFFER));
  51505. p = buf;
  51506. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  51507. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  51508. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  51509. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  51510. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  51511. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  51512. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  51513. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  51514. tmp = buf;
  51515. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  51516. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  51517. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  51518. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  51519. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  51520. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  51521. tmp = buf;
  51522. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  51523. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  51524. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  51525. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  51526. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  51527. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  51528. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  51529. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  51530. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  51531. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  51532. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  51533. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  51534. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  51535. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51536. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  51537. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51538. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  51539. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51540. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  51541. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51542. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  51543. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  51544. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  51545. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  51546. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  51547. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  51548. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  51549. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51550. buf = NULL;
  51551. buf = NULL;
  51552. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  51553. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51554. DYNAMIC_TYPE_TMP_BUFFER));
  51555. p = buf;
  51556. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  51557. p = NULL;
  51558. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  51559. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51560. p = NULL;
  51561. /* Bad point is also an invalid private key. */
  51562. tmp = octBad;
  51563. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  51564. tmp = buf;
  51565. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  51566. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51567. buf = NULL;
  51568. buf = NULL;
  51569. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  51570. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  51571. DYNAMIC_TYPE_TMP_BUFFER));
  51572. p = buf;
  51573. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  51574. p = NULL;
  51575. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  51576. tmp = buf;
  51577. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  51578. tmp = octBad;
  51579. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  51580. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  51581. ExpectIntEQ(EC_KEY_check_key(key), 1);
  51582. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  51583. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  51584. EC_KEY_free(key);
  51585. EC_KEY_free(copy);
  51586. #endif
  51587. return EXPECT_RESULT();
  51588. }
  51589. static int test_wolfSSL_EC_curve(void)
  51590. {
  51591. EXPECT_DECLS;
  51592. #if defined(OPENSSL_EXTRA)
  51593. int nid = NID_secp160k1;
  51594. const char* nid_name = NULL;
  51595. ExpectNull(EC_curve_nid2nist(NID_sha256));
  51596. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  51597. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  51598. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  51599. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  51600. #endif
  51601. return EXPECT_RESULT();
  51602. }
  51603. static int test_wolfSSL_EC_KEY_dup(void)
  51604. {
  51605. EXPECT_DECLS;
  51606. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  51607. WOLFSSL_EC_KEY* ecKey = NULL;
  51608. WOLFSSL_EC_KEY* dupKey = NULL;
  51609. ecc_key* srcKey = NULL;
  51610. ecc_key* destKey = NULL;
  51611. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  51612. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  51613. /* Valid cases */
  51614. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51615. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  51616. /* Compare pubkey */
  51617. if (ecKey != NULL) {
  51618. srcKey = (ecc_key*)ecKey->internal;
  51619. }
  51620. if (dupKey != NULL) {
  51621. destKey = (ecc_key*)dupKey->internal;
  51622. }
  51623. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  51624. /* compare EC_GROUP */
  51625. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  51626. /* compare EC_POINT */
  51627. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  51628. dupKey->pub_key, NULL), MP_EQ);
  51629. /* compare BIGNUM */
  51630. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  51631. wolfSSL_EC_KEY_free(dupKey);
  51632. dupKey = NULL;
  51633. /* Invalid cases */
  51634. /* NULL key */
  51635. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  51636. /* NULL ecc_key */
  51637. if (ecKey != NULL) {
  51638. wc_ecc_free((ecc_key*)ecKey->internal);
  51639. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  51640. ecKey->internal = NULL; /* Set ecc_key to NULL */
  51641. }
  51642. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51643. wolfSSL_EC_KEY_free(ecKey);
  51644. ecKey = NULL;
  51645. wolfSSL_EC_KEY_free(dupKey);
  51646. dupKey = NULL;
  51647. /* NULL Group */
  51648. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  51649. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  51650. if (ecKey != NULL) {
  51651. wolfSSL_EC_GROUP_free(ecKey->group);
  51652. ecKey->group = NULL; /* Set group to NULL */
  51653. }
  51654. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51655. wolfSSL_EC_KEY_free(ecKey);
  51656. ecKey = NULL;
  51657. wolfSSL_EC_KEY_free(dupKey);
  51658. dupKey = NULL;
  51659. /* NULL public key */
  51660. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  51661. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  51662. if (ecKey != NULL) {
  51663. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  51664. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  51665. }
  51666. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51667. if (ecKey != NULL) {
  51668. wolfSSL_EC_POINT_free(ecKey->pub_key);
  51669. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  51670. }
  51671. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51672. wolfSSL_EC_KEY_free(ecKey);
  51673. ecKey = NULL;
  51674. wolfSSL_EC_KEY_free(dupKey);
  51675. dupKey = NULL;
  51676. /* NULL private key */
  51677. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  51678. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  51679. if (ecKey != NULL) {
  51680. wolfSSL_BN_free(ecKey->priv_key);
  51681. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  51682. }
  51683. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51684. wolfSSL_EC_KEY_free(ecKey);
  51685. ecKey = NULL;
  51686. wolfSSL_EC_KEY_free(dupKey);
  51687. dupKey = NULL;
  51688. /* Test EC_KEY_up_ref */
  51689. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  51690. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  51691. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  51692. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  51693. /* reference count doesn't follow duplicate */
  51694. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  51695. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  51696. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  51697. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  51698. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  51699. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  51700. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  51701. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  51702. #endif
  51703. return EXPECT_RESULT();
  51704. }
  51705. static int test_wolfSSL_EC_KEY_set_group(void)
  51706. {
  51707. EXPECT_DECLS;
  51708. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  51709. defined(OPENSSL_EXTRA)
  51710. EC_KEY *key = NULL;
  51711. EC_GROUP *group = NULL;
  51712. const EC_GROUP *group2 = NULL;
  51713. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  51714. ExpectNotNull(key = EC_KEY_new());
  51715. ExpectNull(EC_KEY_get0_group(NULL));
  51716. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  51717. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  51718. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  51719. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  51720. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  51721. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  51722. EC_GROUP_free(group);
  51723. EC_KEY_free(key);
  51724. #endif
  51725. return EXPECT_RESULT();
  51726. }
  51727. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  51728. {
  51729. EXPECT_DECLS;
  51730. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51731. BIO* bio = NULL;
  51732. EC_KEY* key = NULL;
  51733. /* Error condition: NULL key. */
  51734. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  51735. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  51736. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  51737. /* Conversion form defaults to uncompressed. */
  51738. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  51739. #ifdef HAVE_COMP_KEY
  51740. /* Explicitly set to compressed. */
  51741. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  51742. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  51743. #else
  51744. /* Will still work just won't change anything. */
  51745. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  51746. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  51747. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  51748. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  51749. #endif
  51750. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  51751. BIO_free(bio);
  51752. EC_KEY_free(key);
  51753. #endif
  51754. return EXPECT_RESULT();
  51755. }
  51756. static int test_wolfSSL_EC_KEY_private_key(void)
  51757. {
  51758. EXPECT_DECLS;
  51759. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51760. WOLFSSL_EC_KEY* key = NULL;
  51761. WOLFSSL_BIGNUM* priv = NULL;
  51762. WOLFSSL_BIGNUM* priv2 = NULL;
  51763. WOLFSSL_BIGNUM* bn;
  51764. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51765. ExpectNotNull(priv = wolfSSL_BN_new());
  51766. ExpectNotNull(priv2 = wolfSSL_BN_new());
  51767. ExpectIntNE(BN_set_word(priv, 2), 0);
  51768. ExpectIntNE(BN_set_word(priv2, 2), 0);
  51769. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  51770. /* No private key set. */
  51771. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  51772. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  51773. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  51774. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  51775. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  51776. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  51777. ExpectPtrNE(bn, priv);
  51778. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  51779. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  51780. ExpectPtrNE(bn, priv2);
  51781. wolfSSL_BN_free(priv2);
  51782. wolfSSL_BN_free(priv);
  51783. wolfSSL_EC_KEY_free(key);
  51784. #endif
  51785. return EXPECT_RESULT();
  51786. }
  51787. static int test_wolfSSL_EC_KEY_public_key(void)
  51788. {
  51789. EXPECT_DECLS;
  51790. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51791. WOLFSSL_EC_KEY* key = NULL;
  51792. WOLFSSL_EC_POINT* pub = NULL;
  51793. WOLFSSL_EC_POINT* point = NULL;
  51794. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51795. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  51796. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  51797. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  51798. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  51799. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  51800. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  51801. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  51802. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  51803. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  51804. ExpectPtrEq(point, pub);
  51805. wolfSSL_EC_KEY_free(key);
  51806. #endif
  51807. return EXPECT_RESULT();
  51808. }
  51809. static int test_wolfSSL_EC_KEY_print_fp(void)
  51810. {
  51811. EXPECT_DECLS;
  51812. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  51813. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  51814. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  51815. !defined(NO_STDIO_FILESYSTEM)
  51816. EC_KEY* key = NULL;
  51817. /* Bad file pointer. */
  51818. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  51819. /* NULL key. */
  51820. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  51821. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  51822. /* Negative indent. */
  51823. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  51824. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  51825. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  51826. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  51827. wolfSSL_EC_KEY_free(key);
  51828. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  51829. NID_X9_62_prime256v1)));
  51830. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  51831. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  51832. wolfSSL_EC_KEY_free(key);
  51833. #endif
  51834. return EXPECT_RESULT();
  51835. }
  51836. static int test_wolfSSL_EC_get_builtin_curves(void)
  51837. {
  51838. EXPECT_DECLS;
  51839. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  51840. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51841. EC_builtin_curve* curves = NULL;
  51842. size_t crv_len = 0;
  51843. size_t i = 0;
  51844. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  51845. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  51846. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  51847. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  51848. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  51849. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  51850. if (curves[i].comment != NULL) {
  51851. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  51852. }
  51853. }
  51854. if (crv_len > 1) {
  51855. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  51856. }
  51857. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51858. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51859. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  51860. return EXPECT_RESULT();
  51861. }
  51862. static int test_wolfSSL_ECDSA_SIG(void)
  51863. {
  51864. EXPECT_DECLS;
  51865. #ifdef OPENSSL_EXTRA
  51866. WOLFSSL_ECDSA_SIG* sig = NULL;
  51867. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  51868. WOLFSSL_BIGNUM* r = NULL;
  51869. WOLFSSL_BIGNUM* s = NULL;
  51870. const WOLFSSL_BIGNUM* r2 = NULL;
  51871. const WOLFSSL_BIGNUM* s2 = NULL;
  51872. const unsigned char* cp = NULL;
  51873. unsigned char* p = NULL;
  51874. unsigned char outSig[8];
  51875. unsigned char sigData[8] =
  51876. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  51877. unsigned char sigDataBad[8] =
  51878. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  51879. wolfSSL_ECDSA_SIG_free(NULL);
  51880. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  51881. ExpectNotNull(r = wolfSSL_BN_new());
  51882. ExpectNotNull(s = wolfSSL_BN_new());
  51883. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  51884. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  51885. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  51886. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  51887. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  51888. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  51889. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  51890. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  51891. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  51892. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  51893. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  51894. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  51895. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  51896. r2 = NULL;
  51897. s2 = NULL;
  51898. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  51899. ExpectNull(r2);
  51900. ExpectNull(s2);
  51901. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  51902. if (EXPECT_FAIL()) {
  51903. wolfSSL_BN_free(r);
  51904. wolfSSL_BN_free(s);
  51905. }
  51906. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  51907. ExpectPtrEq(r2, r);
  51908. ExpectPtrEq(s2, s);
  51909. r2 = NULL;
  51910. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  51911. ExpectPtrEq(r2, r);
  51912. s2 = NULL;
  51913. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  51914. ExpectPtrEq(s2, s);
  51915. /* r and s are freed when sig is freed. */
  51916. wolfSSL_ECDSA_SIG_free(sig);
  51917. sig = NULL;
  51918. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  51919. cp = sigDataBad;
  51920. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  51921. cp = sigData;
  51922. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  51923. ExpectIntEQ((cp == sigData + 8), 1);
  51924. cp = sigData;
  51925. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  51926. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  51927. ExpectIntEQ((sig == sig2), 1);
  51928. cp = outSig;
  51929. p = outSig;
  51930. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  51931. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  51932. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  51933. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  51934. ExpectIntEQ((p == outSig + 8), 1);
  51935. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  51936. wolfSSL_ECDSA_SIG_free(sig);
  51937. #endif
  51938. return EXPECT_RESULT();
  51939. }
  51940. static int test_ECDSA_size_sign(void)
  51941. {
  51942. EXPECT_DECLS;
  51943. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  51944. EC_KEY* key = NULL;
  51945. ECDSA_SIG* ecdsaSig = NULL;
  51946. int id;
  51947. byte hash[WC_MAX_DIGEST_SIZE];
  51948. byte hash2[WC_MAX_DIGEST_SIZE];
  51949. byte sig[ECC_MAX_SIG_SIZE];
  51950. unsigned int sigSz = sizeof(sig);
  51951. XMEMSET(hash, 123, sizeof(hash));
  51952. XMEMSET(hash2, 234, sizeof(hash2));
  51953. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  51954. ExpectIntEQ(id, ECC_SECP256R1);
  51955. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51956. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  51957. ExpectIntGE(ECDSA_size(NULL), 0);
  51958. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  51959. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  51960. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  51961. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  51962. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  51963. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  51964. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  51965. ExpectIntGE(ECDSA_size(key), sigSz);
  51966. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  51967. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  51968. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  51969. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  51970. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  51971. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  51972. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  51973. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  51974. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  51975. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  51976. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  51977. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  51978. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  51979. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  51980. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  51981. ECDSA_SIG_free(ecdsaSig);
  51982. EC_KEY_free(key);
  51983. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  51984. return EXPECT_RESULT();
  51985. }
  51986. static int test_ECDH_compute_key(void)
  51987. {
  51988. EXPECT_DECLS;
  51989. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  51990. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  51991. EC_KEY* key1 = NULL;
  51992. EC_KEY* key2 = NULL;
  51993. EC_POINT* pub1 = NULL;
  51994. EC_POINT* pub2 = NULL;
  51995. byte secret1[32];
  51996. byte secret2[32];
  51997. int i;
  51998. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  51999. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  52000. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  52001. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52002. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  52003. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  52004. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  52005. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  52006. 0);
  52007. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  52008. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  52009. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  52010. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  52011. 0);
  52012. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  52013. 0);
  52014. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  52015. NULL), 0);
  52016. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  52017. sizeof(secret1));
  52018. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  52019. sizeof(secret2));
  52020. for (i = 0; i < (int)sizeof(secret1); i++) {
  52021. ExpectIntEQ(secret1[i], secret2[i]);
  52022. }
  52023. EC_KEY_free(key2);
  52024. EC_KEY_free(key1);
  52025. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  52026. * !WOLF_CRYPTO_CB_ONLY_ECC */
  52027. return EXPECT_RESULT();
  52028. }
  52029. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  52030. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  52031. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  52032. !defined(NO_ASN_TIME)
  52033. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  52034. int expectedDerSz)
  52035. {
  52036. EXPECT_DECLS;
  52037. X509* x509 = NULL;
  52038. BIGNUM* serial_number = NULL;
  52039. X509_NAME* name = NULL;
  52040. time_t epoch_off = 0;
  52041. ASN1_INTEGER* asn1_serial_number;
  52042. long not_before, not_after;
  52043. int derSz;
  52044. ExpectNotNull(x509 = X509_new());
  52045. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  52046. ExpectNotNull(serial_number = BN_new());
  52047. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  52048. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  52049. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  52050. /* version 3 */
  52051. ExpectIntNE(X509_set_version(x509, 2L), 0);
  52052. ExpectNotNull(name = X509_NAME_new());
  52053. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  52054. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  52055. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  52056. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  52057. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  52058. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  52059. not_before = (long)wc_Time(NULL);
  52060. not_after = not_before + (365 * 24 * 60 * 60);
  52061. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  52062. &epoch_off));
  52063. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  52064. &epoch_off));
  52065. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  52066. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  52067. ExpectIntGE(derSz, expectedDerSz);
  52068. BN_free(serial_number);
  52069. X509_NAME_free(name);
  52070. X509_free(x509);
  52071. return EXPECT_RESULT();
  52072. }
  52073. #endif
  52074. static int test_openssl_generate_key_and_cert(void)
  52075. {
  52076. EXPECT_DECLS;
  52077. #if defined(OPENSSL_EXTRA)
  52078. int expectedDerSz;
  52079. EVP_PKEY* pkey = NULL;
  52080. #ifdef HAVE_ECC
  52081. EC_KEY* ec_key = NULL;
  52082. #endif
  52083. #if !defined(NO_RSA)
  52084. int key_length = 2048;
  52085. BIGNUM* exponent = NULL;
  52086. RSA* rsa = NULL;
  52087. ExpectNotNull(pkey = EVP_PKEY_new());
  52088. ExpectNotNull(exponent = BN_new());
  52089. ExpectNotNull(rsa = RSA_new());
  52090. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  52091. #ifndef WOLFSSL_KEY_GEN
  52092. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52093. #if defined(USE_CERT_BUFFERS_1024)
  52094. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  52095. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52096. key_length = 1024;
  52097. #elif defined(USE_CERT_BUFFERS_2048)
  52098. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  52099. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  52100. #else
  52101. RSA_free(rsa);
  52102. rsa = NULL;
  52103. #endif
  52104. #else
  52105. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  52106. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  52107. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  52108. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  52109. #endif
  52110. if (rsa) {
  52111. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  52112. if (EXPECT_FAIL()) {
  52113. RSA_free(rsa);
  52114. }
  52115. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52116. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52117. expectedDerSz = 743;
  52118. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  52119. expectedDerSz), TEST_SUCCESS);
  52120. #endif
  52121. }
  52122. EVP_PKEY_free(pkey);
  52123. pkey = NULL;
  52124. BN_free(exponent);
  52125. #endif /* !NO_RSA */
  52126. #ifdef HAVE_ECC
  52127. ExpectNotNull(pkey = EVP_PKEY_new());
  52128. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  52129. #ifndef NO_WOLFSSL_STUB
  52130. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  52131. #endif
  52132. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  52133. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  52134. if (EXPECT_FAIL()) {
  52135. EC_KEY_free(ec_key);
  52136. }
  52137. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  52138. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  52139. expectedDerSz = 344;
  52140. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  52141. TEST_SUCCESS);
  52142. #endif
  52143. EVP_PKEY_free(pkey);
  52144. #endif /* HAVE_ECC */
  52145. (void)pkey;
  52146. (void)expectedDerSz;
  52147. #endif /* OPENSSL_EXTRA */
  52148. return EXPECT_RESULT();
  52149. }
  52150. static int test_stubs_are_stubs(void)
  52151. {
  52152. EXPECT_DECLS;
  52153. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  52154. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  52155. WOLFSSL_CTX* ctx = NULL;
  52156. WOLFSSL_CTX* ctxN = NULL;
  52157. #ifndef NO_WOLFSSL_CLIENT
  52158. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52159. #elif !defined(NO_WOLFSSL_SERVER)
  52160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52161. #endif
  52162. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  52163. ExpectIntEQ((int) x(z), 0)
  52164. /* test logic, all stubs return same result regardless of ctx being NULL
  52165. * as there are no sanity checks, it's just a stub! If at some
  52166. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  52167. * if invalid inputs are supplied. Test calling both
  52168. * with and without valid inputs, if a stub functionality remains unchanged.
  52169. */
  52170. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  52171. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  52172. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  52173. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  52174. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  52175. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  52176. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  52177. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  52178. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  52179. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  52180. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  52181. wolfSSL_CTX_free(ctx);
  52182. ctx = NULL;
  52183. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  52184. * !NO_WOLFSSL_SERVER) */
  52185. return EXPECT_RESULT();
  52186. }
  52187. static int test_CONF_modules_xxx(void)
  52188. {
  52189. int res = TEST_SKIPPED;
  52190. #if defined(OPENSSL_EXTRA)
  52191. CONF_modules_free();
  52192. CONF_modules_unload(0);
  52193. CONF_modules_unload(1);
  52194. CONF_modules_unload(-1);
  52195. res = TEST_SUCCESS;
  52196. #endif /* OPENSSL_EXTRA */
  52197. return res;
  52198. }
  52199. static int test_CRYPTO_set_dynlock_xxx(void)
  52200. {
  52201. int res = TEST_SKIPPED;
  52202. #if defined(OPENSSL_EXTRA)
  52203. CRYPTO_set_dynlock_create_callback(
  52204. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  52205. CRYPTO_set_dynlock_create_callback(
  52206. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  52207. CRYPTO_set_dynlock_destroy_callback(
  52208. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  52209. CRYPTO_set_dynlock_destroy_callback(
  52210. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  52211. CRYPTO_set_dynlock_lock_callback(
  52212. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  52213. CRYPTO_set_dynlock_lock_callback(
  52214. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  52215. res = TEST_SUCCESS;
  52216. #endif /* OPENSSL_EXTRA */
  52217. return res;
  52218. }
  52219. static int test_CRYPTO_THREADID_xxx(void)
  52220. {
  52221. EXPECT_DECLS;
  52222. #if defined(OPENSSL_EXTRA)
  52223. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  52224. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  52225. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  52226. #endif /* OPENSSL_EXTRA */
  52227. return EXPECT_RESULT();
  52228. }
  52229. static int test_ENGINE_cleanup(void)
  52230. {
  52231. int res = TEST_SKIPPED;
  52232. #if defined(OPENSSL_EXTRA)
  52233. ENGINE_cleanup();
  52234. res = TEST_SUCCESS;
  52235. #endif /* OPENSSL_EXTRA */
  52236. return res;
  52237. }
  52238. static int test_wolfSSL_CTX_LoadCRL(void)
  52239. {
  52240. EXPECT_DECLS;
  52241. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  52242. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  52243. WOLFSSL_CTX* ctx = NULL;
  52244. WOLFSSL* ssl = NULL;
  52245. const char* badPath = "dummypath";
  52246. const char* validPath = "./certs/crl";
  52247. const char* validFilePath = "./certs/crl/cliCrl.pem";
  52248. const char* issuerCert = "./certs/client-cert.pem";
  52249. int derType = WOLFSSL_FILETYPE_ASN1;
  52250. int pemType = WOLFSSL_FILETYPE_PEM;
  52251. #ifdef HAVE_CRL_MONITOR
  52252. int monitor = WOLFSSL_CRL_MONITOR;
  52253. #else
  52254. int monitor = 0;
  52255. #endif
  52256. WOLFSSL_CERT_MANAGER* cm = NULL;
  52257. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  52258. BAD_FUNC_ARG)
  52259. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  52260. NOT_COMPILED_IN)
  52261. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  52262. WOLFSSL_SUCCESS)
  52263. #ifndef NO_WOLFSSL_CLIENT
  52264. #define NEW_CTX(ctx) ExpectNotNull( \
  52265. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  52266. #elif !defined(NO_WOLFSSL_SERVER)
  52267. #define NEW_CTX(ctx) ExpectNotNull( \
  52268. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  52269. #else
  52270. #define NEW_CTX(ctx) return
  52271. #endif
  52272. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  52273. NEW_CTX(ctx);
  52274. #ifndef HAVE_CRL_MONITOR
  52275. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  52276. wolfSSL_CTX_free(ctx);
  52277. NEW_CTX(ctx);
  52278. #endif
  52279. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  52280. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  52281. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  52282. wolfSSL_CTX_free(ctx);
  52283. ctx = NULL;
  52284. NEW_CTX(ctx);
  52285. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  52286. WOLFSSL_SUCCESS);
  52287. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  52288. wolfSSL_CTX_free(ctx);
  52289. ctx = NULL;
  52290. NEW_CTX(ctx);
  52291. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  52292. WOLFSSL_SUCCESS);
  52293. ExpectNotNull(ssl = wolfSSL_new(ctx));
  52294. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  52295. wolfSSL_free(ssl);
  52296. ssl = NULL;
  52297. wolfSSL_CTX_free(ctx);
  52298. ctx = NULL;
  52299. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  52300. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  52301. WOLFSSL_SUCCESS);
  52302. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  52303. WOLFSSL_SUCCESS);
  52304. wolfSSL_CertManagerFree(cm);
  52305. #endif
  52306. return EXPECT_RESULT();
  52307. }
  52308. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  52309. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  52310. {
  52311. EXPECT_DECLS;
  52312. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  52313. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  52314. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  52315. return EXPECT_RESULT();
  52316. }
  52317. #endif
  52318. static int test_multiple_crls_same_issuer(void)
  52319. {
  52320. EXPECT_DECLS;
  52321. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  52322. test_ssl_cbf client_cbs, server_cbs;
  52323. struct {
  52324. const char* server_cert;
  52325. const char* server_key;
  52326. } test_params[] = {
  52327. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  52328. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  52329. };
  52330. size_t i;
  52331. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  52332. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  52333. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  52334. server_cbs.certPemFile = test_params[i].server_cert;
  52335. server_cbs.keyPemFile = test_params[i].server_key;
  52336. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  52337. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  52338. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  52339. &server_cbs, NULL), TEST_FAIL);
  52340. }
  52341. #endif
  52342. return EXPECT_RESULT();
  52343. }
  52344. static int test_SetTmpEC_DHE_Sz(void)
  52345. {
  52346. EXPECT_DECLS;
  52347. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  52348. WOLFSSL_CTX *ctx = NULL;
  52349. WOLFSSL *ssl = NULL;
  52350. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52351. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  52352. ExpectNotNull(ssl = wolfSSL_new(ctx));
  52353. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  52354. wolfSSL_free(ssl);
  52355. wolfSSL_CTX_free(ctx);
  52356. #endif
  52357. return EXPECT_RESULT();
  52358. }
  52359. static int test_wolfSSL_CTX_get0_privatekey(void)
  52360. {
  52361. EXPECT_DECLS;
  52362. #ifdef OPENSSL_ALL
  52363. WOLFSSL_CTX* ctx = NULL;
  52364. (void)ctx;
  52365. #ifndef NO_RSA
  52366. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52367. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  52368. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  52369. WOLFSSL_FILETYPE_PEM));
  52370. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  52371. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  52372. WOLFSSL_FILETYPE_PEM));
  52373. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  52374. wolfSSL_CTX_free(ctx);
  52375. ctx = NULL;
  52376. #endif
  52377. #ifdef HAVE_ECC
  52378. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52379. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  52380. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  52381. WOLFSSL_FILETYPE_PEM));
  52382. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  52383. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  52384. WOLFSSL_FILETYPE_PEM));
  52385. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  52386. wolfSSL_CTX_free(ctx);
  52387. #endif
  52388. #endif
  52389. return EXPECT_RESULT();
  52390. }
  52391. static int test_wolfSSL_dtls_set_mtu(void)
  52392. {
  52393. EXPECT_DECLS;
  52394. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  52395. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  52396. WOLFSSL_CTX* ctx = NULL;
  52397. WOLFSSL* ssl = NULL;
  52398. const char* testCertFile;
  52399. const char* testKeyFile;
  52400. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  52401. #ifndef NO_RSA
  52402. testCertFile = svrCertFile;
  52403. testKeyFile = svrKeyFile;
  52404. #elif defined(HAVE_ECC)
  52405. testCertFile = eccCertFile;
  52406. testKeyFile = eccKeyFile;
  52407. #endif
  52408. if (testCertFile != NULL && testKeyFile != NULL) {
  52409. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  52410. WOLFSSL_FILETYPE_PEM));
  52411. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52412. WOLFSSL_FILETYPE_PEM));
  52413. }
  52414. ExpectNotNull(ssl = wolfSSL_new(ctx));
  52415. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  52416. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  52417. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  52418. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  52419. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  52420. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  52421. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  52422. wolfSSL_free(ssl);
  52423. wolfSSL_CTX_free(ctx);
  52424. #endif
  52425. return EXPECT_RESULT();
  52426. }
  52427. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  52428. defined(WOLFSSL_DTLS)
  52429. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  52430. enum HandShakeType hsType, word16 length)
  52431. {
  52432. size_t idx = 0;
  52433. byte* l;
  52434. /* record layer */
  52435. /* handshake type */
  52436. out[idx++] = handshake;
  52437. /* protocol version */
  52438. out[idx++] = 0xfe;
  52439. out[idx++] = 0xfd; /* DTLS 1.2 */
  52440. /* epoch 0 */
  52441. XMEMSET(out + idx, 0, 2);
  52442. idx += 2;
  52443. /* sequence number */
  52444. XMEMSET(out + idx, 0, 6);
  52445. c32toa(seq, out + idx + 2);
  52446. idx += 6;
  52447. /* length in BE */
  52448. if (length)
  52449. c16toa(length, out + idx);
  52450. else
  52451. c16toa(outSz - idx - 2, out + idx);
  52452. idx += 2;
  52453. /* handshake layer */
  52454. /* handshake type */
  52455. out[idx++] = (byte)hsType;
  52456. /* length */
  52457. l = out + idx;
  52458. idx += 3;
  52459. /* message seq */
  52460. c16toa(0, out + idx);
  52461. idx += 2;
  52462. /* frag offset */
  52463. c32to24(0, out + idx);
  52464. idx += 3;
  52465. /* frag length */
  52466. c32to24((word32)outSz - (word32)idx - 3, l);
  52467. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  52468. idx += 3;
  52469. XMEMSET(out + idx, 0, outSz - idx);
  52470. }
  52471. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  52472. {
  52473. byte msg[] = "This is a msg for the client";
  52474. byte reply[40];
  52475. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  52476. reply[sizeof(reply) - 1] = '\0';
  52477. fprintf(stderr, "Client message: %s\n", reply);
  52478. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  52479. }
  52480. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  52481. {
  52482. byte ch[50];
  52483. int fd = wolfSSL_get_fd(ssl);
  52484. byte msg[] = "This is a msg for the server";
  52485. byte reply[40];
  52486. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  52487. /* Server should ignore this datagram */
  52488. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  52489. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  52490. /* Server should ignore this datagram */
  52491. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  52492. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  52493. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  52494. reply[sizeof(reply) - 1] = '\0';
  52495. fprintf(stderr, "Server response: %s\n", reply);
  52496. }
  52497. static int test_wolfSSL_dtls_plaintext(void)
  52498. {
  52499. callback_functions func_cb_client;
  52500. callback_functions func_cb_server;
  52501. size_t i;
  52502. struct test_params {
  52503. method_provider client_meth;
  52504. method_provider server_meth;
  52505. ssl_callback on_result_server;
  52506. ssl_callback on_result_client;
  52507. } params[] = {
  52508. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  52509. test_wolfSSL_dtls_plaintext_server,
  52510. test_wolfSSL_dtls_plaintext_client},
  52511. };
  52512. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  52513. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52514. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52515. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52516. func_cb_server.method = params[i].server_meth;
  52517. func_cb_client.method = params[i].client_meth;
  52518. func_cb_client.on_result = params[i].on_result_client;
  52519. func_cb_server.on_result = params[i].on_result_server;
  52520. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52521. if (!func_cb_client.return_code)
  52522. return TEST_FAIL;
  52523. if (!func_cb_server.return_code)
  52524. return TEST_FAIL;
  52525. }
  52526. return TEST_RES_CHECK(1);
  52527. }
  52528. #else
  52529. static int test_wolfSSL_dtls_plaintext(void) {
  52530. return TEST_SKIPPED;
  52531. }
  52532. #endif
  52533. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  52534. defined(WOLFSSL_DTLS)
  52535. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  52536. {
  52537. byte b[1100]; /* buffer for the messages to send */
  52538. size_t idx = 0;
  52539. size_t seq_offset = 0;
  52540. size_t msg_offset = 0;
  52541. int i;
  52542. int fd = wolfSSL_get_fd(ssl);
  52543. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  52544. word32 seq_number = 100; /* start high so server definitely reads this */
  52545. word16 msg_number = 50; /* start high so server has to buffer this */
  52546. AssertIntEQ(ret, 1);
  52547. /* Now let's start spamming the peer with fragments it needs to store */
  52548. XMEMSET(b, -1, sizeof(b));
  52549. /* record layer */
  52550. /* handshake type */
  52551. b[idx++] = 22;
  52552. /* protocol version */
  52553. b[idx++] = 0xfe;
  52554. b[idx++] = 0xfd; /* DTLS 1.2 */
  52555. /* epoch 0 */
  52556. XMEMSET(b + idx, 0, 2);
  52557. idx += 2;
  52558. /* sequence number */
  52559. XMEMSET(b + idx, 0, 6);
  52560. seq_offset = idx + 2; /* increment only the low 32 bits */
  52561. idx += 6;
  52562. /* static length in BE */
  52563. c16toa(42, b + idx);
  52564. idx += 2;
  52565. /* handshake layer */
  52566. /* cert type */
  52567. b[idx++] = 11;
  52568. /* length */
  52569. c32to24(1000, b + idx);
  52570. idx += 3;
  52571. /* message seq */
  52572. c16toa(0, b + idx);
  52573. msg_offset = idx;
  52574. idx += 2;
  52575. /* frag offset */
  52576. c32to24(500, b + idx);
  52577. idx += 3;
  52578. /* frag length */
  52579. c32to24(30, b + idx);
  52580. idx += 3;
  52581. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  52582. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  52583. seq_number++, msg_number++, i++) {
  52584. struct timespec delay;
  52585. XMEMSET(&delay, 0, sizeof(delay));
  52586. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  52587. c32toa(seq_number, b + seq_offset);
  52588. c16toa(msg_number, b + msg_offset);
  52589. ret = (int)send(fd, b, 55, 0);
  52590. nanosleep(&delay, NULL);
  52591. }
  52592. }
  52593. #ifdef WOLFSSL_DTLS13
  52594. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  52595. {
  52596. const word16 sendCountMax = 100;
  52597. byte b[150]; /* buffer for the messages to send */
  52598. size_t idx = 0;
  52599. size_t msg_offset = 0;
  52600. int fd = wolfSSL_get_fd(ssl);
  52601. word16 msg_number = 10; /* start high so server has to buffer this */
  52602. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  52603. AssertIntEQ(ret, 1);
  52604. /* Now let's start spamming the peer with fragments it needs to store */
  52605. XMEMSET(b, -1, sizeof(b));
  52606. /* handshake type */
  52607. b[idx++] = 11;
  52608. /* length */
  52609. c32to24(10000, b + idx);
  52610. idx += 3;
  52611. /* message_seq */
  52612. msg_offset = idx;
  52613. idx += 2;
  52614. /* fragment_offset */
  52615. c32to24(5000, b + idx);
  52616. idx += 3;
  52617. /* fragment_length */
  52618. c32to24(100, b + idx);
  52619. idx += 3;
  52620. /* fragment contents */
  52621. idx += 100;
  52622. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  52623. byte sendBuf[150];
  52624. int sendSz = sizeof(sendBuf);
  52625. struct timespec delay;
  52626. XMEMSET(&delay, 0, sizeof(delay));
  52627. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  52628. c16toa(msg_number, b + msg_offset);
  52629. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  52630. (int)idx, handshake, 0, 0, 0);
  52631. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  52632. nanosleep(&delay, NULL);
  52633. }
  52634. }
  52635. #endif
  52636. static int test_wolfSSL_dtls_fragments(void)
  52637. {
  52638. EXPECT_DECLS;
  52639. callback_functions func_cb_client;
  52640. callback_functions func_cb_server;
  52641. size_t i;
  52642. struct test_params {
  52643. method_provider client_meth;
  52644. method_provider server_meth;
  52645. ssl_callback spammer;
  52646. } params[] = {
  52647. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  52648. test_wolfSSL_dtls12_fragments_spammer},
  52649. #ifdef WOLFSSL_DTLS13
  52650. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  52651. test_wolfSSL_dtls13_fragments_spammer},
  52652. #endif
  52653. };
  52654. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  52655. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52656. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52657. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52658. func_cb_server.method = params[i].server_meth;
  52659. func_cb_client.method = params[i].client_meth;
  52660. func_cb_client.ssl_ready = params[i].spammer;
  52661. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52662. ExpectFalse(func_cb_client.return_code);
  52663. ExpectFalse(func_cb_server.return_code);
  52664. /* The socket should be closed by the server resulting in a
  52665. * socket error, fatal error or reading a close notify alert */
  52666. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  52667. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  52668. func_cb_client.last_err != FATAL_ERROR) {
  52669. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  52670. }
  52671. /* Check the server returned an error indicating the msg buffer
  52672. * was full */
  52673. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  52674. if (EXPECT_FAIL())
  52675. break;
  52676. }
  52677. return EXPECT_RESULT();
  52678. }
  52679. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  52680. {
  52681. int fd, ret;
  52682. byte alert_msg[] = {
  52683. 0x15, /* alert type */
  52684. 0xfe, 0xfd, /* version */
  52685. 0x00, 0x00, /* epoch */
  52686. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  52687. 0x00, 0x02, /* length */
  52688. 0x02, /* level: fatal */
  52689. 0x46 /* protocol version */
  52690. };
  52691. fd = wolfSSL_get_fd(ssl);
  52692. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  52693. AssertIntGT(ret, 0);
  52694. }
  52695. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  52696. {
  52697. callback_functions client_cbs, server_cbs;
  52698. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  52699. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  52700. client_cbs.doUdp = server_cbs.doUdp = 1;
  52701. if (version12) {
  52702. client_cbs.method = wolfDTLSv1_2_client_method;
  52703. server_cbs.method = wolfDTLSv1_2_server_method;
  52704. }
  52705. else {
  52706. #ifdef WOLFSSL_DTLS13
  52707. client_cbs.method = wolfDTLSv1_3_client_method;
  52708. server_cbs.method = wolfDTLSv1_3_server_method;
  52709. #else
  52710. return TEST_SKIPPED;
  52711. #endif /* WOLFSSL_DTLS13 */
  52712. }
  52713. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  52714. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  52715. if (!client_cbs.return_code)
  52716. return TEST_FAIL;
  52717. if (!server_cbs.return_code)
  52718. return TEST_FAIL;
  52719. return TEST_SUCCESS;
  52720. }
  52721. static int test_wolfSSL_ignore_alert_before_cookie(void)
  52722. {
  52723. int ret;
  52724. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  52725. if (ret != 0)
  52726. return ret;
  52727. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  52728. if (ret != 0)
  52729. return ret;
  52730. return 0;
  52731. }
  52732. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  52733. {
  52734. int ret;
  52735. int fd;
  52736. byte bad_msg[] = {
  52737. 0x17, /* app data */
  52738. 0xaa, 0xfd, /* bad version */
  52739. 0x00, 0x01, /* epoch 1 */
  52740. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  52741. 0x00, 0x26, /* length: 38 bytes */
  52742. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  52743. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  52744. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  52745. 0x03, 0x18, 0x72
  52746. };
  52747. fd = wolfSSL_get_fd(ssl);
  52748. AssertIntGE(fd, 0);
  52749. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  52750. AssertIntEQ(ret, sizeof(bad_msg));
  52751. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  52752. AssertIntEQ(ret, sizeof("badrecordtest"));
  52753. }
  52754. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  52755. {
  52756. byte buf[100];
  52757. int ret;
  52758. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  52759. AssertIntGT(ret, 0);
  52760. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  52761. }
  52762. static int _test_wolfSSL_dtls_bad_record(
  52763. method_provider client_method, method_provider server_method)
  52764. {
  52765. callback_functions client_cbs, server_cbs;
  52766. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  52767. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  52768. client_cbs.doUdp = server_cbs.doUdp = 1;
  52769. client_cbs.method = client_method;
  52770. server_cbs.method = server_method;
  52771. client_cbs.on_result = test_wolfSSL_send_bad_record;
  52772. server_cbs.on_result = test_wolfSSL_read_string;
  52773. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  52774. if (!client_cbs.return_code)
  52775. return TEST_FAIL;
  52776. if (!server_cbs.return_code)
  52777. return TEST_FAIL;
  52778. return TEST_SUCCESS;
  52779. }
  52780. static int test_wolfSSL_dtls_bad_record(void)
  52781. {
  52782. int ret;
  52783. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  52784. wolfDTLSv1_2_server_method);
  52785. #ifdef WOLFSSL_DTLS13
  52786. if (ret != TEST_SUCCESS)
  52787. return ret;
  52788. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  52789. wolfDTLSv1_3_server_method);
  52790. #else
  52791. return ret;
  52792. #endif /* WOLFSSL_DTLS13 */
  52793. }
  52794. #else
  52795. static int test_wolfSSL_dtls_fragments(void) {
  52796. return TEST_SKIPPED;
  52797. }
  52798. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  52799. return TEST_SKIPPED;
  52800. }
  52801. static int test_wolfSSL_dtls_bad_record(void) {
  52802. return TEST_SKIPPED;
  52803. }
  52804. #endif
  52805. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  52806. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  52807. defined(HAVE_IO_TESTS_DEPENDENCIES)
  52808. static byte test_AEAD_fail_decryption = 0;
  52809. static byte test_AEAD_seq_num = 0;
  52810. static byte test_AEAD_done = 0;
  52811. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  52812. {
  52813. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  52814. if (ret > 0) {
  52815. if (test_AEAD_fail_decryption) {
  52816. /* Modify the packet to trigger a decryption failure */
  52817. buf[ret/2] ^= 0xFF;
  52818. if (test_AEAD_fail_decryption == 1)
  52819. test_AEAD_fail_decryption = 0;
  52820. }
  52821. }
  52822. (void)ctx;
  52823. return ret;
  52824. }
  52825. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  52826. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  52827. {
  52828. if (sendLimit)
  52829. w64Zero(sendLimit);
  52830. switch (ssl->specs.bulk_cipher_algorithm) {
  52831. case wolfssl_aes_gcm:
  52832. if (sendLimit)
  52833. *sendLimit = AEAD_AES_LIMIT;
  52834. FALL_THROUGH;
  52835. case wolfssl_chacha:
  52836. if (hardLimit)
  52837. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  52838. if (keyUpdateLimit)
  52839. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  52840. break;
  52841. case wolfssl_aes_ccm:
  52842. if (sendLimit)
  52843. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  52844. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  52845. if (hardLimit)
  52846. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  52847. if (keyUpdateLimit)
  52848. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  52849. }
  52850. else {
  52851. if (hardLimit)
  52852. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  52853. if (keyUpdateLimit)
  52854. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  52855. }
  52856. break;
  52857. default:
  52858. fprintf(stderr, "Unrecognized bulk cipher");
  52859. AssertFalse(1);
  52860. break;
  52861. }
  52862. }
  52863. static void test_AEAD_limit_client(WOLFSSL* ssl)
  52864. {
  52865. int ret;
  52866. int i;
  52867. int didReKey = 0;
  52868. char msgBuf[20];
  52869. w64wrapper hardLimit;
  52870. w64wrapper keyUpdateLimit;
  52871. w64wrapper counter;
  52872. w64wrapper sendLimit;
  52873. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  52874. w64Zero(&counter);
  52875. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  52876. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  52877. for (i = 0; i < 10; i++) {
  52878. /* Test some failed decryptions */
  52879. test_AEAD_fail_decryption = 1;
  52880. w64Increment(&counter);
  52881. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  52882. /* Should succeed since decryption failures are dropped */
  52883. AssertIntGT(ret, 0);
  52884. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  52885. }
  52886. test_AEAD_fail_decryption = 1;
  52887. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  52888. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  52889. /* 100 read calls should be enough to complete the key update */
  52890. w64Zero(&counter);
  52891. for (i = 0; i < 100; i++) {
  52892. /* Key update should be sent and negotiated */
  52893. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  52894. AssertIntGT(ret, 0);
  52895. /* Epoch after one key update is 4 */
  52896. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  52897. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  52898. didReKey = 1;
  52899. break;
  52900. }
  52901. }
  52902. AssertTrue(didReKey);
  52903. if (!w64IsZero(sendLimit)) {
  52904. /* Test the sending limit for AEAD ciphers */
  52905. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  52906. test_AEAD_seq_num = 1;
  52907. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  52908. AssertIntGT(ret, 0);
  52909. didReKey = 0;
  52910. w64Zero(&counter);
  52911. /* 100 read calls should be enough to complete the key update */
  52912. for (i = 0; i < 100; i++) {
  52913. /* Key update should be sent and negotiated */
  52914. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  52915. AssertIntGT(ret, 0);
  52916. /* Epoch after another key update is 5 */
  52917. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  52918. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  52919. didReKey = 1;
  52920. break;
  52921. }
  52922. }
  52923. AssertTrue(didReKey);
  52924. }
  52925. test_AEAD_fail_decryption = 2;
  52926. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  52927. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  52928. /* Connection should fail with a DECRYPT_ERROR */
  52929. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  52930. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  52931. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  52932. test_AEAD_done = 1;
  52933. }
  52934. int counter = 0;
  52935. static void test_AEAD_limit_server(WOLFSSL* ssl)
  52936. {
  52937. char msgBuf[] = "Sending data";
  52938. int ret = WOLFSSL_SUCCESS;
  52939. w64wrapper sendLimit;
  52940. SOCKET_T fd = wolfSSL_get_fd(ssl);
  52941. struct timespec delay;
  52942. XMEMSET(&delay, 0, sizeof(delay));
  52943. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  52944. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  52945. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  52946. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  52947. while (!test_AEAD_done && ret > 0) {
  52948. counter++;
  52949. if (test_AEAD_seq_num) {
  52950. /* We need to update the seq number so that we can understand the
  52951. * peer. Otherwise we will incorrectly interpret the seq number. */
  52952. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  52953. AssertNotNull(e);
  52954. e->nextPeerSeqNumber = sendLimit;
  52955. test_AEAD_seq_num = 0;
  52956. }
  52957. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  52958. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  52959. nanosleep(&delay, NULL);
  52960. }
  52961. }
  52962. static int test_wolfSSL_dtls_AEAD_limit(void)
  52963. {
  52964. callback_functions func_cb_client;
  52965. callback_functions func_cb_server;
  52966. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  52967. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  52968. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  52969. func_cb_server.method = wolfDTLSv1_3_server_method;
  52970. func_cb_client.method = wolfDTLSv1_3_client_method;
  52971. func_cb_server.on_result = test_AEAD_limit_server;
  52972. func_cb_client.on_result = test_AEAD_limit_client;
  52973. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  52974. if (!func_cb_client.return_code)
  52975. return TEST_FAIL;
  52976. if (!func_cb_server.return_code)
  52977. return TEST_FAIL;
  52978. return TEST_SUCCESS;
  52979. }
  52980. #else
  52981. static int test_wolfSSL_dtls_AEAD_limit(void)
  52982. {
  52983. return TEST_SKIPPED;
  52984. }
  52985. #endif
  52986. #if defined(WOLFSSL_DTLS) && \
  52987. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  52988. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  52989. {
  52990. int fd, ret;
  52991. byte ch_msg[] = {
  52992. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  52993. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  52994. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  52995. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  52996. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  52997. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  52998. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  52999. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53000. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53001. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53002. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53003. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53004. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53005. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  53006. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  53007. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  53008. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  53009. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  53010. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  53011. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  53012. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  53013. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  53014. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  53015. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  53016. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  53017. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  53018. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  53019. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  53020. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  53021. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  53022. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  53023. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  53024. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  53025. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  53026. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  53027. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  53028. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  53029. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  53030. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  53031. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  53032. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  53033. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  53034. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  53035. 0x31, 0x68, 0x4c
  53036. };
  53037. fd = wolfSSL_get_fd(ssl);
  53038. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  53039. AssertIntGT(ret, 0);
  53040. /* consume the HRR otherwise handshake will fail */
  53041. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  53042. AssertIntGT(ret, 0);
  53043. }
  53044. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53045. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  53046. {
  53047. int fd, ret;
  53048. byte ch_msh_invalid_cookie[] = {
  53049. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  53050. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  53051. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  53052. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  53053. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  53054. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  53055. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  53056. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  53057. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  53058. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  53059. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  53060. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  53061. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  53062. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  53063. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53064. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53065. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53066. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53067. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  53068. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  53069. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  53070. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  53071. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  53072. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  53073. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  53074. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  53075. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  53076. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  53077. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  53078. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  53079. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  53080. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  53081. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  53082. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  53083. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  53084. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  53085. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  53086. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  53087. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  53088. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  53089. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  53090. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  53091. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  53092. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  53093. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  53094. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  53095. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  53096. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  53097. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  53098. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  53099. 0x23, 0x00, 0x00
  53100. };
  53101. byte alert_reply[50];
  53102. byte expected_alert_reply[] = {
  53103. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  53104. 0x02, 0x02, 0x2f
  53105. };
  53106. fd = wolfSSL_get_fd(ssl);
  53107. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  53108. AssertIntGT(ret, 0);
  53109. /* should reply with an illegal_parameter reply */
  53110. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  53111. AssertIntEQ(ret, sizeof(expected_alert_reply));
  53112. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  53113. }
  53114. #endif
  53115. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  53116. {
  53117. #ifndef NO_MD5
  53118. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  53119. #elif !defined(NO_SHA)
  53120. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  53121. #elif !defined(NO_SHA256)
  53122. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  53123. #else
  53124. #error "We need a digest to hash the WOLFSSL object"
  53125. #endif
  53126. byte hashBuf[WC_MAX_DIGEST_SIZE];
  53127. wc_HashAlg hash;
  53128. const TLSX* exts = ssl->extensions;
  53129. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  53130. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  53131. * InitHandshakeHashes */
  53132. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  53133. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  53134. /* Following fields are not important to compare */
  53135. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53136. sslCopy.buffers.inputBuffer.buffer = NULL;
  53137. sslCopy.buffers.inputBuffer.bufferSize = 0;
  53138. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  53139. sslCopy.buffers.inputBuffer.offset = 0;
  53140. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  53141. sslCopy.buffers.outputBuffer.buffer = NULL;
  53142. sslCopy.buffers.outputBuffer.bufferSize = 0;
  53143. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  53144. sslCopy.buffers.outputBuffer.offset = 0;
  53145. sslCopy.error = 0;
  53146. sslCopy.curSize = 0;
  53147. sslCopy.keys.curSeq_lo = 0;
  53148. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  53149. #ifdef WOLFSSL_DTLS13
  53150. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  53151. sslCopy.dtls13FastTimeout = 0;
  53152. #endif
  53153. sslCopy.keys.dtls_peer_handshake_number = 0;
  53154. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  53155. sslCopy.hsHashes = NULL;
  53156. #ifdef WOLFSSL_ASYNC_IO
  53157. #ifdef WOLFSSL_ASYNC_CRYPT
  53158. sslCopy.asyncDev = NULL;
  53159. #endif
  53160. sslCopy.async = NULL;
  53161. #endif
  53162. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  53163. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  53164. /* hash extension list */
  53165. while (exts != NULL) {
  53166. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  53167. exts = exts->next;
  53168. }
  53169. /* Hash suites */
  53170. if (sslCopy.suites != NULL) {
  53171. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  53172. sizeof(struct Suites)), 0);
  53173. }
  53174. /* Hash hsHashes */
  53175. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  53176. sizeof(*hsHashes)), 0);
  53177. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  53178. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  53179. return MakeWordFromHash(hashBuf);
  53180. }
  53181. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  53182. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  53183. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  53184. char *buf, int sz, void *ctx)
  53185. {
  53186. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  53187. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  53188. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  53189. }
  53190. else {
  53191. return WOLFSSL_CBIO_ERR_WANT_READ;
  53192. }
  53193. }
  53194. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  53195. {
  53196. /* Compare the ssl object before and after one ClientHello msg */
  53197. SOCKET_T fd = wolfSSL_get_fd(ssl);
  53198. int res;
  53199. int err;
  53200. word32 initHash;
  53201. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  53202. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  53203. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  53204. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  53205. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  53206. (void)initHash;
  53207. res = tcp_select(fd, 5);
  53208. /* We are expecting a msg. A timeout indicates failure. */
  53209. AssertIntEQ(res, TEST_RECV_READY);
  53210. res = wolfSSL_accept(ssl);
  53211. err = wolfSSL_get_error(ssl, res);
  53212. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  53213. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  53214. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  53215. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  53216. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  53217. }
  53218. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53219. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  53220. {
  53221. int ret;
  53222. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  53223. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  53224. test_wolfSSL_dtls_compare_stateless(ssl);
  53225. }
  53226. #endif
  53227. static int test_wolfSSL_dtls_stateless(void)
  53228. {
  53229. callback_functions client_cbs, server_cbs;
  53230. size_t i;
  53231. struct {
  53232. method_provider client_meth;
  53233. method_provider server_meth;
  53234. ssl_callback client_ssl_ready;
  53235. ssl_callback server_ssl_ready;
  53236. } test_params[] = {
  53237. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  53238. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  53239. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  53240. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53241. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  53242. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  53243. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  53244. #endif
  53245. };
  53246. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  53247. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  53248. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  53249. client_cbs.doUdp = server_cbs.doUdp = 1;
  53250. client_cbs.method = test_params[i].client_meth;
  53251. server_cbs.method = test_params[i].server_meth;
  53252. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  53253. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  53254. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  53255. if (!client_cbs.return_code)
  53256. return TEST_FAIL;
  53257. if (!server_cbs.return_code)
  53258. return TEST_FAIL;
  53259. }
  53260. return TEST_SUCCESS;
  53261. }
  53262. #else
  53263. static int test_wolfSSL_dtls_stateless(void)
  53264. {
  53265. return TEST_SKIPPED;
  53266. }
  53267. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  53268. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  53269. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  53270. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  53271. !defined(WOLFSSL_NO_CLIENT_AUTH))
  53272. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  53273. {
  53274. int ret;
  53275. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  53276. fprintf(stderr, "loading cert %s failed\n", certA);
  53277. fprintf(stderr, "Error: (%d): %s\n", ret,
  53278. wolfSSL_ERR_reason_error_string(ret));
  53279. return -1;
  53280. }
  53281. return 0;
  53282. }
  53283. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  53284. {
  53285. int ret;
  53286. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  53287. != WOLFSSL_SUCCESS) {
  53288. fprintf(stderr, "could not verify the cert: %s\n", certA);
  53289. fprintf(stderr, "Error: (%d): %s\n", ret,
  53290. wolfSSL_ERR_reason_error_string(ret));
  53291. return -1;
  53292. }
  53293. else {
  53294. fprintf(stderr, "successfully verified: %s\n", certA);
  53295. }
  53296. return 0;
  53297. }
  53298. #define LOAD_ONE_CA(a, b, c, d) \
  53299. do { \
  53300. (a) = load_ca_into_cm(c, d); \
  53301. if ((a) != 0) \
  53302. return (b); \
  53303. else \
  53304. (b)--; \
  53305. } while(0)
  53306. #define VERIFY_ONE_CERT(a, b, c, d) \
  53307. do { \
  53308. (a) = verify_cert_with_cm(c, d);\
  53309. if ((a) != 0) \
  53310. return (b); \
  53311. else \
  53312. (b)--; \
  53313. } while(0)
  53314. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  53315. {
  53316. int ret;
  53317. int i = -1;
  53318. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  53319. char chainGArr[9][50] = {"certs/ca-cert.pem",
  53320. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  53321. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  53322. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  53323. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  53324. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  53325. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  53326. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  53327. "certs/test-pathlen/chainG-entity.pem"};
  53328. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  53329. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  53330. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  53331. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  53332. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  53333. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  53334. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  53335. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  53336. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  53337. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  53338. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  53339. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  53340. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  53341. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  53342. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  53343. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  53344. /* test validating the entity twice, should have no effect on pathLen since
  53345. * entity/leaf cert */
  53346. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  53347. return ret;
  53348. }
  53349. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  53350. {
  53351. int ret;
  53352. int i = -1;
  53353. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  53354. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  53355. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  53356. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  53357. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  53358. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  53359. */
  53360. char chainHArr[6][50] = {"certs/ca-cert.pem",
  53361. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  53362. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  53363. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  53364. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  53365. "certs/test-pathlen/chainH-entity.pem"};
  53366. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  53367. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  53368. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  53369. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  53370. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  53371. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  53372. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  53373. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  53374. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  53375. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  53376. return ret;
  53377. }
  53378. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  53379. {
  53380. int ret;
  53381. int i = -1;
  53382. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  53383. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  53384. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  53385. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  53386. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  53387. */
  53388. char chainIArr[5][50] = {"certs/ca-cert.pem",
  53389. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  53390. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  53391. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  53392. "certs/test-pathlen/chainI-entity.pem"};
  53393. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  53394. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  53395. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  53396. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  53397. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  53398. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  53399. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  53400. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  53401. return ret;
  53402. }
  53403. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  53404. {
  53405. int ret;
  53406. int i = -1;
  53407. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  53408. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  53409. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  53410. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  53411. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  53412. */
  53413. char chainJArr[6][50] = {"certs/ca-cert.pem",
  53414. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  53415. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  53416. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  53417. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  53418. "certs/test-pathlen/chainJ-entity.pem"};
  53419. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  53420. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  53421. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  53422. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  53423. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  53424. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  53425. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  53426. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  53427. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  53428. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  53429. return ret;
  53430. }
  53431. static int test_various_pathlen_chains(void)
  53432. {
  53433. EXPECT_DECLS;
  53434. WOLFSSL_CERT_MANAGER* cm = NULL;
  53435. /* Test chain G (large chain with varying pathLens) */
  53436. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53437. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  53438. ExpectIntEQ(test_chainG(cm), -1);
  53439. #else
  53440. ExpectIntEQ(test_chainG(cm), 0);
  53441. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  53442. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53443. wolfSSL_CertManagerFree(cm);
  53444. /* end test chain G */
  53445. /* Test chain H (5 chain with same pathLens) */
  53446. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53447. ExpectIntLT(test_chainH(cm), 0);
  53448. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53449. wolfSSL_CertManagerFree(cm);
  53450. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53451. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53452. wolfSSL_CertManagerFree(cm);
  53453. /* end test chain H */
  53454. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  53455. * followed by 2 ICA's, should pass) */
  53456. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53457. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  53458. ExpectIntEQ(test_chainI(cm), -1);
  53459. #else
  53460. ExpectIntEQ(test_chainI(cm), 0);
  53461. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  53462. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53463. wolfSSL_CertManagerFree(cm);
  53464. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53465. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53466. wolfSSL_CertManagerFree(cm);
  53467. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  53468. * this time followed by 3 ICA's, should fail */
  53469. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53470. ExpectIntLT(test_chainJ(cm), 0);
  53471. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53472. wolfSSL_CertManagerFree(cm);
  53473. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  53474. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  53475. wolfSSL_CertManagerFree(cm);
  53476. return EXPECT_RESULT();
  53477. }
  53478. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  53479. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  53480. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  53481. {
  53482. EXPECT_DECLS;
  53483. byte ekm[100] = {0};
  53484. (void)ctx;
  53485. /* Success Cases */
  53486. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53487. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  53488. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53489. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  53490. /* Use some random context */
  53491. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53492. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  53493. /* Failure cases */
  53494. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53495. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  53496. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53497. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  53498. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53499. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  53500. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53501. "extended master secret", XSTR_SIZEOF("extended master secret"),
  53502. NULL, 0, 0), 0);
  53503. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  53504. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  53505. return EXPECT_RESULT();
  53506. }
  53507. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  53508. {
  53509. wolfSSL_KeepArrays(ssl);
  53510. return TEST_SUCCESS;
  53511. }
  53512. static int test_export_keying_material(void)
  53513. {
  53514. EXPECT_DECLS;
  53515. test_ssl_cbf serverCb;
  53516. test_ssl_cbf clientCb;
  53517. XMEMSET(&serverCb, 0, sizeof(serverCb));
  53518. XMEMSET(&clientCb, 0, sizeof(clientCb));
  53519. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  53520. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  53521. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  53522. return EXPECT_RESULT();
  53523. }
  53524. #endif /* HAVE_KEYING_MATERIAL */
  53525. static int test_wolfSSL_THREADID_hash(void)
  53526. {
  53527. EXPECT_DECLS;
  53528. #if defined(OPENSSL_EXTRA)
  53529. CRYPTO_THREADID id;
  53530. CRYPTO_THREADID_current(NULL);
  53531. /* Hash result is unsigned long. */
  53532. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  53533. XMEMSET(&id, 0, sizeof(id));
  53534. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  53535. #endif /* OPENSSL_EXTRA */
  53536. return EXPECT_RESULT();
  53537. }
  53538. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  53539. {
  53540. EXPECT_DECLS;
  53541. #if defined(OPENSSL_EXTRA)
  53542. WOLFSSL_CTX* ctx = NULL;
  53543. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  53544. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  53545. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  53546. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  53547. #endif /* OPENSSL_EXTRA */
  53548. return EXPECT_RESULT();
  53549. }
  53550. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  53551. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  53552. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  53553. {
  53554. EXPECT_DECLS;
  53555. callback_functions* callbacks = NULL;
  53556. WOLFSSL_CTX* ctx = NULL;
  53557. WOLFSSL* ssl = NULL;
  53558. SOCKET_T sfd = 0;
  53559. SOCKET_T cfd = 0;
  53560. word16 port;
  53561. char msg[] = "I hear you fa shizzle!";
  53562. int len = (int) XSTRLEN(msg);
  53563. char input[1024];
  53564. int ret;
  53565. int err = 0;
  53566. if (!args)
  53567. WOLFSSL_RETURN_FROM_THREAD(0);
  53568. ((func_args*)args)->return_code = TEST_FAIL;
  53569. callbacks = ((func_args*)args)->callbacks;
  53570. ctx = wolfSSL_CTX_new(callbacks->method());
  53571. #if defined(USE_WINDOWS_API)
  53572. port = ((func_args*)args)->signal->port;
  53573. #else
  53574. /* Let tcp_listen assign port */
  53575. port = 0;
  53576. #endif
  53577. #ifdef WOLFSSL_TIRTOS
  53578. fdOpenSession(Task_self());
  53579. #endif
  53580. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  53581. caCertFile, 0));
  53582. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  53583. svrCertFile, WOLFSSL_FILETYPE_PEM));
  53584. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  53585. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  53586. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  53587. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  53588. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53589. #elif !defined(NO_DH)
  53590. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  53591. #endif
  53592. if (callbacks->ctx_ready)
  53593. callbacks->ctx_ready(ctx);
  53594. ssl = wolfSSL_new(ctx);
  53595. ExpectNotNull(ssl);
  53596. /* listen and accept */
  53597. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  53598. CloseSocket(sfd);
  53599. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  53600. if (callbacks->ssl_ready)
  53601. callbacks->ssl_ready(ssl);
  53602. if (EXPECT_SUCCESS()) {
  53603. do {
  53604. err = 0; /* Reset error */
  53605. ret = wolfSSL_accept(ssl);
  53606. if (ret != WOLFSSL_SUCCESS) {
  53607. err = wolfSSL_get_error(ssl, 0);
  53608. }
  53609. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  53610. }
  53611. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  53612. /* read and write data */
  53613. XMEMSET(input, 0, sizeof(input));
  53614. while (EXPECT_SUCCESS()) {
  53615. ret = wolfSSL_read(ssl, input, sizeof(input));
  53616. if (ret > 0) {
  53617. break;
  53618. }
  53619. else {
  53620. err = wolfSSL_get_error(ssl,ret);
  53621. if (err == WOLFSSL_ERROR_WANT_READ) {
  53622. continue;
  53623. }
  53624. break;
  53625. }
  53626. }
  53627. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  53628. do {
  53629. ret = wolfSSL_write(ssl, msg, len);
  53630. if (ret > 0) {
  53631. break;
  53632. }
  53633. } while (ret < 0);
  53634. }
  53635. /* bidirectional shutdown */
  53636. while (EXPECT_SUCCESS()) {
  53637. ret = wolfSSL_shutdown(ssl);
  53638. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  53639. if (ret == WOLFSSL_SUCCESS) {
  53640. break;
  53641. }
  53642. }
  53643. if (EXPECT_SUCCESS()) {
  53644. /* wait for the peer to disconnect the tcp connection */
  53645. do {
  53646. ret = wolfSSL_read(ssl, input, sizeof(input));
  53647. err = wolfSSL_get_error(ssl, ret);
  53648. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  53649. }
  53650. /* detect TCP disconnect */
  53651. ExpectIntLE(ret,WOLFSSL_FAILURE);
  53652. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  53653. ((func_args*)args)->return_code = EXPECT_RESULT();
  53654. wolfSSL_free(ssl);
  53655. wolfSSL_CTX_free(ctx);
  53656. CloseSocket(cfd);
  53657. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  53658. wc_ecc_fp_free(); /* free per thread cache */
  53659. #endif
  53660. WOLFSSL_RETURN_FROM_THREAD(0);
  53661. }
  53662. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  53663. {
  53664. EXPECT_DECLS;
  53665. callback_functions* callbacks = NULL;
  53666. WOLFSSL_CTX* ctx = NULL;
  53667. WOLFSSL* ssl = NULL;
  53668. SOCKET_T sfd = 0;
  53669. char msg[] = "hello wolfssl server!";
  53670. int len = (int) XSTRLEN(msg);
  53671. char input[1024];
  53672. int idx;
  53673. int ret, err;
  53674. if (!args)
  53675. WOLFSSL_RETURN_FROM_THREAD(0);
  53676. ((func_args*)args)->return_code = TEST_FAIL;
  53677. callbacks = ((func_args*)args)->callbacks;
  53678. ctx = wolfSSL_CTX_new(callbacks->method());
  53679. #ifdef WOLFSSL_TIRTOS
  53680. fdOpenSession(Task_self());
  53681. #endif
  53682. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  53683. caCertFile, 0));
  53684. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  53685. cliCertFile, WOLFSSL_FILETYPE_PEM));
  53686. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  53687. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  53688. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  53689. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  53690. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  53691. if (EXPECT_SUCCESS()) {
  53692. do {
  53693. err = 0; /* Reset error */
  53694. ret = wolfSSL_connect(ssl);
  53695. if (ret != WOLFSSL_SUCCESS) {
  53696. err = wolfSSL_get_error(ssl, 0);
  53697. }
  53698. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  53699. }
  53700. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  53701. if (EXPECT_SUCCESS()) {
  53702. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  53703. input[idx] = 0;
  53704. }
  53705. }
  53706. if (EXPECT_SUCCESS()) {
  53707. ret = wolfSSL_shutdown(ssl);
  53708. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  53709. ret = wolfSSL_shutdown(ssl);
  53710. }
  53711. }
  53712. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  53713. ((func_args*)args)->return_code = EXPECT_RESULT();
  53714. wolfSSL_free(ssl);
  53715. wolfSSL_CTX_free(ctx);
  53716. CloseSocket(sfd);
  53717. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  53718. wc_ecc_fp_free(); /* free per thread cache */
  53719. #endif
  53720. WOLFSSL_RETURN_FROM_THREAD(0);
  53721. }
  53722. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  53723. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  53724. /* This test is to check wolfSSL_read behaves as same as
  53725. * openSSL when it is called after SSL_shutdown completes.
  53726. */
  53727. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  53728. {
  53729. EXPECT_DECLS;
  53730. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  53731. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  53732. tcp_ready ready;
  53733. func_args client_args;
  53734. func_args server_args;
  53735. THREAD_TYPE serverThread;
  53736. THREAD_TYPE clientThread;
  53737. callback_functions server_cbf;
  53738. callback_functions client_cbf;
  53739. #ifdef WOLFSSL_TIRTOS
  53740. fdOpenSession(Task_self());
  53741. #endif
  53742. StartTCP();
  53743. InitTcpReady(&ready);
  53744. #if defined(USE_WINDOWS_API)
  53745. /* use RNG to get random port if using windows */
  53746. ready.port = GetRandomPort();
  53747. #endif
  53748. XMEMSET(&client_args, 0, sizeof(func_args));
  53749. XMEMSET(&server_args, 0, sizeof(func_args));
  53750. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  53751. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  53752. server_cbf.method = wolfTLSv1_2_server_method;
  53753. client_cbf.method = wolfTLSv1_2_client_method;
  53754. server_args.callbacks = &server_cbf;
  53755. client_args.callbacks = &client_cbf;
  53756. server_args.signal = &ready;
  53757. client_args.signal = &ready;
  53758. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  53759. wait_tcp_ready(&server_args);
  53760. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  53761. join_thread(clientThread);
  53762. join_thread(serverThread);
  53763. ExpectTrue(client_args.return_code);
  53764. ExpectTrue(server_args.return_code);
  53765. FreeTcpReady(&ready);
  53766. #endif
  53767. return EXPECT_RESULT();
  53768. }
  53769. static int test_wolfSSL_CTX_get_min_proto_version(void)
  53770. {
  53771. EXPECT_DECLS;
  53772. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  53773. WOLFSSL_CTX *ctx = NULL;
  53774. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53775. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  53776. WOLFSSL_SUCCESS);
  53777. #ifdef WOLFSSL_ALLOW_SSLV3
  53778. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  53779. #else
  53780. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  53781. #endif
  53782. wolfSSL_CTX_free(ctx);
  53783. ctx = NULL;
  53784. #ifdef WOLFSSL_ALLOW_TLSV10
  53785. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  53786. #else
  53787. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53788. #endif
  53789. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  53790. WOLFSSL_SUCCESS);
  53791. #ifdef WOLFSSL_ALLOW_TLSV10
  53792. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  53793. #else
  53794. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  53795. #endif
  53796. wolfSSL_CTX_free(ctx);
  53797. ctx = NULL;
  53798. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  53799. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  53800. WOLFSSL_SUCCESS);
  53801. #ifndef NO_OLD_TLS
  53802. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  53803. #else
  53804. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  53805. #endif
  53806. wolfSSL_CTX_free(ctx);
  53807. ctx = NULL;
  53808. #ifndef WOLFSSL_NO_TLS12
  53809. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  53810. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  53811. WOLFSSL_SUCCESS);
  53812. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  53813. wolfSSL_CTX_free(ctx);
  53814. ctx = NULL;
  53815. #endif
  53816. #ifdef WOLFSSL_TLS13
  53817. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  53818. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  53819. WOLFSSL_SUCCESS);
  53820. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  53821. wolfSSL_CTX_free(ctx);
  53822. ctx = NULL;
  53823. #endif
  53824. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  53825. return EXPECT_RESULT();
  53826. }
  53827. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  53828. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  53829. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  53830. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  53831. static int test_wolfSSL_set_SSL_CTX(void)
  53832. {
  53833. EXPECT_DECLS;
  53834. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  53835. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  53836. !defined(NO_RSA)
  53837. WOLFSSL_CTX *ctx1 = NULL;
  53838. WOLFSSL_CTX *ctx2 = NULL;
  53839. WOLFSSL *ssl = NULL;
  53840. const byte *session_id1 = (const byte *)"CTX1";
  53841. const byte *session_id2 = (const byte *)"CTX2";
  53842. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  53843. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  53844. WOLFSSL_FILETYPE_PEM));
  53845. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  53846. WOLFSSL_FILETYPE_PEM));
  53847. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  53848. WOLFSSL_SUCCESS);
  53849. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  53850. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  53851. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  53852. WOLFSSL_SUCCESS);
  53853. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  53854. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  53855. WOLFSSL_FILETYPE_PEM));
  53856. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  53857. WOLFSSL_FILETYPE_PEM));
  53858. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  53859. WOLFSSL_SUCCESS);
  53860. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  53861. WOLFSSL_SUCCESS);
  53862. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  53863. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  53864. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  53865. WOLFSSL_SUCCESS);
  53866. #ifdef HAVE_SESSION_TICKET
  53867. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  53868. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  53869. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  53870. #endif
  53871. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  53872. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  53873. #ifdef WOLFSSL_INT_H
  53874. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  53875. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  53876. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  53877. #endif
  53878. #ifdef HAVE_SESSION_TICKET
  53879. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  53880. #endif
  53881. /* Set the ctx1 that has TLSv1.3 as max proto version */
  53882. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  53883. /* MUST not change proto versions of ssl */
  53884. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  53885. #ifdef HAVE_SESSION_TICKET
  53886. /* MUST not change */
  53887. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  53888. #endif
  53889. /* MUST change */
  53890. #ifdef WOLFSSL_INT_H
  53891. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  53892. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  53893. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  53894. #endif
  53895. wolfSSL_free(ssl);
  53896. wolfSSL_CTX_free(ctx1);
  53897. wolfSSL_CTX_free(ctx2);
  53898. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  53899. return EXPECT_RESULT();
  53900. }
  53901. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  53902. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  53903. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  53904. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  53905. static int test_wolfSSL_security_level(void)
  53906. {
  53907. EXPECT_DECLS;
  53908. #if defined(OPENSSL_EXTRA)
  53909. SSL_CTX *ctx = NULL;
  53910. #ifdef WOLFSSL_TLS13
  53911. #ifdef NO_WOLFSSL_SERVER
  53912. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  53913. #else
  53914. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  53915. #endif
  53916. SSL_CTX_set_security_level(NULL, 1);
  53917. SSL_CTX_set_security_level(ctx, 1);
  53918. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  53919. /* Stub so nothing happens. */
  53920. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  53921. SSL_CTX_free(ctx);
  53922. #else
  53923. (void)ctx;
  53924. #endif
  53925. #endif
  53926. return EXPECT_RESULT();
  53927. }
  53928. static int test_wolfSSL_SSL_in_init(void)
  53929. {
  53930. EXPECT_DECLS;
  53931. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  53932. SSL_CTX* ctx = NULL;
  53933. SSL* ssl = NULL;
  53934. const char* testCertFile;
  53935. const char* testKeyFile;
  53936. #ifdef WOLFSSL_TLS13
  53937. #ifdef NO_WOLFSSL_SERVER
  53938. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  53939. #else
  53940. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  53941. #endif
  53942. #else
  53943. #ifdef NO_WOLFSSL_SERVER
  53944. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  53945. #else
  53946. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53947. #endif
  53948. #endif
  53949. #ifndef NO_RSA
  53950. testCertFile = svrCertFile;
  53951. testKeyFile = svrKeyFile;
  53952. #elif defined(HAVE_ECC)
  53953. testCertFile = eccCertFile;
  53954. testKeyFile = eccKeyFile;
  53955. #else
  53956. testCertFile = NULL;
  53957. testKeyFile = NULL;
  53958. #endif
  53959. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  53960. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  53961. SSL_FILETYPE_PEM));
  53962. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  53963. SSL_FILETYPE_PEM));
  53964. }
  53965. ExpectNotNull(ssl = SSL_new(ctx));
  53966. ExpectIntEQ(SSL_in_init(ssl), 1);
  53967. SSL_CTX_free(ctx);
  53968. SSL_free(ssl);
  53969. #endif
  53970. return EXPECT_RESULT();
  53971. }
  53972. static int test_wolfSSL_CTX_set_timeout(void)
  53973. {
  53974. EXPECT_DECLS;
  53975. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  53976. int timeout;
  53977. WOLFSSL_CTX* ctx = NULL;
  53978. (void)timeout;
  53979. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  53980. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  53981. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  53982. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  53983. */
  53984. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  53985. /* giving 0 as timeout value sets default timeout */
  53986. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  53987. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  53988. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  53989. #else
  53990. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  53991. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  53992. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  53993. #endif
  53994. wolfSSL_CTX_free(ctx);
  53995. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  53996. return EXPECT_RESULT();
  53997. }
  53998. static int test_wolfSSL_OpenSSL_version(void)
  53999. {
  54000. EXPECT_DECLS;
  54001. #if defined(OPENSSL_EXTRA)
  54002. const char* ver;
  54003. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  54004. ExpectNotNull(ver = OpenSSL_version(0));
  54005. #else
  54006. ExpectNotNull(ver = OpenSSL_version());
  54007. #endif
  54008. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  54009. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  54010. #endif
  54011. return EXPECT_RESULT();
  54012. }
  54013. static int test_CONF_CTX_CMDLINE(void)
  54014. {
  54015. EXPECT_DECLS;
  54016. #if defined(OPENSSL_ALL)
  54017. SSL_CTX* ctx = NULL;
  54018. SSL_CONF_CTX* cctx = NULL;
  54019. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  54020. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54021. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54022. /* set flags */
  54023. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  54024. WOLFSSL_CONF_FLAG_CMDLINE);
  54025. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54026. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54027. /* cmd invalid command */
  54028. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54029. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54030. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54031. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54032. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54033. /* cmd Certificate and Private Key*/
  54034. {
  54035. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54036. const char* ourCert = svrCertFile;
  54037. const char* ourKey = svrKeyFile;
  54038. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  54039. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  54040. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  54041. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  54042. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54043. #endif
  54044. }
  54045. /* cmd curves */
  54046. {
  54047. #if defined(HAVE_ECC)
  54048. const char* curve = "secp256r1";
  54049. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  54050. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  54051. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54052. #endif
  54053. }
  54054. /* cmd CipherString */
  54055. {
  54056. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54057. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  54058. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  54059. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54060. }
  54061. /* cmd DH parameter */
  54062. {
  54063. #if !defined(NO_DH) && !defined(NO_BIO)
  54064. const char* ourdhcert = "./certs/dh2048.pem";
  54065. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  54066. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  54067. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54068. #endif
  54069. }
  54070. SSL_CTX_free(ctx);
  54071. SSL_CONF_CTX_free(cctx);
  54072. #endif /* OPENSSL_EXTRA */
  54073. return EXPECT_RESULT();
  54074. }
  54075. static int test_CONF_CTX_FILE(void)
  54076. {
  54077. EXPECT_DECLS;
  54078. #if defined(OPENSSL_ALL)
  54079. SSL_CTX* ctx = NULL;
  54080. SSL_CONF_CTX* cctx = NULL;
  54081. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  54082. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54083. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54084. /* set flags */
  54085. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  54086. WOLFSSL_CONF_FLAG_FILE);
  54087. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  54088. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  54089. /* sanity check */
  54090. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  54091. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  54092. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  54093. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  54094. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  54095. /* cmd Certificate and Private Key*/
  54096. {
  54097. #if !defined(NO_CERTS) && !defined(NO_RSA)
  54098. const char* ourCert = svrCertFile;
  54099. const char* ourKey = svrKeyFile;
  54100. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  54101. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  54102. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  54103. WOLFSSL_SUCCESS);
  54104. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  54105. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54106. #endif
  54107. }
  54108. /* cmd curves */
  54109. {
  54110. #if defined(HAVE_ECC)
  54111. const char* curve = "secp256r1";
  54112. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  54113. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  54114. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54115. #endif
  54116. }
  54117. /* cmd CipherString */
  54118. {
  54119. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  54120. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  54121. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  54122. WOLFSSL_SUCCESS);
  54123. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54124. }
  54125. /* cmd DH parameter */
  54126. {
  54127. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  54128. const char* ourdhcert = "./certs/dh3072.pem";
  54129. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  54130. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  54131. WOLFSSL_SUCCESS);
  54132. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  54133. #endif
  54134. }
  54135. SSL_CTX_free(ctx);
  54136. SSL_CONF_CTX_free(cctx);
  54137. #endif /* OPENSSL_EXTRA */
  54138. return EXPECT_RESULT();
  54139. }
  54140. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  54141. {
  54142. EXPECT_DECLS;
  54143. #ifdef HAVE_EX_DATA
  54144. int idx1, idx2;
  54145. /* test for unsupported class index */
  54146. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  54147. 0,NULL, NULL, NULL, NULL ), -1);
  54148. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  54149. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  54150. 0,NULL, NULL, NULL, NULL ), -1);
  54151. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  54152. 0,NULL, NULL, NULL, NULL ), -1);
  54153. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  54154. 0,NULL, NULL, NULL, NULL ), -1);
  54155. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  54156. 0,NULL, NULL, NULL, NULL ), -1);
  54157. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  54158. 0,NULL, NULL, NULL, NULL ), -1);
  54159. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  54160. 0,NULL, NULL, NULL, NULL ), -1);
  54161. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  54162. 0,NULL, NULL, NULL, NULL ), -1);
  54163. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  54164. 0,NULL, NULL, NULL, NULL ), -1);
  54165. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  54166. 0,NULL, NULL, NULL, NULL ), -1);
  54167. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  54168. 0,NULL, NULL, NULL, NULL ), -1);
  54169. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  54170. 0,NULL, NULL, NULL, NULL ), -1);
  54171. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  54172. 0,NULL, NULL, NULL, NULL ), -1);
  54173. /* test for supported class index */
  54174. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54175. 0,NULL, NULL, NULL, NULL );
  54176. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  54177. 0,NULL, NULL, NULL, NULL );
  54178. ExpectIntNE(idx1, -1);
  54179. ExpectIntNE(idx2, -1);
  54180. ExpectIntNE(idx1, idx2);
  54181. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54182. 0,NULL, NULL, NULL, NULL );
  54183. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  54184. 0,NULL, NULL, NULL, NULL );
  54185. ExpectIntNE(idx1, -1);
  54186. ExpectIntNE(idx2, -1);
  54187. ExpectIntNE(idx1, idx2);
  54188. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54189. 0,NULL, NULL, NULL, NULL );
  54190. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  54191. 0,NULL, NULL, NULL, NULL );
  54192. ExpectIntNE(idx1, -1);
  54193. ExpectIntNE(idx2, -1);
  54194. ExpectIntNE(idx1, idx2);
  54195. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54196. 0,NULL, NULL, NULL, NULL );
  54197. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  54198. 0,NULL, NULL, NULL, NULL );
  54199. ExpectIntNE(idx1, -1);
  54200. ExpectIntNE(idx2, -1);
  54201. ExpectIntNE(idx1, idx2);
  54202. #endif /* HAVE_EX_DATA */
  54203. return EXPECT_RESULT();
  54204. }
  54205. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  54206. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  54207. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  54208. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  54209. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  54210. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  54211. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  54212. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  54213. #define SESSION_NEW_IDX_PTR "Testing"
  54214. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  54215. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54216. {
  54217. AssertNotNull(p);
  54218. AssertNull(ptr);
  54219. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  54220. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54221. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  54222. }
  54223. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  54224. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  54225. void* arg)
  54226. {
  54227. EXPECT_DECLS;
  54228. ExpectNotNull(out);
  54229. ExpectNotNull(in);
  54230. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  54231. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  54232. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  54233. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  54234. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  54235. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  54236. if (EXPECT_SUCCESS()) {
  54237. return SSL_SUCCESS;
  54238. }
  54239. else {
  54240. return SSL_FAILURE;
  54241. }
  54242. }
  54243. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  54244. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  54245. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  54246. {
  54247. EXPECT_DECLS;
  54248. ExpectNotNull(p);
  54249. ExpectNull(ptr);
  54250. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  54251. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  54252. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  54253. if (EXPECT_SUCCESS()) {
  54254. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  54255. }
  54256. }
  54257. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  54258. {
  54259. EXPECT_DECLS;
  54260. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  54261. (void*)SESSION_NEW_IDX_PTR,
  54262. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  54263. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  54264. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  54265. SSL_SESSION* s = SSL_SESSION_new();
  54266. SSL_SESSION* d = NULL;
  54267. ExpectNotNull(s);
  54268. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  54269. ExpectNotNull(d = SSL_SESSION_dup(s));
  54270. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  54271. SSL_SESSION_free(s);
  54272. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  54273. SSL_SESSION_free(d);
  54274. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  54275. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  54276. crypto_ex_cb_ctx_session = NULL;
  54277. return EXPECT_RESULT();
  54278. }
  54279. #else
  54280. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  54281. {
  54282. return TEST_SKIPPED;
  54283. }
  54284. #endif
  54285. static int test_wolfSSL_set_psk_use_session_callback(void)
  54286. {
  54287. EXPECT_DECLS;
  54288. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  54289. SSL_CTX* ctx = NULL;
  54290. SSL* ssl = NULL;
  54291. const char* testCertFile;
  54292. const char* testKeyFile;
  54293. #ifdef WOLFSSL_TLS13
  54294. #ifdef NO_WOLFSSL_SERVER
  54295. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  54296. #else
  54297. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  54298. #endif
  54299. #else
  54300. #ifdef NO_WOLFSSL_SERVER
  54301. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54302. #else
  54303. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54304. #endif
  54305. #endif
  54306. #ifndef NO_RSA
  54307. testCertFile = svrCertFile;
  54308. testKeyFile = svrKeyFile;
  54309. #elif defined(HAVE_ECC)
  54310. testCertFile = eccCertFile;
  54311. testKeyFile = eccKeyFile;
  54312. #else
  54313. testCertFile = NULL;
  54314. testKeyFile = NULL;
  54315. #endif
  54316. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  54317. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54318. SSL_FILETYPE_PEM));
  54319. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54320. SSL_FILETYPE_PEM));
  54321. }
  54322. ExpectNotNull(ssl = SSL_new(ctx));
  54323. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  54324. SSL_CTX_free(ctx);
  54325. SSL_free(ssl);
  54326. #endif
  54327. return EXPECT_RESULT();
  54328. }
  54329. static int test_wolfSSL_ERR_strings(void)
  54330. {
  54331. EXPECT_DECLS;
  54332. #if !defined(NO_ERROR_STRINGS)
  54333. const char* err1 = "unsupported cipher suite";
  54334. const char* err2 = "wolfSSL PEM routines";
  54335. const char* err = NULL;
  54336. (void)err;
  54337. (void)err1;
  54338. (void)err2;
  54339. #if defined(OPENSSL_EXTRA)
  54340. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  54341. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  54342. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  54343. ExpectIntEQ((*err == '\0'), 1);
  54344. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  54345. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  54346. #else
  54347. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  54348. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  54349. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  54350. ExpectIntEQ((*err == '\0'), 1);
  54351. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  54352. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  54353. ExpectIntEQ((*err == '\0'), 1);
  54354. #endif
  54355. #endif
  54356. return EXPECT_RESULT();
  54357. }
  54358. static int test_wolfSSL_EVP_shake128(void)
  54359. {
  54360. EXPECT_DECLS;
  54361. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  54362. defined(WOLFSSL_SHAKE128)
  54363. const EVP_MD* md = NULL;
  54364. ExpectNotNull(md = EVP_shake128());
  54365. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  54366. #endif
  54367. return EXPECT_RESULT();
  54368. }
  54369. static int test_wolfSSL_EVP_shake256(void)
  54370. {
  54371. EXPECT_DECLS;
  54372. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  54373. defined(WOLFSSL_SHAKE256)
  54374. const EVP_MD* md = NULL;
  54375. ExpectNotNull(md = EVP_shake256());
  54376. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  54377. #endif
  54378. return EXPECT_RESULT();
  54379. }
  54380. /*
  54381. * Testing EVP digest API with SM3
  54382. */
  54383. static int test_wolfSSL_EVP_sm3(void)
  54384. {
  54385. int res = TEST_SKIPPED;
  54386. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  54387. EXPECT_DECLS;
  54388. const EVP_MD* md = NULL;
  54389. EVP_MD_CTX* mdCtx = NULL;
  54390. byte data[WC_SM3_BLOCK_SIZE * 4];
  54391. byte hash[WC_SM3_DIGEST_SIZE];
  54392. byte calcHash[WC_SM3_DIGEST_SIZE];
  54393. byte expHash[WC_SM3_DIGEST_SIZE] = {
  54394. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  54395. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  54396. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  54397. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  54398. };
  54399. word32 chunk;
  54400. word32 i;
  54401. unsigned int sz;
  54402. int ret;
  54403. XMEMSET(data, 0, sizeof(data));
  54404. md = EVP_sm3();
  54405. ExpectTrue(md != NULL);
  54406. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  54407. mdCtx = EVP_MD_CTX_new();
  54408. ExpectTrue(mdCtx != NULL);
  54409. /* Invalid Parameters */
  54410. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  54411. /* Valid Parameters */
  54412. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  54413. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  54414. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  54415. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  54416. /* Valid Parameters */
  54417. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  54418. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  54419. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  54420. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  54421. WOLFSSL_SUCCESS);
  54422. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  54423. WOLFSSL_SUCCESS);
  54424. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  54425. WOLFSSL_SUCCESS);
  54426. /* Ensure too many bytes for lengths. */
  54427. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  54428. WOLFSSL_SUCCESS);
  54429. /* Invalid Parameters */
  54430. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  54431. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  54432. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  54433. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  54434. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  54435. /* Valid Parameters */
  54436. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  54437. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  54438. /* Chunk tests. */
  54439. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  54440. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  54441. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  54442. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  54443. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  54444. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  54445. WOLFSSL_SUCCESS);
  54446. }
  54447. if (i < (word32)sizeof(data)) {
  54448. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  54449. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  54450. }
  54451. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  54452. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  54453. }
  54454. /* Not testing when the low 32-bit length overflows. */
  54455. ret = EVP_MD_CTX_cleanup(mdCtx);
  54456. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  54457. wolfSSL_EVP_MD_CTX_free(mdCtx);
  54458. res = EXPECT_RESULT();
  54459. #endif
  54460. return res;
  54461. } /* END test_EVP_sm3 */
  54462. static int test_EVP_blake2(void)
  54463. {
  54464. EXPECT_DECLS;
  54465. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  54466. const EVP_MD* md = NULL;
  54467. (void)md;
  54468. #if defined(HAVE_BLAKE2)
  54469. ExpectNotNull(md = EVP_blake2b512());
  54470. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  54471. #endif
  54472. #if defined(HAVE_BLAKE2S)
  54473. ExpectNotNull(md = EVP_blake2s256());
  54474. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  54475. #endif
  54476. #endif
  54477. return EXPECT_RESULT();
  54478. }
  54479. #if defined(OPENSSL_EXTRA)
  54480. static void list_md_fn(const EVP_MD* m, const char* from,
  54481. const char* to, void* arg)
  54482. {
  54483. const char* mn;
  54484. BIO *bio;
  54485. (void) from;
  54486. (void) to;
  54487. (void) arg;
  54488. (void) mn;
  54489. (void) bio;
  54490. if (!m) {
  54491. /* alias */
  54492. AssertNull(m);
  54493. AssertNotNull(to);
  54494. }
  54495. else {
  54496. AssertNotNull(m);
  54497. AssertNull(to);
  54498. }
  54499. AssertNotNull(from);
  54500. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  54501. mn = EVP_get_digestbyname(from);
  54502. /* print to stderr */
  54503. AssertNotNull(arg);
  54504. bio = BIO_new(BIO_s_file());
  54505. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  54506. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  54507. BIO_free(bio);
  54508. #endif
  54509. }
  54510. #endif
  54511. static int test_EVP_MD_do_all(void)
  54512. {
  54513. int res = TEST_SKIPPED;
  54514. #if defined(OPENSSL_EXTRA)
  54515. EVP_MD_do_all(NULL, stderr);
  54516. EVP_MD_do_all(list_md_fn, stderr);
  54517. res = TEST_SUCCESS;
  54518. #endif
  54519. return res;
  54520. }
  54521. #if defined(OPENSSL_EXTRA)
  54522. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  54523. {
  54524. (void)arg;
  54525. (void)nm;
  54526. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  54527. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  54528. /* print to stderr */
  54529. AssertNotNull(arg);
  54530. BIO *bio = BIO_new(BIO_s_file());
  54531. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  54532. BIO_printf(bio, "%s\n", nm);
  54533. BIO_free(bio);
  54534. #endif
  54535. }
  54536. #endif
  54537. static int test_OBJ_NAME_do_all(void)
  54538. {
  54539. int res = TEST_SKIPPED;
  54540. #if defined(OPENSSL_EXTRA)
  54541. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  54542. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  54543. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  54544. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  54545. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  54546. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  54547. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  54548. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  54549. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  54550. res = TEST_SUCCESS;
  54551. #endif
  54552. return res;
  54553. }
  54554. static int test_SSL_CIPHER_get_xxx(void)
  54555. {
  54556. EXPECT_DECLS;
  54557. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  54558. !defined(NO_FILESYSTEM)
  54559. const SSL_CIPHER* cipher = NULL;
  54560. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  54561. int i, numCiphers = 0;
  54562. SSL_CTX* ctx = NULL;
  54563. SSL* ssl = NULL;
  54564. const char* testCertFile;
  54565. const char* testKeyFile;
  54566. char buf[256] = {0};
  54567. const char* cipher_id = NULL;
  54568. int expect_nid1 = NID_undef;
  54569. int expect_nid2 = NID_undef;
  54570. int expect_nid3 = NID_undef;
  54571. int expect_nid4 = NID_undef;
  54572. int expect_nid5 = 0;
  54573. const char* cipher_id2 = NULL;
  54574. int expect_nid21 = NID_undef;
  54575. int expect_nid22 = NID_undef;
  54576. int expect_nid23 = NID_undef;
  54577. int expect_nid24 = NID_undef;
  54578. int expect_nid25 = 0;
  54579. (void)cipher;
  54580. (void)supportedCiphers;
  54581. (void)i;
  54582. (void)numCiphers;
  54583. (void)ctx;
  54584. (void)ssl;
  54585. (void)testCertFile;
  54586. (void)testKeyFile;
  54587. #if defined(WOLFSSL_TLS13)
  54588. cipher_id = "TLS13-AES128-GCM-SHA256";
  54589. expect_nid1 = NID_auth_rsa;
  54590. expect_nid2 = NID_aes_128_gcm;
  54591. expect_nid3 = NID_sha256;
  54592. expect_nid4 = NID_kx_any;
  54593. expect_nid5 = 1;
  54594. #if !defined(WOLFSSL_NO_TLS12)
  54595. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  54596. expect_nid21 = NID_auth_rsa;
  54597. expect_nid22 = NID_aes_256_gcm;
  54598. expect_nid23 = NID_sha384;
  54599. expect_nid24 = NID_kx_ecdhe;
  54600. expect_nid25 = 1;
  54601. #endif
  54602. #endif
  54603. #ifdef NO_WOLFSSL_SERVER
  54604. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  54605. #else
  54606. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  54607. #endif
  54608. if (cipher_id) {
  54609. #ifndef NO_RSA
  54610. testCertFile = svrCertFile;
  54611. testKeyFile = svrKeyFile;
  54612. #elif defined(HAVE_ECC)
  54613. testCertFile = eccCertFile;
  54614. testKeyFile = eccKeyFile;
  54615. #else
  54616. testCertFile = NULL;
  54617. testKeyFile = NULL;
  54618. #endif
  54619. if (testCertFile != NULL && testKeyFile != NULL) {
  54620. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  54621. SSL_FILETYPE_PEM));
  54622. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  54623. SSL_FILETYPE_PEM));
  54624. }
  54625. ExpectNotNull(ssl = SSL_new(ctx));
  54626. ExpectIntEQ(SSL_in_init(ssl), 1);
  54627. supportedCiphers = SSL_get_ciphers(ssl);
  54628. numCiphers = sk_num(supportedCiphers);
  54629. for (i = 0; i < numCiphers; ++i) {
  54630. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54631. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54632. }
  54633. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  54634. break;
  54635. }
  54636. }
  54637. /* test case for */
  54638. if (i != numCiphers) {
  54639. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  54640. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  54641. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  54642. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  54643. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  54644. }
  54645. if (cipher_id2) {
  54646. for (i = 0; i < numCiphers; ++i) {
  54647. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  54648. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  54649. }
  54650. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  54651. break;
  54652. }
  54653. }
  54654. /* test case for */
  54655. if (i != numCiphers) {
  54656. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  54657. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  54658. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  54659. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  54660. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  54661. }
  54662. }
  54663. }
  54664. SSL_CTX_free(ctx);
  54665. SSL_free(ssl);
  54666. #endif
  54667. return EXPECT_RESULT();
  54668. }
  54669. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  54670. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  54671. int* keyFormat)
  54672. {
  54673. int ret;
  54674. byte* key_buf = NULL;
  54675. size_t key_sz = 0;
  54676. EncryptedInfo encInfo;
  54677. XMEMSET(&encInfo, 0, sizeof(encInfo));
  54678. ret = load_file(privKeyFile, &key_buf, &key_sz);
  54679. if (ret == 0) {
  54680. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  54681. NULL, &encInfo, keyFormat);
  54682. }
  54683. if (key_buf != NULL) {
  54684. free(key_buf); key_buf = NULL;
  54685. }
  54686. (void)encInfo; /* not used in this test */
  54687. #ifdef DEBUG_WOLFSSL
  54688. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  54689. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  54690. (*pDer)->length);
  54691. #endif
  54692. return ret;
  54693. }
  54694. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  54695. {
  54696. int ret = CRYPTOCB_UNAVAILABLE;
  54697. const char* privKeyFile = (const char*)ctx;
  54698. DerBuffer* pDer = NULL;
  54699. int keyFormat = 0;
  54700. if (info->algo_type == WC_ALGO_TYPE_PK) {
  54701. #ifdef DEBUG_WOLFSSL
  54702. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  54703. #endif
  54704. #ifndef NO_RSA
  54705. if (info->pk.type == WC_PK_TYPE_RSA) {
  54706. switch (info->pk.rsa.type) {
  54707. case RSA_PUBLIC_ENCRYPT:
  54708. case RSA_PUBLIC_DECRYPT:
  54709. /* perform software based RSA public op */
  54710. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  54711. break;
  54712. case RSA_PRIVATE_ENCRYPT:
  54713. case RSA_PRIVATE_DECRYPT:
  54714. {
  54715. RsaKey key;
  54716. /* perform software based RSA private op */
  54717. #ifdef DEBUG_WOLFSSL
  54718. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  54719. #endif
  54720. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  54721. &keyFormat);
  54722. if (ret != 0) {
  54723. return ret;
  54724. }
  54725. ret = wc_InitRsaKey(&key, HEAP_HINT);
  54726. if (ret == 0) {
  54727. word32 keyIdx = 0;
  54728. /* load RSA private key and perform private transform */
  54729. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  54730. &key, pDer->length);
  54731. if (ret == 0) {
  54732. ret = wc_RsaFunction(
  54733. info->pk.rsa.in, info->pk.rsa.inLen,
  54734. info->pk.rsa.out, info->pk.rsa.outLen,
  54735. info->pk.rsa.type, &key, info->pk.rsa.rng);
  54736. }
  54737. else {
  54738. /* if decode fails, then fall-back to software based crypto */
  54739. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  54740. "key decode failed %d, falling back to "
  54741. "software\n", ret);
  54742. ret = CRYPTOCB_UNAVAILABLE;
  54743. }
  54744. wc_FreeRsaKey(&key);
  54745. }
  54746. wc_FreeDer(&pDer); pDer = NULL;
  54747. break;
  54748. }
  54749. }
  54750. #ifdef DEBUG_WOLFSSL
  54751. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  54752. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  54753. #endif
  54754. }
  54755. #endif /* !NO_RSA */
  54756. #ifdef HAVE_ECC
  54757. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  54758. /* mark this key as ephemeral */
  54759. if (info->pk.eckg.key != NULL) {
  54760. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  54761. sizeof(info->pk.eckg.key->label));
  54762. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  54763. }
  54764. }
  54765. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  54766. ecc_key key;
  54767. /* perform software based ECC sign */
  54768. #ifdef DEBUG_WOLFSSL
  54769. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  54770. #endif
  54771. if (info->pk.eccsign.key != NULL &&
  54772. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  54773. /* this is an empheral key */
  54774. #ifdef DEBUG_WOLFSSL
  54775. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  54776. "ephemeral key\n");
  54777. #endif
  54778. return CRYPTOCB_UNAVAILABLE;
  54779. }
  54780. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  54781. if (ret != 0) {
  54782. return ret;
  54783. }
  54784. ret = wc_ecc_init(&key);
  54785. if (ret == 0) {
  54786. word32 keyIdx = 0;
  54787. /* load ECC private key and perform private transform */
  54788. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  54789. &key, pDer->length);
  54790. if (ret == 0) {
  54791. ret = wc_ecc_sign_hash(
  54792. info->pk.eccsign.in, info->pk.eccsign.inlen,
  54793. info->pk.eccsign.out, info->pk.eccsign.outlen,
  54794. info->pk.eccsign.rng, &key);
  54795. }
  54796. else {
  54797. /* if decode fails, then fall-back to software based crypto */
  54798. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  54799. "decode failed %d, falling back to software\n", ret);
  54800. ret = CRYPTOCB_UNAVAILABLE;
  54801. }
  54802. wc_ecc_free(&key);
  54803. }
  54804. wc_FreeDer(&pDer); pDer = NULL;
  54805. #ifdef DEBUG_WOLFSSL
  54806. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  54807. ret, *info->pk.eccsign.outlen);
  54808. #endif
  54809. }
  54810. #endif /* HAVE_ECC */
  54811. #ifdef HAVE_ED25519
  54812. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  54813. ed25519_key key;
  54814. /* perform software based ED25519 sign */
  54815. #ifdef DEBUG_WOLFSSL
  54816. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  54817. #endif
  54818. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  54819. if (ret != 0) {
  54820. return ret;
  54821. }
  54822. ret = wc_ed25519_init(&key);
  54823. if (ret == 0) {
  54824. word32 keyIdx = 0;
  54825. /* load ED25519 private key and perform private transform */
  54826. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  54827. &key, pDer->length);
  54828. if (ret == 0) {
  54829. /* calculate public key */
  54830. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  54831. if (ret == 0) {
  54832. key.pubKeySet = 1;
  54833. ret = wc_ed25519_sign_msg_ex(
  54834. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  54835. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  54836. &key, info->pk.ed25519sign.type,
  54837. info->pk.ed25519sign.context,
  54838. info->pk.ed25519sign.contextLen);
  54839. }
  54840. }
  54841. else {
  54842. /* if decode fails, then fall-back to software based crypto */
  54843. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  54844. "decode failed %d, falling back to software\n", ret);
  54845. ret = CRYPTOCB_UNAVAILABLE;
  54846. }
  54847. wc_ed25519_free(&key);
  54848. }
  54849. wc_FreeDer(&pDer); pDer = NULL;
  54850. #ifdef DEBUG_WOLFSSL
  54851. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  54852. ret, *info->pk.ed25519sign.outLen);
  54853. #endif
  54854. }
  54855. #endif /* HAVE_ED25519 */
  54856. }
  54857. (void)thisDevId;
  54858. (void)keyFormat;
  54859. return ret;
  54860. }
  54861. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  54862. static int test_wc_CryptoCb_TLS(int tlsVer,
  54863. const char* cliCaPemFile, const char* cliCertPemFile,
  54864. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  54865. const char* svrCaPemFile, const char* svrCertPemFile,
  54866. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  54867. {
  54868. EXPECT_DECLS;
  54869. callback_functions client_cbf;
  54870. callback_functions server_cbf;
  54871. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  54872. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  54873. if (tlsVer == WOLFSSL_TLSV1_3) {
  54874. #ifdef WOLFSSL_TLS13
  54875. server_cbf.method = wolfTLSv1_3_server_method;
  54876. client_cbf.method = wolfTLSv1_3_client_method;
  54877. #endif
  54878. }
  54879. else if (tlsVer == WOLFSSL_TLSV1_2) {
  54880. #ifndef WOLFSSL_NO_TLS12
  54881. server_cbf.method = wolfTLSv1_2_server_method;
  54882. client_cbf.method = wolfTLSv1_2_client_method;
  54883. #endif
  54884. }
  54885. else if (tlsVer == WOLFSSL_TLSV1_1) {
  54886. #ifndef NO_OLD_TLS
  54887. server_cbf.method = wolfTLSv1_1_server_method;
  54888. client_cbf.method = wolfTLSv1_1_client_method;
  54889. #endif
  54890. }
  54891. else if (tlsVer == WOLFSSL_TLSV1) {
  54892. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  54893. server_cbf.method = wolfTLSv1_server_method;
  54894. client_cbf.method = wolfTLSv1_client_method;
  54895. #endif
  54896. }
  54897. else if (tlsVer == WOLFSSL_SSLV3) {
  54898. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  54899. defined(WOLFSSL_STATIC_RSA)
  54900. server_cbf.method = wolfSSLv3_server_method;
  54901. client_cbf.method = wolfSSLv3_client_method;
  54902. #endif
  54903. }
  54904. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  54905. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  54906. server_cbf.method = wolfDTLSv1_2_server_method;
  54907. client_cbf.method = wolfDTLSv1_2_client_method;
  54908. #endif
  54909. }
  54910. else if (tlsVer == WOLFSSL_DTLSV1) {
  54911. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  54912. server_cbf.method = wolfDTLSv1_server_method;
  54913. client_cbf.method = wolfDTLSv1_client_method;
  54914. #endif
  54915. }
  54916. if (server_cbf.method == NULL) {
  54917. /* not enabled */
  54918. return TEST_SUCCESS;
  54919. }
  54920. /* Setup the keys for the TLS test */
  54921. client_cbf.certPemFile = cliCertPemFile;
  54922. client_cbf.keyPemFile = cliPubKeyPemFile;
  54923. client_cbf.caPemFile = cliCaPemFile;
  54924. server_cbf.certPemFile = svrCertPemFile;
  54925. server_cbf.keyPemFile = svrPubKeyPemFile;
  54926. server_cbf.caPemFile = svrCaPemFile;
  54927. /* Setup a crypto callback with pointer to private key file for testing */
  54928. client_cbf.devId = 1;
  54929. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  54930. (void*)cliPrivKeyPemFile);
  54931. server_cbf.devId = 2;
  54932. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  54933. (void*)svrPrivKeyPemFile);
  54934. /* Perform TLS server and client test */
  54935. /* First test is at WOLFSSL_CTX level */
  54936. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  54937. /* Check for success */
  54938. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  54939. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  54940. if (EXPECT_SUCCESS()) {
  54941. /* Second test is a WOLFSSL object level */
  54942. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  54943. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  54944. }
  54945. /* Check for success */
  54946. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  54947. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  54948. /* Un register the devId's */
  54949. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  54950. client_cbf.devId = INVALID_DEVID;
  54951. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  54952. server_cbf.devId = INVALID_DEVID;
  54953. return EXPECT_RESULT();
  54954. }
  54955. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  54956. static int test_wc_CryptoCb(void)
  54957. {
  54958. EXPECT_DECLS;
  54959. #ifdef WOLF_CRYPTO_CB
  54960. /* TODO: Add crypto callback API tests */
  54961. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  54962. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  54963. int tlsVer;
  54964. #endif
  54965. #ifndef NO_RSA
  54966. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  54967. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  54968. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  54969. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  54970. TEST_SUCCESS);
  54971. }
  54972. #endif
  54973. #ifdef HAVE_ECC
  54974. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  54975. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  54976. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  54977. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  54978. TEST_SUCCESS);
  54979. }
  54980. #endif
  54981. #ifdef HAVE_ED25519
  54982. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  54983. if (tlsVer == WOLFSSL_DTLSV1) continue;
  54984. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  54985. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  54986. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  54987. TEST_SUCCESS);
  54988. }
  54989. #endif
  54990. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  54991. #endif /* WOLF_CRYPTO_CB */
  54992. return EXPECT_RESULT();
  54993. }
  54994. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  54995. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  54996. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  54997. const char* cliCaPemFile, const char* cliCertPemFile,
  54998. const char* cliPrivKeyPemFile,
  54999. const char* svrCaPemFile, const char* svrCertPemFile,
  55000. const char* svrPrivKeyPemFile,
  55001. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  55002. {
  55003. EXPECT_DECLS;
  55004. callback_functions client_cbf;
  55005. callback_functions server_cbf;
  55006. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55007. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55008. if (tlsVer == WOLFSSL_TLSV1_3) {
  55009. #ifdef WOLFSSL_TLS13
  55010. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  55011. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  55012. #endif
  55013. }
  55014. else if (tlsVer == WOLFSSL_TLSV1_2) {
  55015. #ifndef WOLFSSL_NO_TLS12
  55016. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  55017. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  55018. #endif
  55019. }
  55020. else if (tlsVer == WOLFSSL_TLSV1_1) {
  55021. #ifndef NO_OLD_TLS
  55022. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  55023. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  55024. #endif
  55025. }
  55026. else if (tlsVer == WOLFSSL_TLSV1) {
  55027. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  55028. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  55029. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  55030. #endif
  55031. }
  55032. else if (tlsVer == WOLFSSL_SSLV3) {
  55033. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  55034. defined(WOLFSSL_STATIC_RSA)
  55035. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  55036. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  55037. #endif
  55038. }
  55039. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  55040. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55041. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  55042. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  55043. #endif
  55044. }
  55045. else if (tlsVer == WOLFSSL_DTLSV1) {
  55046. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  55047. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  55048. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  55049. #endif
  55050. }
  55051. if (server_cbf.method_ex == NULL) {
  55052. /* not enabled */
  55053. return TEST_SUCCESS;
  55054. }
  55055. /* Setup the keys for the TLS test */
  55056. client_cbf.certPemFile = cliCertPemFile;
  55057. client_cbf.keyPemFile = cliPrivKeyPemFile;
  55058. client_cbf.caPemFile = cliCaPemFile;
  55059. server_cbf.certPemFile = svrCertPemFile;
  55060. server_cbf.keyPemFile = svrPrivKeyPemFile;
  55061. server_cbf.caPemFile = svrCaPemFile;
  55062. client_cbf.mem = cliMem;
  55063. client_cbf.memSz = cliMemSz;
  55064. server_cbf.mem = svrMem;
  55065. server_cbf.memSz = svrMemSz;
  55066. client_cbf.devId = INVALID_DEVID;
  55067. server_cbf.devId = INVALID_DEVID;
  55068. /* Perform TLS server and client test */
  55069. /* First test is at WOLFSSL_CTX level */
  55070. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55071. /* Check for success */
  55072. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55073. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55074. if (EXPECT_SUCCESS()) {
  55075. /* Second test is a WOLFSSL object level */
  55076. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  55077. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  55078. }
  55079. /* Check for success */
  55080. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  55081. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  55082. return EXPECT_RESULT();
  55083. }
  55084. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  55085. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55086. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  55087. {
  55088. EXPECT_DECLS;
  55089. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  55090. WOLFSSL_MEM_STATS mem_stats;
  55091. WOLFSSL_MEM_CONN_STATS ssl_stats;
  55092. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  55093. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55094. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55095. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55096. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55097. #endif
  55098. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  55099. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  55100. /* this should fail because kMaxCtxClients == 2 */
  55101. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  55102. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  55103. #ifdef DEBUG_WOLFSSL
  55104. wolfSSL_PrintStatsConn(&ssl_stats);
  55105. #endif
  55106. (void)ssl_stats;
  55107. }
  55108. /* display collected statistics */
  55109. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  55110. #ifdef DEBUG_WOLFSSL
  55111. wolfSSL_PrintStats(&mem_stats);
  55112. #endif
  55113. (void)mem_stats;
  55114. }
  55115. wolfSSL_free(ssl1);
  55116. wolfSSL_free(ssl2);
  55117. return EXPECT_RESULT();
  55118. }
  55119. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55120. static int test_wolfSSL_CTX_StaticMemory(void)
  55121. {
  55122. EXPECT_DECLS;
  55123. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  55124. wolfSSL_method_func method_func;
  55125. WOLFSSL_CTX* ctx;
  55126. const int kMaxCtxClients = 2;
  55127. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55128. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  55129. int tlsVer;
  55130. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  55131. #endif
  55132. #endif
  55133. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  55134. #ifndef NO_WOLFSSL_SERVER
  55135. #ifndef WOLFSSL_NO_TLS12
  55136. method_func = wolfTLSv1_2_server_method_ex;
  55137. #else
  55138. method_func = wolfTLSv1_3_server_method_ex;
  55139. #endif
  55140. #else
  55141. #ifndef WOLFSSL_NO_TLS12
  55142. method_func = wolfTLSv1_2_client_method_ex;
  55143. #else
  55144. method_func = wolfTLSv1_3_client_method_ex;
  55145. #endif
  55146. #endif
  55147. /* Test creating CTX directly from static memory pool */
  55148. ctx = NULL;
  55149. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  55150. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55151. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  55152. wolfSSL_CTX_free(ctx);
  55153. ctx = NULL;
  55154. /* Test for heap allocated CTX, then assigning static pool to it */
  55155. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  55156. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  55157. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  55158. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  55159. wolfSSL_CTX_free(ctx);
  55160. /* TLS Level Tests using static memory */
  55161. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  55162. #ifndef NO_RSA
  55163. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55164. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55165. svrCertFile, cliCertFile, cliKeyFile,
  55166. cliCertFile, svrCertFile, svrKeyFile,
  55167. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55168. TEST_SUCCESS);
  55169. }
  55170. #endif
  55171. #ifdef HAVE_ECC
  55172. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  55173. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55174. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  55175. cliEccCertFile, eccCertFile, eccKeyFile,
  55176. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55177. TEST_SUCCESS);
  55178. }
  55179. #endif
  55180. #ifdef HAVE_ED25519
  55181. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  55182. if (tlsVer == WOLFSSL_DTLSV1) continue;
  55183. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  55184. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  55185. cliEdCertFile, edCertFile, edKeyFile,
  55186. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  55187. TEST_SUCCESS);
  55188. }
  55189. #endif
  55190. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  55191. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  55192. return EXPECT_RESULT();
  55193. }
  55194. static int test_openssl_FIPS_drbg(void)
  55195. {
  55196. EXPECT_DECLS;
  55197. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  55198. DRBG_CTX* dctx = NULL;
  55199. byte data1[32], data2[32], zeroData[32];
  55200. byte testSeed[16];
  55201. size_t dlen = sizeof(data1);
  55202. int i;
  55203. XMEMSET(data1, 0, dlen);
  55204. XMEMSET(data2, 0, dlen);
  55205. XMEMSET(zeroData, 0, sizeof(zeroData));
  55206. for (i = 0; i < (int)sizeof(testSeed); i++) {
  55207. testSeed[i] = (byte)i;
  55208. }
  55209. ExpectNotNull(dctx = FIPS_get_default_drbg());
  55210. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  55211. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  55212. WOLFSSL_SUCCESS);
  55213. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  55214. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  55215. WOLFSSL_SUCCESS);
  55216. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55217. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  55218. WOLFSSL_SUCCESS);
  55219. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  55220. WOLFSSL_SUCCESS);
  55221. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  55222. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  55223. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  55224. #ifndef HAVE_GLOBAL_RNG
  55225. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  55226. wolfSSL_FIPS_drbg_free(dctx);
  55227. #endif
  55228. #endif
  55229. return EXPECT_RESULT();
  55230. }
  55231. static int test_wolfSSL_FIPS_mode(void)
  55232. {
  55233. EXPECT_DECLS;
  55234. #if defined(OPENSSL_ALL)
  55235. #ifdef HAVE_FIPS
  55236. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  55237. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  55238. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  55239. #else
  55240. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  55241. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  55242. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  55243. #endif
  55244. #endif
  55245. return EXPECT_RESULT();
  55246. }
  55247. #ifdef WOLFSSL_DTLS
  55248. /* Prints out the current window */
  55249. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  55250. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  55251. int i;
  55252. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  55253. word32 b = w[i];
  55254. int j;
  55255. /* Prints out a 32 bit binary number in big endian order */
  55256. for (j = 0; j < 32; j++, b <<= 1) {
  55257. if (b & (((word32)1) << 31))
  55258. fprintf(stderr, "1");
  55259. else
  55260. fprintf(stderr, "0");
  55261. }
  55262. fprintf(stderr, " ");
  55263. }
  55264. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  55265. #else
  55266. (void)h;
  55267. (void)l;
  55268. (void)w;
  55269. #endif
  55270. }
  55271. /* a - cur_hi
  55272. * b - cur_lo
  55273. * c - next_hi
  55274. * d - next_lo
  55275. * e - window
  55276. * f - expected next_hi
  55277. * g - expected next_lo
  55278. * h - expected window[1]
  55279. * i - expected window[0]
  55280. */
  55281. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  55282. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  55283. DUW_TEST_print_window_binary((a), (b), (e)); \
  55284. ExpectIntEQ((c), (f)); \
  55285. ExpectIntEQ((d), (g)); \
  55286. ExpectIntEQ((e)[1], (h)); \
  55287. ExpectIntEQ((e)[0], (i)); \
  55288. } while (0)
  55289. static int test_wolfSSL_DtlsUpdateWindow(void)
  55290. {
  55291. EXPECT_DECLS;
  55292. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  55293. word32 next_lo = 0;
  55294. word16 next_hi = 0;
  55295. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  55296. fprintf(stderr, "\n");
  55297. #endif
  55298. XMEMSET(window, 0, sizeof window);
  55299. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  55300. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  55301. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  55302. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  55303. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  55304. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  55305. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  55306. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  55307. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  55308. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  55309. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  55310. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  55311. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  55312. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  55313. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  55314. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  55315. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  55316. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  55317. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  55318. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  55319. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  55320. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  55321. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  55322. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  55323. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  55324. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  55325. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  55326. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55327. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55328. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  55329. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  55330. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  55331. return EXPECT_RESULT();
  55332. }
  55333. #endif /* WOLFSSL_DTLS */
  55334. #ifdef WOLFSSL_DTLS
  55335. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  55336. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  55337. {
  55338. DtlsMsg* cur;
  55339. static byte msg[100];
  55340. static byte msgInit = 0;
  55341. if (!msgInit) {
  55342. int i;
  55343. for (i = 0; i < 100; i++)
  55344. msg[i] = i + 1;
  55345. msgInit = 1;
  55346. }
  55347. /* Sanitize test parameters */
  55348. if (len > sizeof(msg))
  55349. return -1;
  55350. if (f_offset + f_len > sizeof(msg))
  55351. return -1;
  55352. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  55353. if (ssl->dtls_rx_msg_list == NULL)
  55354. return -100;
  55355. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  55356. return -200;
  55357. if (cur->fragBucketListCount != f_count)
  55358. return -300;
  55359. if (cur->ready != ready)
  55360. return -400;
  55361. if (cur->bytesReceived != bytesReceived)
  55362. return -500;
  55363. if (ready) {
  55364. if (cur->fragBucketList != NULL)
  55365. return -600;
  55366. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  55367. return -700;
  55368. }
  55369. else {
  55370. DtlsFragBucket* fb;
  55371. if (cur->fragBucketList == NULL)
  55372. return -800;
  55373. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  55374. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  55375. return -900;
  55376. }
  55377. }
  55378. return 0;
  55379. }
  55380. static int test_wolfSSL_DTLS_fragment_buckets(void)
  55381. {
  55382. EXPECT_DECLS;
  55383. WOLFSSL ssl[1];
  55384. XMEMSET(ssl, 0, sizeof(*ssl));
  55385. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  55386. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55387. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  55388. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  55389. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  55390. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  55391. /* Test all permutations of 3 regions */
  55392. /* 1 2 3 */
  55393. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55394. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  55395. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  55396. /* 1 3 2 */
  55397. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55398. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  55399. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  55400. /* 2 1 3 */
  55401. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  55402. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  55403. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  55404. /* 2 3 1 */
  55405. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  55406. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  55407. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  55408. /* 3 1 2 */
  55409. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  55410. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  55411. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  55412. /* 3 2 1 */
  55413. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  55414. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  55415. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  55416. /* Test overlapping regions */
  55417. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  55418. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  55419. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  55420. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  55421. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  55422. /* Test overlapping multiple regions */
  55423. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55424. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  55425. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  55426. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  55427. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  55428. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  55429. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  55430. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  55431. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  55432. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  55433. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  55434. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  55435. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  55436. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  55437. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  55438. ssl->dtls_rx_msg_list = NULL;
  55439. ssl->dtls_rx_msg_list_sz = 0;
  55440. return EXPECT_RESULT();
  55441. }
  55442. #endif
  55443. #if !defined(NO_FILESYSTEM) && \
  55444. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55445. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  55446. static int test_wolfSSL_dtls_stateless2(void)
  55447. {
  55448. EXPECT_DECLS;
  55449. WOLFSSL *ssl_c = NULL;
  55450. WOLFSSL *ssl_c2 = NULL;
  55451. WOLFSSL *ssl_s = NULL;
  55452. struct test_memio_ctx test_ctx;
  55453. WOLFSSL_CTX *ctx_c = NULL;
  55454. WOLFSSL_CTX *ctx_s = NULL;
  55455. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55456. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55457. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  55458. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  55459. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55460. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55461. /* send CH */
  55462. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  55463. (ssl_c2->error == WANT_READ));
  55464. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55465. (ssl_s->error == WANT_READ));
  55466. ExpectIntNE(test_ctx.c_len, 0);
  55467. /* consume HRR */
  55468. test_ctx.c_len = 0;
  55469. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55470. wolfSSL_free(ssl_c2);
  55471. wolfSSL_free(ssl_c);
  55472. wolfSSL_free(ssl_s);
  55473. wolfSSL_CTX_free(ctx_c);
  55474. wolfSSL_CTX_free(ctx_s);
  55475. return EXPECT_RESULT();
  55476. }
  55477. #ifdef HAVE_MAX_FRAGMENT
  55478. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  55479. {
  55480. EXPECT_DECLS;
  55481. WOLFSSL *ssl_c = NULL;
  55482. WOLFSSL *ssl_c2 = NULL;
  55483. WOLFSSL *ssl_s = NULL;
  55484. struct test_memio_ctx test_ctx;
  55485. WOLFSSL_CTX *ctx_c = NULL;
  55486. WOLFSSL_CTX *ctx_s = NULL;
  55487. word16 max_fragment = 0;
  55488. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55489. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55490. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  55491. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  55492. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  55493. WOLFSSL_SUCCESS);
  55494. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55495. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55496. if (ssl_s != NULL) {
  55497. max_fragment = ssl_s->max_fragment;
  55498. }
  55499. /* send CH */
  55500. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  55501. (ssl_c2->error == WANT_READ));
  55502. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55503. (ssl_s->error == WANT_READ));
  55504. /* CH without cookie shouldn't change state */
  55505. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  55506. ExpectIntNE(test_ctx.c_len, 0);
  55507. /* consume HRR from buffer */
  55508. test_ctx.c_len = 0;
  55509. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55510. wolfSSL_free(ssl_c2);
  55511. wolfSSL_free(ssl_c);
  55512. wolfSSL_free(ssl_s);
  55513. wolfSSL_CTX_free(ctx_c);
  55514. wolfSSL_CTX_free(ctx_s);
  55515. return EXPECT_RESULT();
  55516. }
  55517. #endif /* HAVE_MAX_FRAGMENT */
  55518. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  55519. #define ROUNDS_WITH_HVR 4
  55520. #define ROUNDS_WITHOUT_HVR 2
  55521. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  55522. static int buf_is_hvr(const byte *data, int len)
  55523. {
  55524. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  55525. return 0;
  55526. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  55527. }
  55528. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  55529. {
  55530. EXPECT_DECLS;
  55531. struct test_memio_ctx test_ctx;
  55532. WOLFSSL_CTX *ctx_c = NULL;
  55533. WOLFSSL_CTX *ctx_s = NULL;
  55534. WOLFSSL *ssl_c = NULL;
  55535. WOLFSSL *ssl_s = NULL;
  55536. WOLFSSL_SESSION *sess = NULL;
  55537. int round_trips;
  55538. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55539. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  55540. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  55541. #ifdef HAVE_SESSION_TICKET
  55542. if (useticket) {
  55543. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  55544. }
  55545. #endif
  55546. round_trips = ROUNDS_WITH_HVR;
  55547. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  55548. &round_trips), 0);
  55549. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  55550. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  55551. wolfSSL_shutdown(ssl_c);
  55552. wolfSSL_shutdown(ssl_s);
  55553. wolfSSL_free(ssl_c);
  55554. ssl_c = NULL;
  55555. wolfSSL_free(ssl_s);
  55556. ssl_s = NULL;
  55557. test_ctx.c_len = test_ctx.s_len = 0;
  55558. /* make resumption invalid */
  55559. if (bad && (sess != NULL)) {
  55560. if (useticket) {
  55561. #ifdef HAVE_SESSION_TICKET
  55562. if (sess->ticket != NULL) {
  55563. sess->ticket[0] = !sess->ticket[0];
  55564. }
  55565. #endif /* HAVE_SESSION_TICKET */
  55566. }
  55567. else {
  55568. sess->sessionID[0] = !sess->sessionID[0];
  55569. }
  55570. }
  55571. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  55572. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  55573. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  55574. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  55575. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  55576. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  55577. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  55578. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  55579. (ssl_c->error == WANT_READ));
  55580. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55581. (ssl_s->error == WANT_READ));
  55582. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  55583. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  55584. if (!useticket) {
  55585. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  55586. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  55587. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  55588. }
  55589. wolfSSL_SESSION_free(sess);
  55590. wolfSSL_free(ssl_c);
  55591. wolfSSL_free(ssl_s);
  55592. wolfSSL_CTX_free(ctx_c);
  55593. wolfSSL_CTX_free(ctx_s);
  55594. return EXPECT_RESULT();
  55595. }
  55596. static int test_wolfSSL_dtls_stateless_resume(void)
  55597. {
  55598. EXPECT_DECLS;
  55599. #ifdef HAVE_SESSION_TICKET
  55600. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  55601. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  55602. #endif /* HAVE_SESION_TICKET */
  55603. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  55604. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  55605. return EXPECT_RESULT();
  55606. }
  55607. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  55608. #if !defined(NO_OLD_TLS)
  55609. static int test_wolfSSL_dtls_stateless_downgrade(void)
  55610. {
  55611. EXPECT_DECLS;
  55612. WOLFSSL_CTX *ctx_c = NULL;
  55613. WOLFSSL_CTX *ctx_c2 = NULL;
  55614. WOLFSSL_CTX *ctx_s = NULL;
  55615. WOLFSSL *ssl_c = NULL;
  55616. WOLFSSL *ssl_c2 = NULL;
  55617. WOLFSSL *ssl_s = NULL;
  55618. struct test_memio_ctx test_ctx;
  55619. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55620. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55621. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  55622. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  55623. WOLFSSL_SUCCESS);
  55624. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  55625. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  55626. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  55627. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  55628. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  55629. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  55630. /* send CH */
  55631. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  55632. (ssl_c2->error == WANT_READ));
  55633. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55634. (ssl_s->error == WANT_READ));
  55635. ExpectIntNE(test_ctx.c_len, 0);
  55636. /* consume HRR */
  55637. test_ctx.c_len = 0;
  55638. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55639. wolfSSL_free(ssl_c2);
  55640. wolfSSL_free(ssl_c);
  55641. wolfSSL_free(ssl_s);
  55642. wolfSSL_CTX_free(ctx_c);
  55643. wolfSSL_CTX_free(ctx_c2);
  55644. wolfSSL_CTX_free(ctx_s);
  55645. return EXPECT_RESULT();
  55646. }
  55647. #endif /* !defined(NO_OLD_TLS) */
  55648. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55649. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  55650. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55651. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  55652. static int test_WOLFSSL_dtls_version_alert(void)
  55653. {
  55654. EXPECT_DECLS;
  55655. struct test_memio_ctx test_ctx;
  55656. WOLFSSL_CTX *ctx_c = NULL;
  55657. WOLFSSL_CTX *ctx_s = NULL;
  55658. WOLFSSL *ssl_c = NULL;
  55659. WOLFSSL *ssl_s = NULL;
  55660. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55661. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55662. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  55663. /* client hello */
  55664. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  55665. (ssl_c->error == WANT_READ));
  55666. /* hrr */
  55667. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55668. (ssl_s->error == WANT_READ));
  55669. /* client hello 1 */
  55670. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  55671. (ssl_c->error == WANT_READ));
  55672. /* server hello */
  55673. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55674. (ssl_s->error == WANT_READ));
  55675. /* should fail */
  55676. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  55677. (ssl_c->error == VERSION_ERROR));
  55678. /* shuould fail */
  55679. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  55680. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  55681. wolfSSL_free(ssl_c);
  55682. wolfSSL_free(ssl_s);
  55683. wolfSSL_CTX_free(ctx_c);
  55684. wolfSSL_CTX_free(ctx_s);
  55685. return EXPECT_RESULT();
  55686. }
  55687. #else
  55688. static int test_WOLFSSL_dtls_version_alert(void)
  55689. {
  55690. return TEST_SKIPPED;
  55691. }
  55692. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  55693. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  55694. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  55695. */
  55696. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  55697. && defined(WOLFSSL_TLS13) && \
  55698. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  55699. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  55700. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  55701. {
  55702. struct test_memio_ctx *test_ctx;
  55703. byte buf[2048];
  55704. int idx, sz;
  55705. word32 tmp;
  55706. int ret;
  55707. idx = 5; /* space for record header */
  55708. buf[idx] = session_ticket; /* type */
  55709. idx++;
  55710. tmp = OPAQUE32_LEN +
  55711. OPAQUE32_LEN +
  55712. OPAQUE8_LEN + nonceLength +
  55713. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  55714. c32to24(tmp, buf + idx);
  55715. idx += OPAQUE24_LEN;
  55716. c32toa((word32)12345, buf+idx); /* lifetime */
  55717. idx += OPAQUE32_LEN;
  55718. c32toa((word32)12345, buf+idx); /* add */
  55719. idx += OPAQUE32_LEN;
  55720. buf[idx] = nonceLength; /* nonce length */
  55721. idx++;
  55722. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  55723. idx += nonceLength;
  55724. tmp = 1; /* ticket len */
  55725. c16toa((word16)tmp, buf+idx);
  55726. idx += 2;
  55727. buf[idx] = 0xFF; /* ticket */
  55728. idx++;
  55729. tmp = 0; /* ext len */
  55730. c16toa((word16)tmp, buf+idx);
  55731. idx += 2;
  55732. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  55733. handshake, 0, 0, 0);
  55734. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  55735. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  55736. return !(ret == sz);
  55737. }
  55738. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  55739. {
  55740. int ret = 0;
  55741. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  55742. ret = -1;
  55743. }
  55744. else {
  55745. int i;
  55746. for (i = 0; i < len; i++) {
  55747. if (sess->ticketNonce.data[i] != len) {
  55748. ret = -1;
  55749. break;
  55750. }
  55751. }
  55752. }
  55753. return ret;
  55754. }
  55755. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  55756. {
  55757. EXPECT_DECLS;
  55758. char *buf[1024];
  55759. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  55760. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  55761. (ssl_c->error == WANT_READ));
  55762. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  55763. return EXPECT_RESULT();
  55764. }
  55765. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  55766. {
  55767. EXPECT_DECLS;
  55768. WOLFSSL_SESSION *sess = NULL;
  55769. WOLFSSL_SESSION *cached = NULL;
  55770. WOLFSSL_CTX *ctx = ssl_c->ctx;
  55771. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  55772. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  55773. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  55774. NULL, ssl_c->options.side, 1,NULL), 0);
  55775. ExpectNotNull(cached = wolfSSL_SESSION_new());
  55776. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  55777. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  55778. wolfSSL_SESSION_free(cached);
  55779. wolfSSL_SESSION_free(sess);
  55780. return EXPECT_RESULT();
  55781. }
  55782. static int test_ticket_nonce_malloc(void)
  55783. {
  55784. EXPECT_DECLS;
  55785. struct test_memio_ctx test_ctx;
  55786. WOLFSSL_CTX *ctx_c = NULL;
  55787. WOLFSSL_CTX *ctx_s = NULL;
  55788. WOLFSSL *ssl_c = NULL;
  55789. WOLFSSL *ssl_s = NULL;
  55790. byte small;
  55791. byte medium;
  55792. byte big;
  55793. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55794. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55795. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  55796. /* will send ticket manually */
  55797. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  55798. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  55799. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  55800. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  55801. (ssl_s->options.handShakeDone == 0)) {
  55802. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  55803. (ssl_c->error == WANT_READ));
  55804. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  55805. (ssl_s->error == WANT_READ));
  55806. }
  55807. small = TLS13_TICKET_NONCE_STATIC_SZ;
  55808. medium = small + 20 <= 255 ? small + 20 : 255;
  55809. big = medium + 20 <= 255 ? small + 20 : 255;
  55810. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  55811. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  55812. ssl_c->session->ticketNonce.dataStatic);
  55813. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  55814. TEST_SUCCESS);
  55815. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  55816. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  55817. TEST_SUCCESS);
  55818. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  55819. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  55820. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  55821. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  55822. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  55823. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  55824. wolfSSL_free(ssl_c);
  55825. wolfSSL_free(ssl_s);
  55826. wolfSSL_CTX_free(ctx_c);
  55827. wolfSSL_CTX_free(ctx_s);
  55828. return EXPECT_RESULT();
  55829. }
  55830. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  55831. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  55832. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  55833. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55834. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  55835. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  55836. static int test_ticket_ret_create(void)
  55837. {
  55838. EXPECT_DECLS;
  55839. WOLFSSL_CTX *ctx_c = NULL;
  55840. WOLFSSL_CTX *ctx_s = NULL;
  55841. WOLFSSL *ssl_c = NULL;
  55842. WOLFSSL *ssl_s = NULL;
  55843. byte ticket[SESSION_TICKET_LEN];
  55844. struct test_memio_ctx test_ctx;
  55845. WOLFSSL_SESSION *sess = NULL;
  55846. word16 ticketLen = 0;
  55847. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55848. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55849. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  55850. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  55851. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  55852. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  55853. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55854. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  55855. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  55856. if (sess != NULL) {
  55857. ticketLen = sess->ticketLen;
  55858. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  55859. }
  55860. wolfSSL_free(ssl_c);
  55861. ssl_c = NULL;
  55862. wolfSSL_free(ssl_s);
  55863. ssl_s = NULL;
  55864. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  55865. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  55866. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  55867. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  55868. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  55869. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  55870. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  55871. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55872. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  55873. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  55874. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  55875. wolfSSL_SESSION_free(sess);
  55876. wolfSSL_free(ssl_c);
  55877. wolfSSL_free(ssl_s);
  55878. wolfSSL_CTX_free(ctx_c);
  55879. wolfSSL_CTX_free(ctx_s);
  55880. return EXPECT_RESULT();
  55881. }
  55882. #else
  55883. static int test_ticket_ret_create(void)
  55884. {
  55885. return TEST_SKIPPED;
  55886. }
  55887. #endif
  55888. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  55889. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  55890. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  55891. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  55892. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  55893. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  55894. {
  55895. int ret;
  55896. WOLFSSL_SESSION* session = NULL;
  55897. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  55898. if (!wolfSSL_is_server(ssl)) {
  55899. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  55900. AssertNotNull(session);
  55901. }
  55902. do {
  55903. ret = wolfSSL_shutdown(ssl);
  55904. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  55905. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  55906. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  55907. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  55908. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  55909. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  55910. #endif
  55911. if (!wolfSSL_is_server(ssl)) {
  55912. /* client */
  55913. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  55914. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  55915. wolfSSL_set_session(ssl, session);
  55916. wolfSSL_SESSION_free(session);
  55917. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  55918. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  55919. }
  55920. else {
  55921. /* server */
  55922. /* Different ciphersuite so that the ticket will be invalidated based on
  55923. * the ciphersuite */
  55924. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  55925. WOLFSSL_SUCCESS);
  55926. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  55927. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  55928. }
  55929. }
  55930. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  55931. {
  55932. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  55933. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  55934. WOLFSSL_SUCCESS);
  55935. }
  55936. static int test_ticket_and_psk_mixing(void)
  55937. {
  55938. EXPECT_DECLS;
  55939. /* Test mixing tickets and regular PSK */
  55940. callback_functions client_cbs, server_cbs;
  55941. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55942. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55943. client_cbs.method = wolfTLSv1_3_client_method;
  55944. server_cbs.method = wolfTLSv1_3_server_method;
  55945. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  55946. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  55947. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  55948. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55949. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  55950. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  55951. return EXPECT_RESULT();
  55952. }
  55953. #else
  55954. static int test_ticket_and_psk_mixing(void)
  55955. {
  55956. return TEST_SKIPPED;
  55957. }
  55958. #endif
  55959. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  55960. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  55961. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  55962. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  55963. static int test_prioritize_psk_cb_called = FALSE;
  55964. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  55965. const char* identity, unsigned char* key, unsigned int key_max_len,
  55966. const char** ciphersuite)
  55967. {
  55968. test_prioritize_psk_cb_called = TRUE;
  55969. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  55970. }
  55971. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  55972. {
  55973. int ret;
  55974. WOLFSSL_SESSION* session = NULL;
  55975. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  55976. if (!wolfSSL_is_server(ssl)) {
  55977. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  55978. AssertNotNull(session);
  55979. }
  55980. do {
  55981. ret = wolfSSL_shutdown(ssl);
  55982. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  55983. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  55984. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  55985. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  55986. * important. */
  55987. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  55988. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  55989. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  55990. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  55991. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  55992. #endif
  55993. if (!wolfSSL_is_server(ssl)) {
  55994. /* client */
  55995. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  55996. wolfSSL_set_session(ssl, session);
  55997. wolfSSL_SESSION_free(session);
  55998. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  55999. }
  56000. else {
  56001. /* server */
  56002. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  56003. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  56004. #ifdef WOLFSSL_PRIORITIZE_PSK
  56005. /* The ticket should be first tried with all ciphersuites and chosen */
  56006. AssertFalse(test_prioritize_psk_cb_called);
  56007. #else
  56008. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  56009. * callback that sets this var. */
  56010. AssertTrue(test_prioritize_psk_cb_called);
  56011. #endif
  56012. }
  56013. }
  56014. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  56015. {
  56016. if (!wolfSSL_is_server(ssl))
  56017. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56018. WOLFSSL_SUCCESS);
  56019. else
  56020. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56021. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  56022. }
  56023. static int test_prioritize_psk(void)
  56024. {
  56025. EXPECT_DECLS;
  56026. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  56027. * of the PSK's will be followed instead of the ciphersuite. */
  56028. callback_functions client_cbs, server_cbs;
  56029. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56030. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56031. client_cbs.method = wolfTLSv1_3_client_method;
  56032. server_cbs.method = wolfTLSv1_3_server_method;
  56033. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56034. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  56035. client_cbs.on_result = test_prioritize_psk_on_result;
  56036. server_cbs.on_result = test_prioritize_psk_on_result;
  56037. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56038. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  56039. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  56040. return EXPECT_RESULT();
  56041. }
  56042. #else
  56043. static int test_prioritize_psk(void)
  56044. {
  56045. return TEST_SKIPPED;
  56046. }
  56047. #endif
  56048. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  56049. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56050. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56051. !defined(WOLFSSL_NO_TLS12)
  56052. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  56053. {
  56054. EXPECT_DECLS;
  56055. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  56056. /* Set TLS 1.3 specific suite */
  56057. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  56058. return EXPECT_RESULT();
  56059. }
  56060. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56061. {
  56062. EXPECT_DECLS;
  56063. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  56064. * do a 1.2 connection. */
  56065. test_ssl_cbf client_cbs, server_cbs;
  56066. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56067. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56068. client_cbs.method = wolfTLSv1_2_client_method;
  56069. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  56070. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  56071. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56072. &server_cbs, NULL), TEST_SUCCESS);
  56073. return EXPECT_RESULT();
  56074. }
  56075. #else
  56076. static int test_wolfSSL_CTX_set_ciphersuites(void)
  56077. {
  56078. return TEST_SKIPPED;
  56079. }
  56080. #endif
  56081. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  56082. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56083. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  56084. {
  56085. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56086. return TEST_SUCCESS;
  56087. }
  56088. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  56089. {
  56090. EXPECT_DECLS;
  56091. WOLFSSL_ALERT_HISTORY h;
  56092. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56093. ExpectIntEQ(h.last_rx.level, alert_fatal);
  56094. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  56095. return EXPECT_RESULT();
  56096. }
  56097. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56098. {
  56099. EXPECT_DECLS;
  56100. test_ssl_cbf client_cbs, server_cbs;
  56101. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56102. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56103. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  56104. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  56105. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  56106. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  56107. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  56108. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56109. &server_cbs, NULL), TEST_FAIL);
  56110. return EXPECT_RESULT();
  56111. }
  56112. #else
  56113. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  56114. {
  56115. return TEST_SKIPPED;
  56116. }
  56117. #endif
  56118. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  56119. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  56120. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  56121. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  56122. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  56123. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56124. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  56125. static int test_TLS_13_ticket_different_ciphers_run = 0;
  56126. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  56127. {
  56128. EXPECT_DECLS;
  56129. switch (test_TLS_13_ticket_different_ciphers_run) {
  56130. case 0:
  56131. /* First run */
  56132. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  56133. WOLFSSL_SUCCESS);
  56134. if (wolfSSL_is_server(ssl)) {
  56135. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  56136. wolfSSL_get_SSL_CTX(ssl));
  56137. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  56138. test_TLS_13_ticket_different_ciphers_ctx));
  56139. }
  56140. break;
  56141. case 1:
  56142. /* Second run */
  56143. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  56144. "TLS13-AES128-GCM-SHA256"),
  56145. WOLFSSL_SUCCESS);
  56146. if (!wolfSSL_is_server(ssl)) {
  56147. ExpectIntEQ(wolfSSL_set_session(ssl,
  56148. test_TLS_13_ticket_different_ciphers_session),
  56149. WOLFSSL_SUCCESS);
  56150. }
  56151. break;
  56152. default:
  56153. /* Bad state? */
  56154. Fail(("Should not enter here"), ("Should not enter here"));
  56155. }
  56156. return EXPECT_RESULT();
  56157. }
  56158. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  56159. {
  56160. EXPECT_DECLS;
  56161. switch (test_TLS_13_ticket_different_ciphers_run) {
  56162. case 0:
  56163. /* First run */
  56164. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  56165. wolfSSL_get1_session(ssl));
  56166. break;
  56167. case 1:
  56168. /* Second run */
  56169. ExpectTrue(wolfSSL_session_reused(ssl));
  56170. break;
  56171. default:
  56172. /* Bad state? */
  56173. Fail(("Should not enter here"), ("Should not enter here"));
  56174. }
  56175. return EXPECT_RESULT();
  56176. }
  56177. static int test_TLS_13_ticket_different_ciphers(void)
  56178. {
  56179. EXPECT_DECLS;
  56180. /* Check that we handle the connection when the ticket doesn't match
  56181. * the first ciphersuite. */
  56182. test_ssl_cbf client_cbs, server_cbs;
  56183. struct test_params {
  56184. method_provider client_meth;
  56185. method_provider server_meth;
  56186. int doUdp;
  56187. } params[] = {
  56188. #ifdef WOLFSSL_DTLS13
  56189. /* Test that the stateless code handles sessions correctly */
  56190. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  56191. #endif
  56192. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  56193. };
  56194. size_t i;
  56195. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56196. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56197. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56198. test_TLS_13_ticket_different_ciphers_run = 0;
  56199. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  56200. client_cbs.method = params[i].client_meth;
  56201. server_cbs.method = params[i].server_meth;
  56202. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56203. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  56204. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  56205. server_cbs.ticNoInit = 1;
  56206. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56207. &server_cbs, NULL), TEST_SUCCESS);
  56208. test_TLS_13_ticket_different_ciphers_run++;
  56209. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  56210. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56211. &server_cbs, NULL), TEST_SUCCESS);
  56212. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  56213. test_TLS_13_ticket_different_ciphers_session = NULL;
  56214. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  56215. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  56216. }
  56217. return EXPECT_RESULT();
  56218. }
  56219. #else
  56220. static int test_TLS_13_ticket_different_ciphers(void)
  56221. {
  56222. return TEST_SKIPPED;
  56223. }
  56224. #endif
  56225. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  56226. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56227. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  56228. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  56229. byte test_extra_alerts_wrong_cs_sh[] = {
  56230. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  56231. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  56232. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  56233. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  56234. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  56235. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  56236. 0xbb, 0x07, 0x54, 0x1c,
  56237. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  56238. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  56239. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  56240. };
  56241. static int test_extra_alerts_wrong_cs(void)
  56242. {
  56243. EXPECT_DECLS;
  56244. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  56245. struct test_memio_ctx test_ctx;
  56246. WOLFSSL_CTX *ctx_c = NULL;
  56247. WOLFSSL_ALERT_HISTORY h;
  56248. WOLFSSL *ssl_c = NULL;
  56249. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56250. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  56251. wolfTLSv1_2_client_method, NULL), 0);
  56252. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  56253. WOLFSSL_SUCCESS);
  56254. /* CH */
  56255. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56256. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56257. WOLFSSL_ERROR_WANT_READ);
  56258. /* consume CH */
  56259. test_ctx.s_len = 0;
  56260. /* inject SH */
  56261. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  56262. sizeof(test_extra_alerts_wrong_cs_sh));
  56263. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  56264. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56265. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56266. WOLFSSL_ERROR_WANT_READ);
  56267. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  56268. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  56269. ExpectIntEQ(h.last_tx.level, alert_fatal);
  56270. wolfSSL_free(ssl_c);
  56271. wolfSSL_CTX_free(ctx_c);
  56272. #endif
  56273. return EXPECT_RESULT();
  56274. }
  56275. #else
  56276. static int test_extra_alerts_wrong_cs(void)
  56277. {
  56278. return TEST_SKIPPED;
  56279. }
  56280. #endif
  56281. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  56282. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  56283. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  56284. {
  56285. tail_len -= msg_length;
  56286. XMEMMOVE(msg, msg + msg_length, tail_len);
  56287. *len = *len - msg_length;
  56288. }
  56289. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  56290. byte *found)
  56291. {
  56292. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  56293. const unsigned int _RECORD_HEADER_SZ = 5;
  56294. const int _change_cipher_hs = 55;
  56295. const int _change_cipher = 20;
  56296. const int _handshake = 22;
  56297. unsigned int tail_len;
  56298. byte *idx, *curr;
  56299. word8 currType;
  56300. word16 rLength;
  56301. word32 hLength;
  56302. idx = buf;
  56303. tail_len = *len;
  56304. *found = 0;
  56305. while (tail_len > _RECORD_HEADER_SZ) {
  56306. curr = idx;
  56307. currType = *idx;
  56308. ato16(idx + 3, &rLength);
  56309. idx += _RECORD_HEADER_SZ;
  56310. tail_len -= _RECORD_HEADER_SZ;
  56311. if (tail_len < rLength)
  56312. return -1;
  56313. if (type == _change_cipher_hs && currType == _change_cipher) {
  56314. if (rLength != 1)
  56315. return -1;
  56316. /* match */
  56317. test_remove_msg(curr, *len - (int)(curr - buf),
  56318. len, _RECORD_HEADER_SZ + 1);
  56319. *found = 1;
  56320. return 0;
  56321. }
  56322. if (currType != _handshake) {
  56323. idx += rLength;
  56324. tail_len -= rLength;
  56325. continue;
  56326. }
  56327. if (rLength < _HANDSHAKE_HEADER_SZ)
  56328. return -1;
  56329. currType = *idx;
  56330. ato24(idx+1, &hLength);
  56331. hLength += _HANDSHAKE_HEADER_SZ;
  56332. if (tail_len < hLength)
  56333. return -1;
  56334. if (currType != type) {
  56335. idx += hLength;
  56336. tail_len -= hLength;
  56337. continue;
  56338. }
  56339. /* match */
  56340. test_remove_msg(curr, *len - (int)(curr - buf), len,
  56341. hLength + _RECORD_HEADER_SZ);
  56342. *found = 1;
  56343. return 0;
  56344. }
  56345. /* not found */
  56346. return 0;
  56347. }
  56348. static int test_remove_hs_message(byte hs_message_type,
  56349. int extra_round, byte alert_type)
  56350. {
  56351. EXPECT_DECLS;
  56352. WOLFSSL_CTX *ctx_c = NULL;
  56353. WOLFSSL_CTX *ctx_s = NULL;
  56354. WOLFSSL *ssl_c = NULL;
  56355. WOLFSSL *ssl_s = NULL;
  56356. struct test_memio_ctx test_ctx;
  56357. WOLFSSL_ALERT_HISTORY h;
  56358. byte found = 0;
  56359. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56360. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56361. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  56362. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56363. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56364. WOLFSSL_ERROR_WANT_READ);
  56365. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  56366. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56367. WOLFSSL_ERROR_WANT_READ);
  56368. if (extra_round) {
  56369. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56370. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56371. WOLFSSL_ERROR_WANT_READ);
  56372. /* this will complete handshake from server side */
  56373. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  56374. }
  56375. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  56376. &test_ctx.c_len, hs_message_type, &found), 0);
  56377. if (!found) {
  56378. wolfSSL_free(ssl_c);
  56379. wolfSSL_CTX_free(ctx_c);
  56380. wolfSSL_free(ssl_s);
  56381. wolfSSL_CTX_free(ctx_s);
  56382. return TEST_SKIPPED;
  56383. }
  56384. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56385. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56386. WOLFSSL_ERROR_WANT_READ);
  56387. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  56388. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  56389. ExpectIntEQ(h.last_tx.level, alert_fatal);
  56390. wolfSSL_free(ssl_c);
  56391. wolfSSL_CTX_free(ctx_c);
  56392. wolfSSL_free(ssl_s);
  56393. wolfSSL_CTX_free(ctx_s);
  56394. return EXPECT_RESULT();
  56395. }
  56396. static int test_extra_alerts_skip_hs(void)
  56397. {
  56398. EXPECT_DECLS;
  56399. const byte _server_key_exchange = 12;
  56400. const byte _server_hello = 2;
  56401. const byte _certificate = 11;
  56402. /* server_hello */
  56403. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  56404. unexpected_message), TEST_FAIL);
  56405. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  56406. 0xff), TEST_FAIL);
  56407. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  56408. unexpected_message), TEST_FAIL);
  56409. return EXPECT_RESULT();
  56410. }
  56411. #else
  56412. static int test_extra_alerts_skip_hs(void)
  56413. {
  56414. return TEST_SKIPPED;
  56415. }
  56416. #endif
  56417. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  56418. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  56419. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  56420. unsigned char* key, unsigned int key_max_len)
  56421. {
  56422. (void)ssl;
  56423. (void)id;
  56424. (void)key_max_len;
  56425. /* zero means error */
  56426. key[0] = 0x10;
  56427. return 1;
  56428. }
  56429. static int test_extra_alerts_bad_psk(void)
  56430. {
  56431. EXPECT_DECLS;
  56432. WOLFSSL_CTX *ctx_c = NULL;
  56433. WOLFSSL_CTX *ctx_s = NULL;
  56434. WOLFSSL *ssl_c = NULL;
  56435. WOLFSSL *ssl_s = NULL;
  56436. struct test_memio_ctx test_ctx;
  56437. WOLFSSL_ALERT_HISTORY h;
  56438. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56439. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56440. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  56441. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  56442. WOLFSSL_SUCCESS);
  56443. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  56444. WOLFSSL_SUCCESS);
  56445. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  56446. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56447. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56448. WOLFSSL_ERROR_WANT_READ);
  56449. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  56450. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  56451. WOLFSSL_ERROR_WANT_READ);
  56452. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56453. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56454. WOLFSSL_ERROR_WANT_READ);
  56455. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  56456. ExpectIntEQ(h.last_tx.code, handshake_failure);
  56457. ExpectIntEQ(h.last_tx.level, alert_fatal);
  56458. wolfSSL_free(ssl_c);
  56459. wolfSSL_CTX_free(ctx_c);
  56460. wolfSSL_free(ssl_s);
  56461. wolfSSL_CTX_free(ctx_s);
  56462. return EXPECT_RESULT();
  56463. }
  56464. #else
  56465. static int test_extra_alerts_bad_psk(void)
  56466. {
  56467. return TEST_SKIPPED;
  56468. }
  56469. #endif
  56470. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  56471. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56472. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  56473. int sz, void *ctx)
  56474. {
  56475. static int sentServerHello = FALSE;
  56476. if (!sentServerHello) {
  56477. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  56478. size_t i;
  56479. if (sz < (int)sizeof(renegExt))
  56480. return WOLFSSL_CBIO_ERR_GENERAL;
  56481. /* Remove SCR from ServerHello */
  56482. for (i = 0; i < sz - sizeof(renegExt); i++) {
  56483. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  56484. /* Found the extension. Change it to something unrecognized. */
  56485. buf[i+1] = 0x11;
  56486. break;
  56487. }
  56488. }
  56489. sentServerHello = TRUE;
  56490. }
  56491. return EmbedSend(ssl, buf, sz, ctx);
  56492. }
  56493. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  56494. {
  56495. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  56496. }
  56497. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  56498. {
  56499. WOLFSSL_ALERT_HISTORY h;
  56500. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  56501. AssertIntEQ(h.last_rx.code, handshake_failure);
  56502. AssertIntEQ(h.last_rx.level, alert_fatal);
  56503. }
  56504. static int test_harden_no_secure_renegotiation(void)
  56505. {
  56506. EXPECT_DECLS;
  56507. callback_functions client_cbs, server_cbs;
  56508. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56509. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56510. client_cbs.method = wolfTLSv1_2_client_method;
  56511. server_cbs.method = wolfTLSv1_2_server_method;
  56512. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  56513. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  56514. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56515. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  56516. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  56517. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  56518. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  56519. server_cbs.last_err == FATAL_ERROR);
  56520. return EXPECT_RESULT();
  56521. }
  56522. #else
  56523. static int test_harden_no_secure_renegotiation(void)
  56524. {
  56525. return TEST_SKIPPED;
  56526. }
  56527. #endif
  56528. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56529. static int test_override_alt_cert_chain_cert_cb(int preverify,
  56530. WOLFSSL_X509_STORE_CTX* store)
  56531. {
  56532. fprintf(stderr, "preverify: %d\n", preverify);
  56533. fprintf(stderr, "store->error: %d\n", store->error);
  56534. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  56535. if (store->error == OCSP_INVALID_STATUS) {
  56536. fprintf(stderr, "Overriding OCSP error\n");
  56537. return 1;
  56538. }
  56539. #ifndef WOLFSSL_ALT_CERT_CHAINS
  56540. else if ((store->error == ASN_NO_SIGNER_E ||
  56541. store->error == ASN_SELF_SIGNED_E
  56542. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  56543. defined(HAVE_WEBSERVER)
  56544. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  56545. #endif
  56546. ) && store->error_depth == store->totalCerts - 1) {
  56547. fprintf(stderr, "Overriding no signer error only for root cert\n");
  56548. return 1;
  56549. }
  56550. #endif
  56551. else
  56552. return preverify;
  56553. }
  56554. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  56555. int urlSz, unsigned char* request, int requestSz,
  56556. unsigned char** response)
  56557. {
  56558. (void)ioCtx;
  56559. (void)url;
  56560. (void)urlSz;
  56561. (void)request;
  56562. (void)requestSz;
  56563. (void)response;
  56564. return -1;
  56565. }
  56566. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  56567. {
  56568. EXPECT_DECLS;
  56569. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  56570. test_override_alt_cert_chain_cert_cb);
  56571. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  56572. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  56573. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  56574. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  56575. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  56576. WOLFSSL_SUCCESS);
  56577. return EXPECT_RESULT();
  56578. }
  56579. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  56580. {
  56581. EXPECT_DECLS;
  56582. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  56583. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  56584. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  56585. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  56586. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  56587. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  56588. WOLFSSL_SUCCESS);
  56589. return EXPECT_RESULT();
  56590. }
  56591. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  56592. {
  56593. EXPECT_DECLS;
  56594. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  56595. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  56596. return EXPECT_RESULT();
  56597. }
  56598. static int test_override_alt_cert_chain(void)
  56599. {
  56600. EXPECT_DECLS;
  56601. size_t i;
  56602. struct test_params {
  56603. ctx_cb client_ctx_cb;
  56604. ctx_cb server_ctx_cb;
  56605. int result;
  56606. } params[] = {
  56607. {test_override_alt_cert_chain_client_ctx_ready,
  56608. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  56609. {test_override_alt_cert_chain_client_ctx_ready2,
  56610. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  56611. };
  56612. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56613. test_ssl_cbf client_cbs, server_cbs;
  56614. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56615. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56616. fprintf(stderr, "test config: %d\n", (int)i);
  56617. client_cbs.ctx_ready = params[i].client_ctx_cb;
  56618. server_cbs.ctx_ready = params[i].server_ctx_cb;
  56619. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  56620. &server_cbs, NULL), params[i].result);
  56621. ExpectIntEQ(client_cbs.return_code, params[i].result);
  56622. ExpectIntEQ(server_cbs.return_code, params[i].result);
  56623. }
  56624. return EXPECT_RESULT();
  56625. }
  56626. #else
  56627. static int test_override_alt_cert_chain(void)
  56628. {
  56629. return TEST_SKIPPED;
  56630. }
  56631. #endif
  56632. #if defined(HAVE_RPK)
  56633. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  56634. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  56635. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  56636. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  56637. {
  56638. int ret = WOLFSSL_SUCCESS;
  56639. (void)mode;
  56640. (void)strctx;
  56641. WOLFSSL_ENTER("MyRpkVerifyCb");
  56642. return ret;
  56643. }
  56644. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  56645. static WC_INLINE int test_rpk_memio_setup(
  56646. struct test_memio_ctx *ctx,
  56647. WOLFSSL_CTX **ctx_c,
  56648. WOLFSSL_CTX **ctx_s,
  56649. WOLFSSL **ssl_c,
  56650. WOLFSSL **ssl_s,
  56651. method_provider method_c,
  56652. method_provider method_s,
  56653. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  56654. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  56655. const char* pkey_c, int fmt_kc, /* client private key and format */
  56656. const char* pkey_s, int fmt_ks /* server private key and format */
  56657. )
  56658. {
  56659. int ret;
  56660. if (ctx_c != NULL && *ctx_c == NULL) {
  56661. *ctx_c = wolfSSL_CTX_new(method_c());
  56662. if (*ctx_c == NULL) {
  56663. return -1;
  56664. }
  56665. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  56666. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  56667. if (ret != WOLFSSL_SUCCESS) {
  56668. return -1;
  56669. }
  56670. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  56671. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  56672. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  56673. if (ret != WOLFSSL_SUCCESS) {
  56674. return -1;
  56675. }
  56676. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  56677. if (ret != WOLFSSL_SUCCESS) {
  56678. return -1;
  56679. }
  56680. }
  56681. if (ctx_s != NULL && *ctx_s == NULL) {
  56682. *ctx_s = wolfSSL_CTX_new(method_s());
  56683. if (*ctx_s == NULL) {
  56684. return -1;
  56685. }
  56686. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  56687. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  56688. if (ret != WOLFSSL_SUCCESS) {
  56689. return -1;
  56690. }
  56691. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  56692. if (ret != WOLFSSL_SUCCESS) {
  56693. return -1;
  56694. }
  56695. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  56696. if (ret != WOLFSSL_SUCCESS) {
  56697. return -1;
  56698. }
  56699. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  56700. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  56701. if (ctx->s_ciphers != NULL) {
  56702. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  56703. if (ret != WOLFSSL_SUCCESS) {
  56704. return -1;
  56705. }
  56706. }
  56707. }
  56708. if (ctx_c != NULL && ssl_c != NULL) {
  56709. *ssl_c = wolfSSL_new(*ctx_c);
  56710. if (*ssl_c == NULL) {
  56711. return -1;
  56712. }
  56713. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  56714. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  56715. }
  56716. if (ctx_s != NULL && ssl_s != NULL) {
  56717. *ssl_s = wolfSSL_new(*ctx_s);
  56718. if (*ssl_s == NULL) {
  56719. return -1;
  56720. }
  56721. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  56722. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  56723. #if !defined(NO_DH)
  56724. SetDH(*ssl_s);
  56725. #endif
  56726. }
  56727. return 0;
  56728. }
  56729. #endif /* HAVE_RPK */
  56730. static int test_rpk_set_xxx_cert_type(void)
  56731. {
  56732. EXPECT_DECLS;
  56733. #if defined(HAVE_RPK)
  56734. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  56735. WOLFSSL_CTX* ctx = NULL;
  56736. WOLFSSL* ssl = NULL;
  56737. int tp;
  56738. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  56739. ExpectNotNull(ctx);
  56740. ssl = wolfSSL_new(ctx);
  56741. ExpectNotNull(ssl);
  56742. /*--------------------------------------------*/
  56743. /* tests for wolfSSL_CTX_set_client_cert_type */
  56744. /*--------------------------------------------*/
  56745. /* illegal parameter test caces */
  56746. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  56747. MAX_CLIENT_CERT_TYPE_CNT),
  56748. BAD_FUNC_ARG);
  56749. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  56750. sizeof(ctype)),
  56751. BAD_FUNC_ARG);
  56752. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  56753. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  56754. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  56755. MAX_CLIENT_CERT_TYPE_CNT),
  56756. BAD_FUNC_ARG);
  56757. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  56758. ctype[1] = 10; /* set unknown cert type */
  56759. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  56760. MAX_CLIENT_CERT_TYPE_CNT),
  56761. BAD_FUNC_ARG);
  56762. /* pass larger type count */
  56763. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  56764. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  56765. ctype[2] = 1; /* pass unacceptable type count */
  56766. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  56767. MAX_CLIENT_CERT_TYPE_CNT + 1),
  56768. BAD_FUNC_ARG);
  56769. /* should accept NULL for type buffer */
  56770. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  56771. MAX_CLIENT_CERT_TYPE_CNT),
  56772. WOLFSSL_SUCCESS);
  56773. /* should accept zero for type count */
  56774. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  56775. 0),
  56776. WOLFSSL_SUCCESS);
  56777. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  56778. MAX_CLIENT_CERT_TYPE_CNT),
  56779. WOLFSSL_SUCCESS);
  56780. /*--------------------------------------------*/
  56781. /* tests for wolfSSL_CTX_set_server_cert_type */
  56782. /*--------------------------------------------*/
  56783. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  56784. MAX_SERVER_CERT_TYPE_CNT),
  56785. BAD_FUNC_ARG);
  56786. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  56787. sizeof(ctype)),
  56788. BAD_FUNC_ARG);
  56789. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  56790. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  56791. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  56792. MAX_SERVER_CERT_TYPE_CNT),
  56793. BAD_FUNC_ARG);
  56794. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  56795. ctype[1] = 10; /* set unknown cert type */
  56796. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  56797. MAX_SERVER_CERT_TYPE_CNT),
  56798. BAD_FUNC_ARG);
  56799. /* pass larger type count */
  56800. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  56801. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  56802. ctype[2] = 1; /* pass unacceptable type count */
  56803. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  56804. MAX_SERVER_CERT_TYPE_CNT + 1),
  56805. BAD_FUNC_ARG);
  56806. /* should accept NULL for type buffer */
  56807. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  56808. MAX_SERVER_CERT_TYPE_CNT),
  56809. WOLFSSL_SUCCESS);
  56810. /* should accept zero for type count */
  56811. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  56812. 0),
  56813. WOLFSSL_SUCCESS);
  56814. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  56815. MAX_CLIENT_CERT_TYPE_CNT),
  56816. WOLFSSL_SUCCESS);
  56817. /*--------------------------------------------*/
  56818. /* tests for wolfSSL_set_client_cert_type */
  56819. /*--------------------------------------------*/
  56820. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  56821. MAX_CLIENT_CERT_TYPE_CNT),
  56822. BAD_FUNC_ARG);
  56823. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  56824. sizeof(ctype)),
  56825. BAD_FUNC_ARG);
  56826. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  56827. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  56828. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  56829. MAX_CLIENT_CERT_TYPE_CNT),
  56830. BAD_FUNC_ARG);
  56831. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  56832. ctype[1] = 10; /* set unknown cert type */
  56833. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  56834. MAX_CLIENT_CERT_TYPE_CNT),
  56835. BAD_FUNC_ARG);
  56836. /* pass larger type count */
  56837. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  56838. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  56839. ctype[2] = 1; /* pass unacceptable type count */
  56840. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  56841. MAX_CLIENT_CERT_TYPE_CNT + 1),
  56842. BAD_FUNC_ARG);
  56843. /* should accept NULL for type buffer */
  56844. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  56845. MAX_CLIENT_CERT_TYPE_CNT),
  56846. WOLFSSL_SUCCESS);
  56847. /* should accept zero for type count */
  56848. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  56849. 0),
  56850. WOLFSSL_SUCCESS);
  56851. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  56852. MAX_CLIENT_CERT_TYPE_CNT),
  56853. WOLFSSL_SUCCESS);
  56854. /*--------------------------------------------*/
  56855. /* tests for wolfSSL_CTX_set_server_cert_type */
  56856. /*--------------------------------------------*/
  56857. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  56858. MAX_SERVER_CERT_TYPE_CNT),
  56859. BAD_FUNC_ARG);
  56860. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  56861. sizeof(ctype)),
  56862. BAD_FUNC_ARG);
  56863. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  56864. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  56865. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  56866. MAX_SERVER_CERT_TYPE_CNT),
  56867. BAD_FUNC_ARG);
  56868. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  56869. ctype[1] = 10; /* set unknown cert type */
  56870. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  56871. MAX_SERVER_CERT_TYPE_CNT),
  56872. BAD_FUNC_ARG);
  56873. /* pass larger type count */
  56874. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  56875. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  56876. ctype[2] = 1; /* pass unacceptable type count */
  56877. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  56878. MAX_SERVER_CERT_TYPE_CNT + 1),
  56879. BAD_FUNC_ARG);
  56880. /* should accept NULL for type buffer */
  56881. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  56882. MAX_SERVER_CERT_TYPE_CNT),
  56883. WOLFSSL_SUCCESS);
  56884. /* should accept zero for type count */
  56885. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  56886. 0),
  56887. WOLFSSL_SUCCESS);
  56888. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  56889. MAX_SERVER_CERT_TYPE_CNT),
  56890. WOLFSSL_SUCCESS);
  56891. /*------------------------------------------------*/
  56892. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  56893. /*------------------------------------------------*/
  56894. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  56895. BAD_FUNC_ARG);
  56896. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  56897. BAD_FUNC_ARG);
  56898. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  56899. BAD_FUNC_ARG);
  56900. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  56901. BAD_FUNC_ARG);
  56902. /* clean up */
  56903. wolfSSL_free(ssl);
  56904. wolfSSL_CTX_free(ctx);
  56905. #endif
  56906. return EXPECT_RESULT();
  56907. }
  56908. static int test_tls13_rpk_handshake(void)
  56909. {
  56910. EXPECT_DECLS;
  56911. #if defined(HAVE_RPK)
  56912. int ret = 0;
  56913. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  56914. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  56915. struct test_memio_ctx test_ctx;
  56916. int err;
  56917. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  56918. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  56919. int typeCnt_c;
  56920. int typeCnt_s;
  56921. int tp;
  56922. (void)err;
  56923. (void)typeCnt_c;
  56924. (void)typeCnt_s;
  56925. (void)certType_c;
  56926. (void)certType_s;
  56927. /* TLS1.2
  56928. * Both client and server load x509 cert and start handshaking.
  56929. * Check no negotiation occurred.
  56930. */
  56931. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56932. ExpectIntEQ(
  56933. test_rpk_memio_setup(
  56934. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56935. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  56936. cliCertFile, WOLFSSL_FILETYPE_PEM,
  56937. svrCertFile, WOLFSSL_FILETYPE_PEM,
  56938. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  56939. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  56940. , 0);
  56941. /* set client certificate type in client end */
  56942. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  56943. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  56944. typeCnt_c = 2;
  56945. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  56946. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  56947. typeCnt_s = 2;
  56948. /* both clien and server do not call client/server_cert_type APIs,
  56949. * expecting default settings works and no negotiation performed.
  56950. */
  56951. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  56952. return TEST_FAIL;
  56953. /* confirm no negotiation occurred */
  56954. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  56955. WOLFSSL_SUCCESS);
  56956. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  56957. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  56958. WOLFSSL_SUCCESS);
  56959. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  56960. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  56961. WOLFSSL_SUCCESS);
  56962. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  56963. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  56964. WOLFSSL_SUCCESS);
  56965. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  56966. wolfSSL_free(ssl_c);
  56967. wolfSSL_CTX_free(ctx_c);
  56968. wolfSSL_free(ssl_s);
  56969. wolfSSL_CTX_free(ctx_s);
  56970. ssl_c = ssl_s = NULL;
  56971. ctx_c = ctx_s = NULL;
  56972. /* Both client and server load x509 cert and start handshaking.
  56973. * Check no negotiation occurred.
  56974. */
  56975. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56976. ExpectIntEQ(
  56977. test_rpk_memio_setup(
  56978. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56979. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  56980. cliCertFile, WOLFSSL_FILETYPE_PEM,
  56981. svrCertFile, WOLFSSL_FILETYPE_PEM,
  56982. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  56983. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  56984. , 0);
  56985. /* set client certificate type in client end */
  56986. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  56987. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  56988. typeCnt_c = 2;
  56989. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  56990. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  56991. typeCnt_s = 2;
  56992. /* both clien and server do not call client/server_cert_type APIs,
  56993. * expecting default settings works and no negotiation performed.
  56994. */
  56995. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  56996. return TEST_FAIL;
  56997. /* confirm no negotiation occurred */
  56998. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  56999. WOLFSSL_SUCCESS);
  57000. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57001. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57002. WOLFSSL_SUCCESS);
  57003. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57004. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57005. WOLFSSL_SUCCESS);
  57006. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57007. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57008. WOLFSSL_SUCCESS);
  57009. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57010. wolfSSL_free(ssl_c);
  57011. wolfSSL_CTX_free(ctx_c);
  57012. wolfSSL_free(ssl_s);
  57013. wolfSSL_CTX_free(ctx_s);
  57014. ssl_c = ssl_s = NULL;
  57015. ctx_c = ctx_s = NULL;
  57016. /* Both client and server load RPK cert and start handshaking.
  57017. * Confirm negotiated cert types match as expected.
  57018. */
  57019. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57020. ExpectIntEQ(
  57021. test_rpk_memio_setup(
  57022. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57023. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57024. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57025. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57026. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57027. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57028. , 0);
  57029. /* set client certificate type in client end */
  57030. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57031. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57032. typeCnt_c = 2;
  57033. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57034. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57035. typeCnt_s = 2;
  57036. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57037. WOLFSSL_SUCCESS);
  57038. /* set server certificate type in client end */
  57039. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57040. WOLFSSL_SUCCESS);
  57041. /* set client certificate type in server end */
  57042. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57043. WOLFSSL_SUCCESS);
  57044. /* set server certificate type in server end */
  57045. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57046. WOLFSSL_SUCCESS);
  57047. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57048. return TEST_FAIL;
  57049. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57050. WOLFSSL_SUCCESS);
  57051. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57052. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57053. WOLFSSL_SUCCESS);
  57054. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57055. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57056. WOLFSSL_SUCCESS);
  57057. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57058. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57059. WOLFSSL_SUCCESS);
  57060. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57061. wolfSSL_free(ssl_c);
  57062. wolfSSL_CTX_free(ctx_c);
  57063. wolfSSL_free(ssl_s);
  57064. wolfSSL_CTX_free(ctx_s);
  57065. ssl_c = ssl_s = NULL;
  57066. ctx_c = ctx_s = NULL;
  57067. /* TLS1.2
  57068. * Both client and server load RPK cert and start handshaking.
  57069. * Confirm negotiated cert types match as expected.
  57070. */
  57071. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57072. ExpectIntEQ(
  57073. test_rpk_memio_setup(
  57074. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57075. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  57076. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57077. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57078. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57079. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57080. , 0);
  57081. /* set client certificate type in client end */
  57082. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57083. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57084. typeCnt_c = 2;
  57085. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57086. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57087. typeCnt_s = 2;
  57088. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57089. WOLFSSL_SUCCESS);
  57090. /* set server certificate type in client end */
  57091. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57092. WOLFSSL_SUCCESS);
  57093. /* set client certificate type in server end */
  57094. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57095. WOLFSSL_SUCCESS);
  57096. /* set server certificate type in server end */
  57097. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57098. WOLFSSL_SUCCESS);
  57099. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57100. return TEST_FAIL;
  57101. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57102. WOLFSSL_SUCCESS);
  57103. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57104. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57105. WOLFSSL_SUCCESS);
  57106. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57107. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57108. WOLFSSL_SUCCESS);
  57109. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57110. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57111. WOLFSSL_SUCCESS);
  57112. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57113. wolfSSL_free(ssl_c);
  57114. wolfSSL_CTX_free(ctx_c);
  57115. wolfSSL_free(ssl_s);
  57116. wolfSSL_CTX_free(ctx_s);
  57117. ssl_c = ssl_s = NULL;
  57118. ctx_c = ctx_s = NULL;
  57119. /* Both client and server load x509 cert.
  57120. * Have client call set_client_cert_type with both RPK and x509.
  57121. * This doesn't makes client add client cert type extension to ClientHello,
  57122. * since it does not load RPK cert actually.
  57123. */
  57124. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57125. ExpectIntEQ(
  57126. test_rpk_memio_setup(
  57127. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57128. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57129. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57130. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57131. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57132. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57133. , 0);
  57134. /* set client certificate type in client end */
  57135. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57136. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57137. typeCnt_c = 2;
  57138. /* client indicates both RPK and x509 certs are available but loaded RPK
  57139. * cert only. It does not have client add client-cert-type extension in CH.
  57140. */
  57141. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57142. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57143. typeCnt_c = 2;
  57144. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57145. WOLFSSL_SUCCESS);
  57146. /* client indicates both RPK and x509 certs are acceptable */
  57147. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57148. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57149. typeCnt_s = 2;
  57150. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57151. WOLFSSL_SUCCESS);
  57152. /* server indicates both RPK and x509 certs are acceptable */
  57153. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57154. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57155. typeCnt_c = 2;
  57156. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57157. WOLFSSL_SUCCESS);
  57158. /* server should indicate only RPK cert is available */
  57159. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57160. certType_s[1] = -1;
  57161. typeCnt_s = 1;
  57162. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57163. WOLFSSL_SUCCESS);
  57164. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57165. return TEST_FAIL;
  57166. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  57167. * be returned as cert type.
  57168. */
  57169. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57170. WOLFSSL_SUCCESS);
  57171. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57172. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57173. WOLFSSL_SUCCESS);
  57174. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57175. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57176. WOLFSSL_SUCCESS);
  57177. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57178. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57179. WOLFSSL_SUCCESS);
  57180. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57181. wolfSSL_free(ssl_c);
  57182. wolfSSL_CTX_free(ctx_c);
  57183. wolfSSL_free(ssl_s);
  57184. wolfSSL_CTX_free(ctx_s);
  57185. ssl_c = ssl_s = NULL;
  57186. ctx_c = ctx_s = NULL;
  57187. /* Have client load RPK cert and have server load x509 cert.
  57188. * Check the negotiation result from both ends.
  57189. */
  57190. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57191. ExpectIntEQ(
  57192. test_rpk_memio_setup(
  57193. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57194. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57195. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57196. svrCertFile, WOLFSSL_FILETYPE_PEM,
  57197. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57198. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57199. , 0);
  57200. /* have client tell to use RPK cert */
  57201. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57202. certType_c[1] = -1;
  57203. typeCnt_c = 1;
  57204. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57205. WOLFSSL_SUCCESS);
  57206. /* have client tell to accept both RPK and x509 cert */
  57207. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57208. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  57209. typeCnt_s = 2;
  57210. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57211. WOLFSSL_SUCCESS);
  57212. /* have server accept to both RPK and x509 cert */
  57213. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  57214. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  57215. typeCnt_c = 2;
  57216. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57217. WOLFSSL_SUCCESS);
  57218. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  57219. * end, expecting the default setting works.
  57220. */
  57221. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  57222. return TEST_FAIL;
  57223. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57224. WOLFSSL_SUCCESS);
  57225. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57226. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57227. WOLFSSL_SUCCESS);
  57228. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57229. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57230. WOLFSSL_SUCCESS);
  57231. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57232. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57233. WOLFSSL_SUCCESS);
  57234. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57235. wolfSSL_free(ssl_c);
  57236. wolfSSL_CTX_free(ctx_c);
  57237. wolfSSL_free(ssl_s);
  57238. wolfSSL_CTX_free(ctx_s);
  57239. ssl_c = ssl_s = NULL;
  57240. ctx_c = ctx_s = NULL;
  57241. /* Have both client and server load RPK cert, however, have server
  57242. * indicate its cert type x509.
  57243. * Client is expected to detect the cert type mismatch then to send alert
  57244. * with "unsupported_certificate".
  57245. */
  57246. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57247. ExpectIntEQ(
  57248. test_rpk_memio_setup(
  57249. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57250. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57251. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57252. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  57253. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57254. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57255. , 0);
  57256. /* have client tell to use RPK cert */
  57257. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57258. certType_c[1] = -1;
  57259. typeCnt_c = 1;
  57260. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57261. WOLFSSL_SUCCESS);
  57262. /* have client tell to accept both RPK and x509 cert */
  57263. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57264. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  57265. typeCnt_s = 2;
  57266. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57267. WOLFSSL_SUCCESS);
  57268. /* have server accept to both RPK and x509 cert */
  57269. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  57270. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  57271. typeCnt_c = 2;
  57272. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57273. WOLFSSL_SUCCESS);
  57274. /* have server tell to use x509 cert intentionally. This will bring
  57275. * certificate type mismatch in client side.
  57276. */
  57277. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57278. certType_s[1] = -1;
  57279. typeCnt_s = 1;
  57280. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57281. WOLFSSL_SUCCESS);
  57282. /* expect client detect cert type mismatch then send Alert */
  57283. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  57284. if (ret != -1)
  57285. return TEST_FAIL;
  57286. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  57287. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57288. WOLFSSL_SUCCESS);
  57289. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57290. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57291. WOLFSSL_SUCCESS);
  57292. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57293. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57294. WOLFSSL_SUCCESS);
  57295. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57296. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57297. WOLFSSL_SUCCESS);
  57298. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57299. wolfSSL_free(ssl_c);
  57300. wolfSSL_CTX_free(ctx_c);
  57301. wolfSSL_free(ssl_s);
  57302. wolfSSL_CTX_free(ctx_s);
  57303. ssl_c = ssl_s = NULL;
  57304. ctx_c = ctx_s = NULL;
  57305. /* Have client load x509 cert and server load RPK cert,
  57306. * however, have client indicate its cert type RPK.
  57307. * Server is expected to detect the cert type mismatch then to send alert
  57308. * with "unsupported_certificate".
  57309. */
  57310. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57311. ExpectIntEQ(
  57312. test_rpk_memio_setup(
  57313. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57314. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57315. cliCertFile, WOLFSSL_FILETYPE_PEM,
  57316. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57317. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57318. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57319. , 0);
  57320. /* have client tell to use RPK cert intentionally */
  57321. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57322. certType_c[1] = -1;
  57323. typeCnt_c = 1;
  57324. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57325. WOLFSSL_SUCCESS);
  57326. /* have client tell to accept both RPK and x509 cert */
  57327. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57328. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  57329. typeCnt_s = 2;
  57330. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57331. WOLFSSL_SUCCESS);
  57332. /* have server accept to both RPK and x509 cert */
  57333. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  57334. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  57335. typeCnt_c = 2;
  57336. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57337. WOLFSSL_SUCCESS);
  57338. /* have server tell to use x509 cert intentionally. This will bring
  57339. * certificate type mismatch in client side.
  57340. */
  57341. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  57342. certType_s[1] = -1;
  57343. typeCnt_s = 1;
  57344. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57345. WOLFSSL_SUCCESS);
  57346. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  57347. /* expect server detect cert type mismatch then send Alert */
  57348. ExpectIntNE(ret, 0);
  57349. err = wolfSSL_get_error(ssl_c, ret);
  57350. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  57351. /* client did not load RPK cert actually, so negotiation did not happen */
  57352. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57353. WOLFSSL_SUCCESS);
  57354. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57355. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57356. WOLFSSL_SUCCESS);
  57357. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57358. /* client did not load RPK cert actually, so negotiation did not happen */
  57359. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57360. WOLFSSL_SUCCESS);
  57361. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  57362. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57363. WOLFSSL_SUCCESS);
  57364. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  57365. wolfSSL_free(ssl_c);
  57366. wolfSSL_CTX_free(ctx_c);
  57367. wolfSSL_free(ssl_s);
  57368. wolfSSL_CTX_free(ctx_s);
  57369. ssl_c = ssl_s = NULL;
  57370. ctx_c = ctx_s = NULL;
  57371. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  57372. /* Both client and server load RPK cert and set certificate verify
  57373. * callbacks then start handshaking.
  57374. * Confirm both side can refer the peer's cert.
  57375. */
  57376. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57377. ExpectIntEQ(
  57378. test_rpk_memio_setup(
  57379. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57380. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  57381. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57382. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  57383. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  57384. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  57385. , 0);
  57386. /* set client certificate type in client end */
  57387. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  57388. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  57389. typeCnt_c = 2;
  57390. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  57391. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  57392. typeCnt_s = 2;
  57393. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  57394. WOLFSSL_SUCCESS);
  57395. /* set server certificate type in client end */
  57396. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  57397. WOLFSSL_SUCCESS);
  57398. /* set client certificate type in server end */
  57399. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  57400. WOLFSSL_SUCCESS);
  57401. /* set server certificate type in server end */
  57402. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  57403. WOLFSSL_SUCCESS);
  57404. /* set certificate verify callback to both client and server */
  57405. int isServer = 0;
  57406. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  57407. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  57408. isServer = 1;
  57409. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  57410. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  57411. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  57412. if (ret != 0)
  57413. return TEST_FAIL;
  57414. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  57415. WOLFSSL_SUCCESS);
  57416. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57417. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  57418. WOLFSSL_SUCCESS);
  57419. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57420. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  57421. WOLFSSL_SUCCESS);
  57422. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57423. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  57424. WOLFSSL_SUCCESS);
  57425. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  57426. wolfSSL_free(ssl_c);
  57427. wolfSSL_CTX_free(ctx_c);
  57428. wolfSSL_free(ssl_s);
  57429. wolfSSL_CTX_free(ctx_s);
  57430. ssl_c = ssl_s = NULL;
  57431. ctx_c = ctx_s = NULL;
  57432. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  57433. #endif /* HAVE_RPK */
  57434. return EXPECT_RESULT();
  57435. }
  57436. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  57437. static int test_dtls13_bad_epoch_ch(void)
  57438. {
  57439. EXPECT_DECLS;
  57440. WOLFSSL_CTX *ctx_c = NULL;
  57441. WOLFSSL_CTX *ctx_s = NULL;
  57442. WOLFSSL *ssl_c = NULL;
  57443. WOLFSSL *ssl_s = NULL;
  57444. struct test_memio_ctx test_ctx;
  57445. const int EPOCH_OFF = 3;
  57446. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57447. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57448. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  57449. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  57450. * with just one message */
  57451. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  57452. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57453. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  57454. WOLFSSL_ERROR_WANT_READ);
  57455. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  57456. /* first CH should use epoch 0x0 */
  57457. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  57458. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  57459. /* change epoch to 2 */
  57460. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  57461. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57462. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  57463. WOLFSSL_ERROR_WANT_READ);
  57464. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  57465. /* resend the CH */
  57466. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  57467. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57468. wolfSSL_free(ssl_c);
  57469. wolfSSL_CTX_free(ctx_c);
  57470. wolfSSL_free(ssl_s);
  57471. wolfSSL_CTX_free(ctx_s);
  57472. return EXPECT_RESULT();
  57473. }
  57474. #else
  57475. static int test_dtls13_bad_epoch_ch(void)
  57476. {
  57477. return TEST_SKIPPED;
  57478. }
  57479. #endif
  57480. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  57481. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  57482. {
  57483. EXPECT_DECLS;
  57484. WOLFSSL_SESSION *sess = NULL;
  57485. /* Setup the session to avoid errors */
  57486. ssl->session->timeout = -1;
  57487. ssl->session->side = WOLFSSL_CLIENT_END;
  57488. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  57489. defined(HAVE_SESSION_TICKET))
  57490. ssl->session->version = ssl->version;
  57491. #endif
  57492. /* Force a short session ID to be sent */
  57493. ssl->session->sessionIDSz = 4;
  57494. #ifndef NO_SESSION_CACHE_REF
  57495. /* Allow the client cache to be used */
  57496. ssl->session->idLen = 4;
  57497. #endif
  57498. ssl->session->isSetup = 1;
  57499. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  57500. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  57501. return EXPECT_RESULT();
  57502. }
  57503. static int test_short_session_id(void)
  57504. {
  57505. EXPECT_DECLS;
  57506. test_ssl_cbf client_cbf;
  57507. test_ssl_cbf server_cbf;
  57508. size_t i;
  57509. struct {
  57510. method_provider client_meth;
  57511. method_provider server_meth;
  57512. const char* tls_version;
  57513. } params[] = {
  57514. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  57515. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  57516. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  57517. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  57518. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  57519. #ifdef WOLFSSL_DTLS13
  57520. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  57521. #endif
  57522. #endif
  57523. #ifndef WOLFSSL_NO_TLS12
  57524. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  57525. #ifdef WOLFSSL_DTLS
  57526. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  57527. #endif
  57528. #endif
  57529. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  57530. !defined(NO_DES3))
  57531. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  57532. #ifdef WOLFSSL_DTLS
  57533. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  57534. #endif
  57535. #endif
  57536. };
  57537. fprintf(stderr, "\n");
  57538. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  57539. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57540. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57541. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  57542. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  57543. client_cbf.method = params[i].client_meth;
  57544. server_cbf.method = params[i].server_meth;
  57545. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  57546. &server_cbf, NULL), TEST_SUCCESS);
  57547. }
  57548. return EXPECT_RESULT();
  57549. }
  57550. #else
  57551. static int test_short_session_id(void)
  57552. {
  57553. return TEST_SKIPPED;
  57554. }
  57555. #endif
  57556. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  57557. && defined(WOLFSSL_DTLS13)
  57558. static byte* test_find_string(const char *string,
  57559. byte *buf, int buf_size)
  57560. {
  57561. int string_size, i;
  57562. string_size = (int)XSTRLEN(string);
  57563. for (i = 0; i < buf_size - string_size - 1; i++) {
  57564. if (XSTRCMP((char*)&buf[i], string) == 0)
  57565. return &buf[i];
  57566. }
  57567. return NULL;
  57568. }
  57569. static int test_wolfSSL_dtls13_null_cipher(void)
  57570. {
  57571. EXPECT_DECLS;
  57572. WOLFSSL_CTX *ctx_c = NULL;
  57573. WOLFSSL_CTX *ctx_s = NULL;
  57574. WOLFSSL *ssl_c = NULL;
  57575. WOLFSSL *ssl_s = NULL;
  57576. struct test_memio_ctx test_ctx;
  57577. const char *test_str = "test";
  57578. int test_str_size;
  57579. byte buf[255], *ptr = NULL;
  57580. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57581. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  57582. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  57583. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  57584. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  57585. test_str_size = XSTRLEN("test") + 1;
  57586. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  57587. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  57588. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  57589. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  57590. /* check that the packet was sent cleartext */
  57591. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  57592. test_ctx.s_len));
  57593. if (ptr != NULL) {
  57594. /* modify the message */
  57595. *ptr = 'H';
  57596. /* bad messages should be ignored in DTLS */
  57597. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  57598. ExpectIntEQ(ssl_s->error, WANT_READ);
  57599. }
  57600. wolfSSL_free(ssl_c);
  57601. wolfSSL_free(ssl_s);
  57602. wolfSSL_CTX_free(ctx_c);
  57603. wolfSSL_CTX_free(ctx_s);
  57604. return TEST_SUCCESS;
  57605. }
  57606. #else
  57607. static int test_wolfSSL_dtls13_null_cipher(void)
  57608. {
  57609. return TEST_SKIPPED;
  57610. }
  57611. #endif
  57612. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57613. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57614. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  57615. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  57616. {
  57617. SOCKADDR_S peer;
  57618. XSOCKLENT len;
  57619. int ret;
  57620. XMEMSET((byte*)&peer, 0, sizeof(peer));
  57621. len = sizeof(peer);
  57622. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  57623. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  57624. return -1;
  57625. switch (peer.ss_family) {
  57626. #ifdef WOLFSSL_IPV6
  57627. case WOLFSSL_IP6: {
  57628. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  57629. break;
  57630. }
  57631. #endif /* WOLFSSL_IPV6 */
  57632. case WOLFSSL_IP4:
  57633. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  57634. break;
  57635. default:
  57636. return -1;
  57637. }
  57638. return 0;
  57639. }
  57640. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  57641. {
  57642. char buf[1] = {'t'};
  57643. SOCKADDR_IN_T addr;
  57644. int sock_fd;
  57645. word16 port;
  57646. int err;
  57647. (void)ssl;
  57648. (void)ctx;
  57649. if (ssl == NULL)
  57650. return -1;
  57651. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  57652. if (err != 0)
  57653. return -1;
  57654. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  57655. if (sock_fd == -1)
  57656. return -1;
  57657. build_addr(&addr, wolfSSLIP, port, 1, 0);
  57658. /* send a packet to the server. Being another socket, the kernel will ensure
  57659. * the source port will be different. */
  57660. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  57661. sizeof(addr));
  57662. close(sock_fd);
  57663. if (err == -1)
  57664. return -1;
  57665. return 0;
  57666. }
  57667. /* setup a SSL session but just after the handshake send a packet to the server
  57668. * with a source address different than the one of the connected client. The I/O
  57669. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  57670. * is done in test_dtls_msg_from_other_peer_cb */
  57671. static int test_dtls_msg_from_other_peer(void)
  57672. {
  57673. EXPECT_DECLS;
  57674. callback_functions client_cbs;
  57675. callback_functions server_cbs;
  57676. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  57677. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  57678. client_cbs.method = wolfDTLSv1_2_client_method;
  57679. server_cbs.method = wolfDTLSv1_2_server_method;
  57680. client_cbs.doUdp = 1;
  57681. server_cbs.doUdp = 1;
  57682. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  57683. test_dtls_msg_from_other_peer_cb);
  57684. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  57685. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  57686. return EXPECT_RESULT();
  57687. }
  57688. #else
  57689. static int test_dtls_msg_from_other_peer(void)
  57690. {
  57691. return TEST_SKIPPED;
  57692. }
  57693. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57694. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57695. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  57696. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  57697. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57698. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57699. static int test_dtls_ipv6_check(void)
  57700. {
  57701. EXPECT_DECLS;
  57702. WOLFSSL_CTX *ctx_c = NULL;
  57703. WOLFSSL_CTX *ctx_s = NULL;
  57704. WOLFSSL *ssl_c = NULL;
  57705. WOLFSSL *ssl_s = NULL;
  57706. SOCKADDR_IN fake_addr6;
  57707. int sockfd = -1;
  57708. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  57709. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  57710. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  57711. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  57712. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57713. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  57714. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57715. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  57716. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  57717. /* mimic a sockaddr_in6 struct, this way we can't test without
  57718. * WOLFSSL_IPV6 */
  57719. fake_addr6.sin_family = WOLFSSL_IP6;
  57720. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  57721. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  57722. /* can't return error here, as the peer is opaque for wolfssl library at
  57723. * this point */
  57724. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  57725. WOLFSSL_SUCCESS);
  57726. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  57727. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  57728. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  57729. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  57730. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  57731. WOLFSSL_SUCCESS);
  57732. /* reuse the socket */
  57733. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  57734. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  57735. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  57736. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  57737. if (sockfd != -1)
  57738. close(sockfd);
  57739. wolfSSL_free(ssl_c);
  57740. wolfSSL_CTX_free(ctx_c);
  57741. wolfSSL_free(ssl_s);
  57742. wolfSSL_CTX_free(ctx_s);
  57743. return EXPECT_RESULT();
  57744. }
  57745. #else
  57746. static int test_dtls_ipv6_check(void)
  57747. {
  57748. return TEST_SKIPPED;
  57749. }
  57750. #endif
  57751. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57752. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  57753. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  57754. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  57755. {
  57756. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  57757. }
  57758. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  57759. {
  57760. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  57761. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  57762. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  57763. }
  57764. else {
  57765. char testMsg[] = "Message after SCR";
  57766. char msgBuf[sizeof(testMsg)];
  57767. int ret;
  57768. if (!wolfSSL_is_server(ssl)) {
  57769. AssertIntEQ(WOLFSSL_SUCCESS,
  57770. wolfSSL_set_session(ssl,
  57771. test_wolfSSL_SCR_after_resumption_session));
  57772. }
  57773. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  57774. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  57775. sizeof(testMsg));
  57776. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  57777. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  57778. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  57779. AssertIntEQ(ret, sizeof(msgBuf));
  57780. }
  57781. }
  57782. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  57783. {
  57784. AssertIntEQ(WOLFSSL_SUCCESS,
  57785. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  57786. }
  57787. static int test_wolfSSL_SCR_after_resumption(void)
  57788. {
  57789. EXPECT_DECLS;
  57790. callback_functions func_cb_client;
  57791. callback_functions func_cb_server;
  57792. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  57793. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  57794. func_cb_client.method = wolfTLSv1_2_client_method;
  57795. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  57796. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  57797. func_cb_server.method = wolfTLSv1_2_server_method;
  57798. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  57799. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  57800. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  57801. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  57802. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  57803. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  57804. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  57805. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  57806. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  57807. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  57808. return EXPECT_RESULT();
  57809. }
  57810. #else
  57811. static int test_wolfSSL_SCR_after_resumption(void)
  57812. {
  57813. return TEST_SKIPPED;
  57814. }
  57815. #endif
  57816. static int test_wolfSSL_configure_args(void)
  57817. {
  57818. EXPECT_DECLS;
  57819. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  57820. ExpectNotNull(wolfSSL_configure_args());
  57821. #endif
  57822. return EXPECT_RESULT();
  57823. }
  57824. static int test_dtls_no_extensions(void)
  57825. {
  57826. EXPECT_DECLS;
  57827. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  57828. WOLFSSL *ssl_s = NULL;
  57829. WOLFSSL_CTX *ctx_s = NULL;
  57830. struct test_memio_ctx test_ctx;
  57831. const byte chNoExtensions[] = {
  57832. /* Handshake type */
  57833. 0x16,
  57834. /* Version */
  57835. 0xfe, 0xff,
  57836. /* Epoch */
  57837. 0x00, 0x00,
  57838. /* Seq number */
  57839. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57840. /* Length */
  57841. 0x00, 0x40,
  57842. /* CH type */
  57843. 0x01,
  57844. /* Length */
  57845. 0x00, 0x00, 0x34,
  57846. /* Msg Seq */
  57847. 0x00, 0x00,
  57848. /* Frag offset */
  57849. 0x00, 0x00, 0x00,
  57850. /* Frag length */
  57851. 0x00, 0x00, 0x34,
  57852. /* Version */
  57853. 0xfe, 0xff,
  57854. /* Random */
  57855. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  57856. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  57857. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  57858. /* Session Length */
  57859. 0x00,
  57860. /* Cookie Length */
  57861. 0x00,
  57862. /* CS Length */
  57863. 0x00, 0x0c,
  57864. /* CS */
  57865. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  57866. /* Comp Meths Length */
  57867. 0x01,
  57868. /* Comp Meths */
  57869. 0x00
  57870. /* And finally... no extensions */
  57871. };
  57872. int i;
  57873. #ifdef OPENSSL_EXTRA
  57874. int repeats = 2;
  57875. #else
  57876. int repeats = 1;
  57877. #endif
  57878. for (i = 0; i < repeats; i++) {
  57879. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57880. ssl_s = NULL;
  57881. ctx_s = NULL;
  57882. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  57883. NULL, wolfDTLS_server_method), 0);
  57884. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  57885. test_ctx.s_len = sizeof(chNoExtensions);
  57886. #ifdef OPENSSL_EXTRA
  57887. if (i > 0) {
  57888. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  57889. WOLFSSL_SUCCESS);
  57890. }
  57891. #endif
  57892. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  57893. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  57894. /* Expecting a handshake msg. Either HVR or SH. */
  57895. ExpectIntGT(test_ctx.c_len, 0);
  57896. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  57897. wolfSSL_free(ssl_s);
  57898. wolfSSL_CTX_free(ctx_s);
  57899. }
  57900. #endif
  57901. return EXPECT_RESULT();
  57902. }
  57903. static int test_TLSX_CA_NAMES_bad_extension(void)
  57904. {
  57905. EXPECT_DECLS;
  57906. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  57907. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  57908. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  57909. defined(HAVE_NULL_CIPHER)
  57910. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  57911. * parse the CA Names extension. Otherwise it will return other non-related
  57912. * errors. If CA Names will be parsed in more configurations, that should
  57913. * be reflected in the macro guard above. */
  57914. WOLFSSL *ssl_c = NULL;
  57915. WOLFSSL_CTX *ctx_c = NULL;
  57916. struct test_memio_ctx test_ctx;
  57917. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  57918. const byte shBadCaNamesExt[] = {
  57919. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  57920. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  57921. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  57922. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  57923. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  57924. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  57925. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  57926. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  57927. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  57928. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  57929. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  57930. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  57931. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  57932. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  57933. };
  57934. const byte shBadCaNamesExt2[] = {
  57935. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  57936. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  57937. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  57938. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  57939. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  57940. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  57941. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  57942. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  57943. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  57944. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  57945. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  57946. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  57947. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  57948. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  57949. };
  57950. int i = 0;
  57951. for (i = 0; i < 2; i++) {
  57952. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  57953. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  57954. wolfTLSv1_3_client_method, NULL), 0);
  57955. switch (i) {
  57956. case 0:
  57957. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  57958. sizeof(shBadCaNamesExt));
  57959. test_ctx.c_len = sizeof(shBadCaNamesExt);
  57960. break;
  57961. case 1:
  57962. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  57963. sizeof(shBadCaNamesExt2));
  57964. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  57965. break;
  57966. }
  57967. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  57968. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  57969. wolfSSL_free(ssl_c);
  57970. ssl_c = NULL;
  57971. wolfSSL_CTX_free(ctx_c);
  57972. ctx_c = NULL;
  57973. }
  57974. #endif
  57975. return EXPECT_RESULT();
  57976. }
  57977. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57978. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57979. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  57980. {
  57981. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  57982. WOLFSSL_SUCCESS);
  57983. }
  57984. static int test_dtls_1_0_hvr_downgrade(void)
  57985. {
  57986. EXPECT_DECLS;
  57987. callback_functions func_cb_client;
  57988. callback_functions func_cb_server;
  57989. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  57990. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  57991. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  57992. func_cb_client.method = wolfDTLS_client_method;
  57993. func_cb_server.method = wolfDTLSv1_2_server_method;
  57994. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  57995. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  57996. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  57997. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  57998. return EXPECT_RESULT();
  57999. }
  58000. #else
  58001. static int test_dtls_1_0_hvr_downgrade(void)
  58002. {
  58003. EXPECT_DECLS;
  58004. return EXPECT_RESULT();
  58005. }
  58006. #endif
  58007. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  58008. defined(HAVE_SESSION_TICKET)
  58009. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  58010. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  58011. {
  58012. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  58013. AssertNotNull(test_session_ticket_no_id_session);
  58014. }
  58015. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  58016. {
  58017. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  58018. }
  58019. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  58020. {
  58021. test_session_ticket_no_id_session->sessionIDSz = 0;
  58022. AssertIntEQ(WOLFSSL_SUCCESS,
  58023. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  58024. }
  58025. static int test_session_ticket_no_id(void)
  58026. {
  58027. /* We are testing an expired (invalid crypto context in out case since the
  58028. * ctx changes) session ticket being sent with the session ID being 0
  58029. * length. */
  58030. EXPECT_DECLS;
  58031. callback_functions func_cb_client;
  58032. callback_functions func_cb_server;
  58033. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58034. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58035. func_cb_client.method = wolfTLSv1_2_client_method;
  58036. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58037. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  58038. func_cb_server.method = wolfTLSv1_2_server_method;
  58039. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58040. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58041. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58042. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58043. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  58044. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  58045. func_cb_client.method = wolfTLSv1_2_client_method;
  58046. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58047. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  58048. func_cb_server.method = wolfTLSv1_2_server_method;
  58049. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  58050. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58051. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58052. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58053. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  58054. return EXPECT_RESULT();
  58055. }
  58056. #else
  58057. static int test_session_ticket_no_id(void)
  58058. {
  58059. EXPECT_DECLS;
  58060. return EXPECT_RESULT();
  58061. }
  58062. #endif
  58063. static int test_session_ticket_hs_update(void)
  58064. {
  58065. EXPECT_DECLS;
  58066. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58067. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  58068. struct test_memio_ctx test_ctx;
  58069. struct test_memio_ctx test_ctx2;
  58070. struct test_memio_ctx test_ctx3;
  58071. WOLFSSL_CTX *ctx_c = NULL;
  58072. WOLFSSL_CTX *ctx_s = NULL;
  58073. WOLFSSL *ssl_c = NULL;
  58074. WOLFSSL *ssl_c2 = NULL;
  58075. WOLFSSL *ssl_c3 = NULL;
  58076. WOLFSSL *ssl_s = NULL;
  58077. WOLFSSL *ssl_s2 = NULL;
  58078. WOLFSSL *ssl_s3 = NULL;
  58079. WOLFSSL_SESSION *sess = NULL;
  58080. byte read_data[1];
  58081. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58082. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  58083. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  58084. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58085. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58086. /* Generate tickets */
  58087. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58088. wolfSSL_SetLoggingPrefix("client");
  58089. /* Read the ticket msg */
  58090. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  58091. WOLFSSL_FATAL_ERROR);
  58092. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58093. WOLFSSL_ERROR_WANT_READ);
  58094. wolfSSL_SetLoggingPrefix(NULL);
  58095. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  58096. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58097. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  58098. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58099. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58100. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  58101. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  58102. wolfSSL_SetLoggingPrefix("client");
  58103. /* Exchange initial flights for the second connection */
  58104. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  58105. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  58106. WOLFSSL_ERROR_WANT_READ);
  58107. wolfSSL_SetLoggingPrefix(NULL);
  58108. wolfSSL_SetLoggingPrefix("server");
  58109. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  58110. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  58111. WOLFSSL_ERROR_WANT_READ);
  58112. wolfSSL_SetLoggingPrefix(NULL);
  58113. /* Complete third connection so that new tickets are exchanged */
  58114. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  58115. /* Read the ticket msg */
  58116. wolfSSL_SetLoggingPrefix("client");
  58117. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  58118. WOLFSSL_FATAL_ERROR);
  58119. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  58120. WOLFSSL_ERROR_WANT_READ);
  58121. wolfSSL_SetLoggingPrefix(NULL);
  58122. /* Complete second connection */
  58123. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  58124. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  58125. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  58126. wolfSSL_free(ssl_c);
  58127. wolfSSL_free(ssl_c2);
  58128. wolfSSL_free(ssl_c3);
  58129. wolfSSL_free(ssl_s);
  58130. wolfSSL_free(ssl_s2);
  58131. wolfSSL_free(ssl_s3);
  58132. wolfSSL_CTX_free(ctx_c);
  58133. wolfSSL_CTX_free(ctx_s);
  58134. wolfSSL_SESSION_free(sess);
  58135. #endif
  58136. return EXPECT_RESULT();
  58137. }
  58138. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58139. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58140. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  58141. {
  58142. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58143. WOLFSSL_SUCCESS);
  58144. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58145. }
  58146. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  58147. {
  58148. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58149. }
  58150. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  58151. {
  58152. char testMsg[] = "Message after SCR";
  58153. char msgBuf[sizeof(testMsg)];
  58154. if (wolfSSL_is_server(ssl)) {
  58155. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  58156. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  58157. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58158. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58159. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58160. sizeof(testMsg));
  58161. }
  58162. else {
  58163. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58164. sizeof(testMsg));
  58165. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58166. }
  58167. }
  58168. static int test_dtls_downgrade_scr_server(void)
  58169. {
  58170. EXPECT_DECLS;
  58171. callback_functions func_cb_client;
  58172. callback_functions func_cb_server;
  58173. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58174. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58175. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58176. func_cb_client.method = wolfDTLSv1_2_client_method;
  58177. func_cb_server.method = wolfDTLS_server_method;
  58178. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  58179. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  58180. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  58181. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  58182. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58183. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58184. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58185. return EXPECT_RESULT();
  58186. }
  58187. #else
  58188. static int test_dtls_downgrade_scr_server(void)
  58189. {
  58190. EXPECT_DECLS;
  58191. return EXPECT_RESULT();
  58192. }
  58193. #endif
  58194. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58195. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  58196. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  58197. {
  58198. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  58199. WOLFSSL_SUCCESS);
  58200. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  58201. }
  58202. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  58203. {
  58204. char testMsg[] = "Message after SCR";
  58205. char msgBuf[sizeof(testMsg)];
  58206. if (wolfSSL_is_server(ssl)) {
  58207. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  58208. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  58209. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58210. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  58211. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58212. sizeof(testMsg));
  58213. }
  58214. else {
  58215. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  58216. sizeof(testMsg));
  58217. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  58218. }
  58219. }
  58220. static int test_dtls_downgrade_scr(void)
  58221. {
  58222. EXPECT_DECLS;
  58223. callback_functions func_cb_client;
  58224. callback_functions func_cb_server;
  58225. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58226. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58227. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58228. func_cb_client.method = wolfDTLS_client_method;
  58229. func_cb_server.method = wolfDTLSv1_2_server_method;
  58230. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  58231. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  58232. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  58233. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58234. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58235. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58236. return EXPECT_RESULT();
  58237. }
  58238. #else
  58239. static int test_dtls_downgrade_scr(void)
  58240. {
  58241. EXPECT_DECLS;
  58242. return EXPECT_RESULT();
  58243. }
  58244. #endif
  58245. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58246. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  58247. char *data, int sz, void *ctx)
  58248. {
  58249. static int call_counter = 0;
  58250. call_counter++;
  58251. (void)ssl;
  58252. (void)data;
  58253. (void)sz;
  58254. (void)ctx;
  58255. switch (call_counter) {
  58256. case 1:
  58257. case 2:
  58258. return WOLFSSL_CBIO_ERR_TIMEOUT;
  58259. case 3:
  58260. return WOLFSSL_CBIO_ERR_WANT_READ;
  58261. default:
  58262. AssertIntLE(call_counter, 3);
  58263. return -1;
  58264. }
  58265. }
  58266. #endif
  58267. /* Make sure we don't send acks before getting a server hello */
  58268. static int test_dtls_client_hello_timeout_downgrade(void)
  58269. {
  58270. EXPECT_DECLS;
  58271. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58272. WOLFSSL_CTX *ctx_c = NULL;
  58273. WOLFSSL_CTX *ctx_s = NULL;
  58274. WOLFSSL *ssl_c = NULL;
  58275. WOLFSSL *ssl_s = NULL;
  58276. struct test_memio_ctx test_ctx;
  58277. DtlsRecordLayerHeader* dtlsRH;
  58278. size_t len;
  58279. byte sequence_number[8];
  58280. int i;
  58281. for (i = 0; i < 2; i++) {
  58282. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58283. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58284. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  58285. if (i == 0) {
  58286. /* First time simulate timeout in IO layer */
  58287. /* CH1 */
  58288. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58289. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58290. /* HVR */
  58291. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58292. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58293. /* CH2 */
  58294. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58295. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58296. /* SH flight */
  58297. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58298. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58299. /* Drop the SH */
  58300. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  58301. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  58302. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  58303. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  58304. (sizeof(DtlsRecordLayerHeader) + len));
  58305. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  58306. /* Read the remainder of the flight */
  58307. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58308. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58309. wolfSSL_SSLSetIORecv(ssl_c,
  58310. test_dtls_client_hello_timeout_downgrade_read_cb);
  58311. /* CH3 */
  58312. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58313. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58314. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  58315. }
  58316. else {
  58317. /* Second time call wolfSSL_dtls_got_timeout */
  58318. /* CH1 */
  58319. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58320. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58321. /* HVR */
  58322. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58323. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58324. /* CH2 */
  58325. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58326. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58327. /* SH flight */
  58328. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58329. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58330. /* Drop the SH */
  58331. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  58332. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  58333. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  58334. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  58335. (sizeof(DtlsRecordLayerHeader) + len));
  58336. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  58337. /* Read the remainder of the flight */
  58338. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58339. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58340. /* Quick timeout should be set as we received at least one msg */
  58341. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  58342. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58343. /* Quick timeout should be cleared after a quick timeout */
  58344. /* CH3 */
  58345. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  58346. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58347. }
  58348. /* Parse out to make sure we got exactly one ClientHello message */
  58349. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  58350. /* Second ClientHello after HVR */
  58351. sequence_number[7] = 2;
  58352. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  58353. ExpectIntEQ(dtlsRH->type, handshake);
  58354. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  58355. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  58356. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  58357. sizeof(sequence_number)), 0);
  58358. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  58359. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  58360. /* Connection should be able to continue */
  58361. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58362. wolfSSL_free(ssl_c);
  58363. wolfSSL_free(ssl_s);
  58364. wolfSSL_CTX_free(ctx_c);
  58365. wolfSSL_CTX_free(ctx_s);
  58366. ssl_c = NULL;
  58367. ssl_s = NULL;
  58368. ctx_c = NULL;
  58369. ctx_s = NULL;
  58370. if (!EXPECT_SUCCESS())
  58371. break;
  58372. }
  58373. #endif
  58374. return EXPECT_RESULT();
  58375. }
  58376. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58377. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  58378. int sz, void *ctx)
  58379. {
  58380. static int call_counter = 0;
  58381. call_counter++;
  58382. (void)ssl;
  58383. (void)data;
  58384. (void)sz;
  58385. (void)ctx;
  58386. switch (call_counter) {
  58387. case 1:
  58388. return WOLFSSL_CBIO_ERR_TIMEOUT;
  58389. case 2:
  58390. return WOLFSSL_CBIO_ERR_WANT_READ;
  58391. default:
  58392. AssertIntLE(call_counter, 2);
  58393. return -1;
  58394. }
  58395. }
  58396. #endif
  58397. /* Make sure we don't send acks before getting a server hello */
  58398. static int test_dtls_client_hello_timeout(void)
  58399. {
  58400. EXPECT_DECLS;
  58401. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58402. WOLFSSL *ssl_c = NULL;
  58403. WOLFSSL_CTX *ctx_c = NULL;
  58404. struct test_memio_ctx test_ctx;
  58405. DtlsRecordLayerHeader* dtlsRH;
  58406. size_t idx;
  58407. size_t len;
  58408. byte sequence_number[8];
  58409. int i;
  58410. for (i = 0; i < 2; i++) {
  58411. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58412. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  58413. wolfDTLSv1_3_client_method, NULL), 0);
  58414. if (i == 0) {
  58415. /* First time simulate timeout in IO layer */
  58416. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  58417. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  58418. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58419. }
  58420. else {
  58421. /* Second time call wolfSSL_dtls_got_timeout */
  58422. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  58423. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58424. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58425. }
  58426. /* Parse out to make sure we got exactly two ClientHello messages */
  58427. idx = 0;
  58428. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  58429. /* First ClientHello */
  58430. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  58431. ExpectIntEQ(dtlsRH->type, handshake);
  58432. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  58433. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  58434. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  58435. sizeof(sequence_number)), 0);
  58436. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  58437. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  58438. idx += sizeof(DtlsRecordLayerHeader) + len;
  58439. /* Second ClientHello */
  58440. sequence_number[7] = 1;
  58441. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  58442. ExpectIntEQ(dtlsRH->type, handshake);
  58443. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  58444. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  58445. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  58446. sizeof(sequence_number)), 0);
  58447. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  58448. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  58449. wolfSSL_free(ssl_c);
  58450. wolfSSL_CTX_free(ctx_c);
  58451. ssl_c = NULL;
  58452. ctx_c = NULL;
  58453. if (!EXPECT_SUCCESS())
  58454. break;
  58455. }
  58456. #endif
  58457. return EXPECT_RESULT();
  58458. }
  58459. /* DTLS test when dropping the changed cipher spec message */
  58460. static int test_dtls_dropped_ccs(void)
  58461. {
  58462. EXPECT_DECLS;
  58463. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  58464. WOLFSSL_CTX *ctx_c = NULL;
  58465. WOLFSSL_CTX *ctx_s = NULL;
  58466. WOLFSSL *ssl_c = NULL;
  58467. WOLFSSL *ssl_s = NULL;
  58468. struct test_memio_ctx test_ctx;
  58469. DtlsRecordLayerHeader* dtlsRH;
  58470. size_t len;
  58471. byte data[1];
  58472. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58473. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58474. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58475. /* CH1 */
  58476. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58477. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58478. /* HVR */
  58479. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58480. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58481. /* CH2 */
  58482. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58483. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58484. /* Server first flight */
  58485. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58486. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58487. /* Client flight */
  58488. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58489. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58490. /* Server ccs + finished */
  58491. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  58492. /* Drop the ccs */
  58493. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  58494. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  58495. ExpectIntEQ(len, 1);
  58496. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  58497. if (EXPECT_SUCCESS()) {
  58498. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  58499. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  58500. (sizeof(DtlsRecordLayerHeader) + len));
  58501. }
  58502. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  58503. /* Client rtx flight */
  58504. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58505. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58506. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  58507. /* Server ccs + finished rtx */
  58508. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  58509. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58510. /* Client processes finished */
  58511. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  58512. wolfSSL_free(ssl_c);
  58513. wolfSSL_free(ssl_s);
  58514. wolfSSL_CTX_free(ctx_c);
  58515. wolfSSL_CTX_free(ctx_s);
  58516. #endif
  58517. return EXPECT_RESULT();
  58518. }
  58519. /**
  58520. * Make sure we don't send RSA Signature Hash Algorithms in the
  58521. * CertificateRequest when we don't have any such ciphers set.
  58522. * @return EXPECT_RESULT()
  58523. */
  58524. static int test_certreq_sighash_algos(void)
  58525. {
  58526. EXPECT_DECLS;
  58527. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  58528. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  58529. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  58530. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  58531. WOLFSSL_CTX *ctx_c = NULL;
  58532. WOLFSSL_CTX *ctx_s = NULL;
  58533. WOLFSSL *ssl_c = NULL;
  58534. WOLFSSL *ssl_s = NULL;
  58535. struct test_memio_ctx test_ctx;
  58536. int idx = 0;
  58537. int maxIdx = 0;
  58538. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58539. test_ctx.c_ciphers = test_ctx.s_ciphers =
  58540. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  58541. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58542. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58543. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  58544. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  58545. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  58546. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  58547. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58548. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  58549. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58550. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  58551. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  58552. WOLFSSL_ERROR_WANT_READ);
  58553. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  58554. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  58555. WOLFSSL_ERROR_WANT_READ);
  58556. /* Find the CertificateRequest message */
  58557. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  58558. word16 len;
  58559. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  58560. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  58561. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  58562. ato16(test_ctx.c_buff + idx, &len);
  58563. idx += OPAQUE16_LEN;
  58564. if (test_ctx.c_buff[idx] == certificate_request) {
  58565. idx++;
  58566. /* length */
  58567. idx += OPAQUE24_LEN;
  58568. /* cert types */
  58569. idx += 1 + test_ctx.c_buff[idx];
  58570. /* Sig algos */
  58571. ato16(test_ctx.c_buff + idx, &len);
  58572. idx += OPAQUE16_LEN;
  58573. maxIdx = idx + (int)len;
  58574. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  58575. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  58576. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  58577. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  58578. else
  58579. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  58580. }
  58581. break;
  58582. }
  58583. else {
  58584. idx += (int)len;
  58585. }
  58586. }
  58587. ExpectIntLT(idx, test_ctx.c_len);
  58588. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58589. wolfSSL_free(ssl_c);
  58590. wolfSSL_free(ssl_s);
  58591. wolfSSL_CTX_free(ctx_c);
  58592. wolfSSL_CTX_free(ctx_s);
  58593. #endif
  58594. return EXPECT_RESULT();
  58595. }
  58596. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58597. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  58598. {
  58599. EXPECT_DECLS;
  58600. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  58601. myVerifyAction = VERIFY_USE_PREVERFIY;
  58602. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  58603. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  58604. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  58605. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  58606. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  58607. WOLFSSL_SUCCESS);
  58608. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  58609. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  58610. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58611. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  58612. "./certs/crl/ca-int.pem",
  58613. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58614. return EXPECT_RESULT();
  58615. }
  58616. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  58617. {
  58618. EXPECT_DECLS;
  58619. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  58620. myVerifyAction = VERIFY_USE_PREVERFIY;
  58621. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  58622. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  58623. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  58624. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  58625. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  58626. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  58627. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  58628. WOLFSSL_SUCCESS);
  58629. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  58630. "./certs/crl/ca-int2.pem",
  58631. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58632. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  58633. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  58634. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58635. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  58636. "./certs/crl/ca-int.pem",
  58637. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58638. return EXPECT_RESULT();
  58639. }
  58640. #endif
  58641. static int test_revoked_loaded_int_cert(void)
  58642. {
  58643. EXPECT_DECLS;
  58644. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58645. test_ssl_cbf client_cbf;
  58646. test_ssl_cbf server_cbf;
  58647. struct {
  58648. const char* certPemFile;
  58649. const char* keyPemFile;
  58650. ctx_cb client_ctx_ready;
  58651. } test_params[] = {
  58652. {"./certs/intermediate/ca-int2-cert.pem",
  58653. "./certs/intermediate/ca-int2-key.pem",
  58654. test_revoked_loaded_int_cert_ctx_ready1},
  58655. {"./certs/intermediate/server-chain.pem",
  58656. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  58657. {"./certs/intermediate/server-chain-short.pem",
  58658. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  58659. };
  58660. size_t i;
  58661. printf("\n");
  58662. for (i = 0; i < XELEM_CNT(test_params); i++) {
  58663. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58664. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58665. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  58666. server_cbf.certPemFile = test_params[i].certPemFile;
  58667. server_cbf.keyPemFile = test_params[i].keyPemFile;
  58668. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  58669. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  58670. &server_cbf, NULL), TEST_FAIL);
  58671. #ifndef WOLFSSL_HAPROXY
  58672. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  58673. #else
  58674. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  58675. #endif
  58676. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  58677. if (!EXPECT_SUCCESS())
  58678. break;
  58679. printf("\t%s passed\n", test_params[i].certPemFile);
  58680. }
  58681. #endif
  58682. return EXPECT_RESULT();
  58683. }
  58684. static int test_dtls13_frag_ch_pq(void)
  58685. {
  58686. EXPECT_DECLS;
  58687. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  58688. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  58689. WOLFSSL_CTX *ctx_c = NULL;
  58690. WOLFSSL_CTX *ctx_s = NULL;
  58691. WOLFSSL *ssl_c = NULL;
  58692. WOLFSSL *ssl_s = NULL;
  58693. struct test_memio_ctx test_ctx;
  58694. const char *test_str = "test";
  58695. int test_str_size;
  58696. byte buf[255];
  58697. int group = WOLFSSL_KYBER_LEVEL5;
  58698. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58699. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58700. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58701. /* Add in a large post-quantum key share to make the CH long. */
  58702. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  58703. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  58704. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  58705. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58706. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  58707. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  58708. test_str_size = XSTRLEN("test") + 1;
  58709. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58710. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  58711. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  58712. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  58713. wolfSSL_free(ssl_c);
  58714. wolfSSL_free(ssl_s);
  58715. wolfSSL_CTX_free(ctx_c);
  58716. wolfSSL_CTX_free(ctx_s);
  58717. #endif
  58718. return EXPECT_RESULT();
  58719. }
  58720. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  58721. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  58722. static int test_dtls_frag_ch_count_records(byte* b, int len)
  58723. {
  58724. DtlsRecordLayerHeader* dtlsRH;
  58725. int records = 0;
  58726. size_t recordLen;
  58727. while (len > 0) {
  58728. records++;
  58729. dtlsRH = (DtlsRecordLayerHeader*)b;
  58730. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  58731. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  58732. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  58733. }
  58734. return records;
  58735. }
  58736. #endif
  58737. static int test_dtls_frag_ch(void)
  58738. {
  58739. EXPECT_DECLS;
  58740. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  58741. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  58742. WOLFSSL_CTX *ctx_c = NULL;
  58743. WOLFSSL_CTX *ctx_s = NULL;
  58744. WOLFSSL *ssl_c = NULL;
  58745. WOLFSSL *ssl_s = NULL;
  58746. struct test_memio_ctx test_ctx;
  58747. static unsigned int DUMMY_MTU = 256;
  58748. unsigned char four_frag_CH[] = {
  58749. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58750. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58751. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  58752. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  58753. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  58754. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  58755. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  58756. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  58757. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  58758. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  58759. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  58760. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  58761. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  58762. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  58763. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  58764. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  58765. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  58766. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  58767. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  58768. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58769. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  58770. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  58771. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  58772. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  58773. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  58774. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  58775. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  58776. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  58777. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  58778. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  58779. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  58780. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  58781. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  58782. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  58783. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  58784. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  58785. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  58786. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  58787. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  58788. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  58789. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  58790. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  58791. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  58792. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  58793. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  58794. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  58795. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  58796. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  58797. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  58798. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  58799. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  58800. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  58801. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  58802. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  58803. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  58804. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  58805. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  58806. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  58807. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  58808. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  58809. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  58810. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  58811. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  58812. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  58813. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  58814. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  58815. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  58816. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  58817. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  58818. 0xa5, 0xcb, 0x68, 0xf3
  58819. };
  58820. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58821. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58822. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58823. /* Fragment msgs */
  58824. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  58825. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  58826. /* Add in some key shares to make the CH long */
  58827. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  58828. WOLFSSL_SUCCESS);
  58829. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  58830. WOLFSSL_SUCCESS);
  58831. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  58832. WOLFSSL_SUCCESS);
  58833. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  58834. WOLFSSL_SUCCESS);
  58835. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  58836. /* Reject fragmented first CH */
  58837. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  58838. sizeof(four_frag_CH)), 4);
  58839. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  58840. test_ctx.s_len = sizeof(four_frag_CH);
  58841. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  58842. int s_len = test_ctx.s_len;
  58843. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58844. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58845. /* Fail if we didn't advance the buffer to avoid infinite loops */
  58846. ExpectIntLT(test_ctx.s_len, s_len);
  58847. }
  58848. /* Expect all fragments read */
  58849. ExpectIntEQ(test_ctx.s_len, 0);
  58850. /* Expect quietly dropping fragmented first CH */
  58851. ExpectIntEQ(test_ctx.c_len, 0);
  58852. /* CH1 */
  58853. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58854. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58855. /* Count records. Expect 1 unfragmented CH */
  58856. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  58857. test_ctx.s_len), 1);
  58858. /* HRR */
  58859. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58860. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58861. /* CH2 */
  58862. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  58863. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  58864. /* Count records. Expect fragmented CH */
  58865. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  58866. test_ctx.s_len), 1);
  58867. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58868. wolfSSL_free(ssl_c);
  58869. wolfSSL_free(ssl_s);
  58870. wolfSSL_CTX_free(ctx_c);
  58871. wolfSSL_CTX_free(ctx_s);
  58872. ssl_c = ssl_s = NULL;
  58873. ctx_c = ctx_s = NULL;
  58874. #endif
  58875. return EXPECT_RESULT();
  58876. }
  58877. static int test_dtls_empty_keyshare_with_cookie(void)
  58878. {
  58879. EXPECT_DECLS;
  58880. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  58881. WOLFSSL_CTX *ctx_s = NULL;
  58882. WOLFSSL *ssl_s = NULL;
  58883. struct test_memio_ctx test_ctx;
  58884. unsigned char ch_empty_keyshare_with_cookie[] = {
  58885. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  58886. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  58887. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  58888. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  58889. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  58890. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  58891. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  58892. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  58893. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  58894. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  58895. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  58896. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  58897. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  58898. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  58899. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  58900. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  58901. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  58902. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  58903. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  58904. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  58905. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  58906. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  58907. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  58908. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  58909. };
  58910. DtlsRecordLayerHeader* dtlsRH;
  58911. byte sequence_number[8];
  58912. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  58913. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58914. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  58915. sizeof(ch_empty_keyshare_with_cookie));
  58916. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  58917. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  58918. NULL, wolfDTLSv1_3_server_method), 0);
  58919. /* CH1 */
  58920. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  58921. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  58922. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  58923. ExpectIntEQ(test_ctx.c_len, 15);
  58924. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  58925. ExpectIntEQ(dtlsRH->type, alert);
  58926. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  58927. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  58928. sequence_number[7] = 1;
  58929. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  58930. sizeof(sequence_number)), 0);
  58931. ExpectIntEQ(dtlsRH->length[0], 0);
  58932. ExpectIntEQ(dtlsRH->length[1], 2);
  58933. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  58934. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  58935. wolfSSL_free(ssl_s);
  58936. wolfSSL_CTX_free(ctx_s);
  58937. #endif
  58938. return EXPECT_RESULT();
  58939. }
  58940. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58941. defined(HAVE_LIBOQS)
  58942. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  58943. {
  58944. int group = WOLFSSL_KYBER_LEVEL5;
  58945. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  58946. }
  58947. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  58948. {
  58949. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  58950. }
  58951. #endif
  58952. static int test_tls13_pq_groups(void)
  58953. {
  58954. EXPECT_DECLS;
  58955. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  58956. defined(HAVE_LIBOQS)
  58957. callback_functions func_cb_client;
  58958. callback_functions func_cb_server;
  58959. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58960. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58961. func_cb_client.method = wolfTLSv1_3_client_method;
  58962. func_cb_server.method = wolfTLSv1_3_server_method;
  58963. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  58964. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  58965. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  58966. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58967. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  58968. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  58969. #endif
  58970. return EXPECT_RESULT();
  58971. }
  58972. static int test_dtls13_early_data(void)
  58973. {
  58974. EXPECT_DECLS;
  58975. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) && \
  58976. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  58977. struct test_memio_ctx test_ctx;
  58978. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  58979. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  58980. WOLFSSL_SESSION *sess = NULL;
  58981. int written = 0;
  58982. int read = 0;
  58983. char msg[] = "This is early data";
  58984. char msg2[] = "This is client data";
  58985. char msg3[] = "This is server data";
  58986. char msgBuf[50];
  58987. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58988. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58989. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  58990. /* Get a ticket so that we can do 0-RTT on the next connection */
  58991. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58992. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58993. wolfSSL_free(ssl_c);
  58994. ssl_c = NULL;
  58995. wolfSSL_free(ssl_s);
  58996. ssl_s = NULL;
  58997. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58998. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58999. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  59000. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59001. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  59002. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  59003. #else
  59004. /* Let's test this but we generally don't recommend turning off the
  59005. * cookie exchange */
  59006. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  59007. #endif
  59008. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  59009. &written), sizeof(msg));
  59010. ExpectIntEQ(written, sizeof(msg));
  59011. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  59012. &read), sizeof(msg));
  59013. ExpectIntEQ(read, sizeof(msg));
  59014. ExpectStrEQ(msg, msgBuf);
  59015. /* Complete handshake */
  59016. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  59017. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  59018. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  59019. * a user would loop until it is true but here we control both sides so we
  59020. * just assert the expected value. wolfSSL_read_early_data does not provide
  59021. * handshake status to us with non-blocking IO and we can't use
  59022. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  59023. * parsing logic. */
  59024. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  59025. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  59026. &read), WOLFSSL_FAILURE);
  59027. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  59028. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59029. /* Test bi-directional write */
  59030. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  59031. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  59032. ExpectStrEQ(msg2, msgBuf);
  59033. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  59034. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  59035. ExpectStrEQ(msg3, msgBuf);
  59036. ExpectTrue(wolfSSL_session_reused(ssl_c));
  59037. ExpectTrue(wolfSSL_session_reused(ssl_s));
  59038. #endif
  59039. return EXPECT_RESULT();
  59040. }
  59041. /*----------------------------------------------------------------------------*
  59042. | Main
  59043. *----------------------------------------------------------------------------*/
  59044. typedef int (*TEST_FUNC)(void);
  59045. typedef struct {
  59046. const char *name;
  59047. TEST_FUNC func;
  59048. byte run:1;
  59049. byte fail:1;
  59050. } TEST_CASE;
  59051. #define TEST_DECL(func) { #func, func, 0, 0 }
  59052. int testAll = 1;
  59053. TEST_CASE testCases[] = {
  59054. TEST_DECL(test_fileAccess),
  59055. /*********************************
  59056. * wolfcrypt
  59057. *********************************/
  59058. TEST_DECL(test_ForceZero),
  59059. TEST_DECL(test_wolfCrypt_Init),
  59060. /* Locking with Compat Mutex */
  59061. TEST_DECL(test_wc_SetMutexCb),
  59062. TEST_DECL(test_wc_LockMutex_ex),
  59063. /* Digests */
  59064. TEST_DECL(test_wc_InitMd5),
  59065. TEST_DECL(test_wc_Md5Update),
  59066. TEST_DECL(test_wc_Md5Final),
  59067. TEST_DECL(test_wc_InitSha),
  59068. TEST_DECL(test_wc_ShaUpdate),
  59069. TEST_DECL(test_wc_ShaFinal),
  59070. TEST_DECL(test_wc_InitSha256),
  59071. TEST_DECL(test_wc_Sha256Update),
  59072. TEST_DECL(test_wc_Sha256Final),
  59073. TEST_DECL(test_wc_Sha256FinalRaw),
  59074. TEST_DECL(test_wc_Sha256GetFlags),
  59075. TEST_DECL(test_wc_Sha256Free),
  59076. TEST_DECL(test_wc_Sha256GetHash),
  59077. TEST_DECL(test_wc_Sha256Copy),
  59078. TEST_DECL(test_wc_InitSha224),
  59079. TEST_DECL(test_wc_Sha224Update),
  59080. TEST_DECL(test_wc_Sha224Final),
  59081. TEST_DECL(test_wc_Sha224SetFlags),
  59082. TEST_DECL(test_wc_Sha224GetFlags),
  59083. TEST_DECL(test_wc_Sha224Free),
  59084. TEST_DECL(test_wc_Sha224GetHash),
  59085. TEST_DECL(test_wc_Sha224Copy),
  59086. TEST_DECL(test_wc_InitSha512),
  59087. TEST_DECL(test_wc_Sha512Update),
  59088. TEST_DECL(test_wc_Sha512Final),
  59089. TEST_DECL(test_wc_Sha512GetFlags),
  59090. TEST_DECL(test_wc_Sha512FinalRaw),
  59091. TEST_DECL(test_wc_Sha512Free),
  59092. TEST_DECL(test_wc_Sha512GetHash),
  59093. TEST_DECL(test_wc_Sha512Copy),
  59094. TEST_DECL(test_wc_InitSha512_224),
  59095. TEST_DECL(test_wc_Sha512_224Update),
  59096. TEST_DECL(test_wc_Sha512_224Final),
  59097. TEST_DECL(test_wc_Sha512_224GetFlags),
  59098. TEST_DECL(test_wc_Sha512_224FinalRaw),
  59099. TEST_DECL(test_wc_Sha512_224Free),
  59100. TEST_DECL(test_wc_Sha512_224GetHash),
  59101. TEST_DECL(test_wc_Sha512_224Copy),
  59102. TEST_DECL(test_wc_InitSha512_256),
  59103. TEST_DECL(test_wc_Sha512_256Update),
  59104. TEST_DECL(test_wc_Sha512_256Final),
  59105. TEST_DECL(test_wc_Sha512_256GetFlags),
  59106. TEST_DECL(test_wc_Sha512_256FinalRaw),
  59107. TEST_DECL(test_wc_Sha512_256Free),
  59108. TEST_DECL(test_wc_Sha512_256GetHash),
  59109. TEST_DECL(test_wc_Sha512_256Copy),
  59110. TEST_DECL(test_wc_InitSha384),
  59111. TEST_DECL(test_wc_Sha384Update),
  59112. TEST_DECL(test_wc_Sha384Final),
  59113. TEST_DECL(test_wc_Sha384GetFlags),
  59114. TEST_DECL(test_wc_Sha384FinalRaw),
  59115. TEST_DECL(test_wc_Sha384Free),
  59116. TEST_DECL(test_wc_Sha384GetHash),
  59117. TEST_DECL(test_wc_Sha384Copy),
  59118. TEST_DECL(test_wc_InitBlake2b),
  59119. TEST_DECL(test_wc_InitBlake2b_WithKey),
  59120. TEST_DECL(test_wc_InitBlake2s_WithKey),
  59121. TEST_DECL(test_wc_InitRipeMd),
  59122. TEST_DECL(test_wc_RipeMdUpdate),
  59123. TEST_DECL(test_wc_RipeMdFinal),
  59124. TEST_DECL(test_wc_InitSha3),
  59125. TEST_DECL(testing_wc_Sha3_Update),
  59126. TEST_DECL(test_wc_Sha3_224_Final),
  59127. TEST_DECL(test_wc_Sha3_256_Final),
  59128. TEST_DECL(test_wc_Sha3_384_Final),
  59129. TEST_DECL(test_wc_Sha3_512_Final),
  59130. TEST_DECL(test_wc_Sha3_224_Copy),
  59131. TEST_DECL(test_wc_Sha3_256_Copy),
  59132. TEST_DECL(test_wc_Sha3_384_Copy),
  59133. TEST_DECL(test_wc_Sha3_512_Copy),
  59134. TEST_DECL(test_wc_Sha3_GetFlags),
  59135. TEST_DECL(test_wc_InitShake256),
  59136. TEST_DECL(testing_wc_Shake256_Update),
  59137. TEST_DECL(test_wc_Shake256_Final),
  59138. TEST_DECL(test_wc_Shake256_Copy),
  59139. TEST_DECL(test_wc_Shake256Hash),
  59140. /* SM3 Digest */
  59141. TEST_DECL(test_wc_InitSm3Free),
  59142. TEST_DECL(test_wc_Sm3UpdateFinal),
  59143. TEST_DECL(test_wc_Sm3GetHash),
  59144. TEST_DECL(test_wc_Sm3Copy),
  59145. TEST_DECL(test_wc_Sm3FinalRaw),
  59146. TEST_DECL(test_wc_Sm3GetSetFlags),
  59147. TEST_DECL(test_wc_Sm3Hash),
  59148. TEST_DECL(test_wc_HashInit),
  59149. TEST_DECL(test_wc_HashSetFlags),
  59150. TEST_DECL(test_wc_HashGetFlags),
  59151. /* HMAC */
  59152. TEST_DECL(test_wc_Md5HmacSetKey),
  59153. TEST_DECL(test_wc_Md5HmacUpdate),
  59154. TEST_DECL(test_wc_Md5HmacFinal),
  59155. TEST_DECL(test_wc_ShaHmacSetKey),
  59156. TEST_DECL(test_wc_ShaHmacUpdate),
  59157. TEST_DECL(test_wc_ShaHmacFinal),
  59158. TEST_DECL(test_wc_Sha224HmacSetKey),
  59159. TEST_DECL(test_wc_Sha224HmacUpdate),
  59160. TEST_DECL(test_wc_Sha224HmacFinal),
  59161. TEST_DECL(test_wc_Sha256HmacSetKey),
  59162. TEST_DECL(test_wc_Sha256HmacUpdate),
  59163. TEST_DECL(test_wc_Sha256HmacFinal),
  59164. TEST_DECL(test_wc_Sha384HmacSetKey),
  59165. TEST_DECL(test_wc_Sha384HmacUpdate),
  59166. TEST_DECL(test_wc_Sha384HmacFinal),
  59167. /* CMAC */
  59168. TEST_DECL(test_wc_InitCmac),
  59169. TEST_DECL(test_wc_CmacUpdate),
  59170. TEST_DECL(test_wc_CmacFinal),
  59171. TEST_DECL(test_wc_AesCmacGenerate),
  59172. /* Cipher */
  59173. TEST_DECL(test_wc_AesGcmStream),
  59174. TEST_DECL(test_wc_Des3_SetIV),
  59175. TEST_DECL(test_wc_Des3_SetKey),
  59176. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  59177. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  59178. TEST_DECL(test_wc_Des3_EcbEncrypt),
  59179. TEST_DECL(test_wc_Chacha_SetKey),
  59180. TEST_DECL(test_wc_Chacha_Process),
  59181. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  59182. TEST_DECL(test_wc_Poly1305SetKey),
  59183. TEST_DECL(test_wc_CamelliaSetKey),
  59184. TEST_DECL(test_wc_CamelliaSetIV),
  59185. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  59186. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  59187. TEST_DECL(test_wc_Arc4SetKey),
  59188. TEST_DECL(test_wc_Arc4Process),
  59189. TEST_DECL(test_wc_Rc2SetKey),
  59190. TEST_DECL(test_wc_Rc2SetIV),
  59191. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  59192. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  59193. /* AES cipher and GMAC. */
  59194. TEST_DECL(test_wc_AesSetKey),
  59195. TEST_DECL(test_wc_AesSetIV),
  59196. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  59197. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  59198. TEST_DECL(test_wc_AesGcmSetKey),
  59199. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  59200. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  59201. TEST_DECL(test_wc_GmacSetKey),
  59202. TEST_DECL(test_wc_GmacUpdate),
  59203. TEST_DECL(test_wc_AesCcmSetKey),
  59204. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  59205. #if defined(WOLFSSL_AES_EAX) && \
  59206. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  59207. TEST_DECL(test_wc_AesEaxVectors),
  59208. TEST_DECL(test_wc_AesEaxEncryptAuth),
  59209. TEST_DECL(test_wc_AesEaxDecryptAuth),
  59210. #endif /* WOLFSSL_AES_EAX */
  59211. /* SM4 cipher */
  59212. TEST_DECL(test_wc_Sm4),
  59213. TEST_DECL(test_wc_Sm4Ecb),
  59214. TEST_DECL(test_wc_Sm4Cbc),
  59215. TEST_DECL(test_wc_Sm4Ctr),
  59216. TEST_DECL(test_wc_Sm4Gcm),
  59217. TEST_DECL(test_wc_Sm4Ccm),
  59218. /* RNG tests */
  59219. #ifdef HAVE_HASHDRBG
  59220. #ifdef TEST_RESEED_INTERVAL
  59221. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  59222. #endif
  59223. TEST_DECL(test_wc_RNG_GenerateBlock),
  59224. #endif
  59225. TEST_DECL(test_get_rand_digit),
  59226. TEST_DECL(test_wc_InitRngNonce),
  59227. TEST_DECL(test_wc_InitRngNonce_ex),
  59228. /* MP API tests */
  59229. TEST_DECL(test_get_digit_count),
  59230. TEST_DECL(test_mp_cond_copy),
  59231. TEST_DECL(test_mp_rand),
  59232. TEST_DECL(test_get_digit),
  59233. TEST_DECL(test_wc_export_int),
  59234. /* RSA */
  59235. TEST_DECL(test_wc_InitRsaKey),
  59236. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  59237. TEST_DECL(test_wc_RsaPublicKeyDecode),
  59238. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  59239. TEST_DECL(test_wc_MakeRsaKey),
  59240. TEST_DECL(test_wc_CheckProbablePrime),
  59241. TEST_DECL(test_wc_RsaPSS_Verify),
  59242. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  59243. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  59244. TEST_DECL(test_wc_RsaKeyToDer),
  59245. TEST_DECL(test_wc_RsaKeyToPublicDer),
  59246. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  59247. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  59248. TEST_DECL(test_wc_RsaEncryptSize),
  59249. TEST_DECL(test_wc_RsaSSL_SignVerify),
  59250. TEST_DECL(test_wc_RsaFlattenPublicKey),
  59251. TEST_DECL(test_RsaDecryptBoundsCheck),
  59252. /* DSA */
  59253. TEST_DECL(test_wc_InitDsaKey),
  59254. TEST_DECL(test_wc_DsaSignVerify),
  59255. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  59256. TEST_DECL(test_wc_MakeDsaKey),
  59257. TEST_DECL(test_wc_DsaKeyToDer),
  59258. TEST_DECL(test_wc_DsaKeyToPublicDer),
  59259. TEST_DECL(test_wc_DsaImportParamsRaw),
  59260. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  59261. TEST_DECL(test_wc_DsaExportParamsRaw),
  59262. TEST_DECL(test_wc_DsaExportKeyRaw),
  59263. /* DH */
  59264. TEST_DECL(test_wc_DhPublicKeyDecode),
  59265. /* wolfCrypt ECC tests */
  59266. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  59267. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  59268. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  59269. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  59270. !defined(HAVE_SELFTEST) && \
  59271. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  59272. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  59273. #endif
  59274. TEST_DECL(test_wc_ecc_make_key),
  59275. TEST_DECL(test_wc_ecc_init),
  59276. TEST_DECL(test_wc_ecc_check_key),
  59277. TEST_DECL(test_wc_ecc_get_generator),
  59278. TEST_DECL(test_wc_ecc_size),
  59279. TEST_DECL(test_wc_ecc_params),
  59280. TEST_DECL(test_wc_ecc_signVerify_hash),
  59281. TEST_DECL(test_wc_ecc_shared_secret),
  59282. TEST_DECL(test_wc_ecc_export_x963),
  59283. TEST_DECL(test_wc_ecc_export_x963_ex),
  59284. TEST_DECL(test_wc_ecc_import_x963),
  59285. TEST_DECL(test_wc_ecc_import_private_key),
  59286. TEST_DECL(test_wc_ecc_export_private_only),
  59287. TEST_DECL(test_wc_ecc_rs_to_sig),
  59288. TEST_DECL(test_wc_ecc_import_raw),
  59289. TEST_DECL(test_wc_ecc_import_unsigned),
  59290. TEST_DECL(test_wc_ecc_sig_size),
  59291. TEST_DECL(test_wc_ecc_ctx_new),
  59292. TEST_DECL(test_wc_ecc_ctx_reset),
  59293. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  59294. TEST_DECL(test_wc_ecc_ctx_set_info),
  59295. TEST_DECL(test_wc_ecc_encryptDecrypt),
  59296. TEST_DECL(test_wc_ecc_del_point),
  59297. TEST_DECL(test_wc_ecc_pointFns),
  59298. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  59299. TEST_DECL(test_wc_ecc_verify_hash_ex),
  59300. TEST_DECL(test_wc_ecc_mulmod),
  59301. TEST_DECL(test_wc_ecc_is_valid_idx),
  59302. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  59303. TEST_DECL(test_wc_ecc_sig_size_calc),
  59304. TEST_DECL(test_wc_EccPrivateKeyToDer),
  59305. /* SM2 elliptic curve */
  59306. TEST_DECL(test_wc_ecc_sm2_make_key),
  59307. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  59308. TEST_DECL(test_wc_ecc_sm2_create_digest),
  59309. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  59310. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  59311. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  59312. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  59313. /* Curve25519 */
  59314. TEST_DECL(test_wc_curve25519_init),
  59315. TEST_DECL(test_wc_curve25519_size),
  59316. TEST_DECL(test_wc_curve25519_export_key_raw),
  59317. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  59318. TEST_DECL(test_wc_curve25519_make_key),
  59319. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  59320. TEST_DECL(test_wc_curve25519_make_pub),
  59321. TEST_DECL(test_wc_curve25519_export_public_ex),
  59322. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  59323. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  59324. TEST_DECL(test_wc_curve25519_import_private),
  59325. /* ED25519 */
  59326. TEST_DECL(test_wc_ed25519_make_key),
  59327. TEST_DECL(test_wc_ed25519_init),
  59328. TEST_DECL(test_wc_ed25519_sign_msg),
  59329. TEST_DECL(test_wc_ed25519_import_public),
  59330. TEST_DECL(test_wc_ed25519_import_private_key),
  59331. TEST_DECL(test_wc_ed25519_export),
  59332. TEST_DECL(test_wc_ed25519_size),
  59333. TEST_DECL(test_wc_ed25519_exportKey),
  59334. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  59335. TEST_DECL(test_wc_Ed25519KeyToDer),
  59336. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  59337. /* Curve448 */
  59338. TEST_DECL(test_wc_curve448_make_key),
  59339. TEST_DECL(test_wc_curve448_shared_secret_ex),
  59340. TEST_DECL(test_wc_curve448_export_public_ex),
  59341. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  59342. TEST_DECL(test_wc_curve448_export_key_raw),
  59343. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  59344. TEST_DECL(test_wc_curve448_import_private),
  59345. TEST_DECL(test_wc_curve448_init),
  59346. TEST_DECL(test_wc_curve448_size),
  59347. /* Ed448 */
  59348. TEST_DECL(test_wc_ed448_make_key),
  59349. TEST_DECL(test_wc_ed448_init),
  59350. TEST_DECL(test_wc_ed448_sign_msg),
  59351. TEST_DECL(test_wc_ed448_import_public),
  59352. TEST_DECL(test_wc_ed448_import_private_key),
  59353. TEST_DECL(test_wc_ed448_export),
  59354. TEST_DECL(test_wc_ed448_size),
  59355. TEST_DECL(test_wc_ed448_exportKey),
  59356. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  59357. TEST_DECL(test_wc_Ed448KeyToDer),
  59358. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  59359. /* Signature API */
  59360. TEST_DECL(test_wc_SignatureGetSize_ecc),
  59361. TEST_DECL(test_wc_SignatureGetSize_rsa),
  59362. /* PEM and DER APIs. */
  59363. TEST_DECL(test_wc_PemToDer),
  59364. TEST_DECL(test_wc_AllocDer),
  59365. TEST_DECL(test_wc_CertPemToDer),
  59366. TEST_DECL(test_wc_KeyPemToDer),
  59367. TEST_DECL(test_wc_PubKeyPemToDer),
  59368. TEST_DECL(test_wc_PemPubKeyToDer),
  59369. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  59370. TEST_DECL(test_wc_CheckCertSigPubKey),
  59371. /* wolfCrypt ASN tests */
  59372. TEST_DECL(test_ToTraditional),
  59373. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  59374. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  59375. /* Certificate */
  59376. TEST_DECL(test_wc_SetSubjectRaw),
  59377. TEST_DECL(test_wc_GetSubjectRaw),
  59378. TEST_DECL(test_wc_SetIssuerRaw),
  59379. TEST_DECL(test_wc_SetIssueBuffer),
  59380. TEST_DECL(test_wc_SetSubjectKeyId),
  59381. TEST_DECL(test_wc_SetSubject),
  59382. TEST_DECL(test_CheckCertSignature),
  59383. TEST_DECL(test_wc_ParseCert),
  59384. TEST_DECL(test_wc_ParseCert_Error),
  59385. TEST_DECL(test_MakeCertWithPathLen),
  59386. TEST_DECL(test_wc_SetKeyUsage),
  59387. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  59388. TEST_DECL(test_wc_SetSubjectBuffer),
  59389. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  59390. /* wolfcrypt PKCS#7 */
  59391. TEST_DECL(test_wc_PKCS7_New),
  59392. TEST_DECL(test_wc_PKCS7_Init),
  59393. TEST_DECL(test_wc_PKCS7_InitWithCert),
  59394. TEST_DECL(test_wc_PKCS7_EncodeData),
  59395. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  59396. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  59397. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  59398. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  59399. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  59400. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  59401. TEST_DECL(test_wc_PKCS7_Degenerate),
  59402. TEST_DECL(test_wc_PKCS7_BER),
  59403. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  59404. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  59405. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  59406. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  59407. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  59408. /* wolfCrypt PKCS#12 */
  59409. TEST_DECL(test_wc_i2d_PKCS12),
  59410. /*
  59411. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  59412. * avoid memory leaks.
  59413. */
  59414. TEST_DECL(test_wolfCrypt_Cleanup),
  59415. TEST_DECL(test_wolfSSL_Init),
  59416. /*********************************
  59417. * OpenSSL compatibility API tests
  59418. *********************************/
  59419. /* If at some point a stub get implemented this test should fail indicating
  59420. * a need to implement a new test case
  59421. */
  59422. TEST_DECL(test_stubs_are_stubs),
  59423. /* ASN.1 compatibility API tests */
  59424. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  59425. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  59426. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  59427. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  59428. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  59429. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  59430. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  59431. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  59432. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  59433. TEST_DECL(test_wolfSSL_ASN1_get_object),
  59434. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  59435. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  59436. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  59437. TEST_DECL(test_wolfSSL_ASN1_STRING),
  59438. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  59439. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  59440. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  59441. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  59442. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  59443. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  59444. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  59445. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  59446. TEST_DECL(test_wolfSSL_ASN1_TIME),
  59447. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  59448. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  59449. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  59450. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  59451. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  59452. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  59453. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  59454. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  59455. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  59456. TEST_DECL(test_wolfSSL_lhash),
  59457. TEST_DECL(test_wolfSSL_certs),
  59458. TEST_DECL(test_wolfSSL_DES),
  59459. TEST_DECL(test_wolfSSL_private_keys),
  59460. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  59461. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  59462. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  59463. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  59464. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  59465. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  59466. #ifndef NO_BIO
  59467. TEST_DECL(test_wolfSSL_BIO),
  59468. TEST_DECL(test_wolfSSL_PEM_read_bio),
  59469. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  59470. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  59471. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  59472. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  59473. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  59474. #endif
  59475. /* EVP API testing */
  59476. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  59477. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  59478. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  59479. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  59480. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  59481. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  59482. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  59483. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  59484. TEST_DECL(test_wolfSSL_EVP_shake128),
  59485. TEST_DECL(test_wolfSSL_EVP_shake256),
  59486. TEST_DECL(test_wolfSSL_EVP_sm3),
  59487. TEST_DECL(test_EVP_blake2),
  59488. #ifdef OPENSSL_ALL
  59489. TEST_DECL(test_wolfSSL_EVP_md4),
  59490. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  59491. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  59492. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  59493. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  59494. #endif
  59495. TEST_DECL(test_EVP_MD_do_all),
  59496. TEST_DECL(test_wolfSSL_EVP_MD_size),
  59497. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  59498. TEST_DECL(test_wolfSSL_EVP_Digest),
  59499. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  59500. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  59501. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  59502. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  59503. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  59504. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  59505. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  59506. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  59507. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  59508. TEST_DECL(test_wolfssl_EVP_chacha20),
  59509. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  59510. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  59511. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  59512. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  59513. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  59514. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  59515. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  59516. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  59517. #ifdef OPENSSL_ALL
  59518. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  59519. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  59520. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  59521. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  59522. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  59523. TEST_DECL(test_wolfSSL_EVP_rc4),
  59524. TEST_DECL(test_wolfSSL_EVP_enc_null),
  59525. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  59526. TEST_DECL(test_wolfSSL_EVP_mdc2),
  59527. TEST_DECL(test_evp_cipher_aes_gcm),
  59528. #endif
  59529. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  59530. #ifdef OPENSSL_EXTRA
  59531. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  59532. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  59533. #endif
  59534. #ifdef OPENSSL_ALL
  59535. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  59536. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  59537. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  59538. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  59539. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  59540. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  59541. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  59542. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  59543. #endif
  59544. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  59545. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  59546. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  59547. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  59548. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  59549. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  59550. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  59551. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  59552. #ifndef NO_BIO
  59553. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  59554. #endif
  59555. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  59556. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  59557. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  59558. #ifndef NO_BIO
  59559. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  59560. #endif /* !NO_BIO */
  59561. #endif
  59562. #ifdef OPENSSL_ALL
  59563. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  59564. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  59565. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  59566. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  59567. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  59568. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  59569. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  59570. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  59571. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  59572. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  59573. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  59574. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  59575. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  59576. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  59577. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  59578. #endif
  59579. TEST_DECL(test_EVP_PKEY_rsa),
  59580. TEST_DECL(test_EVP_PKEY_ec),
  59581. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  59582. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  59583. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  59584. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  59585. TEST_DECL(test_EVP_PKEY_cmp),
  59586. #ifdef OPENSSL_ALL
  59587. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  59588. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  59589. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  59590. #endif
  59591. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  59592. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  59593. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  59594. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  59595. #endif
  59596. #ifndef NO_BIO
  59597. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  59598. TEST_DECL(test_wolfSSL_GetLoggingCb),
  59599. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  59600. TEST_DECL(test_wc_ERR_remove_state),
  59601. TEST_DECL(test_wc_ERR_print_errors_fp),
  59602. #endif
  59603. TEST_DECL(test_wolfSSL_configure_args),
  59604. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  59605. TEST_DECL(test_wolfSSL_set1_curves_list),
  59606. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  59607. TEST_DECL(test_wolfSSL_OtherName),
  59608. TEST_DECL(test_wolfSSL_FPKI),
  59609. TEST_DECL(test_wolfSSL_URI),
  59610. TEST_DECL(test_wolfSSL_TBS),
  59611. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  59612. TEST_DECL(test_X509_STORE_untrusted),
  59613. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  59614. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  59615. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  59616. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  59617. TEST_DECL(test_wolfSSL_X509_Name_canon),
  59618. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  59619. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  59620. TEST_DECL(test_wolfSSL_X509_NID),
  59621. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  59622. TEST_DECL(test_wolfSSL_get0_param),
  59623. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  59624. TEST_DECL(test_wolfSSL_set1_host),
  59625. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  59626. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  59627. TEST_DECL(test_wolfSSL_X509_STORE),
  59628. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  59629. TEST_DECL(test_X509_STORE_get0_objects),
  59630. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  59631. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  59632. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  59633. TEST_DECL(test_wolfSSL_X509_cmp_time),
  59634. TEST_DECL(test_wolfSSL_X509_time_adj),
  59635. /* X509 tests */
  59636. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  59637. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  59638. TEST_DECL(test_wolfSSL_X509_check_host),
  59639. TEST_DECL(test_wolfSSL_X509_check_email),
  59640. TEST_DECL(test_wolfSSL_X509_check_private_key),
  59641. TEST_DECL(test_wolfSSL_X509),
  59642. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  59643. TEST_DECL(test_wolfSSL_X509_sign),
  59644. TEST_DECL(test_wolfSSL_X509_sign2),
  59645. TEST_DECL(test_wolfSSL_X509_verify),
  59646. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  59647. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  59648. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  59649. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  59650. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  59651. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  59652. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  59653. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  59654. TEST_DECL(test_wolfSSL_X509_NAME),
  59655. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  59656. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  59657. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  59658. TEST_DECL(test_wolfSSL_X509_set_name),
  59659. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  59660. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  59661. TEST_DECL(test_wolfSSL_X509_set_version),
  59662. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  59663. TEST_DECL(test_wolfSSL_X509_CRL),
  59664. TEST_DECL(test_wolfSSL_i2d_X509),
  59665. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  59666. TEST_DECL(test_wolfSSL_PEM_read_X509),
  59667. TEST_DECL(test_wolfSSL_X509_check_ca),
  59668. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  59669. TEST_DECL(test_wolfSSL_make_cert),
  59670. #ifndef NO_BIO
  59671. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  59672. TEST_DECL(test_wolfSSL_X509_INFO),
  59673. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  59674. #endif
  59675. #ifdef OPENSSL_ALL
  59676. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  59677. #endif
  59678. TEST_DECL(test_wolfSSL_X509_CA_num),
  59679. TEST_DECL(test_wolfSSL_X509_get_version),
  59680. #ifndef NO_BIO
  59681. TEST_DECL(test_wolfSSL_X509_print),
  59682. TEST_DECL(test_wolfSSL_X509_CRL_print),
  59683. #endif
  59684. TEST_DECL(test_X509_get_signature_nid),
  59685. /* X509 extension testing. */
  59686. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  59687. TEST_DECL(test_wolfSSL_X509_get_ext),
  59688. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  59689. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  59690. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  59691. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  59692. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  59693. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  59694. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  59695. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  59696. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  59697. TEST_DECL(test_wolfSSL_X509V3_EXT),
  59698. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  59699. TEST_DECL(test_wolfSSL_X509_cmp),
  59700. TEST_DECL(test_GENERAL_NAME_set0_othername),
  59701. TEST_DECL(test_othername_and_SID_ext),
  59702. TEST_DECL(test_wolfSSL_dup_CA_list),
  59703. /* OpenSSL sk_X509 API test */
  59704. TEST_DECL(test_sk_X509),
  59705. /* OpenSSL sk_X509_CRL API test */
  59706. TEST_DECL(test_sk_X509_CRL),
  59707. /* OpenSSL X509 REQ API test */
  59708. TEST_DECL(test_X509_REQ),
  59709. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  59710. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  59711. TEST_DECL(test_X509_LOOKUP_add_dir),
  59712. /* RAND compatibility API */
  59713. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  59714. TEST_DECL(test_wolfSSL_RAND_bytes),
  59715. TEST_DECL(test_wolfSSL_RAND),
  59716. /* BN compatibility API */
  59717. TEST_DECL(test_wolfSSL_BN_CTX),
  59718. TEST_DECL(test_wolfSSL_BN),
  59719. TEST_DECL(test_wolfSSL_BN_init),
  59720. TEST_DECL(test_wolfSSL_BN_enc_dec),
  59721. TEST_DECL(test_wolfSSL_BN_word),
  59722. TEST_DECL(test_wolfSSL_BN_bits),
  59723. TEST_DECL(test_wolfSSL_BN_shift),
  59724. TEST_DECL(test_wolfSSL_BN_math),
  59725. TEST_DECL(test_wolfSSL_BN_math_mod),
  59726. TEST_DECL(test_wolfSSL_BN_math_other),
  59727. TEST_DECL(test_wolfSSL_BN_rand),
  59728. TEST_DECL(test_wolfSSL_BN_prime),
  59729. /* OpenSSL PKCS5 API test */
  59730. TEST_DECL(test_wolfSSL_PKCS5),
  59731. /* OpenSSL PKCS8 API test */
  59732. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  59733. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  59734. /* OpenSSL PKCS7 API test */
  59735. TEST_DECL(test_wolfssl_PKCS7),
  59736. TEST_DECL(test_wolfSSL_PKCS7_certs),
  59737. TEST_DECL(test_wolfSSL_PKCS7_sign),
  59738. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  59739. #ifndef NO_BIO
  59740. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  59741. #ifdef HAVE_SMIME
  59742. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  59743. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  59744. #endif /* HAVE_SMIME */
  59745. #endif /* !NO_BIO */
  59746. /* OpenSSL PKCS12 API test */
  59747. TEST_DECL(test_wolfSSL_PKCS12),
  59748. /* Can't memory test as callbacks use Assert. */
  59749. TEST_DECL(test_error_queue_per_thread),
  59750. TEST_DECL(test_wolfSSL_ERR_put_error),
  59751. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  59752. #ifndef NO_BIO
  59753. TEST_DECL(test_wolfSSL_ERR_print_errors),
  59754. #endif
  59755. TEST_DECL(test_OBJ_NAME_do_all),
  59756. TEST_DECL(test_wolfSSL_OBJ),
  59757. TEST_DECL(test_wolfSSL_OBJ_cmp),
  59758. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  59759. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  59760. #ifdef OPENSSL_ALL
  59761. TEST_DECL(test_wolfSSL_OBJ_ln),
  59762. TEST_DECL(test_wolfSSL_OBJ_sn),
  59763. #endif
  59764. #ifndef NO_BIO
  59765. TEST_DECL(test_wolfSSL_BIO_gets),
  59766. TEST_DECL(test_wolfSSL_BIO_puts),
  59767. TEST_DECL(test_wolfSSL_BIO_dump),
  59768. /* Can't memory test as server hangs. */
  59769. TEST_DECL(test_wolfSSL_BIO_should_retry),
  59770. TEST_DECL(test_wolfSSL_BIO_write),
  59771. TEST_DECL(test_wolfSSL_BIO_printf),
  59772. TEST_DECL(test_wolfSSL_BIO_f_md),
  59773. TEST_DECL(test_wolfSSL_BIO_up_ref),
  59774. TEST_DECL(test_wolfSSL_BIO_reset),
  59775. TEST_DECL(test_wolfSSL_BIO_get_len),
  59776. #endif
  59777. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59778. TEST_DECL(test_wolfSSL_check_domain),
  59779. #endif
  59780. TEST_DECL(test_wolfSSL_cert_cb),
  59781. /* Can't memory test as tcp_connect aborts. */
  59782. TEST_DECL(test_wolfSSL_SESSION),
  59783. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  59784. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  59785. TEST_DECL(test_wolfSSL_ticket_keys),
  59786. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  59787. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  59788. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  59789. TEST_DECL(test_wolfSSL_verify_mode),
  59790. TEST_DECL(test_wolfSSL_verify_depth),
  59791. TEST_DECL(test_wolfSSL_verify_result),
  59792. TEST_DECL(test_wolfSSL_msg_callback),
  59793. TEST_DECL(test_wolfSSL_MD4),
  59794. TEST_DECL(test_wolfSSL_MD5),
  59795. TEST_DECL(test_wolfSSL_MD5_Transform),
  59796. TEST_DECL(test_wolfSSL_SHA),
  59797. TEST_DECL(test_wolfSSL_SHA_Transform),
  59798. TEST_DECL(test_wolfSSL_SHA224),
  59799. TEST_DECL(test_wolfSSL_SHA256),
  59800. TEST_DECL(test_wolfSSL_SHA256_Transform),
  59801. TEST_DECL(test_wolfSSL_SHA512_Transform),
  59802. TEST_DECL(test_wolfSSL_HMAC_CTX),
  59803. TEST_DECL(test_wolfSSL_HMAC),
  59804. TEST_DECL(test_wolfSSL_CMAC),
  59805. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  59806. TEST_DECL(test_wolfSSL_DES_ncbc),
  59807. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  59808. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  59809. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  59810. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  59811. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  59812. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  59813. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  59814. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  59815. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  59816. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  59817. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  59818. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  59819. TEST_DECL(test_wolfSSL_PEM_read),
  59820. TEST_DECL(test_wolfSSL_OpenSSL_version),
  59821. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  59822. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  59823. TEST_DECL(test_CONF_modules_xxx),
  59824. #ifdef OPENSSL_ALL
  59825. TEST_DECL(test_wolfSSL_TXT_DB),
  59826. TEST_DECL(test_wolfSSL_NCONF),
  59827. #endif
  59828. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  59829. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  59830. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  59831. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  59832. TEST_DECL(test_CRYPTO_THREADID_xxx),
  59833. TEST_DECL(test_ENGINE_cleanup),
  59834. /* test the no op functions for compatibility */
  59835. TEST_DECL(test_no_op_functions),
  59836. /* OpenSSL error API tests */
  59837. TEST_DECL(test_ERR_load_crypto_strings),
  59838. #ifdef OPENSSL_ALL
  59839. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  59840. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  59841. TEST_DECL(test_wolfSSL_CTX_ctrl),
  59842. #endif /* OPENSSL_ALL */
  59843. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  59844. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  59845. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  59846. TEST_DECL(test_wolfSSL_RSA),
  59847. TEST_DECL(test_wolfSSL_RSA_DER),
  59848. TEST_DECL(test_wolfSSL_RSA_print),
  59849. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  59850. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  59851. TEST_DECL(test_wolfSSL_RSA_get0_key),
  59852. TEST_DECL(test_wolfSSL_RSA_meth),
  59853. TEST_DECL(test_wolfSSL_RSA_verify),
  59854. TEST_DECL(test_wolfSSL_RSA_sign),
  59855. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  59856. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  59857. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  59858. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  59859. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  59860. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  59861. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  59862. TEST_DECL(test_wolfSSL_RSA_ex_data),
  59863. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  59864. TEST_DECL(test_wolfSSL_RSA_To_Der),
  59865. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  59866. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  59867. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  59868. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  59869. TEST_DECL(test_wolfSSL_DH),
  59870. TEST_DECL(test_wolfSSL_DH_dup),
  59871. TEST_DECL(test_wolfSSL_DH_check),
  59872. TEST_DECL(test_wolfSSL_DH_prime),
  59873. TEST_DECL(test_wolfSSL_DH_1536_prime),
  59874. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  59875. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  59876. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  59877. TEST_DECL(test_wolfSSL_d2i_DHparams),
  59878. TEST_DECL(test_wolfSSL_DH_LoadDer),
  59879. TEST_DECL(test_wolfSSL_i2d_DHparams),
  59880. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  59881. TEST_DECL(test_wolfSSL_EC_GROUP),
  59882. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  59883. TEST_DECL(test_wolfSSL_EC_POINT),
  59884. TEST_DECL(test_wolfSSL_SPAKE),
  59885. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  59886. TEST_DECL(test_EC_i2d),
  59887. TEST_DECL(test_wolfSSL_EC_curve),
  59888. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  59889. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  59890. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  59891. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  59892. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  59893. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  59894. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  59895. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  59896. TEST_DECL(test_ECDSA_size_sign),
  59897. TEST_DECL(test_ECDH_compute_key),
  59898. #endif
  59899. #ifdef OPENSSL_EXTRA
  59900. TEST_DECL(test_ED25519),
  59901. TEST_DECL(test_ED448),
  59902. #endif
  59903. TEST_DECL(test_DSA_do_sign_verify),
  59904. #ifdef OPENSSL_ALL
  59905. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  59906. TEST_DECL(test_wolfSSL_DSA_SIG),
  59907. #endif
  59908. TEST_DECL(test_openssl_generate_key_and_cert),
  59909. TEST_DECL(test_wolfSSL_FIPS_mode),
  59910. TEST_DECL(test_openssl_FIPS_drbg),
  59911. /*********************************
  59912. * CertManager API tests
  59913. *********************************/
  59914. TEST_DECL(test_wolfSSL_CertManagerAPI),
  59915. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  59916. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  59917. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  59918. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  59919. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  59920. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  59921. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  59922. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  59923. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  59924. TEST_DECL(test_wolfSSL_CertManagerCRL),
  59925. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  59926. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  59927. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  59928. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  59929. !defined(WOLFSSL_NO_CLIENT_AUTH))
  59930. TEST_DECL(test_various_pathlen_chains),
  59931. #endif
  59932. /*********************************
  59933. * SSL/TLS API tests
  59934. *********************************/
  59935. TEST_DECL(test_wolfSSL_Method_Allocators),
  59936. #ifndef NO_WOLFSSL_SERVER
  59937. TEST_DECL(test_wolfSSL_CTX_new),
  59938. #endif
  59939. TEST_DECL(test_server_wolfSSL_new),
  59940. TEST_DECL(test_client_wolfSSL_new),
  59941. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  59942. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  59943. TEST_DECL(test_for_double_Free),
  59944. #endif
  59945. TEST_DECL(test_wolfSSL_set_options),
  59946. #ifdef WOLFSSL_TLS13
  59947. /* TLS v1.3 API tests */
  59948. TEST_DECL(test_tls13_apis),
  59949. TEST_DECL(test_tls13_cipher_suites),
  59950. #endif
  59951. TEST_DECL(test_wolfSSL_tmp_dh),
  59952. TEST_DECL(test_wolfSSL_ctrl),
  59953. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59954. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59955. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59956. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  59957. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  59958. #endif
  59959. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  59960. TEST_DECL(test_wolfSSL_security_level),
  59961. TEST_DECL(test_wolfSSL_SSL_in_init),
  59962. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  59963. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  59964. TEST_DECL(test_CONF_CTX_FILE),
  59965. TEST_DECL(test_CONF_CTX_CMDLINE),
  59966. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  59967. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  59968. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  59969. /* Bad certificate signature tests */
  59970. TEST_DECL(test_EccSigFailure_cm),
  59971. TEST_DECL(test_RsaSigFailure_cm),
  59972. #endif /* NO_CERTS */
  59973. /* PKCS8 testing */
  59974. TEST_DECL(test_wolfSSL_no_password_cb),
  59975. TEST_DECL(test_wolfSSL_PKCS8),
  59976. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  59977. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  59978. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  59979. TEST_DECL(test_wolfSSL_get_finished),
  59980. /* Uses Assert in handshake callback. */
  59981. TEST_DECL(test_wolfSSL_CTX_add_session),
  59982. /* Large number of memory allocations. */
  59983. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  59984. /* Large number of memory allocations. */
  59985. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  59986. /* Large number of memory allocations. */
  59987. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  59988. /* Large number of memory allocations. */
  59989. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  59990. /* Large number of memory allocations. */
  59991. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  59992. /* Large number of memory allocations. */
  59993. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  59994. #endif
  59995. TEST_DECL(test_SSL_CIPHER_get_xxx),
  59996. TEST_DECL(test_wolfSSL_ERR_strings),
  59997. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  59998. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  59999. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  60000. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  60001. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  60002. /* Large number of memory allocations. */
  60003. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  60004. TEST_DECL(test_wolfSSL_CertRsaPss),
  60005. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  60006. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  60007. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  60008. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  60009. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  60010. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  60011. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  60012. TEST_DECL(test_multiple_crls_same_issuer),
  60013. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  60014. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  60015. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  60016. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  60017. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  60018. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  60019. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  60020. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  60021. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  60022. TEST_DECL(test_SetTmpEC_DHE_Sz),
  60023. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  60024. #ifdef WOLFSSL_DTLS
  60025. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  60026. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  60027. #endif
  60028. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  60029. /* Uses Assert in handshake callback. */
  60030. TEST_DECL(test_wolfSSL_dtls_plaintext),
  60031. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60032. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60033. TEST_DECL(test_wolfSSL_read_write),
  60034. /* Can't memory test as server hangs if client fails before second connect.
  60035. */
  60036. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  60037. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  60038. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  60039. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  60040. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  60041. /* Can't memory test as server hangs. */
  60042. TEST_DECL(test_wolfSSL_dtls_export),
  60043. /* Uses Assert in handshake callback. */
  60044. TEST_DECL(test_wolfSSL_tls_export),
  60045. #endif
  60046. TEST_DECL(test_wolfSSL_SetMinVersion),
  60047. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  60048. /* wolfSSL handshake APIs. */
  60049. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  60050. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  60051. TEST_DECL(test_wolfSSL_BUF),
  60052. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  60053. /* Can't memory test as server hangs. */
  60054. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  60055. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  60056. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  60057. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  60058. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  60059. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  60060. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  60061. /* Can't memory test as server hangs. */
  60062. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  60063. TEST_DECL(test_wolfSSL_Tls13_postauth),
  60064. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  60065. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  60066. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  60067. TEST_DECL(test_wolfSSL_THREADID_hash),
  60068. /* TLS extensions tests */
  60069. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60070. #ifdef HAVE_SNI
  60071. TEST_DECL(test_wolfSSL_UseSNI_params),
  60072. /* Uses Assert in handshake callback. */
  60073. TEST_DECL(test_wolfSSL_UseSNI_connection),
  60074. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  60075. #endif /* HAVE_SNI */
  60076. #endif
  60077. TEST_DECL(test_wolfSSL_UseTrustedCA),
  60078. TEST_DECL(test_wolfSSL_UseMaxFragment),
  60079. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  60080. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  60081. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60082. /* Uses Assert in handshake callback. */
  60083. TEST_DECL(test_wolfSSL_UseALPN_connection),
  60084. TEST_DECL(test_wolfSSL_UseALPN_params),
  60085. #endif
  60086. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  60087. /* Uses Assert in handshake callback. */
  60088. TEST_DECL(test_wolfSSL_set_alpn_protos),
  60089. #endif
  60090. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  60091. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  60092. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  60093. TEST_DECL(test_tls_ext_duplicate),
  60094. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  60095. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60096. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  60097. /* Uses Assert in handshake callback. */
  60098. TEST_DECL(test_wolfSSL_Tls13_ECH),
  60099. #endif
  60100. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  60101. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  60102. /* OCSP Stapling */
  60103. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  60104. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  60105. /* Multicast */
  60106. TEST_DECL(test_wolfSSL_mcast),
  60107. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  60108. TEST_DECL(test_wolfSSL_msgCb),
  60109. TEST_DECL(test_wolfSSL_either_side),
  60110. TEST_DECL(test_wolfSSL_DTLS_either_side),
  60111. /* Uses Assert in handshake callback. */
  60112. TEST_DECL(test_wolfSSL_dtls_fragments),
  60113. /* Uses Assert in handshake callback. */
  60114. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  60115. /* Uses Assert in handshake callback. */
  60116. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  60117. /* Uses Assert in handshake callback. */
  60118. TEST_DECL(test_wolfSSL_dtls_bad_record),
  60119. /* Uses Assert in handshake callback. */
  60120. TEST_DECL(test_wolfSSL_dtls_stateless),
  60121. TEST_DECL(test_generate_cookie),
  60122. #ifndef NO_BIO
  60123. /* Can't memory test as server hangs. */
  60124. TEST_DECL(test_wolfSSL_BIO_connect),
  60125. /* Can't memory test as server Asserts in thread. */
  60126. TEST_DECL(test_wolfSSL_BIO_accept),
  60127. TEST_DECL(test_wolfSSL_BIO_tls),
  60128. #endif
  60129. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  60130. TEST_DECL(test_DhCallbacks),
  60131. #endif
  60132. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  60133. TEST_DECL(test_export_keying_material),
  60134. #endif
  60135. /* Can't memory test as client/server Asserts in thread. */
  60136. TEST_DECL(test_ticket_and_psk_mixing),
  60137. /* Can't memory test as client/server Asserts in thread. */
  60138. TEST_DECL(test_prioritize_psk),
  60139. /* Can't memory test as client/server hangs. */
  60140. TEST_DECL(test_wc_CryptoCb),
  60141. /* Can't memory test as client/server hangs. */
  60142. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  60143. #if !defined(NO_FILESYSTEM) && \
  60144. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60145. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  60146. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  60147. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  60148. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  60149. #ifdef HAVE_MAX_FRAGMENT
  60150. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  60151. #endif /* HAVE_MAX_FRAGMENT */
  60152. #ifndef NO_RSA
  60153. TEST_DECL(test_wolfSSL_dtls_stateless2),
  60154. #if !defined(NO_OLD_TLS)
  60155. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  60156. #endif /* !defined(NO_OLD_TLS) */
  60157. #endif /* ! NO_RSA */
  60158. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60159. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  60160. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  60161. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  60162. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  60163. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  60164. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  60165. && defined(WOLFSSL_TLS13) && \
  60166. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  60167. TEST_DECL(test_ticket_nonce_malloc),
  60168. #endif
  60169. TEST_DECL(test_ticket_ret_create),
  60170. TEST_DECL(test_extra_alerts_wrong_cs),
  60171. TEST_DECL(test_extra_alerts_skip_hs),
  60172. TEST_DECL(test_extra_alerts_bad_psk),
  60173. /* Can't memory test as client/server Asserts. */
  60174. TEST_DECL(test_harden_no_secure_renegotiation),
  60175. TEST_DECL(test_override_alt_cert_chain),
  60176. TEST_DECL(test_rpk_set_xxx_cert_type),
  60177. TEST_DECL(test_tls13_rpk_handshake),
  60178. TEST_DECL(test_dtls13_bad_epoch_ch),
  60179. TEST_DECL(test_short_session_id),
  60180. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  60181. /* Can't memory test as client/server hangs. */
  60182. TEST_DECL(test_dtls_msg_from_other_peer),
  60183. TEST_DECL(test_dtls_ipv6_check),
  60184. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  60185. TEST_DECL(test_dtls_no_extensions),
  60186. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  60187. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  60188. TEST_DECL(test_session_ticket_no_id),
  60189. TEST_DECL(test_session_ticket_hs_update),
  60190. TEST_DECL(test_dtls_downgrade_scr_server),
  60191. TEST_DECL(test_dtls_downgrade_scr),
  60192. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  60193. TEST_DECL(test_dtls_client_hello_timeout),
  60194. TEST_DECL(test_dtls_dropped_ccs),
  60195. TEST_DECL(test_certreq_sighash_algos),
  60196. TEST_DECL(test_revoked_loaded_int_cert),
  60197. TEST_DECL(test_dtls_frag_ch),
  60198. TEST_DECL(test_dtls13_frag_ch_pq),
  60199. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  60200. TEST_DECL(test_tls13_pq_groups),
  60201. TEST_DECL(test_dtls13_early_data),
  60202. /* This test needs to stay at the end to clean up any caches allocated. */
  60203. TEST_DECL(test_wolfSSL_Cleanup)
  60204. };
  60205. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  60206. static void TestSetup(void)
  60207. {
  60208. /* Stub, for now. Add common test setup code here. */
  60209. }
  60210. static void TestCleanup(void)
  60211. {
  60212. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  60213. /* Clear any errors added to the error queue during the test run. */
  60214. wolfSSL_ERR_clear_error();
  60215. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  60216. }
  60217. /* Print out all API test cases with numeric identifier.
  60218. */
  60219. void ApiTest_PrintTestCases(void)
  60220. {
  60221. int i;
  60222. printf("All Test Cases:\n");
  60223. for (i = 0; i < TEST_CASE_CNT; i++) {
  60224. printf("%3d: %s\n", i + 1, testCases[i].name);
  60225. }
  60226. }
  60227. /* Add test case with index to the list to run.
  60228. *
  60229. * @param [in] idx Index of test case to run starting at 1.
  60230. * @return 0 on success.
  60231. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  60232. */
  60233. int ApiTest_RunIdx(int idx)
  60234. {
  60235. if (idx < 1 || idx > TEST_CASE_CNT) {
  60236. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  60237. return BAD_FUNC_ARG;
  60238. }
  60239. testAll = 0;
  60240. testCases[idx-1].run = 1;
  60241. return 0;
  60242. }
  60243. /* Add test case with name to the list to run.
  60244. *
  60245. * @param [in] name Name of test case to run.
  60246. * @return 0 on success.
  60247. * @return BAD_FUNC_ARG when name is not a known test case name.
  60248. */
  60249. int ApiTest_RunName(char* name)
  60250. {
  60251. int i;
  60252. for (i = 0; i < TEST_CASE_CNT; i++) {
  60253. if (XSTRCMP(testCases[i].name, name) == 0) {
  60254. testAll = 0;
  60255. testCases[i].run = 1;
  60256. return 0;
  60257. }
  60258. }
  60259. printf("Test case name not found: %s\n", name);
  60260. printf("Use --list to see all test case names.\n");
  60261. return BAD_FUNC_ARG;
  60262. }
  60263. /* Converts the result code to a string.
  60264. *
  60265. * @param [in] res Test result code.
  60266. * @return String describing test result.
  60267. */
  60268. static const char* apitest_res_string(int res)
  60269. {
  60270. const char* str = "invalid result";
  60271. switch (res) {
  60272. case TEST_SUCCESS:
  60273. str = "passed";
  60274. break;
  60275. case TEST_FAIL:
  60276. str = "failed";
  60277. break;
  60278. case TEST_SKIPPED:
  60279. str = "skipped";
  60280. break;
  60281. }
  60282. return str;
  60283. }
  60284. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  60285. static double gettime_secs(void)
  60286. #if defined(_MSC_VER) && defined(_WIN32)
  60287. {
  60288. /* there's no gettimeofday for Windows, so we'll use system time */
  60289. #define EPOCH_DIFF 11644473600LL
  60290. FILETIME currentFileTime;
  60291. GetSystemTimePreciseAsFileTime(&currentFileTime);
  60292. ULARGE_INTEGER uli = { 0, 0 };
  60293. uli.LowPart = currentFileTime.dwLowDateTime;
  60294. uli.HighPart = currentFileTime.dwHighDateTime;
  60295. /* Convert to seconds since Unix epoch */
  60296. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  60297. }
  60298. #else
  60299. {
  60300. struct timeval tv;
  60301. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  60302. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  60303. }
  60304. #endif
  60305. #endif
  60306. int ApiTest(void)
  60307. {
  60308. int i;
  60309. int ret;
  60310. int res = 0;
  60311. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  60312. double timeDiff;
  60313. #endif
  60314. printf(" Begin API Tests\n");
  60315. fflush(stdout);
  60316. /* we must perform init and cleanup if not all tests are running */
  60317. if (!testAll) {
  60318. #ifdef WOLFCRYPT_ONLY
  60319. if (wolfCrypt_Init() != 0) {
  60320. printf("wolfCrypt Initialization failed\n");
  60321. res = 1;
  60322. }
  60323. #else
  60324. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  60325. printf("wolfSSL Initialization failed\n");
  60326. res = 1;
  60327. }
  60328. #endif
  60329. }
  60330. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  60331. if (res == 0) {
  60332. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  60333. printf("failed to create tmp dir\n");
  60334. res = 1;
  60335. }
  60336. else {
  60337. tmpDirNameSet = 1;
  60338. }
  60339. }
  60340. #endif
  60341. if (res == 0) {
  60342. for (i = 0; i < TEST_CASE_CNT; ++i) {
  60343. EXPECT_DECLS;
  60344. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  60345. currentTestName = testCases[i].name;
  60346. #endif
  60347. /* When not testing all cases then skip if not marked for running.
  60348. */
  60349. if (!testAll && !testCases[i].run) {
  60350. continue;
  60351. }
  60352. TestSetup();
  60353. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  60354. fflush(stdout);
  60355. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  60356. timeDiff = gettime_secs();
  60357. #endif
  60358. ret = testCases[i].func();
  60359. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  60360. timeDiff = gettime_secs() - timeDiff;
  60361. #endif
  60362. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  60363. if (ret != TEST_SKIPPED) {
  60364. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  60365. }
  60366. else
  60367. #endif
  60368. {
  60369. printf(" %s\n", apitest_res_string(ret));
  60370. }
  60371. fflush(stdout);
  60372. /* if return code is < 0 and not skipped then assert error */
  60373. Expect((ret > 0 || ret == TEST_SKIPPED),
  60374. ("Test failed\n"),
  60375. ("ret %d", ret));
  60376. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  60377. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  60378. TestCleanup();
  60379. }
  60380. }
  60381. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  60382. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  60383. wc_ecc_fp_free(); /* free per thread cache */
  60384. #endif
  60385. if (!testAll) {
  60386. #ifdef WOLFCRYPT_ONLY
  60387. wolfCrypt_Cleanup();
  60388. #else
  60389. wolfSSL_Cleanup();
  60390. #endif
  60391. }
  60392. (void)testDevId;
  60393. if (res != 0) {
  60394. printf("\nFAILURES:\n");
  60395. for (i = 0; i < TEST_CASE_CNT; ++i) {
  60396. if (testCases[i].fail) {
  60397. printf(" %3d: %s\n", i + 1, testCases[i].name);
  60398. }
  60399. }
  60400. printf("\n");
  60401. fflush(stdout);
  60402. }
  60403. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  60404. if (tmpDirNameSet) {
  60405. printf("\nBinary dumps of the memio streams can be found in the\n"
  60406. "%s directory. This can be imported into\n"
  60407. "Wireshark by transforming the file with\n"
  60408. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  60409. "And then loading test_output.dump.hex into Wireshark using\n"
  60410. "the \"Import from Hex Dump...\" option and selecting the\n"
  60411. "TCP encapsulation option.\n", tmpDirName);
  60412. }
  60413. #endif
  60414. printf(" End API Tests\n");
  60415. fflush(stdout);
  60416. return res;
  60417. }